[Python-Dev] Weakref design questions
Martin v. Loewis
martin@v.loewis.de
19 Oct 2002 06:18:12 +0200
Brian Quinlan <brian@sweetapp.com> writes:
> My current solution to this problem is to create my own callable object
> type that supports weakrefs. That object is then used to wrap the real
> bound method object e.g.
>
> def getattr(self, name): # This is C code
> callable = MyCallAble_New(Py_FindMethod(...);
> objects_to_kill_after_py_func_call.add(callable);
> return PyWeakref_NewProxy(callable);
>
> Avoiding this hassle is the reason for my question.
I notice that you don't need the Weakref property of those proxies at
all: You know precisely the set of all objects to consider when the
underlying C object has gone away.
So you merely want a proxy, not a weak proxy: both for the entire
object, and for the methods. So for this code, you can save the proxy,
and return your callable object. Make the null pointer check in its
tp_call slot, and don't kill it after py func call, but merely clear
the pointer.
I also notice that you rely on the fact that Python code has no way to
find out the underlying object of a weak proxy. I think this is a weak
assumption - there is no guarantee that this is not possible, or might
not be possible in the future.
Regards,
Martin