From: Daniele Varrazzo Date: Mon, 27 Sep 2021 00:01:30 +0000 (+0200) Subject: Print a log instead of a warning on swallowed exit exception X-Git-Tag: 3.0~52 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ade69ef31153567c37b2f09066f71e88f9feba3a;p=thirdparty%2Fpsycopg.git Print a log instead of a warning on swallowed exit exception --- diff --git a/psycopg/psycopg/connection.py b/psycopg/psycopg/connection.py index 12bc45e03..3d9258dca 100644 --- a/psycopg/psycopg/connection.py +++ b/psycopg/psycopg/connection.py @@ -588,9 +588,10 @@ class Connection(BaseConnection[Row]): try: self.rollback() except Exception as exc2: - warnings.warn( - f"error rolling back the transaction on {self}: {exc2}", - RuntimeWarning, + logger.warning( + "error ignored rolling back transaction on %s: %s", + self, + exc2, ) else: self.commit() diff --git a/psycopg/psycopg/connection_async.py b/psycopg/psycopg/connection_async.py index 91b31a7dc..fd2f0397f 100644 --- a/psycopg/psycopg/connection_async.py +++ b/psycopg/psycopg/connection_async.py @@ -6,7 +6,6 @@ psycopg async connection objects import asyncio import logging -import warnings from types import TracebackType from typing import Any, AsyncIterator, Dict, Optional, Type, Union from typing import cast, overload, TYPE_CHECKING @@ -122,9 +121,10 @@ class AsyncConnection(BaseConnection[Row]): try: await self.rollback() except Exception as exc2: - warnings.warn( - f"error rolling back the transaction on {self}: {exc2}", - RuntimeWarning, + logger.warning( + "error ignored rolling back transaction on %s: %s", + self, + exc2, ) else: await self.commit() diff --git a/tests/test_connection.py b/tests/test_connection.py index b75866e91..3e7070182 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -159,7 +159,9 @@ def test_context_close(conn): conn.close() -def test_context_rollback_no_clobber(conn, dsn, recwarn): +def test_context_rollback_no_clobber(conn, dsn, caplog): + caplog.set_level(logging.WARNING, logger="psycopg") + with pytest.raises(ZeroDivisionError): with psycopg.connect(dsn) as conn2: conn2.execute("select 1") @@ -169,7 +171,10 @@ def test_context_rollback_no_clobber(conn, dsn, recwarn): ) 1 / 0 - assert "rolling back" in str(recwarn.pop(RuntimeWarning).message) + assert len(caplog.records) == 1 + rec = caplog.records[0] + assert rec.levelno == logging.WARNING + assert "rolling back" in rec.message def test_weakref(dsn): diff --git a/tests/test_connection_async.py b/tests/test_connection_async.py index 64b30a928..1e5dbda15 100644 --- a/tests/test_connection_async.py +++ b/tests/test_connection_async.py @@ -165,7 +165,7 @@ async def test_context_close(aconn): await aconn.close() -async def test_context_rollback_no_clobber(conn, dsn, recwarn): +async def test_context_rollback_no_clobber(conn, dsn, caplog): with pytest.raises(ZeroDivisionError): async with await psycopg.AsyncConnection.connect(dsn) as conn2: await conn2.execute("select 1") @@ -175,7 +175,10 @@ async def test_context_rollback_no_clobber(conn, dsn, recwarn): ) 1 / 0 - assert "rolling back" in str(recwarn.pop(RuntimeWarning).message) + assert len(caplog.records) == 1 + rec = caplog.records[0] + assert rec.levelno == logging.WARNING + assert "rolling back" in rec.message async def test_weakref(dsn):