[pypy-dev] Bringing Cython and PyPy closer together

Amaury Forgeot d'Arc amauryfa at gmail.com
Sat Feb 18 15:18:30 CET 2012


2012/2/18 Stefan Behnel <stefan_ml at behnel.de>

> I couldn't find the PyWeakref_LockObject() function anywhere. That's a
> PyPy-only thing, right? I aliased it to (NULL) when compiling for CPython.
>

Yes, this function is PyPy-only, to fix a flaw of PyWeakref_GetObject:
it returns a borrowed reference, which is very dangerous because the object
can disappear anytime: with a garbage collection, or another thread...
Fortunately the GIL is here to protect you, but the only sane thing to do
is to quickly INCREF the returned reference.
PyWeakref_LockObject directly returns a new reference.

In PyPy, the behavior of PyWeakref_GetObject is even worse:
to avoid returning a refcount of zero, the returned reference is attached
to the weakref, effectively turning it into a strong reference!
I now realize that this was written before we implemented the
"temporary container" for borrowed references: PyWeakref_GetObject()
could return a reference valid for the duration of the current C call.

-- 
Amaury Forgeot d'Arc
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pypy-dev/attachments/20120218/c4f29b4d/attachment.html>


More information about the pypy-dev mailing list