From: Daniele Varrazzo Date: Sun, 2 Jan 2022 18:46:32 +0000 (+0100) Subject: Refactor cur._raise_from_results() into _raise_for_result() X-Git-Tag: pool-3.1~52 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=121c2214add48ad0614a8190cac7bc820591bb86;p=thirdparty%2Fpsycopg.git Refactor cur._raise_from_results() into _raise_for_result() The case of raising with different statuses doesn't practically happen. --- diff --git a/psycopg/psycopg/cursor.py b/psycopg/psycopg/cursor.py index 1f369d730..10c0d9420 100644 --- a/psycopg/psycopg/cursor.py +++ b/psycopg/psycopg/cursor.py @@ -315,7 +315,7 @@ class BaseCursor(Generic[ConnectionType, Row]): else: # Errors, unexpected values - return self._raise_from_results([res]) + return self._raise_for_result(res) def _start_query(self, query: Optional[Query] = None) -> PQGen[None]: """Generator to start the processing of a query. @@ -411,7 +411,23 @@ class BaseCursor(Generic[ConnectionType, Row]): for res in results: if res.status not in self._status_ok: - self._raise_from_results(results) + self._raise_for_result(res) + + def _raise_for_result(self, result: "PGresult") -> NoReturn: + """ + Raise an appropriate error message for an unexpected database result + """ + if result.status == ExecStatus.FATAL_ERROR: + raise e.error_from_result(result, encoding=self._encoding) + elif result.status in self._status_copy: + raise e.ProgrammingError( + "COPY cannot be used with this method; use copy() insead" + ) + else: + raise e.InternalError( + f"unexpected result status from query:" + f" {ExecStatus(result.status).name}" + ) def _set_result(self, i: int, format: Optional[Format] = None) -> None: """ @@ -430,21 +446,6 @@ class BaseCursor(Generic[ConnectionType, Row]): nrows = self.pgresult.command_tuples self._rowcount = nrows if nrows is not None else -1 - def _raise_from_results(self, results: List["PGresult"]) -> NoReturn: - statuses = {res.status for res in results} - badstats = statuses.difference(self._status_ok) - if results[-1].status == ExecStatus.FATAL_ERROR: - raise e.error_from_result(results[-1], encoding=self._encoding) - elif statuses.intersection(self._status_copy): - raise e.ProgrammingError( - "COPY cannot be used with this method; use copy() insead" - ) - else: - raise e.InternalError( - f"got unexpected status from query:" - f" {', '.join(sorted(ExecStatus(s).name for s in badstats))}" - ) - def _send_prepare(self, name: bytes, query: PostgresQuery) -> None: self._pgconn.send_prepare(name, query.query, param_types=query.types) diff --git a/psycopg/psycopg/server_cursor.py b/psycopg/psycopg/server_cursor.py index b769918ce..f973a7aac 100644 --- a/psycopg/psycopg/server_cursor.py +++ b/psycopg/psycopg/server_cursor.py @@ -70,7 +70,7 @@ class ServerCursorHelper(Generic[ConnectionType, Row]): cur._execute_send(pgq, no_pqexec=True) results = yield from execute(cur._conn.pgconn) if results[-1].status != pq.ExecStatus.COMMAND_OK: - cur._raise_from_results(results) + cur._raise_for_result(results[-1]) # Set the format, which will be used by describe and fetch operations if binary is None: