[Python-Dev] PyWeakref_GetObject() borrows its reference from... whom?

Larry Hastings larry at hastings.org
Mon Oct 10 05:35:31 EDT 2016



On 10/10/2016 11:05 AM, Devin Jeanpierre wrote:
> > whose reference is it borrowing?
>
> I think this is a red herring sort of question. "borrowed" only means 
> "unowned". But anyway, we borrowed from the weakref. (Borrowing from 
> somebody doesn't imply they own a reference -- we can borrow from a 
> borrowed reference, for example, and this is common.)

Huh?  In all other circumstances, a "borrowed" reference is exactly 
that: X has a reference, and you are relying on X's reference to keep 
the object alive.  Borrowing from a borrowed reference is simply a chain 
of these; Z borrows from Y, Y borrows from X, and X is the original 
person who did the incref.  But you're borrowing from something 
specific, somebody who the API guarantees has a legitimate reference on 
the object and won't drop it while you're using it.  I bet for every 
other spot in the API I can tell you from whom you're borrowing the 
reference.

In contrast, the "borrowed" reference returned by PyWeakRef_GetObject() 
seems to be "borrowed" from some unspecified entity.  The fact that the 
object is live in Python directly implies that, yes, *somebody* must 
have a reference, somewhere.  But ISTM (and apparently you) that this is 
relying on the GIL preventing that unknown other actor from dropping 
their reference while you've borrow it.  A guarantee that the 
post-Gilectomy Python interpreter can no longer make!

In any case, I see nothing in the documentation that suggests "borrowed 
only means unowned" as you suggest.  In contrast, the documentation 
seems to suggest that the metaphor is how I understood it; that when you 
"borrow" a reference, there is another object who has a reference and 
you're relying on their reference to keep the object alive.

    https://docs.python.org/3.6/extending/extending.html#reference-counts



//arry/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20161010/f80ea4f7/attachment.html>


More information about the Python-Dev mailing list