Skip to content
Myr

Merge pull request #2409 from bijanvakili/clarify-app-teardown

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

Clarify docs and tests for exception handling in Flask.teardown_appcontext()

authored by David Lord · committed by GitHub · parents 5909e26, 9560f22 · Browse files · 2 files+48 added−3 removed
This commit

State

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

Diff stats

2 files+48 added−3 removed

  • tests:+44 added−1 removed
  • other:+4 added−2 removed

2 files+48 added−3 removed

modified:flask/app.py+4 added-2 removed
1482 unmodified lines
14831484 Since a request context typically also manages an application1485 context it would also be called when you pop a request context.14861487 When a teardown function was called because of an exception it will1488 be passed an error object.1487 When a teardown function was called because of an unhandled exception1488 it will be passed an error object. If an :meth:`errorhandler` is1489 registered, it will handle the exception and the teardown will not1490 receive it.14911492 The return values of teardown functions are ignored.14931494 .. versionadded:: 0.9
691 unmodified lines
modified:tests/test_appctx.py+44 added-1 removed
79 unmodified lines
8081 assert cleanup_stuff == [None]828384def test_app_tearing_down_with_handled_exception(app):84def test_app_tearing_down_with_handled_exception_by_except_block(app):85 cleanup_stuff = []8687 @app.teardown_appcontext88 def cleanup(exception):
7 unmodified lines
9697 assert cleanup_stuff == [None]9899100def test_app_tearing_down_with_handled_exception_by_app_handler(app, client):101 app.config['PROPAGATE_EXCEPTIONS'] = True102 cleanup_stuff = []103104 @app.teardown_appcontext105 def cleanup(exception):106 cleanup_stuff.append(exception)107108 @app.route('/')109 def index():110 raise Exception('dummy')111112 @app.errorhandler(Exception)113 def handler(f):114 return flask.jsonify(str(f))115116 with app.app_context():117 client.get('/')118119 assert cleanup_stuff == [None]120121122def test_app_tearing_down_with_unhandled_exception(app, client):123 app.config['PROPAGATE_EXCEPTIONS'] = True124 cleanup_stuff = []125126 @app.teardown_appcontext127 def cleanup(exception):128 cleanup_stuff.append(exception)129130 @app.route('/')131 def index():132 raise Exception('dummy')133134 with pytest.raises(Exception):135 with app.app_context():136 client.get('/')137138 assert len(cleanup_stuff) == 1139 assert isinstance(cleanup_stuff[0], Exception)140 assert str(cleanup_stuff[0]) == 'dummy'141142143def test_app_ctx_globals_methods(app, app_ctx):144 # get145 assert flask.g.get('foo') is None146 assert flask.g.get('foo', 'bar') == 'bar'
73 unmodified lines