From 4f2e690aed6141df4b3448ec3eeef5c224b08708 Mon Sep 17 00:00:00 2001 From: garenchan <1412950785@qq.com> Date: Sun, 23 Sep 2018 17:58:42 +0800 Subject: [PATCH] Don't use app_context context manager while accessing configuration. If we just want to access configuration, it's important to not use push and pop methods of the actual app context object since that would mean the cleanup handlers will be called. Fix issue #2900. --- flask/testing.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/flask/testing.py b/flask/testing.py index 4bf0ebc1..42d3980c 100644 --- a/flask/testing.py +++ b/flask/testing.py @@ -16,7 +16,7 @@ from contextlib import contextmanager from click.testing import CliRunner from flask.cli import ScriptInfo from werkzeug.test import Client, EnvironBuilder -from flask import _request_ctx_stack +from flask import _request_ctx_stack, _app_ctx_stack from flask.json import dumps as json_dumps from werkzeug.urls import url_parse @@ -78,9 +78,15 @@ def make_test_environ_builder( "Client cannot provide both 'json' and 'data'." ) - # push a context so flask.json can use app's json attributes - with app.app_context(): + # push a context so flask.json can use app's json attributes. + # Note that it's important to not use the push and pop methods + # of the actual app context object since that would mean the + # cleanup handlers will be called. + _app_ctx_stack.push(app.app_context()) + try: kwargs['data'] = json_dumps(kwargs.pop('json')) + finally: + _app_ctx_stack.pop() if 'content_type' not in kwargs: kwargs['content_type'] = 'application/json'