Skip to content
Myr

Raise a runtime error if run_async is called without real ContextVars

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

Werkzeug offers a ContextVar replacement for Python < 3.7, however it doesn't work across asyncio tasks, hence it makes sense to error out rather than find there are odd bugs. Note the docs build requires the latest (dev) Werkzeug due to this change (to import ContextVar from werkzeug.local).

authored by pgjones · committed by David Lord · parent 6979265 · Browse files · 3 files+19 added−1 removed
This commit

State

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

Diff stats

3 files+19 added−1 removed

  • tests:+9 added−0 removed
  • other:+10 added−1 removed

3 files+19 added−1 removed

modified:src/flask/helpers.py+6 added
5 unmodified lines
6from threading import RLock78import werkzeug.utils9from werkzeug.exceptions import NotFound10from werkzeug.local import ContextVar11from werkzeug.routing import BuildError12from werkzeug.urls import url_quote1314from .globals import _app_ctx_stack
726 unmodified lines
741 raise RuntimeError(742 "Install Flask with the 'async' extra in order to use async views."743 )744745 if ContextVar.__module__ == "werkzeug.local":746 raise RuntimeError(747 "async cannot be used with this combination of Python & Greenlet versions"748 )749750 @wraps(func)751 def outer(*args, **kwargs):752 """This function grabs the current context for the inner function.753
25 unmodified lines
modified:tests/test_async.py+9 added
1import asyncio2import sys34import pytest56from flask import abort7from flask import Flask8from flask import request9from flask.helpers import run_async101112@pytest.fixture(name="async_app")13def _async_app():
10 unmodified lines
2425 return app262728@pytest.mark.skipif(sys.version_info < (3, 7), reason="requires Python >= 3.7")29def test_async_request_context(async_app):30 test_client = async_app.test_client()31 response = test_client.get("/")32 assert b"GET" in response.get_data()33 response = test_client.post("/")34 assert b"POST" in response.get_data()35 response = test_client.get("/error")36 assert response.status_code == 412373839@pytest.mark.skipif(sys.version_info >= (3, 7), reason="should only raise Python < 3.7")40def test_async_runtime_error():41 with pytest.raises(RuntimeError):42 run_async(None)
modified:tox.ini+4 added-1 removed
23 unmodified lines
24skip_install = true25commands = pre-commit run --all-files --show-diff-on-failure2627[testenv:docs]28deps =28deps = -r requirements/docs.txt29 -r requirements/docs.txt3031 https://github.com/pallets/werkzeug/archive/master.tar.gz32commands = sphinx-build -W -b html -d {envtmpdir}/doctrees docs {envtmpdir}/html