New submission from Armin Rigo <ar...@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 <rep...@bugs.python.org>
<http://bugs.python.org/issue8578>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to