GrahamDumpleton / wrapt

A Python module for decorators, wrappers and monkey patching.
BSD 2-Clause "Simplified" License
2.03k stars 231 forks source link

Allow importing pure-Python implementation without setting `WRAPT_DISABLE_EXTENSIONS` variable #235

Open NiklasRosenstein opened 1 year ago

NiklasRosenstein commented 1 year ago

Without setting the WRAPT_DISABLE_EXTENSIONS environment variable, there is currently no way to specifically import the pure Python implementation of ObjectProxy.

My use case requires the Python implementation to allow me to overwrite an attribute on the ObjectProxy object without modifying the attribute on the wrapped object.

wrapper = ObjectProxy(obj)
object.__setattr__(wrapper, "mymethod", wrapped_mymethod)

assert obj.mymethod is not wrapped_mymethod
assert wrapper.mymethod is wrapped_mymethod
NiklasRosenstein commented 1 year ago

A hacky workaround I found:

attrs = {"mymethod": staticmethod(wrapped_mymethod)}
proxy_cls = type('Proxy', (ObjectProxy,), attrs)
wrapper = proxy_cls(obj)