Skip to content

Commit 9c48a1e

Browse files
Constrain lru_cache proxy detection to BaseObjectProxy subclasses.
The previous check for an __self_setattr__ attribute on the bound instance was too loose, since an unrelated class could expose that name and be misrouted into the proxy cache storage path. Detection now gates on issubclass(type(instance), BaseObjectProxy), using type() so the real proxy type is seen rather than the wrapped object's type reported by the proxy __class__ override, then stores via __self_setattr__.
1 parent 4053838 commit 9c48a1e

1 file changed

Lines changed: 9 additions & 9 deletions

File tree

src/wrapt/caching.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from functools import lru_cache as _functools_lru_cache
1010
from functools import partial
1111

12-
from .__wrapt__ import BoundFunctionWrapper, FunctionWrapper
12+
from .__wrapt__ import BaseObjectProxy, BoundFunctionWrapper, FunctionWrapper
1313
from .decorators import decorator
1414
from .synchronization import synchronized
1515

@@ -74,14 +74,14 @@ def __call__(self, *args, **kwargs):
7474
# If the instance the method is bound to is a wrapt
7575
# object proxy, a plain setattr() would fall through and
7676
# store the cache on the wrapped object rather than the
77-
# proxy. Proxies expose __self_setattr__() which stores
78-
# the attribute on the proxy itself, so use it when it is
79-
# available and fall back to setattr() otherwise.
80-
81-
set_attr = getattr(instance, "__self_setattr__", None)
82-
83-
if set_attr is not None:
84-
set_attr(cache_attr, cache)
77+
# proxy. Use type() rather than isinstance() so the check
78+
# sees the real proxy type and is not fooled by the proxy
79+
# overriding __class__ to report the wrapped object's
80+
# type. Proxies expose __self_setattr__() which stores the
81+
# attribute on the proxy itself.
82+
83+
if issubclass(type(instance), BaseObjectProxy):
84+
instance.__self_setattr__(cache_attr, cache)
8585
else:
8686
setattr(instance, cache_attr, cache)
8787

0 commit comments

Comments
 (0)