Skip to content

Commit b63a992

Browse files
committed
Merge branch '0.10-maintenance'
2 parents 544118b + a3a2f52 commit b63a992

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Version 0.10.2
2424
- Raise an :exc:`AttributeError` in :func:`flask.helpers.find_package` with a
2525
useful message explaining why it is raised when a PEP 302 import hook is used
2626
without an `is_package()` method.
27+
- Fixed an issue causing exceptions raised before entering a request or app
28+
context to be passed to teardown handlers.
2729

2830
Version 0.10.1
2931
--------------

flask/ctx.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ def __init__(self, app):
163163
def push(self):
164164
"""Binds the app context to the current context."""
165165
self._refcnt += 1
166+
if hasattr(sys, 'exc_clear'):
167+
sys.exc_clear()
166168
_app_ctx_stack.push(self)
167169
appcontext_pushed.send(self.app)
168170

@@ -312,6 +314,9 @@ def push(self):
312314
else:
313315
self._implicit_app_ctx_stack.append(None)
314316

317+
if hasattr(sys, 'exc_clear'):
318+
sys.exc_clear()
319+
315320
_request_ctx_stack.push(self)
316321

317322
# Open the session at the moment that the request context is

flask/testsuite/appctx.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,23 @@ def cleanup(exception):
6363

6464
self.assert_equal(cleanup_stuff, [None])
6565

66+
def test_app_tearing_down_with_previous_exception(self):
67+
cleanup_stuff = []
68+
app = flask.Flask(__name__)
69+
@app.teardown_appcontext
70+
def cleanup(exception):
71+
cleanup_stuff.append(exception)
72+
73+
try:
74+
raise Exception('dummy')
75+
except Exception:
76+
pass
77+
78+
with app.app_context():
79+
pass
80+
81+
self.assert_equal(cleanup_stuff, [None])
82+
6683
def test_custom_app_ctx_globals_class(self):
6784
class CustomRequestGlobals(object):
6885
def __init__(self):

flask/testsuite/reqctx.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,22 @@ def end_of_request(exception):
3333
ctx.pop()
3434
self.assert_equal(buffer, [None])
3535

36+
def test_teardown_with_previous_exception(self):
37+
buffer = []
38+
app = flask.Flask(__name__)
39+
@app.teardown_request
40+
def end_of_request(exception):
41+
buffer.append(exception)
42+
43+
try:
44+
raise Exception('dummy')
45+
except Exception:
46+
pass
47+
48+
with app.test_request_context():
49+
self.assert_equal(buffer, [])
50+
self.assert_equal(buffer, [None])
51+
3652
def test_proper_test_request_context(self):
3753
app = flask.Flask(__name__)
3854
app.config.update(

0 commit comments

Comments
 (0)