[New-bugs-announce] [issue8578] PyWeakref_GetObject

Armin Rigo report at bugs.python.org
Fri Apr 30 15:58:21 CEST 2010

New submission from Armin Rigo <arigo at users.sourceforge.net>:

PyWeakref_GetObject(wref) returns a borrowed reference, but that's rather dangerous.  The fact that wref stays alive does not prevent the returned object from being collected (by definition -- wref is a weak reference).  That means that either we must explicitly and immediately do a Py_INCREF() (and later Py_DECREF()) on the result of the function, or we must use it for a very short time.

As an example of why this interface encourages buggy behavior, the sole user of PyWeakref_GetObject() in Module/* is Module/_sqlite/connection.c, which does

    statement = PyWeakref_GetObject(weakref);

and then call some functions passing 'statement' as argument.  The called functions can do anything (because they release the GIL).  So in particular they can cause 'statement' to be freed while still in use, either directly or indirectly via the cycle-GC.

This should be fixed; I suggest deprecating PyWeakref_GetObject() and adding another C API function that does not return a borrowed reference.

components: Interpreter Core
messages: 104634
nosy: arigo
priority: normal
severity: normal
status: open
title: PyWeakref_GetObject
type: crash

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list