Browse Source

Clear exceptions when pushing a context

Fixes #882
pull/1208/head
Daniel Neuhäuser 11 years ago
parent
commit
a3a2f521f1
  1. 2
      CHANGES
  2. 5
      flask/ctx.py
  3. 17
      flask/testsuite/appctx.py
  4. 16
      flask/testsuite/reqctx.py

2
CHANGES

@ -12,6 +12,8 @@ Version 0.10.2
- Raise an :exc:`AttributeError` in :func:`flask.helpers.find_package` with a - 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 useful message explaining why it is raised when a PEP 302 import hook is used
without an `is_package()` method. 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 Version 0.10.1
-------------- --------------

5
flask/ctx.py

@ -163,6 +163,8 @@ class AppContext(object):
def push(self): def push(self):
"""Binds the app context to the current context.""" """Binds the app context to the current context."""
self._refcnt += 1 self._refcnt += 1
if hasattr(sys, 'exc_clear'):
sys.exc_clear()
_app_ctx_stack.push(self) _app_ctx_stack.push(self)
appcontext_pushed.send(self.app) appcontext_pushed.send(self.app)
@ -312,6 +314,9 @@ class RequestContext(object):
else: else:
self._implicit_app_ctx_stack.append(None) self._implicit_app_ctx_stack.append(None)
if hasattr(sys, 'exc_clear'):
sys.exc_clear()
_request_ctx_stack.push(self) _request_ctx_stack.push(self)
# Open the session at the moment that the request context is # Open the session at the moment that the request context is

17
flask/testsuite/appctx.py

@ -63,6 +63,23 @@ class AppContextTestCase(FlaskTestCase):
self.assert_equal(cleanup_stuff, [None]) 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): def test_custom_app_ctx_globals_class(self):
class CustomRequestGlobals(object): class CustomRequestGlobals(object):
def __init__(self): def __init__(self):

16
flask/testsuite/reqctx.py

@ -33,6 +33,22 @@ class RequestContextTestCase(FlaskTestCase):
ctx.pop() ctx.pop()
self.assert_equal(buffer, [None]) 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): def test_proper_test_request_context(self):
app = flask.Flask(__name__) app = flask.Flask(__name__)
app.config.update( app.config.update(

Loading…
Cancel
Save