From: Daniele Varrazzo Date: Wed, 10 Sep 2025 19:55:02 +0000 (+0200) Subject: fix: drop warning for objects passed as JSON lods/dump function X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F1166%2Fhead;p=thirdparty%2Fpsycopg.git fix: drop warning for objects passed as JSON lods/dump function 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. --- diff --git a/psycopg/psycopg/types/json.py b/psycopg/psycopg/types/json.py index 5faa32943..6ebab6cbe 100644 --- a/psycopg/psycopg/types/json.py +++ b/psycopg/psycopg/types/json.py @@ -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