]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
fix: drop warning for objects passed as JSON lods/dump function 1166/head
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 10 Sep 2025 19:55:02 +0000 (21:55 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 10 Sep 2025 19:55:02 +0000 (21:55 +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 5faa32943daa7d9ee51208d22479fc09fb28dbf7..6ebab6cbeece331a1f2247cb0ad153114f19feb2 100644 (file)
@@ -8,9 +8,8 @@ from __future__ import annotations
 
 import json
 import logging
-from types import BuiltinFunctionType, CodeType
+from types import CodeType
 from typing import Any, TypeAlias
-from warnings import warn
 from threading import Lock
 from collections.abc import Callable
 
@@ -22,7 +21,7 @@ from ..errors import DataError
 
 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