From: Federico Caselli Date: Sun, 26 Sep 2021 19:53:22 +0000 (+0200) Subject: Require selector event loop on windows; enable aync tests X-Git-Tag: 3.0~50^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=2fe0a2818fa7441a1a0722ee158836caa6e64f5f;p=thirdparty%2Fpsycopg.git Require selector event loop on windows; enable aync tests Fixes #76 --- diff --git a/.gitignore b/.gitignore index 15807e28a..cf12ee0e5 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ __pycache__/ /docs/.venv/ *.html /psycopg_binary/ +.vscode diff --git a/psycopg/psycopg/connection_async.py b/psycopg/psycopg/connection_async.py index fd2f0397f..27faf9b42 100644 --- a/psycopg/psycopg/connection_async.py +++ b/psycopg/psycopg/connection_async.py @@ -6,6 +6,7 @@ psycopg async connection objects import asyncio import logging +import sys from types import TracebackType from typing import Any, AsyncIterator, Dict, Optional, Type, Union from typing import cast, overload, TYPE_CHECKING @@ -90,6 +91,18 @@ class AsyncConnection(BaseConnection[Row]): row_factory: Optional[AsyncRowFactory[Row]] = None, **kwargs: Any, ) -> "AsyncConnection[Any]": + + if sys.platform == "win32": + loop = asyncio.get_running_loop() + if isinstance(loop, asyncio.ProactorEventLoop): + raise e.InterfaceError( + "psycopg does not currently support running in async mode " + "on windows on the 'ProactorEventLoop'. " + "Please ensure that a compatible event loop is used, like " + "by setting ``asyncio.set_event_loop_policy`` to " + "WindowsSelectorEventLoopPolicy" + ) + params = await cls._get_connection_params(conninfo, **kwargs) conninfo = make_conninfo(**params) diff --git a/tests/conftest.py b/tests/conftest.py index 561695317..c9d8d790d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -58,13 +58,6 @@ def pytest_report_header(config): return [f"asyncio loop: {loop}"] -def pytest_runtest_setup(item): - # Skip asyncio tests on Windows: they just don't seem to work - if sys.platform == "win32": - for mark in item.iter_markers(name="asyncio"): - pytest.skip(f"cannot run asyncio tests on {sys.platform}") - - @pytest.fixture def retries(request): """Retry a block in a test a few times before giving up.""" @@ -83,6 +76,8 @@ def retries(request): @pytest.fixture def event_loop(request): """Return the event loop to test asyncio-marked tests.""" + # pytest-asyncio reset the the loop config after each test, so set + # set them each time loop = request.config.getoption("--loop") if loop == "uvloop": @@ -92,6 +87,8 @@ def event_loop(request): else: assert loop == "default" + if sys.platform == "win32": + asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) loop = asyncio.get_event_loop_policy().new_event_loop() yield loop loop.close()