From: Daniele Varrazzo Date: Mon, 4 Apr 2022 00:54:41 +0000 (+0200) Subject: fix: shorten tracebacks on pipeline sync and exit X-Git-Tag: 3.1~122^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1da4e23d01809c1aaac2a0730e7e25f347710aeb;p=thirdparty%2Fpsycopg.git fix: shorten tracebacks on pipeline sync and exit The details shown are only the communication bowels. So we go from: Traceback (most recent call last): File "trace.py", line 301, in conn.execute("select pg_sleep(0.2)") File "/usr/lib/python3.8/contextlib.py", line 120, in __exit__ next(self.gen) File "/home/piro/dev/psycopg3/psycopg/psycopg/connection.py", line 881, in pipeline yield pipeline File "/home/piro/dev/psycopg3/psycopg/psycopg/_pipeline.py", line 197, in __exit__ self.sync() File "/home/piro/dev/psycopg3/psycopg/psycopg/_pipeline.py", line 183, in sync self._conn.wait(self._sync_gen()) File "/home/piro/dev/psycopg3/psycopg/psycopg/connection.py", line 896, in wait return waiting.wait(gen, self.pgconn.socket, timeout=timeout) File "/home/piro/dev/psycopg3/psycopg/psycopg/waiting.py", line 237, in wait_epoll s = gen.send(ready) File "/home/piro/dev/psycopg3/psycopg/psycopg/_pipeline.py", line 92, in _sync_gen yield from self._communicate_gen() File "/home/piro/dev/psycopg3/psycopg/psycopg/_pipeline.py", line 105, in _communicate_gen self._process_results(queued, results) File "/home/piro/dev/psycopg3/psycopg/psycopg/_pipeline.py", line 149, in _process_results raise e.error_from_result(result, encoding=pgconn_encoding(self.pgconn)) psycopg.errors.UndefinedColumn: column "foo" does not exist LINE 1: select foo ^ to: Traceback (most recent call last): File "trace.py", line 301, in conn.execute("select pg_sleep(0.2)") File "/usr/lib/python3.8/contextlib.py", line 120, in __exit__ next(self.gen) File "/home/piro/dev/psycopg3/psycopg/psycopg/connection.py", line 881, in pipeline yield pipeline File "/home/piro/dev/psycopg3/psycopg/psycopg/_pipeline.py", line 205, in __exit__ raise exc2.with_traceback(None) psycopg.errors.UndefinedColumn: column "foo" does not exist LINE 1: select foo ^ which shows much more clearly that the traceback was raised at pipeline block exit. --- diff --git a/psycopg/psycopg/_pipeline.py b/psycopg/psycopg/_pipeline.py index a62aa76b8..63bf765cb 100644 --- a/psycopg/psycopg/_pipeline.py +++ b/psycopg/psycopg/_pipeline.py @@ -179,8 +179,11 @@ class Pipeline(BasePipeline): This is called when exiting the pipeline, but can be used for other purposes (e.g. in nested pipelines). """ - with self._conn.lock: - self._conn.wait(self._sync_gen()) + try: + with self._conn.lock: + self._conn.wait(self._sync_gen()) + except e.Error as ex: + raise ex.with_traceback(None) def __enter__(self) -> "Pipeline": self._enter() @@ -199,7 +202,7 @@ class Pipeline(BasePipeline): if exc_val: logger.warning("error ignored syncing %r: %s", self, exc2) else: - raise + raise exc2.with_traceback(None) finally: try: self._exit() @@ -210,7 +213,7 @@ class Pipeline(BasePipeline): if exc_val: logger.warning("error ignored exiting %r: %s", self, exc2) else: - raise + raise exc2.with_traceback(None) class AsyncPipeline(BasePipeline): @@ -229,8 +232,11 @@ class AsyncPipeline(BasePipeline): This is called when exiting the pipeline, but can be used for other purposes (e.g. in nested pipelines). """ - async with self._conn.lock: - await self._conn.wait(self._sync_gen()) + try: + async with self._conn.lock: + await self._conn.wait(self._sync_gen()) + except e.Error as ex: + raise ex.with_traceback(None) async def __aenter__(self) -> "AsyncPipeline": self._enter() @@ -249,7 +255,7 @@ class AsyncPipeline(BasePipeline): if exc_val: logger.warning("error ignored syncing %r: %s", self, exc2) else: - raise + raise exc2.with_traceback(None) finally: try: self._exit() @@ -257,4 +263,4 @@ class AsyncPipeline(BasePipeline): if exc_val: logger.warning("error ignored exiting %r: %s", self, exc2) else: - raise + raise exc2.with_traceback(None)