From: Daniele Varrazzo Date: Fri, 22 May 2020 05:40:46 +0000 (+1200) Subject: Notice handler renamed to notice callback X-Git-Tag: 3.0.dev0~504 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e925bd5b33bbedc3ce926b21629acc2efdd9a60e;p=thirdparty%2Fpsycopg.git Notice handler renamed to notice callback --- diff --git a/psycopg3/connection.py b/psycopg3/connection.py index 27b147362..ad319fd43 100644 --- a/psycopg3/connection.py +++ b/psycopg3/connection.py @@ -39,7 +39,7 @@ else: connect = generators.connect execute = generators.execute -NoticeCallback = Callable[[e.Diagnostic], None] +NoticeHandler = Callable[[e.Diagnostic], None] class BaseConnection: @@ -72,15 +72,13 @@ class BaseConnection: self._autocommit = False self.dumpers: proto.DumpersMap = {} self.loaders: proto.LoadersMap = {} - self._notice_callbacks: List[NoticeCallback] = [] + self._notice_handlers: List[NoticeHandler] = [] # name of the postgres encoding (in bytes) self._pgenc = b"" wself = ref(self) - pgconn.notice_callback = partial( - BaseConnection._notice_callback, wself - ) + pgconn.notice_handler = partial(BaseConnection._notice_handler, wself) @property def closed(self) -> bool: @@ -140,22 +138,22 @@ class BaseConnection: else: return "UTF8" - def add_notice_callback(self, callback: NoticeCallback) -> None: - self._notice_callbacks.append(callback) + def add_notice_handler(self, callback: NoticeHandler) -> None: + self._notice_handlers.append(callback) - def remove_notice_callback(self, callback: NoticeCallback) -> None: - self._notice_callbacks.remove(callback) + def remove_notice_handler(self, callback: NoticeHandler) -> None: + self._notice_handlers.remove(callback) @staticmethod - def _notice_callback( + def _notice_handler( wself: "ReferenceType[BaseConnection]", res: pq.proto.PGresult ) -> None: self = wself() - if self is None or not self._notice_callback: + if self is None or not self._notice_handler: return diag = e.Diagnostic(res, self.codec.name) - for cb in self._notice_callbacks: + for cb in self._notice_handlers: try: cb(diag) except Exception as ex: diff --git a/psycopg3/pq/pq_ctypes.py b/psycopg3/pq/pq_ctypes.py index 285e9c3fa..29612a6f3 100644 --- a/psycopg3/pq/pq_ctypes.py +++ b/psycopg3/pq/pq_ctypes.py @@ -43,14 +43,14 @@ def version() -> int: class PGconn: __slots__ = ( "pgconn_ptr", - "notice_callback", + "notice_handler", "_notice_receiver", "__weakref__", ) def __init__(self, pgconn_ptr: impl.PGconn_struct): self.pgconn_ptr: Optional[impl.PGconn_struct] = pgconn_ptr - self.notice_callback: Optional[Callable[..., None]] = None + self.notice_handler: Optional[Callable[..., None]] = None w = ref(self) @@ -59,12 +59,12 @@ class PGconn: arg: Any, result_ptr: impl.PGresult_struct ) -> None: pgconn = w() - if pgconn is None or pgconn.notice_callback is None: + if pgconn is None or pgconn.notice_handler is None: return res = PGresult(result_ptr) try: - pgconn.notice_callback(res) + pgconn.notice_handler(res) except Exception as e: logger.exception("error in notice receiver: %s", e) diff --git a/psycopg3/pq/pq_cython.pxd b/psycopg3/pq/pq_cython.pxd index daff3de72..0acf6a85e 100644 --- a/psycopg3/pq/pq_cython.pxd +++ b/psycopg3/pq/pq_cython.pxd @@ -10,7 +10,7 @@ cdef class PGconn: @staticmethod cdef PGconn _from_ptr(impl.PGconn *ptr) - cdef public object notice_callback + cdef public object notice_handler cdef int _ensure_pgconn(self) except 0 cdef char *_call_bytes(self, conn_bytes_f func) except NULL diff --git a/psycopg3/pq/pq_cython.pyx b/psycopg3/pq/pq_cython.pyx index 0380e73c9..e9f77ee32 100644 --- a/psycopg3/pq/pq_cython.pyx +++ b/psycopg3/pq/pq_cython.pyx @@ -36,12 +36,12 @@ def version(): cdef void notice_receiver(void *arg, const impl.PGresult *res_ptr): cdef PGconn pgconn = arg - if pgconn.notice_callback is None: + if pgconn.notice_handler is None: return cdef PGresult res = PGresult._from_ptr(res_ptr) try: - pgconn.notice_callback(res) + pgconn.notice_handler(res) except Exception as e: logger.exception("error in notice receiver: %s", e) diff --git a/psycopg3/pq/proto.py b/psycopg3/pq/proto.py index c5d69e67a..686f33eca 100644 --- a/psycopg3/pq/proto.py +++ b/psycopg3/pq/proto.py @@ -23,7 +23,7 @@ if TYPE_CHECKING: class PGconn(Protocol): - notice_callback: Optional[Callable[["PGresult"], None]] + notice_handler: Optional[Callable[["PGresult"], None]] @classmethod def connect(cls, conninfo: bytes) -> "PGconn": diff --git a/tests/pq/test_pgconn.py b/tests/pq/test_pgconn.py index 27034f0aa..892ec971f 100644 --- a/tests/pq/test_pgconn.py +++ b/tests/pq/test_pgconn.py @@ -345,7 +345,7 @@ def test_notice(pq, pgconn): assert res.status == pq.ExecStatus.NONFATAL_ERROR msgs.append(res.error_field(pq.DiagnosticField.MESSAGE_PRIMARY)) - pgconn.notice_callback = callback + pgconn.notice_handler = callback res = pgconn.exec_( b"do $$begin raise notice 'hello notice'; end$$ language plpgsql" ) @@ -360,7 +360,7 @@ def test_notice_error(pq, pgconn, caplog): def callback(res): raise Exception("hello error") - pgconn.notice_callback = callback + pgconn.notice_handler = callback res = pgconn.exec_( b"do $$begin raise notice 'hello notice'; end$$ language plpgsql" ) diff --git a/tests/test_async_connection.py b/tests/test_async_connection.py index 8827484ab..b7c3d726a 100644 --- a/tests/test_async_connection.py +++ b/tests/test_async_connection.py @@ -218,7 +218,7 @@ def test_broken_connection(aconn, loop): assert aconn.closed -def test_notice_callbacks(aconn, loop, caplog): +def test_notice_handlers(aconn, loop, caplog): caplog.set_level(logging.WARNING, logger="psycopg3") messages = [] severities = [] @@ -229,10 +229,10 @@ def test_notice_callbacks(aconn, loop, caplog): def cb2(res): raise Exception("hello from cb2") - aconn.add_notice_callback(cb1) - aconn.add_notice_callback(cb2) - aconn.add_notice_callback("the wrong thing") - aconn.add_notice_callback( + aconn.add_notice_handler(cb1) + aconn.add_notice_handler(cb2) + aconn.add_notice_handler("the wrong thing") + aconn.add_notice_handler( lambda diag: severities.append(diag.severity_nonlocalized) ) @@ -253,8 +253,8 @@ def test_notice_callbacks(aconn, loop, caplog): assert rec.levelno == logging.ERROR assert "the wrong thing" in rec.message - aconn.remove_notice_callback(cb1) - aconn.remove_notice_callback("the wrong thing") + aconn.remove_notice_handler(cb1) + aconn.remove_notice_handler("the wrong thing") loop.run_until_complete( cur.execute( "do $$begin raise warning 'hello warning'; end$$ language plpgsql" @@ -265,4 +265,4 @@ def test_notice_callbacks(aconn, loop, caplog): assert severities == ["NOTICE", "WARNING"] with pytest.raises(ValueError): - aconn.remove_notice_callback(cb1) + aconn.remove_notice_handler(cb1) diff --git a/tests/test_connection.py b/tests/test_connection.py index 830f94145..dc9fe4313 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -208,7 +208,7 @@ def test_broken_connection(conn): assert conn.closed -def test_notice_callbacks(conn, caplog): +def test_notice_handlers(conn, caplog): caplog.set_level(logging.WARNING, logger="psycopg3") messages = [] severities = [] @@ -219,10 +219,10 @@ def test_notice_callbacks(conn, caplog): def cb2(res): raise Exception("hello from cb2") - conn.add_notice_callback(cb1) - conn.add_notice_callback(cb2) - conn.add_notice_callback("the wrong thing") - conn.add_notice_callback( + conn.add_notice_handler(cb1) + conn.add_notice_handler(cb2) + conn.add_notice_handler("the wrong thing") + conn.add_notice_handler( lambda diag: severities.append(diag.severity_nonlocalized) ) @@ -241,8 +241,8 @@ def test_notice_callbacks(conn, caplog): assert rec.levelno == logging.ERROR assert "the wrong thing" in rec.message - conn.remove_notice_callback(cb1) - conn.remove_notice_callback("the wrong thing") + conn.remove_notice_handler(cb1) + conn.remove_notice_handler("the wrong thing") cur.execute( "do $$begin raise warning 'hello warning'; end$$ language plpgsql" ) @@ -251,4 +251,4 @@ def test_notice_callbacks(conn, caplog): assert severities == ["NOTICE", "WARNING"] with pytest.raises(ValueError): - conn.remove_notice_callback(cb1) + conn.remove_notice_handler(cb1) diff --git a/tests/test_errors.py b/tests/test_errors.py index dc18e7985..52e2ba264 100644 --- a/tests/test_errors.py +++ b/tests/test_errors.py @@ -46,7 +46,7 @@ def test_diag_right_attr(pgconn, pq, monkeypatch): @pytest.mark.parametrize("enc", ["utf8", "latin9"]) def test_diag_encoding(conn, enc): msgs = [] - conn.add_notice_callback(lambda diag: msgs.append(diag.message_primary)) + conn.add_notice_handler(lambda diag: msgs.append(diag.message_primary)) conn.set_client_encoding(enc) cur = conn.cursor() cur.execute(