Method: flask.app.Flask.make_response
Calls: 423, Exceptions: 5, Paths: 14Back
Path 1: 279 calls (0.66)
'' (19) 'GET' (19) 'POST' (12) '3' (9) 'dcba' (9) '\n Success!\n' (9) 'index' (8) '42' (8) 'Hello' (7) 'Hello World' (7)
Response (279)
1def make_response(self, rv: ft.ResponseReturnValue) -> Response:
2 """Convert the return value from a view function to an instance of
3 :attr:`response_class`.
4
5 :param rv: the return value from the view function. The view function
6 must return a response. Returning ``None``, or the view ending
7 without returning, is not allowed. The following types are allowed
8 for ``view_rv``:
9
10 ``str``
11 A response object is created with the string encoded to UTF-8
12 as the body.
13
14 ``bytes``
15 A response object is created with the bytes as the body.
16
17 ``dict``
18 A dictionary that will be jsonify'd before being returned.
19
20 ``list``
21 A list that will be jsonify'd before being returned.
22
23 ``generator`` or ``iterator``
24 A generator that returns ``str`` or ``bytes`` to be
25 streamed as the response.
26
27 ``tuple``
28 Either ``(body, status, headers)``, ``(body, status)``, or
29 ``(body, headers)``, where ``body`` is any of the other types
30 allowed here, ``status`` is a string or an integer, and
31 ``headers`` is a dictionary or a list of ``(key, value)``
32 tuples. If ``body`` is a :attr:`response_class` instance,
33 ``status`` overwrites the exiting value and ``headers`` are
34 extended.
35
36 :attr:`response_class`
37 The object is returned unchanged.
38
39 other :class:`~werkzeug.wrappers.Response` class
40 The object is coerced to :attr:`response_class`.
41
42 :func:`callable`
43 The function is called as a WSGI application. The result is
44 used to create a response object.
45
46 .. versionchanged:: 2.2
47 A generator will be converted to a streaming response.
48 A list will be converted to a JSON response.
49
50 .. versionchanged:: 1.1
51 A dict will be converted to a JSON response.
52
53 .. versionchanged:: 0.9
54 Previously a tuple was interpreted as the arguments for the
55 response object.
56 """
57
58 status = headers = None
59
60 # unpack tuple returns
61 if isinstance(rv, tuple):
62 len_rv = len(rv)
63
64 # a 3-tuple is unpacked directly
65 if len_rv == 3:
66 rv, status, headers = rv # type: ignore[misc]
67 # decide if a 2-tuple has status or headers
68 elif len_rv == 2:
69 if isinstance(rv[1], (Headers, dict, tuple, list)):
70 rv, headers = rv
71 else:
72 rv, status = rv # type: ignore[assignment,misc]
73 # other sized tuples are not allowed
74 else:
75 raise TypeError(
76 "The view function did not return a valid response tuple."
77 " The tuple must have the form (body, status, headers),"
78 " (body, status), or (body, headers)."
79 )
80
81 # the body must not be None
82 if rv is None:
83 raise TypeError(
84 f"The view function for {request.endpoint!r} did not"
85 " return a valid response. The function either returned"
86 " None or ended without a return statement."
87 )
88
89 # make sure the body is an instance of the response class
90 if not isinstance(rv, self.response_class):
91 if isinstance(rv, (str, bytes, bytearray)) or isinstance(rv, _abc_Iterator):
92 # let the response class set the status and headers instead of
93 # waiting to do it manually, so that the class can handle any
94 # special logic
95 rv = self.response_class(
96 rv,
97 status=status,
98 headers=headers, # type: ignore[arg-type]
99 )
100 status = headers = None
101 elif isinstance(rv, (dict, list)):
102 rv = self.json.response(rv)
103 elif isinstance(rv, BaseResponse) or callable(rv):
104 # evaluate a WSGI callable, or coerce a different response
105 # class to the correct type
106 try:
107 rv = self.response_class.force_type(
108 rv, request.environ # type: ignore[arg-type]
109 )
110 except TypeError as e:
111 raise TypeError(
112 f"{e}\nThe view function did not return a valid"
113 " response. The return type must be a string,"
114 " dict, list, tuple with headers or status,"
115 " Response instance, or WSGI callable, but it"
116 f" was a {type(rv).__name__}."
117 ).with_traceback(sys.exc_info()[2]) from None
118 else:
119 raise TypeError(
120 "The view function did not return a valid"
121 " response. The return type must be a string,"
122 " dict, list, tuple with headers or status,"
123 " Response instance, or WSGI callable, but it was a"
124 f" {type(rv).__name__}."
125 )
126
127 rv = t.cast(Response, rv)
128 # prefer the status if it was provided
129 if status is not None:
130 if isinstance(status, (str, bytes, bytearray)):
131 rv.status = status
132 else:
133 rv.status_code = status
134
135 # extend existing headers with provided headers
136 if headers:
137 rv.headers.update(headers) # type: ignore[arg-type]
138
139 return rv
Path 2: 52 calls (0.12)
Response (52)
Response (52)
1def make_response(self, rv: ft.ResponseReturnValue) -> Response:
2 """Convert the return value from a view function to an instance of
3 :attr:`response_class`.
4
5 :param rv: the return value from the view function. The view function
6 must return a response. Returning ``None``, or the view ending
7 without returning, is not allowed. The following types are allowed
8 for ``view_rv``:
9
10 ``str``
11 A response object is created with the string encoded to UTF-8
12 as the body.
13
14 ``bytes``
15 A response object is created with the bytes as the body.
16
17 ``dict``
18 A dictionary that will be jsonify'd before being returned.
19
20 ``list``
21 A list that will be jsonify'd before being returned.
22
23 ``generator`` or ``iterator``
24 A generator that returns ``str`` or ``bytes`` to be
25 streamed as the response.
26
27 ``tuple``
28 Either ``(body, status, headers)``, ``(body, status)``, or
29 ``(body, headers)``, where ``body`` is any of the other types
30 allowed here, ``status`` is a string or an integer, and
31 ``headers`` is a dictionary or a list of ``(key, value)``
32 tuples. If ``body`` is a :attr:`response_class` instance,
33 ``status`` overwrites the exiting value and ``headers`` are
34 extended.
35
36 :attr:`response_class`
37 The object is returned unchanged.
38
39 other :class:`~werkzeug.wrappers.Response` class
40 The object is coerced to :attr:`response_class`.
41
42 :func:`callable`
43 The function is called as a WSGI application. The result is
44 used to create a response object.
45
46 .. versionchanged:: 2.2
47 A generator will be converted to a streaming response.
48 A list will be converted to a JSON response.
49
50 .. versionchanged:: 1.1
51 A dict will be converted to a JSON response.
52
53 .. versionchanged:: 0.9
54 Previously a tuple was interpreted as the arguments for the
55 response object.
56 """
57
58 status = headers = None
59
60 # unpack tuple returns
61 if isinstance(rv, tuple):
62 len_rv = len(rv)
63
64 # a 3-tuple is unpacked directly
65 if len_rv == 3:
66 rv, status, headers = rv # type: ignore[misc]
67 # decide if a 2-tuple has status or headers
68 elif len_rv == 2:
69 if isinstance(rv[1], (Headers, dict, tuple, list)):
70 rv, headers = rv
71 else:
72 rv, status = rv # type: ignore[assignment,misc]
73 # other sized tuples are not allowed
74 else:
75 raise TypeError(
76 "The view function did not return a valid response tuple."
77 " The tuple must have the form (body, status, headers),"
78 " (body, status), or (body, headers)."
79 )
80
81 # the body must not be None
82 if rv is None:
83 raise TypeError(
84 f"The view function for {request.endpoint!r} did not"
85 " return a valid response. The function either returned"
86 " None or ended without a return statement."
87 )
88
89 # make sure the body is an instance of the response class
90 if not isinstance(rv, self.response_class):
91 if isinstance(rv, (str, bytes, bytearray)) or isinstance(rv, _abc_Iterator):
92 # let the response class set the status and headers instead of
93 # waiting to do it manually, so that the class can handle any
94 # special logic
95 rv = self.response_class(
96 rv,
97 status=status,
98 headers=headers, # type: ignore[arg-type]
99 )
100 status = headers = None
101 elif isinstance(rv, (dict, list)):
102 rv = self.json.response(rv)
103 elif isinstance(rv, BaseResponse) or callable(rv):
104 # evaluate a WSGI callable, or coerce a different response
105 # class to the correct type
106 try:
107 rv = self.response_class.force_type(
108 rv, request.environ # type: ignore[arg-type]
109 )
110 except TypeError as e:
111 raise TypeError(
112 f"{e}\nThe view function did not return a valid"
113 " response. The return type must be a string,"
114 " dict, list, tuple with headers or status,"
115 " Response instance, or WSGI callable, but it"
116 f" was a {type(rv).__name__}."
117 ).with_traceback(sys.exc_info()[2]) from None
118 else:
119 raise TypeError(
120 "The view function did not return a valid"
121 " response. The return type must be a string,"
122 " dict, list, tuple with headers or status,"
123 " Response instance, or WSGI callable, but it was a"
124 f" {type(rv).__name__}."
125 )
126
127 rv = t.cast(Response, rv)
128 # prefer the status if it was provided
129 if status is not None:
130 if isinstance(status, (str, bytes, bytearray)):
131 rv.status = status
132 else:
133 rv.status_code = status
134
135 # extend existing headers with provided headers
136 if headers:
137 rv.headers.update(headers) # type: ignore[arg-type]
138
139 return rv
Path 3: 44 calls (0.1)
MethodNotAllowed (15) NotFound (11) InternalServerError (8) BadRequest (5) RequestRedirect (2) BadRequestKeyError (1) HTTPException (1) BadHost (1)
Response (44)
1def make_response(self, rv: ft.ResponseReturnValue) -> Response:
2 """Convert the return value from a view function to an instance of
3 :attr:`response_class`.
4
5 :param rv: the return value from the view function. The view function
6 must return a response. Returning ``None``, or the view ending
7 without returning, is not allowed. The following types are allowed
8 for ``view_rv``:
9
10 ``str``
11 A response object is created with the string encoded to UTF-8
12 as the body.
13
14 ``bytes``
15 A response object is created with the bytes as the body.
16
17 ``dict``
18 A dictionary that will be jsonify'd before being returned.
19
20 ``list``
21 A list that will be jsonify'd before being returned.
22
23 ``generator`` or ``iterator``
24 A generator that returns ``str`` or ``bytes`` to be
25 streamed as the response.
26
27 ``tuple``
28 Either ``(body, status, headers)``, ``(body, status)``, or
29 ``(body, headers)``, where ``body`` is any of the other types
30 allowed here, ``status`` is a string or an integer, and
31 ``headers`` is a dictionary or a list of ``(key, value)``
32 tuples. If ``body`` is a :attr:`response_class` instance,
33 ``status`` overwrites the exiting value and ``headers`` are
34 extended.
35
36 :attr:`response_class`
37 The object is returned unchanged.
38
39 other :class:`~werkzeug.wrappers.Response` class
40 The object is coerced to :attr:`response_class`.
41
42 :func:`callable`
43 The function is called as a WSGI application. The result is
44 used to create a response object.
45
46 .. versionchanged:: 2.2
47 A generator will be converted to a streaming response.
48 A list will be converted to a JSON response.
49
50 .. versionchanged:: 1.1
51 A dict will be converted to a JSON response.
52
53 .. versionchanged:: 0.9
54 Previously a tuple was interpreted as the arguments for the
55 response object.
56 """
57
58 status = headers = None
59
60 # unpack tuple returns
61 if isinstance(rv, tuple):
62 len_rv = len(rv)
63
64 # a 3-tuple is unpacked directly
65 if len_rv == 3:
66 rv, status, headers = rv # type: ignore[misc]
67 # decide if a 2-tuple has status or headers
68 elif len_rv == 2:
69 if isinstance(rv[1], (Headers, dict, tuple, list)):
70 rv, headers = rv
71 else:
72 rv, status = rv # type: ignore[assignment,misc]
73 # other sized tuples are not allowed
74 else:
75 raise TypeError(
76 "The view function did not return a valid response tuple."
77 " The tuple must have the form (body, status, headers),"
78 " (body, status), or (body, headers)."
79 )
80
81 # the body must not be None
82 if rv is None:
83 raise TypeError(
84 f"The view function for {request.endpoint!r} did not"
85 " return a valid response. The function either returned"
86 " None or ended without a return statement."
87 )
88
89 # make sure the body is an instance of the response class
90 if not isinstance(rv, self.response_class):
91 if isinstance(rv, (str, bytes, bytearray)) or isinstance(rv, _abc_Iterator):
92 # let the response class set the status and headers instead of
93 # waiting to do it manually, so that the class can handle any
94 # special logic
95 rv = self.response_class(
96 rv,
97 status=status,
98 headers=headers, # type: ignore[arg-type]
99 )
100 status = headers = None
101 elif isinstance(rv, (dict, list)):
102 rv = self.json.response(rv)
103 elif isinstance(rv, BaseResponse) or callable(rv):
104 # evaluate a WSGI callable, or coerce a different response
105 # class to the correct type
106 try:
107 rv = self.response_class.force_type(
108 rv, request.environ # type: ignore[arg-type]
109 )
110 except TypeError as e:
111 raise TypeError(
112 f"{e}\nThe view function did not return a valid"
113 " response. The return type must be a string,"
114 " dict, list, tuple with headers or status,"
115 " Response instance, or WSGI callable, but it"
116 f" was a {type(rv).__name__}."
117 ).with_traceback(sys.exc_info()[2]) from None
118 else:
119 raise TypeError(
120 "The view function did not return a valid"
121 " response. The return type must be a string,"
122 " dict, list, tuple with headers or status,"
123 " Response instance, or WSGI callable, but it was a"
124 f" {type(rv).__name__}."
125 )
126
127 rv = t.cast(Response, rv)
128 # prefer the status if it was provided
129 if status is not None:
130 if isinstance(status, (str, bytes, bytearray)):
131 rv.status = status
132 else:
133 rv.status_code = status
134
135 # extend existing headers with provided headers
136 if headers:
137 rv.headers.update(headers) # type: ignore[arg-type]
138
139 return rv
Path 4: 33 calls (0.08)
('', 204) (12) ('', 412) (2) ('internal server error', 500) (2) ('Hello Server Error', 500) (2) ('you shall not pass', 403) (2) ('Parent no', 403) (2)...
Response (33)
1def make_response(self, rv: ft.ResponseReturnValue) -> Response:
2 """Convert the return value from a view function to an instance of
3 :attr:`response_class`.
4
5 :param rv: the return value from the view function. The view function
6 must return a response. Returning ``None``, or the view ending
7 without returning, is not allowed. The following types are allowed
8 for ``view_rv``:
9
10 ``str``
11 A response object is created with the string encoded to UTF-8
12 as the body.
13
14 ``bytes``
15 A response object is created with the bytes as the body.
16
17 ``dict``
18 A dictionary that will be jsonify'd before being returned.
19
20 ``list``
21 A list that will be jsonify'd before being returned.
22
23 ``generator`` or ``iterator``
24 A generator that returns ``str`` or ``bytes`` to be
25 streamed as the response.
26
27 ``tuple``
28 Either ``(body, status, headers)``, ``(body, status)``, or
29 ``(body, headers)``, where ``body`` is any of the other types
30 allowed here, ``status`` is a string or an integer, and
31 ``headers`` is a dictionary or a list of ``(key, value)``
32 tuples. If ``body`` is a :attr:`response_class` instance,
33 ``status`` overwrites the exiting value and ``headers`` are
34 extended.
35
36 :attr:`response_class`
37 The object is returned unchanged.
38
39 other :class:`~werkzeug.wrappers.Response` class
40 The object is coerced to :attr:`response_class`.
41
42 :func:`callable`
43 The function is called as a WSGI application. The result is
44 used to create a response object.
45
46 .. versionchanged:: 2.2
47 A generator will be converted to a streaming response.
48 A list will be converted to a JSON response.
49
50 .. versionchanged:: 1.1
51 A dict will be converted to a JSON response.
52
53 .. versionchanged:: 0.9
54 Previously a tuple was interpreted as the arguments for the
55 response object.
56 """
57
58 status = headers = None
59
60 # unpack tuple returns
61 if isinstance(rv, tuple):
62 len_rv = len(rv)
63
64 # a 3-tuple is unpacked directly
65 if len_rv == 3:
66 rv, status, headers = rv # type: ignore[misc]
67 # decide if a 2-tuple has status or headers
68 elif len_rv == 2:
69 if isinstance(rv[1], (Headers, dict, tuple, list)):
70 rv, headers = rv
71 else:
72 rv, status = rv # type: ignore[assignment,misc]
73 # other sized tuples are not allowed
74 else:
75 raise TypeError(
76 "The view function did not return a valid response tuple."
77 " The tuple must have the form (body, status, headers),"
78 " (body, status), or (body, headers)."
79 )
80
81 # the body must not be None
82 if rv is None:
83 raise TypeError(
84 f"The view function for {request.endpoint!r} did not"
85 " return a valid response. The function either returned"
86 " None or ended without a return statement."
87 )
88
89 # make sure the body is an instance of the response class
90 if not isinstance(rv, self.response_class):
91 if isinstance(rv, (str, bytes, bytearray)) or isinstance(rv, _abc_Iterator):
92 # let the response class set the status and headers instead of
93 # waiting to do it manually, so that the class can handle any
94 # special logic
95 rv = self.response_class(
96 rv,
97 status=status,
98 headers=headers, # type: ignore[arg-type]
99 )
100 status = headers = None
101 elif isinstance(rv, (dict, list)):
102 rv = self.json.response(rv)
103 elif isinstance(rv, BaseResponse) or callable(rv):
104 # evaluate a WSGI callable, or coerce a different response
105 # class to the correct type
106 try:
107 rv = self.response_class.force_type(
108 rv, request.environ # type: ignore[arg-type]
109 )
110 except TypeError as e:
111 raise TypeError(
112 f"{e}\nThe view function did not return a valid"
113 " response. The return type must be a string,"
114 " dict, list, tuple with headers or status,"
115 " Response instance, or WSGI callable, but it"
116 f" was a {type(rv).__name__}."
117 ).with_traceback(sys.exc_info()[2]) from None
118 else:
119 raise TypeError(
120 "The view function did not return a valid"
121 " response. The return type must be a string,"
122 " dict, list, tuple with headers or status,"
123 " Response instance, or WSGI callable, but it was a"
124 f" {type(rv).__name__}."
125 )
126
127 rv = t.cast(Response, rv)
128 # prefer the status if it was provided
129 if status is not None:
130 if isinstance(status, (str, bytes, bytearray)):
131 rv.status = status
132 else:
133 rv.status_code = status
134
135 # extend existing headers with provided headers
136 if headers:
137 rv.headers.update(headers) # type: ignore[arg-type]
138
139 return rv
Path 5: 4 calls (0.01)
tuple (4)
Response (4)
1def make_response(self, rv: ft.ResponseReturnValue) -> Response:
2 """Convert the return value from a view function to an instance of
3 :attr:`response_class`.
4
5 :param rv: the return value from the view function. The view function
6 must return a response. Returning ``None``, or the view ending
7 without returning, is not allowed. The following types are allowed
8 for ``view_rv``:
9
10 ``str``
11 A response object is created with the string encoded to UTF-8
12 as the body.
13
14 ``bytes``
15 A response object is created with the bytes as the body.
16
17 ``dict``
18 A dictionary that will be jsonify'd before being returned.
19
20 ``list``
21 A list that will be jsonify'd before being returned.
22
23 ``generator`` or ``iterator``
24 A generator that returns ``str`` or ``bytes`` to be
25 streamed as the response.
26
27 ``tuple``
28 Either ``(body, status, headers)``, ``(body, status)``, or
29 ``(body, headers)``, where ``body`` is any of the other types
30 allowed here, ``status`` is a string or an integer, and
31 ``headers`` is a dictionary or a list of ``(key, value)``
32 tuples. If ``body`` is a :attr:`response_class` instance,
33 ``status`` overwrites the exiting value and ``headers`` are
34 extended.
35
36 :attr:`response_class`
37 The object is returned unchanged.
38
39 other :class:`~werkzeug.wrappers.Response` class
40 The object is coerced to :attr:`response_class`.
41
42 :func:`callable`
43 The function is called as a WSGI application. The result is
44 used to create a response object.
45
46 .. versionchanged:: 2.2
47 A generator will be converted to a streaming response.
48 A list will be converted to a JSON response.
49
50 .. versionchanged:: 1.1
51 A dict will be converted to a JSON response.
52
53 .. versionchanged:: 0.9
54 Previously a tuple was interpreted as the arguments for the
55 response object.
56 """
57
58 status = headers = None
59
60 # unpack tuple returns
61 if isinstance(rv, tuple):
62 len_rv = len(rv)
63
64 # a 3-tuple is unpacked directly
65 if len_rv == 3:
66 rv, status, headers = rv # type: ignore[misc]
67 # decide if a 2-tuple has status or headers
68 elif len_rv == 2:
69 if isinstance(rv[1], (Headers, dict, tuple, list)):
70 rv, headers = rv
71 else:
72 rv, status = rv # type: ignore[assignment,misc]
73 # other sized tuples are not allowed
74 else:
75 raise TypeError(
76 "The view function did not return a valid response tuple."
77 " The tuple must have the form (body, status, headers),"
78 " (body, status), or (body, headers)."
79 )
80
81 # the body must not be None
82 if rv is None:
83 raise TypeError(
84 f"The view function for {request.endpoint!r} did not"
85 " return a valid response. The function either returned"
86 " None or ended without a return statement."
87 )
88
89 # make sure the body is an instance of the response class
90 if not isinstance(rv, self.response_class):
91 if isinstance(rv, (str, bytes, bytearray)) or isinstance(rv, _abc_Iterator):
92 # let the response class set the status and headers instead of
93 # waiting to do it manually, so that the class can handle any
94 # special logic
95 rv = self.response_class(
96 rv,
97 status=status,
98 headers=headers, # type: ignore[arg-type]
99 )
100 status = headers = None
101 elif isinstance(rv, (dict, list)):
102 rv = self.json.response(rv)
103 elif isinstance(rv, BaseResponse) or callable(rv):
104 # evaluate a WSGI callable, or coerce a different response
105 # class to the correct type
106 try:
107 rv = self.response_class.force_type(
108 rv, request.environ # type: ignore[arg-type]
109 )
110 except TypeError as e:
111 raise TypeError(
112 f"{e}\nThe view function did not return a valid"
113 " response. The return type must be a string,"
114 " dict, list, tuple with headers or status,"
115 " Response instance, or WSGI callable, but it"
116 f" was a {type(rv).__name__}."
117 ).with_traceback(sys.exc_info()[2]) from None
118 else:
119 raise TypeError(
120 "The view function did not return a valid"
121 " response. The return type must be a string,"
122 " dict, list, tuple with headers or status,"
123 " Response instance, or WSGI callable, but it was a"
124 f" {type(rv).__name__}."
125 )
126
127 rv = t.cast(Response, rv)
128 # prefer the status if it was provided
129 if status is not None:
130 if isinstance(status, (str, bytes, bytearray)):
131 rv.status = status
132 else:
133 rv.status_code = status
134
135 # extend existing headers with provided headers
136 if headers:
137 rv.headers.update(headers) # type: ignore[arg-type]
138
139 return rv
Path 6: 2 calls (0.0)
tuple (2)
Response (2)
1def make_response(self, rv: ft.ResponseReturnValue) -> Response:
2 """Convert the return value from a view function to an instance of
3 :attr:`response_class`.
4
5 :param rv: the return value from the view function. The view function
6 must return a response. Returning ``None``, or the view ending
7 without returning, is not allowed. The following types are allowed
8 for ``view_rv``:
9
10 ``str``
11 A response object is created with the string encoded to UTF-8
12 as the body.
13
14 ``bytes``
15 A response object is created with the bytes as the body.
16
17 ``dict``
18 A dictionary that will be jsonify'd before being returned.
19
20 ``list``
21 A list that will be jsonify'd before being returned.
22
23 ``generator`` or ``iterator``
24 A generator that returns ``str`` or ``bytes`` to be
25 streamed as the response.
26
27 ``tuple``
28 Either ``(body, status, headers)``, ``(body, status)``, or
29 ``(body, headers)``, where ``body`` is any of the other types
30 allowed here, ``status`` is a string or an integer, and
31 ``headers`` is a dictionary or a list of ``(key, value)``
32 tuples. If ``body`` is a :attr:`response_class` instance,
33 ``status`` overwrites the exiting value and ``headers`` are
34 extended.
35
36 :attr:`response_class`
37 The object is returned unchanged.
38
39 other :class:`~werkzeug.wrappers.Response` class
40 The object is coerced to :attr:`response_class`.
41
42 :func:`callable`
43 The function is called as a WSGI application. The result is
44 used to create a response object.
45
46 .. versionchanged:: 2.2
47 A generator will be converted to a streaming response.
48 A list will be converted to a JSON response.
49
50 .. versionchanged:: 1.1
51 A dict will be converted to a JSON response.
52
53 .. versionchanged:: 0.9
54 Previously a tuple was interpreted as the arguments for the
55 response object.
56 """
57
58 status = headers = None
59
60 # unpack tuple returns
61 if isinstance(rv, tuple):
62 len_rv = len(rv)
63
64 # a 3-tuple is unpacked directly
65 if len_rv == 3:
66 rv, status, headers = rv # type: ignore[misc]
67 # decide if a 2-tuple has status or headers
68 elif len_rv == 2:
69 if isinstance(rv[1], (Headers, dict, tuple, list)):
70 rv, headers = rv
71 else:
72 rv, status = rv # type: ignore[assignment,misc]
73 # other sized tuples are not allowed
74 else:
75 raise TypeError(
76 "The view function did not return a valid response tuple."
77 " The tuple must have the form (body, status, headers),"
78 " (body, status), or (body, headers)."
79 )
80
81 # the body must not be None
82 if rv is None:
83 raise TypeError(
84 f"The view function for {request.endpoint!r} did not"
85 " return a valid response. The function either returned"
86 " None or ended without a return statement."
87 )
88
89 # make sure the body is an instance of the response class
90 if not isinstance(rv, self.response_class):
91 if isinstance(rv, (str, bytes, bytearray)) or isinstance(rv, _abc_Iterator):
92 # let the response class set the status and headers instead of
93 # waiting to do it manually, so that the class can handle any
94 # special logic
95 rv = self.response_class(
96 rv,
97 status=status,
98 headers=headers, # type: ignore[arg-type]
99 )
100 status = headers = None
101 elif isinstance(rv, (dict, list)):
102 rv = self.json.response(rv)
103 elif isinstance(rv, BaseResponse) or callable(rv):
104 # evaluate a WSGI callable, or coerce a different response
105 # class to the correct type
106 try:
107 rv = self.response_class.force_type(
108 rv, request.environ # type: ignore[arg-type]
109 )
110 except TypeError as e:
111 raise TypeError(
112 f"{e}\nThe view function did not return a valid"
113 " response. The return type must be a string,"
114 " dict, list, tuple with headers or status,"
115 " Response instance, or WSGI callable, but it"
116 f" was a {type(rv).__name__}."
117 ).with_traceback(sys.exc_info()[2]) from None
118 else:
119 raise TypeError(
120 "The view function did not return a valid"
121 " response. The return type must be a string,"
122 " dict, list, tuple with headers or status,"
123 " Response instance, or WSGI callable, but it was a"
124 f" {type(rv).__name__}."
125 )
126
127 rv = t.cast(Response, rv)
128 # prefer the status if it was provided
129 if status is not None:
130 if isinstance(status, (str, bytes, bytearray)):
131 rv.status = status
132 else:
133 rv.status_code = status
134
135 # extend existing headers with provided headers
136 if headers:
137 rv.headers.update(headers) # type: ignore[arg-type]
138
139 return rv
Path 7: 2 calls (0.0)
('Hello',) (1) tuple (1)
TypeError (2)
1def make_response(self, rv: ft.ResponseReturnValue) -> Response:
2 """Convert the return value from a view function to an instance of
3 :attr:`response_class`.
4
5 :param rv: the return value from the view function. The view function
6 must return a response. Returning ``None``, or the view ending
7 without returning, is not allowed. The following types are allowed
8 for ``view_rv``:
9
10 ``str``
11 A response object is created with the string encoded to UTF-8
12 as the body.
13
14 ``bytes``
15 A response object is created with the bytes as the body.
16
17 ``dict``
18 A dictionary that will be jsonify'd before being returned.
19
20 ``list``
21 A list that will be jsonify'd before being returned.
22
23 ``generator`` or ``iterator``
24 A generator that returns ``str`` or ``bytes`` to be
25 streamed as the response.
26
27 ``tuple``
28 Either ``(body, status, headers)``, ``(body, status)``, or
29 ``(body, headers)``, where ``body`` is any of the other types
30 allowed here, ``status`` is a string or an integer, and
31 ``headers`` is a dictionary or a list of ``(key, value)``
32 tuples. If ``body`` is a :attr:`response_class` instance,
33 ``status`` overwrites the exiting value and ``headers`` are
34 extended.
35
36 :attr:`response_class`
37 The object is returned unchanged.
38
39 other :class:`~werkzeug.wrappers.Response` class
40 The object is coerced to :attr:`response_class`.
41
42 :func:`callable`
43 The function is called as a WSGI application. The result is
44 used to create a response object.
45
46 .. versionchanged:: 2.2
47 A generator will be converted to a streaming response.
48 A list will be converted to a JSON response.
49
50 .. versionchanged:: 1.1
51 A dict will be converted to a JSON response.
52
53 .. versionchanged:: 0.9
54 Previously a tuple was interpreted as the arguments for the
55 response object.
56 """
57
58 status = headers = None
59
60 # unpack tuple returns
61 if isinstance(rv, tuple):
62 len_rv = len(rv)
63
64 # a 3-tuple is unpacked directly
65 if len_rv == 3:
66 rv, status, headers = rv # type: ignore[misc]
67 # decide if a 2-tuple has status or headers
68 elif len_rv == 2:
69 if isinstance(rv[1], (Headers, dict, tuple, list)):
70 rv, headers = rv
71 else:
72 rv, status = rv # type: ignore[assignment,misc]
73 # other sized tuples are not allowed
74 else:
75 raise TypeError(
76 "The view function did not return a valid response tuple."
77 " The tuple must have the form (body, status, headers),"
78 " (body, status), or (body, headers)."
79 )
80
81 # the body must not be None
82 if rv is None:
83 raise TypeError(
84 f"The view function for {request.endpoint!r} did not"
85 " return a valid response. The function either returned"
86 " None or ended without a return statement."
87 )
88
89 # make sure the body is an instance of the response class
90 if not isinstance(rv, self.response_class):
91 if isinstance(rv, (str, bytes, bytearray)) or isinstance(rv, _abc_Iterator):
92 # let the response class set the status and headers instead of
93 # waiting to do it manually, so that the class can handle any
94 # special logic
95 rv = self.response_class(
96 rv,
97 status=status,
98 headers=headers, # type: ignore[arg-type]
99 )
100 status = headers = None
101 elif isinstance(rv, (dict, list)):
102 rv = self.json.response(rv)
103 elif isinstance(rv, BaseResponse) or callable(rv):
104 # evaluate a WSGI callable, or coerce a different response
105 # class to the correct type
106 try:
107 rv = self.response_class.force_type(
108 rv, request.environ # type: ignore[arg-type]
109 )
110 except TypeError as e:
111 raise TypeError(
112 f"{e}\nThe view function did not return a valid"
113 " response. The return type must be a string,"
114 " dict, list, tuple with headers or status,"
115 " Response instance, or WSGI callable, but it"
116 f" was a {type(rv).__name__}."
117 ).with_traceback(sys.exc_info()[2]) from None
118 else:
119 raise TypeError(
120 "The view function did not return a valid"
121 " response. The return type must be a string,"
122 " dict, list, tuple with headers or status,"
123 " Response instance, or WSGI callable, but it was a"
124 f" {type(rv).__name__}."
125 )
126
127 rv = t.cast(Response, rv)
128 # prefer the status if it was provided
129 if status is not None:
130 if isinstance(status, (str, bytes, bytearray)):
131 rv.status = status
132 else:
133 rv.status_code = status
134
135 # extend existing headers with provided headers
136 if headers:
137 rv.headers.update(headers) # type: ignore[arg-type]
138
139 return rv
Path 8: 1 calls (0.0)
tuple (1)
Response (1)
1def make_response(self, rv: ft.ResponseReturnValue) -> Response:
2 """Convert the return value from a view function to an instance of
3 :attr:`response_class`.
4
5 :param rv: the return value from the view function. The view function
6 must return a response. Returning ``None``, or the view ending
7 without returning, is not allowed. The following types are allowed
8 for ``view_rv``:
9
10 ``str``
11 A response object is created with the string encoded to UTF-8
12 as the body.
13
14 ``bytes``
15 A response object is created with the bytes as the body.
16
17 ``dict``
18 A dictionary that will be jsonify'd before being returned.
19
20 ``list``
21 A list that will be jsonify'd before being returned.
22
23 ``generator`` or ``iterator``
24 A generator that returns ``str`` or ``bytes`` to be
25 streamed as the response.
26
27 ``tuple``
28 Either ``(body, status, headers)``, ``(body, status)``, or
29 ``(body, headers)``, where ``body`` is any of the other types
30 allowed here, ``status`` is a string or an integer, and
31 ``headers`` is a dictionary or a list of ``(key, value)``
32 tuples. If ``body`` is a :attr:`response_class` instance,
33 ``status`` overwrites the exiting value and ``headers`` are
34 extended.
35
36 :attr:`response_class`
37 The object is returned unchanged.
38
39 other :class:`~werkzeug.wrappers.Response` class
40 The object is coerced to :attr:`response_class`.
41
42 :func:`callable`
43 The function is called as a WSGI application. The result is
44 used to create a response object.
45
46 .. versionchanged:: 2.2
47 A generator will be converted to a streaming response.
48 A list will be converted to a JSON response.
49
50 .. versionchanged:: 1.1
51 A dict will be converted to a JSON response.
52
53 .. versionchanged:: 0.9
54 Previously a tuple was interpreted as the arguments for the
55 response object.
56 """
57
58 status = headers = None
59
60 # unpack tuple returns
61 if isinstance(rv, tuple):
62 len_rv = len(rv)
63
64 # a 3-tuple is unpacked directly
65 if len_rv == 3:
66 rv, status, headers = rv # type: ignore[misc]
67 # decide if a 2-tuple has status or headers
68 elif len_rv == 2:
69 if isinstance(rv[1], (Headers, dict, tuple, list)):
70 rv, headers = rv
71 else:
72 rv, status = rv # type: ignore[assignment,misc]
73 # other sized tuples are not allowed
74 else:
75 raise TypeError(
76 "The view function did not return a valid response tuple."
77 " The tuple must have the form (body, status, headers),"
78 " (body, status), or (body, headers)."
79 )
80
81 # the body must not be None
82 if rv is None:
83 raise TypeError(
84 f"The view function for {request.endpoint!r} did not"
85 " return a valid response. The function either returned"
86 " None or ended without a return statement."
87 )
88
89 # make sure the body is an instance of the response class
90 if not isinstance(rv, self.response_class):
91 if isinstance(rv, (str, bytes, bytearray)) or isinstance(rv, _abc_Iterator):
92 # let the response class set the status and headers instead of
93 # waiting to do it manually, so that the class can handle any
94 # special logic
95 rv = self.response_class(
96 rv,
97 status=status,
98 headers=headers, # type: ignore[arg-type]
99 )
100 status = headers = None
101 elif isinstance(rv, (dict, list)):
102 rv = self.json.response(rv)
103 elif isinstance(rv, BaseResponse) or callable(rv):
104 # evaluate a WSGI callable, or coerce a different response
105 # class to the correct type
106 try:
107 rv = self.response_class.force_type(
108 rv, request.environ # type: ignore[arg-type]
109 )
110 except TypeError as e:
111 raise TypeError(
112 f"{e}\nThe view function did not return a valid"
113 " response. The return type must be a string,"
114 " dict, list, tuple with headers or status,"
115 " Response instance, or WSGI callable, but it"
116 f" was a {type(rv).__name__}."
117 ).with_traceback(sys.exc_info()[2]) from None
118 else:
119 raise TypeError(
120 "The view function did not return a valid"
121 " response. The return type must be a string,"
122 " dict, list, tuple with headers or status,"
123 " Response instance, or WSGI callable, but it was a"
124 f" {type(rv).__name__}."
125 )
126
127 rv = t.cast(Response, rv)
128 # prefer the status if it was provided
129 if status is not None:
130 if isinstance(status, (str, bytes, bytearray)):
131 rv.status = status
132 else:
133 rv.status_code = status
134
135 # extend existing headers with provided headers
136 if headers:
137 rv.headers.update(headers) # type: ignore[arg-type]
138
139 return rv
Path 9: 1 calls (0.0)
tuple (1)
Response (1)
1def make_response(self, rv: ft.ResponseReturnValue) -> Response:
2 """Convert the return value from a view function to an instance of
3 :attr:`response_class`.
4
5 :param rv: the return value from the view function. The view function
6 must return a response. Returning ``None``, or the view ending
7 without returning, is not allowed. The following types are allowed
8 for ``view_rv``:
9
10 ``str``
11 A response object is created with the string encoded to UTF-8
12 as the body.
13
14 ``bytes``
15 A response object is created with the bytes as the body.
16
17 ``dict``
18 A dictionary that will be jsonify'd before being returned.
19
20 ``list``
21 A list that will be jsonify'd before being returned.
22
23 ``generator`` or ``iterator``
24 A generator that returns ``str`` or ``bytes`` to be
25 streamed as the response.
26
27 ``tuple``
28 Either ``(body, status, headers)``, ``(body, status)``, or
29 ``(body, headers)``, where ``body`` is any of the other types
30 allowed here, ``status`` is a string or an integer, and
31 ``headers`` is a dictionary or a list of ``(key, value)``
32 tuples. If ``body`` is a :attr:`response_class` instance,
33 ``status`` overwrites the exiting value and ``headers`` are
34 extended.
35
36 :attr:`response_class`
37 The object is returned unchanged.
38
39 other :class:`~werkzeug.wrappers.Response` class
40 The object is coerced to :attr:`response_class`.
41
42 :func:`callable`
43 The function is called as a WSGI application. The result is
44 used to create a response object.
45
46 .. versionchanged:: 2.2
47 A generator will be converted to a streaming response.
48 A list will be converted to a JSON response.
49
50 .. versionchanged:: 1.1
51 A dict will be converted to a JSON response.
52
53 .. versionchanged:: 0.9
54 Previously a tuple was interpreted as the arguments for the
55 response object.
56 """
57
58 status = headers = None
59
60 # unpack tuple returns
61 if isinstance(rv, tuple):
62 len_rv = len(rv)
63
64 # a 3-tuple is unpacked directly
65 if len_rv == 3:
66 rv, status, headers = rv # type: ignore[misc]
67 # decide if a 2-tuple has status or headers
68 elif len_rv == 2:
69 if isinstance(rv[1], (Headers, dict, tuple, list)):
70 rv, headers = rv
71 else:
72 rv, status = rv # type: ignore[assignment,misc]
73 # other sized tuples are not allowed
74 else:
75 raise TypeError(
76 "The view function did not return a valid response tuple."
77 " The tuple must have the form (body, status, headers),"
78 " (body, status), or (body, headers)."
79 )
80
81 # the body must not be None
82 if rv is None:
83 raise TypeError(
84 f"The view function for {request.endpoint!r} did not"
85 " return a valid response. The function either returned"
86 " None or ended without a return statement."
87 )
88
89 # make sure the body is an instance of the response class
90 if not isinstance(rv, self.response_class):
91 if isinstance(rv, (str, bytes, bytearray)) or isinstance(rv, _abc_Iterator):
92 # let the response class set the status and headers instead of
93 # waiting to do it manually, so that the class can handle any
94 # special logic
95 rv = self.response_class(
96 rv,
97 status=status,
98 headers=headers, # type: ignore[arg-type]
99 )
100 status = headers = None
101 elif isinstance(rv, (dict, list)):
102 rv = self.json.response(rv)
103 elif isinstance(rv, BaseResponse) or callable(rv):
104 # evaluate a WSGI callable, or coerce a different response
105 # class to the correct type
106 try:
107 rv = self.response_class.force_type(
108 rv, request.environ # type: ignore[arg-type]
109 )
110 except TypeError as e:
111 raise TypeError(
112 f"{e}\nThe view function did not return a valid"
113 " response. The return type must be a string,"
114 " dict, list, tuple with headers or status,"
115 " Response instance, or WSGI callable, but it"
116 f" was a {type(rv).__name__}."
117 ).with_traceback(sys.exc_info()[2]) from None
118 else:
119 raise TypeError(
120 "The view function did not return a valid"
121 " response. The return type must be a string,"
122 " dict, list, tuple with headers or status,"
123 " Response instance, or WSGI callable, but it was a"
124 f" {type(rv).__name__}."
125 )
126
127 rv = t.cast(Response, rv)
128 # prefer the status if it was provided
129 if status is not None:
130 if isinstance(status, (str, bytes, bytearray)):
131 rv.status = status
132 else:
133 rv.status_code = status
134
135 # extend existing headers with provided headers
136 if headers:
137 rv.headers.update(headers) # type: ignore[arg-type]
138
139 return rv
Path 10: 1 calls (0.0)
tuple (1)
Response (1)
1def make_response(self, rv: ft.ResponseReturnValue) -> Response:
2 """Convert the return value from a view function to an instance of
3 :attr:`response_class`.
4
5 :param rv: the return value from the view function. The view function
6 must return a response. Returning ``None``, or the view ending
7 without returning, is not allowed. The following types are allowed
8 for ``view_rv``:
9
10 ``str``
11 A response object is created with the string encoded to UTF-8
12 as the body.
13
14 ``bytes``
15 A response object is created with the bytes as the body.
16
17 ``dict``
18 A dictionary that will be jsonify'd before being returned.
19
20 ``list``
21 A list that will be jsonify'd before being returned.
22
23 ``generator`` or ``iterator``
24 A generator that returns ``str`` or ``bytes`` to be
25 streamed as the response.
26
27 ``tuple``
28 Either ``(body, status, headers)``, ``(body, status)``, or
29 ``(body, headers)``, where ``body`` is any of the other types
30 allowed here, ``status`` is a string or an integer, and
31 ``headers`` is a dictionary or a list of ``(key, value)``
32 tuples. If ``body`` is a :attr:`response_class` instance,
33 ``status`` overwrites the exiting value and ``headers`` are
34 extended.
35
36 :attr:`response_class`
37 The object is returned unchanged.
38
39 other :class:`~werkzeug.wrappers.Response` class
40 The object is coerced to :attr:`response_class`.
41
42 :func:`callable`
43 The function is called as a WSGI application. The result is
44 used to create a response object.
45
46 .. versionchanged:: 2.2
47 A generator will be converted to a streaming response.
48 A list will be converted to a JSON response.
49
50 .. versionchanged:: 1.1
51 A dict will be converted to a JSON response.
52
53 .. versionchanged:: 0.9
54 Previously a tuple was interpreted as the arguments for the
55 response object.
56 """
57
58 status = headers = None
59
60 # unpack tuple returns
61 if isinstance(rv, tuple):
62 len_rv = len(rv)
63
64 # a 3-tuple is unpacked directly
65 if len_rv == 3:
66 rv, status, headers = rv # type: ignore[misc]
67 # decide if a 2-tuple has status or headers
68 elif len_rv == 2:
69 if isinstance(rv[1], (Headers, dict, tuple, list)):
70 rv, headers = rv
71 else:
72 rv, status = rv # type: ignore[assignment,misc]
73 # other sized tuples are not allowed
74 else:
75 raise TypeError(
76 "The view function did not return a valid response tuple."
77 " The tuple must have the form (body, status, headers),"
78 " (body, status), or (body, headers)."
79 )
80
81 # the body must not be None
82 if rv is None:
83 raise TypeError(
84 f"The view function for {request.endpoint!r} did not"
85 " return a valid response. The function either returned"
86 " None or ended without a return statement."
87 )
88
89 # make sure the body is an instance of the response class
90 if not isinstance(rv, self.response_class):
91 if isinstance(rv, (str, bytes, bytearray)) or isinstance(rv, _abc_Iterator):
92 # let the response class set the status and headers instead of
93 # waiting to do it manually, so that the class can handle any
94 # special logic
95 rv = self.response_class(
96 rv,
97 status=status,
98 headers=headers, # type: ignore[arg-type]
99 )
100 status = headers = None
101 elif isinstance(rv, (dict, list)):
102 rv = self.json.response(rv)
103 elif isinstance(rv, BaseResponse) or callable(rv):
104 # evaluate a WSGI callable, or coerce a different response
105 # class to the correct type
106 try:
107 rv = self.response_class.force_type(
108 rv, request.environ # type: ignore[arg-type]
109 )
110 except TypeError as e:
111 raise TypeError(
112 f"{e}\nThe view function did not return a valid"
113 " response. The return type must be a string,"
114 " dict, list, tuple with headers or status,"
115 " Response instance, or WSGI callable, but it"
116 f" was a {type(rv).__name__}."
117 ).with_traceback(sys.exc_info()[2]) from None
118 else:
119 raise TypeError(
120 "The view function did not return a valid"
121 " response. The return type must be a string,"
122 " dict, list, tuple with headers or status,"
123 " Response instance, or WSGI callable, but it was a"
124 f" {type(rv).__name__}."
125 )
126
127 rv = t.cast(Response, rv)
128 # prefer the status if it was provided
129 if status is not None:
130 if isinstance(status, (str, bytes, bytearray)):
131 rv.status = status
132 else:
133 rv.status_code = status
134
135 # extend existing headers with provided headers
136 if headers:
137 rv.headers.update(headers) # type: ignore[arg-type]
138
139 return rv
Path 11: 1 calls (0.0)
None (1)
TypeError (1)
1def make_response(self, rv: ft.ResponseReturnValue) -> Response:
2 """Convert the return value from a view function to an instance of
3 :attr:`response_class`.
4
5 :param rv: the return value from the view function. The view function
6 must return a response. Returning ``None``, or the view ending
7 without returning, is not allowed. The following types are allowed
8 for ``view_rv``:
9
10 ``str``
11 A response object is created with the string encoded to UTF-8
12 as the body.
13
14 ``bytes``
15 A response object is created with the bytes as the body.
16
17 ``dict``
18 A dictionary that will be jsonify'd before being returned.
19
20 ``list``
21 A list that will be jsonify'd before being returned.
22
23 ``generator`` or ``iterator``
24 A generator that returns ``str`` or ``bytes`` to be
25 streamed as the response.
26
27 ``tuple``
28 Either ``(body, status, headers)``, ``(body, status)``, or
29 ``(body, headers)``, where ``body`` is any of the other types
30 allowed here, ``status`` is a string or an integer, and
31 ``headers`` is a dictionary or a list of ``(key, value)``
32 tuples. If ``body`` is a :attr:`response_class` instance,
33 ``status`` overwrites the exiting value and ``headers`` are
34 extended.
35
36 :attr:`response_class`
37 The object is returned unchanged.
38
39 other :class:`~werkzeug.wrappers.Response` class
40 The object is coerced to :attr:`response_class`.
41
42 :func:`callable`
43 The function is called as a WSGI application. The result is
44 used to create a response object.
45
46 .. versionchanged:: 2.2
47 A generator will be converted to a streaming response.
48 A list will be converted to a JSON response.
49
50 .. versionchanged:: 1.1
51 A dict will be converted to a JSON response.
52
53 .. versionchanged:: 0.9
54 Previously a tuple was interpreted as the arguments for the
55 response object.
56 """
57
58 status = headers = None
59
60 # unpack tuple returns
61 if isinstance(rv, tuple):
62 len_rv = len(rv)
63
64 # a 3-tuple is unpacked directly
65 if len_rv == 3:
66 rv, status, headers = rv # type: ignore[misc]
67 # decide if a 2-tuple has status or headers
68 elif len_rv == 2:
69 if isinstance(rv[1], (Headers, dict, tuple, list)):
70 rv, headers = rv
71 else:
72 rv, status = rv # type: ignore[assignment,misc]
73 # other sized tuples are not allowed
74 else:
75 raise TypeError(
76 "The view function did not return a valid response tuple."
77 " The tuple must have the form (body, status, headers),"
78 " (body, status), or (body, headers)."
79 )
80
81 # the body must not be None
82 if rv is None:
83 raise TypeError(
84 f"The view function for {request.endpoint!r} did not"
85 " return a valid response. The function either returned"
86 " None or ended without a return statement."
87 )
88
89 # make sure the body is an instance of the response class
90 if not isinstance(rv, self.response_class):
91 if isinstance(rv, (str, bytes, bytearray)) or isinstance(rv, _abc_Iterator):
92 # let the response class set the status and headers instead of
93 # waiting to do it manually, so that the class can handle any
94 # special logic
95 rv = self.response_class(
96 rv,
97 status=status,
98 headers=headers, # type: ignore[arg-type]
99 )
100 status = headers = None
101 elif isinstance(rv, (dict, list)):
102 rv = self.json.response(rv)
103 elif isinstance(rv, BaseResponse) or callable(rv):
104 # evaluate a WSGI callable, or coerce a different response
105 # class to the correct type
106 try:
107 rv = self.response_class.force_type(
108 rv, request.environ # type: ignore[arg-type]
109 )
110 except TypeError as e:
111 raise TypeError(
112 f"{e}\nThe view function did not return a valid"
113 " response. The return type must be a string,"
114 " dict, list, tuple with headers or status,"
115 " Response instance, or WSGI callable, but it"
116 f" was a {type(rv).__name__}."
117 ).with_traceback(sys.exc_info()[2]) from None
118 else:
119 raise TypeError(
120 "The view function did not return a valid"
121 " response. The return type must be a string,"
122 " dict, list, tuple with headers or status,"
123 " Response instance, or WSGI callable, but it was a"
124 f" {type(rv).__name__}."
125 )
126
127 rv = t.cast(Response, rv)
128 # prefer the status if it was provided
129 if status is not None:
130 if isinstance(status, (str, bytes, bytearray)):
131 rv.status = status
132 else:
133 rv.status_code = status
134
135 # extend existing headers with provided headers
136 if headers:
137 rv.headers.update(headers) # type: ignore[arg-type]
138
139 return rv
Path 12: 1 calls (0.0)
True (1)
TypeError (1)
1def make_response(self, rv: ft.ResponseReturnValue) -> Response:
2 """Convert the return value from a view function to an instance of
3 :attr:`response_class`.
4
5 :param rv: the return value from the view function. The view function
6 must return a response. Returning ``None``, or the view ending
7 without returning, is not allowed. The following types are allowed
8 for ``view_rv``:
9
10 ``str``
11 A response object is created with the string encoded to UTF-8
12 as the body.
13
14 ``bytes``
15 A response object is created with the bytes as the body.
16
17 ``dict``
18 A dictionary that will be jsonify'd before being returned.
19
20 ``list``
21 A list that will be jsonify'd before being returned.
22
23 ``generator`` or ``iterator``
24 A generator that returns ``str`` or ``bytes`` to be
25 streamed as the response.
26
27 ``tuple``
28 Either ``(body, status, headers)``, ``(body, status)``, or
29 ``(body, headers)``, where ``body`` is any of the other types
30 allowed here, ``status`` is a string or an integer, and
31 ``headers`` is a dictionary or a list of ``(key, value)``
32 tuples. If ``body`` is a :attr:`response_class` instance,
33 ``status`` overwrites the exiting value and ``headers`` are
34 extended.
35
36 :attr:`response_class`
37 The object is returned unchanged.
38
39 other :class:`~werkzeug.wrappers.Response` class
40 The object is coerced to :attr:`response_class`.
41
42 :func:`callable`
43 The function is called as a WSGI application. The result is
44 used to create a response object.
45
46 .. versionchanged:: 2.2
47 A generator will be converted to a streaming response.
48 A list will be converted to a JSON response.
49
50 .. versionchanged:: 1.1
51 A dict will be converted to a JSON response.
52
53 .. versionchanged:: 0.9
54 Previously a tuple was interpreted as the arguments for the
55 response object.
56 """
57
58 status = headers = None
59
60 # unpack tuple returns
61 if isinstance(rv, tuple):
62 len_rv = len(rv)
63
64 # a 3-tuple is unpacked directly
65 if len_rv == 3:
66 rv, status, headers = rv # type: ignore[misc]
67 # decide if a 2-tuple has status or headers
68 elif len_rv == 2:
69 if isinstance(rv[1], (Headers, dict, tuple, list)):
70 rv, headers = rv
71 else:
72 rv, status = rv # type: ignore[assignment,misc]
73 # other sized tuples are not allowed
74 else:
75 raise TypeError(
76 "The view function did not return a valid response tuple."
77 " The tuple must have the form (body, status, headers),"
78 " (body, status), or (body, headers)."
79 )
80
81 # the body must not be None
82 if rv is None:
83 raise TypeError(
84 f"The view function for {request.endpoint!r} did not"
85 " return a valid response. The function either returned"
86 " None or ended without a return statement."
87 )
88
89 # make sure the body is an instance of the response class
90 if not isinstance(rv, self.response_class):
91 if isinstance(rv, (str, bytes, bytearray)) or isinstance(rv, _abc_Iterator):
92 # let the response class set the status and headers instead of
93 # waiting to do it manually, so that the class can handle any
94 # special logic
95 rv = self.response_class(
96 rv,
97 status=status,
98 headers=headers, # type: ignore[arg-type]
99 )
100 status = headers = None
101 elif isinstance(rv, (dict, list)):
102 rv = self.json.response(rv)
103 elif isinstance(rv, BaseResponse) or callable(rv):
104 # evaluate a WSGI callable, or coerce a different response
105 # class to the correct type
106 try:
107 rv = self.response_class.force_type(
108 rv, request.environ # type: ignore[arg-type]
109 )
110 except TypeError as e:
111 raise TypeError(
112 f"{e}\nThe view function did not return a valid"
113 " response. The return type must be a string,"
114 " dict, list, tuple with headers or status,"
115 " Response instance, or WSGI callable, but it"
116 f" was a {type(rv).__name__}."
117 ).with_traceback(sys.exc_info()[2]) from None
118 else:
119 raise TypeError(
120 "The view function did not return a valid"
121 " response. The return type must be a string,"
122 " dict, list, tuple with headers or status,"
123 " Response instance, or WSGI callable, but it was a"
124 f" {type(rv).__name__}."
125 )
126
127 rv = t.cast(Response, rv)
128 # prefer the status if it was provided
129 if status is not None:
130 if isinstance(status, (str, bytes, bytearray)):
131 rv.status = status
132 else:
133 rv.status_code = status
134
135 # extend existing headers with provided headers
136 if headers:
137 rv.headers.update(headers) # type: ignore[arg-type]
138
139 return rv
Path 13: 1 calls (0.0)
test_response_type_errors.
TypeError (1)
1def make_response(self, rv: ft.ResponseReturnValue) -> Response:
2 """Convert the return value from a view function to an instance of
3 :attr:`response_class`.
4
5 :param rv: the return value from the view function. The view function
6 must return a response. Returning ``None``, or the view ending
7 without returning, is not allowed. The following types are allowed
8 for ``view_rv``:
9
10 ``str``
11 A response object is created with the string encoded to UTF-8
12 as the body.
13
14 ``bytes``
15 A response object is created with the bytes as the body.
16
17 ``dict``
18 A dictionary that will be jsonify'd before being returned.
19
20 ``list``
21 A list that will be jsonify'd before being returned.
22
23 ``generator`` or ``iterator``
24 A generator that returns ``str`` or ``bytes`` to be
25 streamed as the response.
26
27 ``tuple``
28 Either ``(body, status, headers)``, ``(body, status)``, or
29 ``(body, headers)``, where ``body`` is any of the other types
30 allowed here, ``status`` is a string or an integer, and
31 ``headers`` is a dictionary or a list of ``(key, value)``
32 tuples. If ``body`` is a :attr:`response_class` instance,
33 ``status`` overwrites the exiting value and ``headers`` are
34 extended.
35
36 :attr:`response_class`
37 The object is returned unchanged.
38
39 other :class:`~werkzeug.wrappers.Response` class
40 The object is coerced to :attr:`response_class`.
41
42 :func:`callable`
43 The function is called as a WSGI application. The result is
44 used to create a response object.
45
46 .. versionchanged:: 2.2
47 A generator will be converted to a streaming response.
48 A list will be converted to a JSON response.
49
50 .. versionchanged:: 1.1
51 A dict will be converted to a JSON response.
52
53 .. versionchanged:: 0.9
54 Previously a tuple was interpreted as the arguments for the
55 response object.
56 """
57
58 status = headers = None
59
60 # unpack tuple returns
61 if isinstance(rv, tuple):
62 len_rv = len(rv)
63
64 # a 3-tuple is unpacked directly
65 if len_rv == 3:
66 rv, status, headers = rv # type: ignore[misc]
67 # decide if a 2-tuple has status or headers
68 elif len_rv == 2:
69 if isinstance(rv[1], (Headers, dict, tuple, list)):
70 rv, headers = rv
71 else:
72 rv, status = rv # type: ignore[assignment,misc]
73 # other sized tuples are not allowed
74 else:
75 raise TypeError(
76 "The view function did not return a valid response tuple."
77 " The tuple must have the form (body, status, headers),"
78 " (body, status), or (body, headers)."
79 )
80
81 # the body must not be None
82 if rv is None:
83 raise TypeError(
84 f"The view function for {request.endpoint!r} did not"
85 " return a valid response. The function either returned"
86 " None or ended without a return statement."
87 )
88
89 # make sure the body is an instance of the response class
90 if not isinstance(rv, self.response_class):
91 if isinstance(rv, (str, bytes, bytearray)) or isinstance(rv, _abc_Iterator):
92 # let the response class set the status and headers instead of
93 # waiting to do it manually, so that the class can handle any
94 # special logic
95 rv = self.response_class(
96 rv,
97 status=status,
98 headers=headers, # type: ignore[arg-type]
99 )
100 status = headers = None
101 elif isinstance(rv, (dict, list)):
102 rv = self.json.response(rv)
103 elif isinstance(rv, BaseResponse) or callable(rv):
104 # evaluate a WSGI callable, or coerce a different response
105 # class to the correct type
106 try:
107 rv = self.response_class.force_type(
108 rv, request.environ # type: ignore[arg-type]
109 )
110 except TypeError as e:
111 raise TypeError(
112 f"{e}\nThe view function did not return a valid"
113 " response. The return type must be a string,"
114 " dict, list, tuple with headers or status,"
115 " Response instance, or WSGI callable, but it"
116 f" was a {type(rv).__name__}."
117 ).with_traceback(sys.exc_info()[2]) from None
118 else:
119 raise TypeError(
120 "The view function did not return a valid"
121 " response. The return type must be a string,"
122 " dict, list, tuple with headers or status,"
123 " Response instance, or WSGI callable, but it was a"
124 f" {type(rv).__name__}."
125 )
126
127 rv = t.cast(Response, rv)
128 # prefer the status if it was provided
129 if status is not None:
130 if isinstance(status, (str, bytes, bytearray)):
131 rv.status = status
132 else:
133 rv.status_code = status
134
135 # extend existing headers with provided headers
136 if headers:
137 rv.headers.update(headers) # type: ignore[arg-type]
138
139 return rv
Path 14: 1 calls (0.0)
tuple (1)
Response (1)
1def make_response(self, rv: ft.ResponseReturnValue) -> Response:
2 """Convert the return value from a view function to an instance of
3 :attr:`response_class`.
4
5 :param rv: the return value from the view function. The view function
6 must return a response. Returning ``None``, or the view ending
7 without returning, is not allowed. The following types are allowed
8 for ``view_rv``:
9
10 ``str``
11 A response object is created with the string encoded to UTF-8
12 as the body.
13
14 ``bytes``
15 A response object is created with the bytes as the body.
16
17 ``dict``
18 A dictionary that will be jsonify'd before being returned.
19
20 ``list``
21 A list that will be jsonify'd before being returned.
22
23 ``generator`` or ``iterator``
24 A generator that returns ``str`` or ``bytes`` to be
25 streamed as the response.
26
27 ``tuple``
28 Either ``(body, status, headers)``, ``(body, status)``, or
29 ``(body, headers)``, where ``body`` is any of the other types
30 allowed here, ``status`` is a string or an integer, and
31 ``headers`` is a dictionary or a list of ``(key, value)``
32 tuples. If ``body`` is a :attr:`response_class` instance,
33 ``status`` overwrites the exiting value and ``headers`` are
34 extended.
35
36 :attr:`response_class`
37 The object is returned unchanged.
38
39 other :class:`~werkzeug.wrappers.Response` class
40 The object is coerced to :attr:`response_class`.
41
42 :func:`callable`
43 The function is called as a WSGI application. The result is
44 used to create a response object.
45
46 .. versionchanged:: 2.2
47 A generator will be converted to a streaming response.
48 A list will be converted to a JSON response.
49
50 .. versionchanged:: 1.1
51 A dict will be converted to a JSON response.
52
53 .. versionchanged:: 0.9
54 Previously a tuple was interpreted as the arguments for the
55 response object.
56 """
57
58 status = headers = None
59
60 # unpack tuple returns
61 if isinstance(rv, tuple):
62 len_rv = len(rv)
63
64 # a 3-tuple is unpacked directly
65 if len_rv == 3:
66 rv, status, headers = rv # type: ignore[misc]
67 # decide if a 2-tuple has status or headers
68 elif len_rv == 2:
69 if isinstance(rv[1], (Headers, dict, tuple, list)):
70 rv, headers = rv
71 else:
72 rv, status = rv # type: ignore[assignment,misc]
73 # other sized tuples are not allowed
74 else:
75 raise TypeError(
76 "The view function did not return a valid response tuple."
77 " The tuple must have the form (body, status, headers),"
78 " (body, status), or (body, headers)."
79 )
80
81 # the body must not be None
82 if rv is None:
83 raise TypeError(
84 f"The view function for {request.endpoint!r} did not"
85 " return a valid response. The function either returned"
86 " None or ended without a return statement."
87 )
88
89 # make sure the body is an instance of the response class
90 if not isinstance(rv, self.response_class):
91 if isinstance(rv, (str, bytes, bytearray)) or isinstance(rv, _abc_Iterator):
92 # let the response class set the status and headers instead of
93 # waiting to do it manually, so that the class can handle any
94 # special logic
95 rv = self.response_class(
96 rv,
97 status=status,
98 headers=headers, # type: ignore[arg-type]
99 )
100 status = headers = None
101 elif isinstance(rv, (dict, list)):
102 rv = self.json.response(rv)
103 elif isinstance(rv, BaseResponse) or callable(rv):
104 # evaluate a WSGI callable, or coerce a different response
105 # class to the correct type
106 try:
107 rv = self.response_class.force_type(
108 rv, request.environ # type: ignore[arg-type]
109 )
110 except TypeError as e:
111 raise TypeError(
112 f"{e}\nThe view function did not return a valid"
113 " response. The return type must be a string,"
114 " dict, list, tuple with headers or status,"
115 " Response instance, or WSGI callable, but it"
116 f" was a {type(rv).__name__}."
117 ).with_traceback(sys.exc_info()[2]) from None
118 else:
119 raise TypeError(
120 "The view function did not return a valid"
121 " response. The return type must be a string,"
122 " dict, list, tuple with headers or status,"
123 " Response instance, or WSGI callable, but it was a"
124 f" {type(rv).__name__}."
125 )
126
127 rv = t.cast(Response, rv)
128 # prefer the status if it was provided
129 if status is not None:
130 if isinstance(status, (str, bytes, bytearray)):
131 rv.status = status
132 else:
133 rv.status_code = status
134
135 # extend existing headers with provided headers
136 if headers:
137 rv.headers.update(headers) # type: ignore[arg-type]
138
139 return rv