]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Refactor cur._raise_from_results() into _raise_for_result()
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sun, 2 Jan 2022 18:46:32 +0000 (19:46 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sun, 2 Jan 2022 19:56:00 +0000 (20:56 +0100)
The case of raising with different statuses doesn't practically happen.

psycopg/psycopg/cursor.py
psycopg/psycopg/server_cursor.py

index 1f369d7302f69f47dd6e244dc8e13e60207297fe..10c0d9420154fa67de9551208f4371e37e60c365 100644 (file)
@@ -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)
 
index b769918cee7b49680bdf82df92f63b3aa32a7e17..f973a7aac7ed88ae6fbf5a933e3fc5fd1037e8b2 100644 (file)
@@ -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: