[C++-sig] Re: Custom WeakPtrs and "None"

David Abrahams dave at boost-consulting.com
Thu Nov 18 21:24:06 CET 2004


haller at ableton.com (Stefan Haller) writes:

> David Abrahams <dave at boost-consulting.com> wrote:
>
>> haller at ableton.com (Stefan Haller) writes:
>> 
>> > The only problem is that when the object goes away, the "weak pointer"
>> > on the Python side isn't set to "None".  Of course, the wrapped C++ weak
>> > pointer is set to NULL, but the python variable isn't aware of that
>> > unless you try to access properties or call methods on it, in which case
>> > it throws an exception.  It would be very nice if a comparison with None
>> > would return true for a weak pointer that has been set to NULL.
>> 
>> Nothing will do that; how would you know all the places the Python
>> object is bound to an attribute name or local variable?
>> 
>> It's in the nature of Python.  It has "name bindings," not "variable
>> values."
>
> Yes, I don't want the Python variable itself to be set to None
> literally, I know there's no way to do that.  I just want it to *look* a
> bit more like None if the WeakPtr is NULL.  For example, I was hoping
> that it is possible to play tricks with overloading __cmp__ (or some
> such) to make the expression (weakptr == None) evaluate to True if
> weakptr is not None, but its pointee is NULL.

You could play that trick manually.  I would definitely *not* want to
do that automatically in the library.

Note that if you wrap this function:

     X* identity(X* x) { return x; }

you actually *will* get None back if you pass it one of these
released weak_ptrs.

> A similar problem is comparison of two weak ptrs; they compare
> different, even if they point to the same underlying object.  It would
> be nice to compare pointees, not pointers here.

That's a very separate but interesting issue.  Perhaps when two C++
objects held by pointer are compared in Python we ought to see if the
held pointers refer to the same thing.

It becomes strange when (multiple) inheritance is involved.  Probably
we'd want to return False unless the two Python objects had the same
type.

-- 
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com




More information about the Cplusplus-sig mailing list