]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
refactor: expose optimized functions directly from the origin modules
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Mon, 15 Aug 2022 00:49:24 +0000 (02:49 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Mon, 15 Aug 2022 13:41:04 +0000 (15:41 +0200)
Don't check for the availability of the optimized module in every module
using potentially optimized functions.

psycopg/psycopg/_pipeline.py
psycopg/psycopg/connection.py
psycopg/psycopg/cursor.py
psycopg/psycopg/generators.py
psycopg/psycopg/server_cursor.py
tests/test_module.py

index 27526c16ab5efbce96c5681fbef7e39a0e1a3bb4..e1593d1ba045c907a025b3dc0fc4c25bc7f908b5 100644 (file)
@@ -12,9 +12,9 @@ from . import pq
 from . import errors as e
 from .abc import PipelineCommand, PQGen
 from ._compat import Deque, TypeAlias
-from ._cmodule import _psycopg
 from ._encodings import pgconn_encoding
 from ._preparing import Key, Prepare
+from .generators import pipeline_communicate, fetch_many, send
 
 if TYPE_CHECKING:
     from .pq.abc import PGresult
@@ -22,17 +22,6 @@ if TYPE_CHECKING:
     from .connection import BaseConnection, Connection
     from .connection_async import AsyncConnection
 
-if _psycopg:
-    pipeline_communicate = _psycopg.pipeline_communicate
-    fetch_many = _psycopg.fetch_many
-    send = _psycopg.send
-
-else:
-    from . import generators
-
-    pipeline_communicate = generators.pipeline_communicate
-    fetch_many = generators.fetch_many
-    send = generators.send
 
 PendingResult: TypeAlias = Union[
     None, Tuple["BaseCursor[Any, Any]", Optional[Tuple[Key, Prepare, bytes]]]
index ee05e8e50fccf3e7b4caf8684be92b5dfec5dc89..471fdec6fb0d1bf5c6f9be42939c71ff24b46ae7 100644 (file)
@@ -28,10 +28,9 @@ from .adapt import AdaptersMap
 from ._enums import IsolationLevel
 from .cursor import Cursor
 from ._compat import TypeAlias, LiteralString
-from ._cmodule import _psycopg
 from .conninfo import make_conninfo, conninfo_to_dict, ConnectionInfo
 from ._pipeline import BasePipeline, Pipeline
-from .generators import notifies
+from .generators import notifies, connect, execute
 from ._encodings import pgconn_encoding
 from ._preparing import PrepareManager
 from .transaction import Transaction
@@ -41,15 +40,6 @@ if TYPE_CHECKING:
     from .pq.abc import PGconn, PGresult
     from psycopg_pool.base import BasePool
 
-if _psycopg:
-    connect = _psycopg.connect
-    execute = _psycopg.execute
-
-else:
-    from . import generators
-
-    connect = generators.connect
-    execute = generators.execute
 
 # Row Type variable for Cursor (when it needs to be distinguished from the
 # connection's one)
index b9875a315d4d2ccce3a9416d7a7b26ff0bdc1d18..09eddbea89d6f85878a93fb45a57ce45b935ae97 100644 (file)
@@ -18,29 +18,17 @@ from .abc import ConnectionType, Query, Params, PQGen
 from .copy import Copy, Writer as CopyWriter
 from .rows import Row, RowMaker, RowFactory
 from ._column import Column
-from ._cmodule import _psycopg
 from ._queries import PostgresQuery, PostgresClientQuery
 from ._pipeline import Pipeline
 from ._encodings import pgconn_encoding
 from ._preparing import Prepare
+from .generators import execute, fetch, send
 
 if TYPE_CHECKING:
     from .abc import Transformer
     from .pq.abc import PGconn, PGresult
     from .connection import Connection
 
-if _psycopg:
-    execute = _psycopg.execute
-    fetch = _psycopg.fetch
-    send = _psycopg.send
-
-else:
-    from . import generators
-
-    execute = generators.execute
-    fetch = generators.fetch
-    send = generators.send
-
 TEXT = pq.Format.TEXT
 BINARY = pq.Format.BINARY
 
index 63df87cb5d9e5db8df5cf52c8c1649273ffa7331..1f5eed2adc8138722d90e32f7fa10da101af5503 100644 (file)
@@ -24,6 +24,7 @@ from .abc import PipelineCommand, PQGen, PQGenConn
 from .pq.abc import PGconn, PGresult
 from .waiting import Wait, Ready
 from ._compat import Deque
+from ._cmodule import _psycopg
 from ._encodings import pgconn_encoding, conninfo_encoding
 
 OK = pq.ConnStatus.OK
@@ -43,7 +44,7 @@ PIPELINE_SYNC = pq.ExecStatus.PIPELINE_SYNC
 logger = logging.getLogger(__name__)
 
 
-def connect(conninfo: str) -> PQGenConn[PGconn]:
+def _connect(conninfo: str) -> PQGenConn[PGconn]:
     """
     Generator to create a database connection without blocking.
 
@@ -77,7 +78,7 @@ def connect(conninfo: str) -> PQGenConn[PGconn]:
     return conn
 
 
-def execute(pgconn: PGconn) -> PQGen[List[PGresult]]:
+def _execute(pgconn: PGconn) -> PQGen[List[PGresult]]:
     """
     Generator sending a query and returning results without blocking.
 
@@ -88,12 +89,12 @@ def execute(pgconn: PGconn) -> PQGen[List[PGresult]]:
     Return the list of results returned by the database (whether success
     or error).
     """
-    yield from send(pgconn)
-    rv = yield from fetch_many(pgconn)
+    yield from _send(pgconn)
+    rv = yield from _fetch_many(pgconn)
     return rv
 
 
-def send(pgconn: PGconn) -> PQGen[None]:
+def _send(pgconn: PGconn) -> PQGen[None]:
     """
     Generator to send a query to the server without blocking.
 
@@ -116,7 +117,7 @@ def send(pgconn: PGconn) -> PQGen[None]:
             pgconn.consume_input()
 
 
-def fetch_many(pgconn: PGconn) -> PQGen[List[PGresult]]:
+def _fetch_many(pgconn: PGconn) -> PQGen[List[PGresult]]:
     """
     Generator retrieving results from the database without blocking.
 
@@ -128,7 +129,7 @@ def fetch_many(pgconn: PGconn) -> PQGen[List[PGresult]]:
     """
     results: List[PGresult] = []
     while True:
-        res = yield from fetch(pgconn)
+        res = yield from _fetch(pgconn)
         if not res:
             break
 
@@ -148,7 +149,7 @@ def fetch_many(pgconn: PGconn) -> PQGen[List[PGresult]]:
     return results
 
 
-def fetch(pgconn: PGconn) -> PQGen[Optional[PGresult]]:
+def _fetch(pgconn: PGconn) -> PQGen[Optional[PGresult]]:
     """
     Generator retrieving a single result from the database without blocking.
 
@@ -176,7 +177,7 @@ def fetch(pgconn: PGconn) -> PQGen[Optional[PGresult]]:
     return pgconn.get_result()
 
 
-def pipeline_communicate(
+def _pipeline_communicate(
     pgconn: PGconn, commands: Deque[PipelineCommand]
 ) -> PQGen[List[List[PGresult]]]:
     """Generator to send queries from a connection in pipeline mode while also
@@ -251,7 +252,7 @@ def copy_from(pgconn: PGconn) -> PQGen[Union[memoryview, PGresult]]:
         return data
 
     # Retrieve the final result of copy
-    results = yield from fetch_many(pgconn)
+    results = yield from _fetch_many(pgconn)
     if len(results) > 1:
         # TODO: too brutal? Copy worked.
         raise e.ProgrammingError("you cannot mix COPY with other operations")
@@ -287,9 +288,27 @@ def copy_end(pgconn: PGconn, error: Optional[bytes]) -> PQGen[PGresult]:
             break
 
     # Retrieve the final result of copy
-    (result,) = yield from fetch_many(pgconn)
+    (result,) = yield from _fetch_many(pgconn)
     if result.status != COMMAND_OK:
         encoding = pgconn_encoding(pgconn)
         raise e.error_from_result(result, encoding=encoding)
 
     return result
+
+
+# Override functions with fast versions if available
+if _psycopg:
+    connect = _psycopg.connect
+    execute = _psycopg.execute
+    send = _psycopg.send
+    fetch_many = _psycopg.fetch_many
+    fetch = _psycopg.fetch
+    pipeline_communicate = _psycopg.pipeline_communicate
+
+else:
+    connect = _connect
+    execute = _execute
+    send = _send
+    fetch_many = _fetch_many
+    fetch = _fetch
+    pipeline_communicate = _pipeline_communicate
index 058cf9327749db8c584980095291d9da59823b34..16f11555186488f6922aa5c7ac1677ad319c851f 100644 (file)
@@ -13,7 +13,8 @@ from . import sql
 from . import errors as e
 from .abc import ConnectionType, Query, Params, PQGen
 from .rows import Row, RowFactory, AsyncRowFactory
-from .cursor import BaseCursor, Cursor, execute
+from .cursor import BaseCursor, Cursor
+from .generators import execute
 from .cursor_async import AsyncCursor
 
 if TYPE_CHECKING:
index 2df4d4705837320997cf0c0a2d8ee9ac6ee03ca7..794ef0f89ec6ca2db34de070ff23a6561b7a314b 100644 (file)
@@ -17,7 +17,7 @@ def test_connect(monkeypatch, dsn, args, kwargs, want_conninfo):
     # Details of the params manipulation are in test_conninfo.
     import psycopg.connection
 
-    orig_connect = psycopg.connection.connect
+    orig_connect = psycopg.connection.connect  # type: ignore
 
     got_conninfo = None