]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Print a log instead of a warning on swallowed exit exception
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Mon, 27 Sep 2021 00:01:30 +0000 (02:01 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Mon, 27 Sep 2021 00:01:30 +0000 (02:01 +0200)
psycopg/psycopg/connection.py
psycopg/psycopg/connection_async.py
tests/test_connection.py
tests/test_connection_async.py

index 12bc45e03b753081651cd91397246a39f87ce0aa..3d9258dca598b52157a0c52651cc3126fb1e0436 100644 (file)
@@ -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()
index 91b31a7dc824fa7bda6c40f6da39d4bded2067cd..fd2f0397f51a8f95e8011f2b88d15f3eb0f91b46 100644 (file)
@@ -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()
index b75866e915a20f8c42eda5f750bff87f330f39e3..3e7070182ecf2ad7d8331af9dc4f88c1bff25777 100644 (file)
@@ -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):
index 64b30a928f9ad4da2e1f4924df3fcb14ba3a58d4..1e5dbda152ebd15ce86fa15c8a9463e7eb13c3ac 100644 (file)
@@ -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):