From efb8c9db3d392669e49e8b9ee8bca45aa4544b72 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Sun, 2 Jan 2022 19:22:34 +0100 Subject: [PATCH] Drop Cursor._set_results() Split it into a _check_results() and leave the caller manage the cursor state. --- psycopg/psycopg/cursor.py | 14 ++++++++------ psycopg/psycopg/server_cursor.py | 3 ++- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/psycopg/psycopg/cursor.py b/psycopg/psycopg/cursor.py index 8527cf9d3..1f369d730 100644 --- a/psycopg/psycopg/cursor.py +++ b/psycopg/psycopg/cursor.py @@ -197,7 +197,8 @@ class BaseCursor(Generic[ConnectionType, Row]): results = yield from self._maybe_prepare_gen( pgq, prepare=prepare, binary=binary ) - self._set_results(results) + self._check_results(results) + self._results = results self._set_result(0) self._last_query = query @@ -220,7 +221,7 @@ class BaseCursor(Generic[ConnectionType, Row]): pgq.dump(params) results = yield from self._maybe_prepare_gen(pgq, prepare=True) - self._set_results(results) + self._check_results(results) for res in results: nrows += res.command_tuples or 0 @@ -398,9 +399,12 @@ class BaseCursor(Generic[ConnectionType, Row]): ExecStatus.COPY_BOTH, ) - def _set_results(self, results: List["PGresult"]) -> None: + def _check_results(self, results: List["PGresult"]) -> None: """ - Set the results from a query into the cursor state. + Verify that the results of a query are valid. + + Verify that the query returned at least one result and that they all + represent a valid result from the database. """ if not results: raise e.InternalError("got no result from the query") @@ -409,8 +413,6 @@ class BaseCursor(Generic[ConnectionType, Row]): if res.status not in self._status_ok: self._raise_from_results(results) - self._results = results - def _set_result(self, i: int, format: Optional[Format] = None) -> None: """ Select one of the results in the cursor as the active one. diff --git a/psycopg/psycopg/server_cursor.py b/psycopg/psycopg/server_cursor.py index ba6a62d4c..b769918ce 100644 --- a/psycopg/psycopg/server_cursor.py +++ b/psycopg/psycopg/server_cursor.py @@ -87,7 +87,8 @@ class ServerCursorHelper(Generic[ConnectionType, Row]): conn = cur._conn conn.pgconn.send_describe_portal(self.name.encode(cur._encoding)) results = yield from execute(conn.pgconn) - cur._set_results(results) + cur._check_results(results) + cur._results = results cur._set_result(0, format=self._format) self.described = True -- 2.47.2