Skip to content
Myr

Update documentation and regression tests to clarify that Flask.teardown_appcontext() only receives unhandled exceptions

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

authored by Bijan Vakili · committed by Bijan Vakili · parent 5909e26 · Browse files · 2 files+56 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+56 added−3 removed

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

2 files+56 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. Note that if a :meth:`errorhandler`1489 is 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+52 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):101 cleanup_stuff = []102103 class AppConfig():104 PROPAGATE_EXCEPTIONS = True105 app.config.from_object(AppConfig())106107 @app.teardown_appcontext108 def cleanup(exception):109 cleanup_stuff.append(exception)110111 @app.route('/')112 def index():113 raise Exception('dummy')114115 @app.errorhandler(Exception)116 def handler(f):117 return flask.jsonify(str(f))118119 test_client = app.test_client()120 with app.app_context():121 test_client.get('/')122123 assert cleanup_stuff == [None]124125126def test_app_tearing_down_with_unhandled_exception(app):127 cleanup_stuff = []128129 class AppConfig():130 PROPAGATE_EXCEPTIONS = True131 app.config.from_object(AppConfig())132133 @app.teardown_appcontext134 def cleanup(exception):135 cleanup_stuff.append(exception)136137 @app.route('/')138 def index():139 raise Exception('dummy')140141 test_client = app.test_client()142 with pytest.raises(Exception):143 with app.app_context():144 test_client.get('/')145146 assert len(cleanup_stuff) == 1147 assert isinstance(cleanup_stuff[0], Exception)148 assert str(cleanup_stuff[0]) == 'dummy'149150151def test_app_ctx_globals_methods(app, app_ctx):152 # get153 assert flask.g.get('foo') is None154 assert flask.g.get('foo', 'bar') == 'bar'
73 unmodified lines