]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Require selector event loop on windows; enable aync tests
authorFederico Caselli <cfederico87@gmail.com>
Sun, 26 Sep 2021 19:53:22 +0000 (21:53 +0200)
committerFederico Caselli <cfederico87@gmail.com>
Mon, 27 Sep 2021 20:25:31 +0000 (22:25 +0200)
Fixes #76

.gitignore
psycopg/psycopg/connection_async.py
tests/conftest.py

index 15807e28ad9b00c140fe91af6e75f80a9dccfa43..cf12ee0e5aae58292045f199b4d1f7ffa93852e9 100644 (file)
@@ -10,3 +10,4 @@ __pycache__/
 /docs/.venv/
 *.html
 /psycopg_binary/
+.vscode
index fd2f0397f51a8f95e8011f2b88d15f3eb0f91b46..27faf9b42d4a5b97fa18d5277b9171ce5b8f0afb 100644 (file)
@@ -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)
 
index 561695317c6c79fdc316bbd603b21b9b8d0abaef..c9d8d790de419a677a74faf1e7390ffa2531e4b0 100644 (file)
@@ -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()