From: Daniele Varrazzo Date: Sat, 23 May 2020 03:07:43 +0000 (+1200) Subject: Smaller init function using a partial instead of a closure X-Git-Tag: 3.0.dev0~496 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cb210b3ae6d16f805ae1ff0f7bd154638c1358af;p=thirdparty%2Fpsycopg.git Smaller init function using a partial instead of a closure --- diff --git a/psycopg3/pq/pq_ctypes.py b/psycopg3/pq/pq_ctypes.py index 29612a6f3..b9bf534fa 100644 --- a/psycopg3/pq/pq_ctypes.py +++ b/psycopg3/pq/pq_ctypes.py @@ -10,6 +10,7 @@ implementation. import logging from weakref import ref +from functools import partial from ctypes import Array, pointer, string_at from ctypes import c_char_p, c_int, c_size_t, c_ulong @@ -40,6 +41,22 @@ def version() -> int: return impl.PQlibVersion() +def notice_receiver( + arg: Any, result_ptr: impl.PGresult_struct, wconn: "ref[PGconn]" +) -> None: + pgconn = wconn() + if pgconn is None or pgconn.notice_handler is None: + return + + res = PGresult(result_ptr) + try: + pgconn.notice_handler(res) + except Exception as e: + logger.exception("error in notice receiver: %s", e) + + res.pgresult_ptr = None # avoid destroying the pgresult_ptr + + class PGconn: __slots__ = ( "pgconn_ptr", @@ -52,26 +69,10 @@ class PGconn: self.pgconn_ptr: Optional[impl.PGconn_struct] = pgconn_ptr self.notice_handler: Optional[Callable[..., None]] = None - w = ref(self) - - @impl.PQnoticeReceiver # type: ignore - def notice_receiver( - arg: Any, result_ptr: impl.PGresult_struct - ) -> None: - pgconn = w() - if pgconn is None or pgconn.notice_handler is None: - return - - res = PGresult(result_ptr) - try: - pgconn.notice_handler(res) - except Exception as e: - logger.exception("error in notice receiver: %s", e) - - res.pgresult_ptr = None # avoid destroying the pgresult_ptr - - impl.PQsetNoticeReceiver(pgconn_ptr, notice_receiver, None) - self._notice_receiver = notice_receiver + self._notice_receiver = impl.PQnoticeReceiver( # type: ignore + partial(notice_receiver, wconn=ref(self)) + ) + impl.PQsetNoticeReceiver(pgconn_ptr, self._notice_receiver, None) def __del__(self) -> None: self.finish()