From 633449a36c959d25577799eb3da76005a803d5cd Mon Sep 17 00:00:00 2001 From: Yourun-Proger Date: Fri, 19 Nov 2021 23:09:38 +0300 Subject: [PATCH 1/2] fix error message --- CHANGES.rst | 2 ++ src/flask/ctx.py | 10 +++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index 1af03f26..3785f737 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -15,6 +15,8 @@ Unreleased - ``app.json_encoder`` and ``json_decoder`` are only passed to ``dumps`` and ``loads`` if they have custom behavior. This improves performance, mainly on PyPy. :issue:`4349` +- Fix error message for ``after_this_request`` when it used outside + request context. :issue:`4333` Version 2.0.2 diff --git a/src/flask/ctx.py b/src/flask/ctx.py index 5c064635..5149c821 100644 --- a/src/flask/ctx.py +++ b/src/flask/ctx.py @@ -130,7 +130,15 @@ def after_this_request(f: AfterRequestCallable) -> AfterRequestCallable: .. versionadded:: 0.9 """ - _request_ctx_stack.top._after_request_functions.append(f) + top = _request_ctx_stack.top + if top is None: + raise RuntimeError( + "This decorator can only be used at local scopes " + "when a request context is on the stack. For instance within " + "view functions." + ) + top._after_request_functions.append(f) + return f From b1a00ebc4c6f15055ddf15ecb17243a089f52a60 Mon Sep 17 00:00:00 2001 From: David Lord Date: Wed, 22 Dec 2021 18:44:37 -0700 Subject: [PATCH 2/2] update error message for after_this_request and copy_current_request_context --- CHANGES.rst | 2 +- src/flask/ctx.py | 15 ++++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 3785f737..cca0c44d 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -15,7 +15,7 @@ Unreleased - ``app.json_encoder`` and ``json_decoder`` are only passed to ``dumps`` and ``loads`` if they have custom behavior. This improves performance, mainly on PyPy. :issue:`4349` -- Fix error message for ``after_this_request`` when it used outside +- Clearer error message when ``after_this_request`` is used outside a request context. :issue:`4333` diff --git a/src/flask/ctx.py b/src/flask/ctx.py index 5149c821..47465fd4 100644 --- a/src/flask/ctx.py +++ b/src/flask/ctx.py @@ -131,14 +131,14 @@ def after_this_request(f: AfterRequestCallable) -> AfterRequestCallable: .. versionadded:: 0.9 """ top = _request_ctx_stack.top + if top is None: raise RuntimeError( - "This decorator can only be used at local scopes " - "when a request context is on the stack. For instance within " - "view functions." + "This decorator can only be used when a request context is" + " active, such as within a view function." ) - top._after_request_functions.append(f) + top._after_request_functions.append(f) return f @@ -167,12 +167,13 @@ def copy_current_request_context(f: t.Callable) -> t.Callable: .. versionadded:: 0.10 """ top = _request_ctx_stack.top + if top is None: raise RuntimeError( - "This decorator can only be used at local scopes " - "when a request context is on the stack. For instance within " - "view functions." + "This decorator can only be used when a request context is" + " active, such as within a view function." ) + reqctx = top.copy() def wrapper(*args, **kwargs):