Skip to content
Myr

Fixed request context preservation and teardown handler interaction.

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 6dfe933 · Browse files · 4 files+59 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+59 added−1 removed

  • other:+59 added−1 removed

4 files+59 added−1 removed

modified:flask/testsuite/basic.py+34 added
1068 unmodified lines
1069 flask._request_ctx_stack.top.pop()1070 self.assert_true(flask._request_ctx_stack.top is None)1071 self.assert_true(flask._app_ctx_stack.top is None)10721073 def test_preserve_remembers_exception(self):1074 app = flask.Flask(__name__)1075 app.debug = True1076 errors = []10771078 @app.route('/fail')1079 def fail_func():1080 1 // 010811082 @app.route('/success')1083 def success_func():1084 return 'Okay'10851086 @app.teardown_request1087 def teardown_handler(exc):1088 errors.append(exc)10891090 c = app.test_client()10911092 # After this failure we did not yet call the teardown handler1093 with self.assert_raises(ZeroDivisionError):1094 c.get('/fail')1095 self.assert_equal(errors, [])10961097 # But this request triggers it, and it's an error1098 c.get('/success')1099 self.assert_equal(len(errors), 2)1100 self.assert_true(isinstance(errors[0], ZeroDivisionError))11011102 # At this point another request does nothing.1103 c.get('/success')1104 self.assert_equal(len(errors), 3)1105 self.assert_equal(errors[1], None)110611071108class SubdomainTestCase(FlaskTestCase):11091110 def test_basic_support(self):1111 app = flask.Flask(__name__)
77 unmodified lines
modified:flask/app.py+7 added
1705 unmodified lines
1706 for func in funcs:1707 rv = func(exc)1708 request_tearing_down.send(self, exc=exc)17091710 # If this interpreter supports clearing the exception information1711 # we do that now. This will only go into effect on Python 2.x,1712 # on 3.x it disappears automatically at the end of the exception1713 # stack.1714 if hasattr(sys, 'exc_clear'):1715 sys.exc_clear()17161717 def do_teardown_appcontext(self, exc=None):1718 """Called when an application context is popped. This works pretty1719 much the same as :meth:`do_teardown_request` but for the application1720 context.
122 unmodified lines
modified:flask/ctx.py+15 added-1 removed
233 unmodified lines
234 # indicator if the context was preserved. Next time another context235 # is pushed the preserved context is popped.236 self.preserved = False237238 # remembers the exception for pop if there is one in case the context239 # preservation kicks in.240 self._preserved_exc = None241242 # Functions that should be executed after the request on the response243 # object. These will be called before the regular "after_request"244 # functions.245 self._after_request_functions = []
53 unmodified lines
299 # memory. This is usually only a problem in testsuite since this300 # functionality is not active in production environments.301 top = _request_ctx_stack.top302 if top is not None and top.preserved:299 top.pop()303 top.pop(top._preserved_exc)304305 # Before we push the request context we have to ensure that there306 # is an application context.307 app_ctx = _app_ctx_stack.top
26 unmodified lines
334335 clear_request = False336 if not self._implicit_app_ctx_stack:337 self.preserved = False338 self._preserved_exc = None339 if exc is None:340 exc = sys.exc_info()[1]341 self.app.do_teardown_request(exc)342343 # If this interpreter supports clearing the exception information344 # we do that now. This will only go into effect on Python 2.x,345 # on 3.x it disappears automatically at the end of the exception346 # stack.347 if hasattr(sys, 'exc_clear'):348 sys.exc_clear()349350 request_close = getattr(self.request, 'close', None)351 if request_close is not None:352 request_close()353 clear_request = True
14 unmodified lines
368 def auto_pop(self, exc):369 if self.request.environ.get('flask._preserve_context') or \370 (exc is not None and self.app.preserve_context_on_exception):371 self.preserved = True372 self._preserved_exc = exc373 else:374 self.pop(exc)375376 def __enter__(self):
18 unmodified lines
modified:CHANGES+3 added
60 unmodified lines
61 for Python 3.3 port.62- Changed how the teardown system is informed about exceptions. This is now63 more reliable in case something handles an exception halfway through64 the error handling process.65- Request context preservation in debug mode now keeps the exception66 information around which means that teardown handlers are able to67 distinguish error from success cases.68- Added the ``JSONIFY_PRETTYPRINT_REGULAR`` configuration variable.69- Flask now orders JSON keys by default to not trash HTTP caches due to70 different hash seeds between different workers.71
368 unmodified lines