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
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")
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