Skip to content
Myr

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

5 files+61 added−33 removed

modified:docs/quickstart.rst+5 added-2 removed
672 unmodified lines
6731. If a response object of the correct type is returned it's directly674 returned from the view.6752. If it's a string, a response object is created with that data and the676 default parameters.6773. If a tuple is returned the response object is created by passing the678 tuple as arguments to the response object's constructor.6773. 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.6824. If none of that works, Flask will assume the return value is a683 valid WSGI application and convert that into a response object.684685If you want to get hold of the resulting response object inside the view
168 unmodified lines
modified:docs/upgrading.rst+15 added
17 unmodified lines
18installation, make sure to pass it the ``-U`` parameter::1920 $ easy_install -U Flask2122Version 0.923-----------2425The behavior of returning tuples from a function was simplified. If you26return a tuple it no longer defines the arguments for the response object27you're creating, it's now always a tuple in the form ``(response, status,28headers)`` where at least one item has to be provided. If you depend on29the old behavior, you can add it easily by subclassing Flask::3031 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)3637Version 0.838-----------3940Flask introduced a new session interface system. We also noticed that
299 unmodified lines
modified:flask/testsuite/basic.py+6 added-9 removed
629 unmodified lines
630 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 lines
679 self.assertEqual(rv.mimetype, 'text/html')680681 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')683684 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')687690688 def test_url_generation(self):689 app = flask.Flask(__name__)690 @app.route('/hello/<name>', methods=['POST'])691 def hello():
455 unmodified lines
modified:flask/app.py+33 added-22 removed
1352 unmodified lines
1353 :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 ======================= ===========================================13651366 :param rv: the return value from the view function13671368 .. 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))13751376 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_response13781379 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)13891390 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)13971398 return rv13991400 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 lines
modified:CHANGES+2 added
61 unmodified lines
62 ``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.6869Version 0.8.170-------------71
294 unmodified lines