diff --git a/CHANGES.rst b/CHANGES.rst index 1fbbed87..a57a97d2 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -47,6 +47,11 @@ Unreleased - Fix type annotation for ``errorhandler`` decorator. :issue:`4295` - Revert a change to the CLI that caused it to hide ``ImportError`` tracebacks when importing the application. :issue:`4307` +- ``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` +- Clearer error message when ``after_this_request`` is used outside a + request context. :issue:`4333` Version 2.0.2 diff --git a/docs/server.rst b/docs/server.rst index 446d5be8..77b25abb 100644 --- a/docs/server.rst +++ b/docs/server.rst @@ -80,27 +80,34 @@ messages: Either identify and stop the other program, or use ``flask run --port 5001`` to pick a different port. -You can use ``netstat`` to identify what process id is using a port, -then use other operating system tools stop that process. The following -example shows that process id 6847 is using port 5000. +You can use ``netstat`` or ``lsof`` to identify what process id is using +a port, then use other operating system tools stop that process. The +following example shows that process id 6847 is using port 5000. .. tabs:: - .. group-tab:: Linux/Mac + .. tab:: ``netstat`` (Linux) .. code-block:: text $ netstat -nlp | grep 5000 tcp 0 0 127.0.0.1:5000 0.0.0.0:* LISTEN 6847/python - .. group-tab:: Windows + .. tab:: ``lsof`` (macOS / Linux) + + .. code-block:: text + + $ lsof -P -i :5000 + Python 6847 IPv4 TCP localhost:5000 (LISTEN) + + .. tab:: ``netstat`` (Windows) .. code-block:: text > netstat -ano | findstr 5000 TCP 127.0.0.1:5000 0.0.0.0:0 LISTENING 6847 -MacOS Monterey and later automatically starts a service that uses port +macOS Monterey and later automatically starts a service that uses port 5000. To disable the service, go to System Preferences, Sharing, and disable "AirPlay Receiver". diff --git a/src/flask/ctx.py b/src/flask/ctx.py index 05ba4938..b3687ba7 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 when a request context is" + " active, such as within a view function." + ) + + top._after_request_functions.append(f) return f @@ -159,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): diff --git a/src/flask/json/__init__.py b/src/flask/json/__init__.py index eac2f576..43c018e7 100644 --- a/src/flask/json/__init__.py +++ b/src/flask/json/__init__.py @@ -74,6 +74,11 @@ def _dump_arg_defaults( if bp is not None and bp.json_encoder is not None: cls = bp.json_encoder + # Only set a custom encoder if it has custom behavior. This is + # faster on PyPy. + if cls is not _json.JSONEncoder: + kwargs.setdefault("cls", cls) + kwargs.setdefault("cls", cls) kwargs.setdefault("ensure_ascii", app.config["JSON_AS_ASCII"]) kwargs.setdefault("sort_keys", app.config["JSON_SORT_KEYS"]) @@ -95,9 +100,10 @@ def _load_arg_defaults( if bp is not None and bp.json_decoder is not None: cls = bp.json_decoder - kwargs.setdefault("cls", cls) - else: - kwargs.setdefault("cls", JSONDecoder) + # Only set a custom decoder if it has custom behavior. This is + # faster on PyPy. + if cls not in {JSONDecoder, _json.JSONDecoder}: + kwargs.setdefault("cls", cls) def dumps(obj: t.Any, app: t.Optional["Flask"] = None, **kwargs: t.Any) -> str: