]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Improve error message if proactor event loop is used on Windows
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 28 Sep 2021 20:14:28 +0000 (22:14 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 28 Sep 2021 20:17:10 +0000 (22:17 +0200)
psycopg/psycopg/connection_async.py
tests/test_connection_async.py

index 97d3d5c894a32df6a5017c3ef25e8fe63eef9105..f291295991607850947f2b2217982378bdf989fa 100644 (file)
@@ -4,9 +4,9 @@ psycopg async connection objects
 
 # Copyright (C) 2020-2021 The Psycopg Team
 
+import sys
 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
@@ -18,7 +18,7 @@ from .abc import AdaptContext, Params, PQGen, PQGenConn, Query, RV
 from .rows import Row, AsyncRowFactory, tuple_row, TupleRow
 from .adapt import AdaptersMap
 from ._enums import IsolationLevel
-from ._compat import asynccontextmanager
+from ._compat import asynccontextmanager, get_running_loop
 from .conninfo import make_conninfo, conninfo_to_dict
 from ._encodings import pgconn_encoding
 from .connection import BaseConnection, CursorRow, Notify
@@ -93,17 +93,13 @@ class AsyncConnection(BaseConnection[Row]):
     ) -> "AsyncConnection[Any]":
 
         if sys.platform == "win32":
-            if sys.version_info < (3, 7):
-                loop = asyncio.get_event_loop()
-            else:
-                loop = asyncio.get_running_loop()
+            loop = 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"
+                    "Psycopg cannot use the 'ProactorEventLoop' to run in async"
+                    " mode. Please use a compatible event loop, for instance by"
+                    " setting 'asyncio.set_event_loop_policy"
+                    "(WindowsSelectorEventLoopPolicy())'"
                 )
 
         params = await cls._get_connection_params(conninfo, **kwargs)
index eee637af95f2a825cac9df7164a63bbaee46dda9..2db77fc964867f9d60836196754f7e360d380360 100644 (file)
@@ -671,14 +671,14 @@ async def test_connect_context_copy(dsn, aconn):
     assert (await cur.fetchone())[0] == "hellob"
 
 
-@pytest.mark.skipif(sys.platform != "win32", reason="windows is required")
+@pytest.mark.skipif(sys.platform != "win32", reason="windows only test")
 def test_windows_error(dsn):
     loop = asyncio.ProactorEventLoop()
 
     async def go():
         with pytest.raises(
             InterfaceError,
-            match="psycopg does not currently support running in async mode",
+            match="Psycopg cannot use the 'ProactorEventLoop'",
         ):
             await psycopg.AsyncConnection.connect(dsn)