Merge branch 'mjpieters-appcontext_ignore_handled_exception'
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Markus Unterwaditzer · committed by Markus Unterwaditzer · parents ecb8f81, f0de0a6 · Browse files · 6 files·+48 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
6 files+48 added−8 removed
- tests:+30 added−0 removed
- other:+18 added−8 removed
Provenance
- Commit
33bad01- Tree
8af677b- Author
Markus Unterwaditzer <markus@unterwaditzer.net>- Committer
Markus Unterwaditzer <markus@unterwaditzer.net>- Parent 1
ecb8f81- Parent 2
f0de0a6
6 files changed+48 added−8 removed
modified:flask/app.py+7 added-4 removed
36 unmodified lines37 38 # a lock used for logger initialization39 _logger_lock = Lock()40 41+# a singleton sentinel value for parameter defaults42+_sentinel = object()43 44+45 def _make_timedelta(value):46 if not isinstance(value, timedelta):47 return timedelta(seconds=value)48 return value1727 unmodified lines1776 if not self.session_interface.is_null_session(ctx.session):1777 self.save_session(ctx.session, response)1778 return response1779 1777- 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 a2 unmodified lines1787 .. 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 line1797 for func in funcs:1798 func(exc)1799 request_tearing_down.send(self, exc=exc)1800 1798- 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.1805 1806 .. 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 linesmodified:flask/ctx.py+8 added-4 removed
19 unmodified lines20 from .signals import appcontext_pushed, appcontext_popped21 from ._compat import BROKEN_PYPY_CTXMGR_EXIT, reraise22 23 24+# a singleton sentinel value for parameter defaults25+_sentinel = object()26+27+28 class _AppCtxGlobals(object):29 """A plain object."""30 31 def get(self, name, default=None):139 unmodified lines171 sys.exc_clear()172 _app_ctx_stack.push(self)173 appcontext_pushed.send(self.app)174 171- 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 lines323 self.session = self.app.open_session(self.request)324 if self.session is None:325 self.session = self.app.make_null_session()326 323- 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 lines337 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)344 345 # If this interpreter supports clearing the exception information54 unmodified linesmodified:tests/test_appctx.py+15 added
76 unmodified lines77 pass78 79 assert cleanup_stuff == [None]80 81+def 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)87+88+ with app.app_context():89+ try:90+ raise Exception('dummy')91+ except Exception:92+ pass93+94+ assert cleanup_stuff == [None]95+96 def test_custom_app_ctx_globals_class():97 class CustomRequestGlobals(object):98 def __init__(self):99 self.spam = 'eggs'27 unmodified linesmodified:tests/test_reqctx.py+15 added
46 unmodified lines47 with app.test_request_context():48 assert buffer == []49 assert buffer == [None]50 51+def 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)57+58+ with app.test_request_context():59+ assert buffer == []60+ try:61+ raise Exception('dummy')62+ except Exception:63+ pass64+ assert buffer == [None]65+66 def test_proper_test_request_context():67 app = flask.Flask(__name__)68 app.config.update(69 SERVER_NAME='localhost.localdomain:5000'130 unmodified linesmodified:AUTHORS+1 added
20 unmodified lines21 - Justin Quick22 - Kenneth Reitz23 - Keyan Pishdadian24 - Marian Sigler25+- Martijn Pieters26 - Matt Campell27 - Matthew Frazier28 - Michael van Tellingen29 - Ron DuPlain5 unmodified linesmodified:CHANGES+2 added
61 unmodified lines62 the user registered an overriding rule with the lowercase-version63 ``options`` (issue ``#1288``).64 - ``flask.json.jsonify`` now supports the ``datetime.date`` type (pull request65 ``#1326``).66+- Don't leak exception info of already catched exceptions to context teardown67+ handlers (pull request ``#1393``).68 69 Version 0.10.270 --------------71 478 unmodified lines