]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
fix: drop warning for objects passed as JSON lods/dump function fix-builtin-dumper-loader
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 10 Sep 2025 19:55:02 +0000 (21:55 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Thu, 11 Sep 2025 10:34:15 +0000 (12:34 +0200)
Just optimistically assume that they are ok and will not cause leaks.
The possibility of there being something else which might cause a leak
is lower than the possibility that we didn't think about something else
and raise a warning, refusing to cache the loader, and really cause a
leak.

psycopg/psycopg/types/json.py

index c99a4a00dc0c3394dadaa056e4c62753da43bc8b..a15659e0b803344e41f2e3528f83a33f45da8ca8 100644 (file)
@@ -8,9 +8,8 @@ from __future__ import annotations
 
 import json
 import logging
-from types import BuiltinFunctionType, CodeType  # noqa[F401]
+from types import CodeType  # noqa[F401]
 from typing import Any, Callable
-from warnings import warn
 from threading import Lock
 
 from .. import _oids, abc
@@ -22,7 +21,7 @@ from .._compat import TypeAlias
 
 JsonDumpsFunction: TypeAlias = Callable[[Any], "str | bytes"]
 JsonLoadsFunction: TypeAlias = Callable[["str | bytes"], Any]
-_AdapterKey: TypeAlias = "tuple[type, CodeType | BuiltinFunctionType | type]"
+_AdapterKey: TypeAlias = "tuple[type, Callable[..., Any] | CodeType]"
 
 logger = logging.getLogger("psycopg")
 
@@ -155,19 +154,14 @@ def _get_adapter_key(t: type, f: Callable[..., Any]) -> _AdapterKey | None:
     the same if a lambda if defined in a function, so we can use it as a more
     stable hash key.
     """
-    # A builtin is stable and has no cache so it's a good candidate as hash key.
-    if isinstance(f, (BuiltinFunctionType, type)):
-        return (t, f)
-
-    # Check if there's an unexpected Python implementation that doesn't define
-    # these dunder attributes. If that's the case, raise a warning, which will
-    # crash our test suite and/or hopefully will be detected by the user.
+    # If this function has no code or closure, optimistically assume that it's
+    # an ok object and stable enough that will not cause a leak. for example it
+    # might be a C function (such as `orjson.loads()`).
     try:
         f.__code__
         f.__closure__
     except AttributeError:
-        warn(f"function {f} has no __code__ or __closure__.", RuntimeWarning)
-        return None
+        return (t, f)
 
     # If there is a closure, the same code might have different effects
     # according to the closure arguments. We could do something funny like