From: Daniele Varrazzo Date: Sun, 9 Apr 2023 18:45:09 +0000 (+0200) Subject: fix: shorter traceback on ctrl-c interrupt X-Git-Tag: 3.1.9~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f8debef2a1a0c642a7bdadf8cbba249bc8562a8f;p=thirdparty%2Fpsycopg.git fix: shorter traceback on ctrl-c interrupt --- diff --git a/psycopg/psycopg/_pipeline.py b/psycopg/psycopg/_pipeline.py index 233d66b23..ecd6f0628 100644 --- a/psycopg/psycopg/_pipeline.py +++ b/psycopg/psycopg/_pipeline.py @@ -219,7 +219,7 @@ class Pipeline(BasePipeline): try: with self._conn.lock: self._conn.wait(self._sync_gen()) - except e.Error as ex: + except e._NO_TRACEBACK as ex: raise ex.with_traceback(None) def __enter__(self: _Self) -> _Self: @@ -260,7 +260,7 @@ class AsyncPipeline(BasePipeline): try: async with self._conn.lock: await self._conn.wait(self._sync_gen()) - except e.Error as ex: + except e._NO_TRACEBACK as ex: raise ex.with_traceback(None) async def __aenter__(self: _Self) -> _Self: diff --git a/psycopg/psycopg/connection.py b/psycopg/psycopg/connection.py index 78ad57751..299c4f3ff 100644 --- a/psycopg/psycopg/connection.py +++ b/psycopg/psycopg/connection.py @@ -724,7 +724,7 @@ class Connection(BaseConnection[Row]): cls._connect_gen(conninfo, autocommit=autocommit), timeout=params["connect_timeout"], ) - except e.Error as ex: + except e._NO_TRACEBACK as ex: raise ex.with_traceback(None) if row_factory: @@ -875,7 +875,7 @@ class Connection(BaseConnection[Row]): return cur.execute(query, params, prepare=prepare) - except e.Error as ex: + except e._NO_TRACEBACK as ex: raise ex.with_traceback(None) def commit(self) -> None: @@ -919,7 +919,7 @@ class Connection(BaseConnection[Row]): with self.lock: try: ns = self.wait(notifies(self.pgconn)) - except e.Error as ex: + except e._NO_TRACEBACK as ex: raise ex.with_traceback(None) enc = pgconn_encoding(self.pgconn) for pgn in ns: diff --git a/psycopg/psycopg/connection_async.py b/psycopg/psycopg/connection_async.py index f6842c6ed..249048075 100644 --- a/psycopg/psycopg/connection_async.py +++ b/psycopg/psycopg/connection_async.py @@ -125,7 +125,7 @@ class AsyncConnection(BaseConnection[Row]): cls._connect_gen(conninfo, autocommit=autocommit), timeout=params["connect_timeout"], ) - except e.Error as ex: + except e._NO_TRACEBACK as ex: raise ex.with_traceback(None) if row_factory: @@ -281,7 +281,7 @@ class AsyncConnection(BaseConnection[Row]): return await cur.execute(query, params, prepare=prepare) - except e.Error as ex: + except e._NO_TRACEBACK as ex: raise ex.with_traceback(None) async def commit(self) -> None: @@ -316,7 +316,7 @@ class AsyncConnection(BaseConnection[Row]): async with self.lock: try: ns = await self.wait(notifies(self.pgconn)) - except e.Error as ex: + except e._NO_TRACEBACK as ex: raise ex.with_traceback(None) enc = pgconn_encoding(self.pgconn) for pgn in ns: diff --git a/psycopg/psycopg/cursor.py b/psycopg/psycopg/cursor.py index 8379c589a..148ea10bb 100644 --- a/psycopg/psycopg/cursor.py +++ b/psycopg/psycopg/cursor.py @@ -719,7 +719,7 @@ class Cursor(BaseCursor["Connection[Any]", Row]): self._conn.wait( self._execute_gen(query, params, prepare=prepare, binary=binary) ) - except e.Error as ex: + except e._NO_TRACEBACK as ex: raise ex.with_traceback(None) return self @@ -754,7 +754,7 @@ class Cursor(BaseCursor["Connection[Any]", Row]): self._conn.wait( self._executemany_gen_no_pipeline(query, params_seq, returning) ) - except e.Error as ex: + except e._NO_TRACEBACK as ex: raise ex.with_traceback(None) def stream( @@ -780,7 +780,7 @@ class Cursor(BaseCursor["Connection[Any]", Row]): yield rec first = False - except e.Error as ex: + except e._NO_TRACEBACK as ex: raise ex.with_traceback(None) finally: @@ -898,7 +898,7 @@ class Cursor(BaseCursor["Connection[Any]", Row]): with Copy(self, writer=writer) as copy: yield copy - except e.Error as ex: + except e._NO_TRACEBACK as ex: raise ex.with_traceback(None) # If a fresher result has been set on the cursor by the Copy object, diff --git a/psycopg/psycopg/cursor_async.py b/psycopg/psycopg/cursor_async.py index 405dc0064..ab7b07348 100644 --- a/psycopg/psycopg/cursor_async.py +++ b/psycopg/psycopg/cursor_async.py @@ -90,7 +90,7 @@ class AsyncCursor(BaseCursor["AsyncConnection[Any]", Row]): await self._conn.wait( self._execute_gen(query, params, prepare=prepare, binary=binary) ) - except e.Error as ex: + except e._NO_TRACEBACK as ex: raise ex.with_traceback(None) return self @@ -121,7 +121,7 @@ class AsyncCursor(BaseCursor["AsyncConnection[Any]", Row]): await self._conn.wait( self._executemany_gen_no_pipeline(query, params_seq, returning) ) - except e.Error as ex: + except e._NO_TRACEBACK as ex: raise ex.with_traceback(None) async def stream( @@ -146,7 +146,7 @@ class AsyncCursor(BaseCursor["AsyncConnection[Any]", Row]): yield rec first = False - except e.Error as ex: + except e._NO_TRACEBACK as ex: raise ex.with_traceback(None) finally: @@ -234,7 +234,7 @@ class AsyncCursor(BaseCursor["AsyncConnection[Any]", Row]): async with AsyncCopy(self, writer=writer) as copy: yield copy - except e.Error as ex: + except e._NO_TRACEBACK as ex: raise ex.with_traceback(None) self._select_current_result(0) diff --git a/psycopg/psycopg/errors.py b/psycopg/psycopg/errors.py index e1769546b..d8b7f6fd8 100644 --- a/psycopg/psycopg/errors.py +++ b/psycopg/psycopg/errors.py @@ -20,6 +20,7 @@ DBAPI-defined Exceptions are defined in the following hierarchy:: from typing import Any, Dict, Optional, Sequence, Tuple, Type, Union from typing_extensions import TypeAlias +from asyncio import CancelledError from .pq.abc import PGconn, PGresult from .pq._enums import DiagnosticField @@ -1533,3 +1534,11 @@ class IndexCorrupted(InternalError, # autogenerated: end # fmt: on + +# Don't show a complete traceback upon raising these exception. +# Usually the traceback starts from internal functions (for instance in the +# server communication callbacks) but, for the end user, it's more important +# to get the high level information about where the exception was raised, for +# instance in a certain `Cursor.execute()`. + +_NO_TRACEBACK = (Error, KeyboardInterrupt, CancelledError) diff --git a/psycopg/psycopg/server_cursor.py b/psycopg/psycopg/server_cursor.py index 31b7f2e67..7a86e599d 100644 --- a/psycopg/psycopg/server_cursor.py +++ b/psycopg/psycopg/server_cursor.py @@ -290,7 +290,7 @@ class ServerCursor(ServerCursorMixin["Connection[Any]", Row], Cursor[Row]): try: with self._conn.lock: self._conn.wait(self._declare_gen(query, params, binary)) - except e.Error as ex: + except e._NO_TRACEBACK as ex: raise ex.with_traceback(None) return self @@ -426,7 +426,7 @@ class AsyncServerCursor( try: async with self._conn.lock: await self._conn.wait(self._declare_gen(query, params, binary)) - except e.Error as ex: + except e._NO_TRACEBACK as ex: raise ex.with_traceback(None) return self