diff --git a/CHANGES b/CHANGES index 8699718c49..99d0013417 100644 --- a/CHANGES +++ b/CHANGES @@ -24,6 +24,8 @@ Version 0.10.2 - Raise an :exc:`AttributeError` in :func:`flask.helpers.find_package` with a useful message explaining why it is raised when a PEP 302 import hook is used without an `is_package()` method. +- Fixed an issue causing exceptions raised before entering a request or app + context to be passed to teardown handlers. Version 0.10.1 -------------- diff --git a/flask/ctx.py b/flask/ctx.py index f13423789c..363e7f6e3b 100644 --- a/flask/ctx.py +++ b/flask/ctx.py @@ -163,6 +163,8 @@ def __init__(self, app): def push(self): """Binds the app context to the current context.""" self._refcnt += 1 + if hasattr(sys, 'exc_clear'): + sys.exc_clear() _app_ctx_stack.push(self) appcontext_pushed.send(self.app) @@ -312,6 +314,9 @@ def push(self): else: self._implicit_app_ctx_stack.append(None) + if hasattr(sys, 'exc_clear'): + sys.exc_clear() + _request_ctx_stack.push(self) # Open the session at the moment that the request context is diff --git a/flask/testsuite/appctx.py b/flask/testsuite/appctx.py index 8524d22ba8..6c3d0595e0 100644 --- a/flask/testsuite/appctx.py +++ b/flask/testsuite/appctx.py @@ -63,6 +63,23 @@ def cleanup(exception): self.assert_equal(cleanup_stuff, [None]) + def test_app_tearing_down_with_previous_exception(self): + cleanup_stuff = [] + app = flask.Flask(__name__) + @app.teardown_appcontext + def cleanup(exception): + cleanup_stuff.append(exception) + + try: + raise Exception('dummy') + except Exception: + pass + + with app.app_context(): + pass + + self.assert_equal(cleanup_stuff, [None]) + def test_custom_app_ctx_globals_class(self): class CustomRequestGlobals(object): def __init__(self): diff --git a/flask/testsuite/reqctx.py b/flask/testsuite/reqctx.py index c232a74ca9..38016b5e05 100644 --- a/flask/testsuite/reqctx.py +++ b/flask/testsuite/reqctx.py @@ -33,6 +33,22 @@ def end_of_request(exception): ctx.pop() self.assert_equal(buffer, [None]) + def test_teardown_with_previous_exception(self): + buffer = [] + app = flask.Flask(__name__) + @app.teardown_request + def end_of_request(exception): + buffer.append(exception) + + try: + raise Exception('dummy') + except Exception: + pass + + with app.test_request_context(): + self.assert_equal(buffer, []) + self.assert_equal(buffer, [None]) + def test_proper_test_request_context(self): app = flask.Flask(__name__) app.config.update(