Changed the implementation of returning tuples from functions
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 3249eeb · Browse files · 5 files·+61 added−33 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+61 added−33 removed
- other:+61 added−33 removed
Provenance
- Commit
cf1641e- Tree
226e934- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
3249eeb
5 files changed+61 added−33 removed
modified:docs/quickstart.rst+5 added-2 removed
672 unmodified lines673 1. If a response object of the correct type is returned it's directly674 returned from the view.675 2. If it's a string, a response object is created with that data and the676 default parameters.677-3. If a tuple is returned the response object is created by passing the678- tuple as arguments to the response object's constructor.677+3. If a tuple is returned the items in the tuple can provide extra678+ information. Such tuples have to be in the form ``(response, status,679+ headers)`` where at least one item has to be in the tuple. The680+ `status` value will override the status code and `headers` can be a681+ list or dictionary of additional header values.682 4. If none of that works, Flask will assume the return value is a683 valid WSGI application and convert that into a response object.684 685 If you want to get hold of the resulting response object inside the view168 unmodified linesmodified:docs/upgrading.rst+15 added
17 unmodified lines18 installation, make sure to pass it the ``-U`` parameter::19 20 $ easy_install -U Flask21 22+Version 0.923+-----------24+25+The behavior of returning tuples from a function was simplified. If you26+return a tuple it no longer defines the arguments for the response object27+you're creating, it's now always a tuple in the form ``(response, status,28+headers)`` where at least one item has to be provided. If you depend on29+the old behavior, you can add it easily by subclassing Flask::30+31+ class TraditionalFlask(Flask):32+ def make_response(self, rv):33+ if isinstance(rv, tuple):34+ return self.response_class(*rv)35+ return Flask.make_response(self, rv)36+37 Version 0.838 -----------39 40 Flask introduced a new session interface system. We also noticed that299 unmodified linesmodified:flask/testsuite/basic.py+6 added-9 removed
629 unmodified lines630 def from_string():631 return u'Hällo Wörld'.encode('utf-8')632 @app.route('/args')633 def from_tuple():634- return 'Meh', 400, {'X-Foo': 'Testing'}, 'text/plain'634+ return 'Meh', 400, {635+ 'X-Foo': 'Testing',636+ 'Content-Type': 'text/plain; charset=utf-8'637+ }638 c = app.test_client()639 self.assert_equal(c.get('/unicode').data, u'Hällo Wörld'.encode('utf-8'))640 self.assert_equal(c.get('/string').data, u'Hällo Wörld'.encode('utf-8'))641 rv = c.get('/args')37 unmodified lines679 self.assertEqual(rv.mimetype, 'text/html')680 681 rv = flask.make_response(682 flask.Response('', headers={'Content-Type': 'text/html'}),680- 400, None, 'application/json')683+ 400, [('X-Foo', 'bar')])684 self.assertEqual(rv.status_code, 400)682- self.assertEqual(rv.headers['Content-Type'], 'application/json')683-684- rv = flask.make_response(685- flask.Response('', mimetype='application/json'),686- 400, {'Content-Type': 'text/html'})687- self.assertEqual(rv.status_code, 400)685 self.assertEqual(rv.headers['Content-Type'], 'text/html')686+ self.assertEqual(rv.headers['X-Foo'], 'bar')687 690-688 def test_url_generation(self):689 app = flask.Flask(__name__)690 @app.route('/hello/<name>', methods=['POST'])691 def hello():455 unmodified linesmodified:flask/app.py+33 added-22 removed
1352 unmodified lines1353 :class:`str` a response object is created with the1354 string as body1355 :class:`unicode` a response object is created with the1356 string encoded to utf-8 as body1357- :class:`tuple` the response object is created with the1358- contents of the tuple as arguments1357 a WSGI function the function is called as WSGI application1358 and buffered as response object1359+ :class:`tuple` A tuple in the form ``(response, status,1360+ headers)`` where `response` is any of the1361+ types defined here, `status` is a string1362+ or an integer and `headers` is a list of1363+ a dictionary with header values.1364 ======================= ===========================================1365 1366 :param rv: the return value from the view function1367+1368+ .. versionchanged:: 0.91369+ Previously a tuple was interpreted as the arguments for the1370+ response object.1371 """1372+ status = headers = None1373+ if isinstance(rv, tuple):1374+ rv, status, headers = rv + (None,) * (3 - len(rv))1375+1376 if rv is None:1377 raise ValueError('View function did not return a response')1367- if isinstance(rv, self.response_class):1368- return rv1369- if isinstance(rv, basestring):1370- return self.response_class(rv)1371- if isinstance(rv, tuple):1372- if len(rv) > 0 and isinstance(rv[0], self.response_class):1373- original = rv[0]1374- new_response = self.response_class('', *rv[1:])1375- if len(rv) < 3:1376- # The args for the response class are1377- # response=None, status=None, headers=None,1378- # mimetype=None, content_type=None, ...1379- # so if there's at least 3 elements the rv1380- # tuple contains header information so the1381- # headers from rv[0] "win."1382- new_response.headers = original.headers1383- new_response.response = original.response1384- return new_response1378+1379+ if not isinstance(rv, self.response_class):1380+ # When we create a response object directly, we let the constructor1381+ # set the headers and status. We do this because there can be1382+ # some extra logic involved when creating these objects with1383+ # specific values (like defualt content type selection).1384+ if isinstance(rv, basestring):1385+ rv = self.response_class(rv, headers=headers, status=status)1386+ headers = status = None1387 else:1386- return self.response_class(*rv)1387- return self.response_class.force_type(rv, request.environ)1388+ rv = self.response_class.force_type(rv, request.environ)1389 1390+ if status is not None:1391+ if isinstance(status, basestring):1392+ rv.status = status1393+ else:1394+ rv.status_code = status1395+ if headers:1396+ rv.headers.extend(headers)1397+1398+ return rv1399+1400 def create_url_adapter(self, request):1401 """Creates a URL adapter for the given request. The URL adapter1402 is created at a point where the request context is not yet set up1403 so the request is passed explicitly.201 unmodified linesmodified:CHANGES+2 added
61 unmodified lines62 ``SEND_FILE_MAX_AGE_DEFAULT`` configuration variable, regardless of whether63 the `get_send_file_options` hook is used.64 - Fixed an assumption in sessions implementation which could break message65 flashing on sessions implementations which use external storage.66+- Changed the behavior of tuple return values from functions. They are no67+ longer arguments to the response object, they now have a defined meaning.68 69 Version 0.8.170 -------------71 294 unmodified lines