Skip to content
Myr

Switch away from using None as default value for the exception when tearing down a context.

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

When an exception has been handled when using the request / app context in a with statement, `sys.exc_info()` will still contain the exception information even though it has been handled already. The `__exit__` methods pass in `None` for the exception value in that case, which needs to be distinguisable from the default value for the `exc` parameter. Use a dedicated singleton sentinel value instead.

authored by Martijn Pieters · committed by Martijn Pieters · parent ec5811d · Browse files · 4 files+45 added−8 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+45 added−8 removed

  • tests:+30 added−0 removed
  • other:+15 added−8 removed

4 files+45 added−8 removed

modified:flask/app.py+7 added-4 removed
36 unmodified lines
3738# a lock used for logger initialization39_logger_lock = Lock()4041# a singleton sentinel value for parameter defaults42_sentinel = object()434445def _make_timedelta(value):46 if not isinstance(value, timedelta):47 return timedelta(seconds=value)48 return value
1727 unmodified lines
1776 if not self.session_interface.is_null_session(ctx.session):1777 self.save_session(ctx.session, response)1778 return response17791777 def do_teardown_request(self, exc=None):1780 def do_teardown_request(self, exc=_sentinel):1781 """Called after the actual request dispatching and will1782 call every as :meth:`teardown_request` decorated function. This is1783 not actually called by the :class:`Flask` object itself but is always1784 triggered when the request context is popped. That way we have a
2 unmodified lines
1787 .. versionchanged:: 0.91788 Added the `exc` argument. Previously this was always using the1789 current exception information.1790 """1788 if exc is None:1791 if exc is _sentinel:1792 exc = sys.exc_info()[1]1793 funcs = reversed(self.teardown_request_funcs.get(None, ()))1794 bp = _request_ctx_stack.top.request.blueprint1795 if bp is not None and bp in self.teardown_request_funcs:
1 unmodified line
1797 for func in funcs:1798 func(exc)1799 request_tearing_down.send(self, exc=exc)18001798 def do_teardown_appcontext(self, exc=None):1801 def do_teardown_appcontext(self, exc=_sentinel):1802 """Called when an application context is popped. This works pretty1803 much the same as :meth:`do_teardown_request` but for the application1804 context.18051806 .. versionadded:: 0.91807 """1805 if exc is None:1808 if exc is _sentinel:1809 exc = sys.exc_info()[1]1810 for func in reversed(self.teardown_appcontext_funcs):1811 func(exc)1812 appcontext_tearing_down.send(self, exc=exc)
114 unmodified lines
modified:flask/ctx.py+8 added-4 removed
19 unmodified lines
20from .signals import appcontext_pushed, appcontext_popped21from ._compat import BROKEN_PYPY_CTXMGR_EXIT, reraise222324# a singleton sentinel value for parameter defaults25_sentinel = object()262728class _AppCtxGlobals(object):29 """A plain object."""3031 def get(self, name, default=None):
139 unmodified lines
171 sys.exc_clear()172 _app_ctx_stack.push(self)173 appcontext_pushed.send(self.app)174171 def pop(self, exc=None):175 def pop(self, exc=_sentinel):176 """Pops the app context."""177 self._refcnt -= 1178 if self._refcnt <= 0:175 if exc is None:179 if exc is _sentinel:180 exc = sys.exc_info()[1]181 self.app.do_teardown_appcontext(exc)182 rv = _app_ctx_stack.pop()183 assert rv is self, 'Popped wrong app context. (%r instead of %r)' \
139 unmodified lines
323 self.session = self.app.open_session(self.request)324 if self.session is None:325 self.session = self.app.make_null_session()326323 def pop(self, exc=None):327 def pop(self, exc=_sentinel):328 """Pops the request context and unbinds it by doing that. This will329 also trigger the execution of functions registered by the330 :meth:`~flask.Flask.teardown_request` decorator.331
5 unmodified lines
337 clear_request = False338 if not self._implicit_app_ctx_stack:339 self.preserved = False340 self._preserved_exc = None337 if exc is None:341 if exc is _sentinel:342 exc = sys.exc_info()[1]343 self.app.do_teardown_request(exc)344345 # If this interpreter supports clearing the exception information
54 unmodified lines
modified:tests/test_appctx.py+15 added
76 unmodified lines
77 pass7879 assert cleanup_stuff == [None]8081def test_app_tearing_down_with_handled_exception():82 cleanup_stuff = []83 app = flask.Flask(__name__)84 @app.teardown_appcontext85 def cleanup(exception):86 cleanup_stuff.append(exception)8788 with app.app_context():89 try:90 raise Exception('dummy')91 except Exception:92 pass9394 assert cleanup_stuff == [None]9596def test_custom_app_ctx_globals_class():97 class CustomRequestGlobals(object):98 def __init__(self):99 self.spam = 'eggs'
27 unmodified lines
modified:tests/test_reqctx.py+15 added
46 unmodified lines
47 with app.test_request_context():48 assert buffer == []49 assert buffer == [None]5051def test_teardown_with_handled_exception():52 buffer = []53 app = flask.Flask(__name__)54 @app.teardown_request55 def end_of_request(exception):56 buffer.append(exception)5758 with app.test_request_context():59 assert buffer == []60 try:61 raise Exception('dummy')62 except Exception:63 pass64 assert buffer == [None]6566def test_proper_test_request_context():67 app = flask.Flask(__name__)68 app.config.update(69 SERVER_NAME='localhost.localdomain:5000'
130 unmodified lines