Aaron Optimizer Digulla wrote:
> Why is this neccessary ? Isn't is sufficient to believe that all
> python objects will be referenced as long as the Gtk object
> exists ? For example, when I add a button to a window, the Python
> Button object must be added to the Python Window object (in order
> to be able to find it again).
We don't want to try maintaining the widget heirarchy at the python
level for 2 reasons:
- It is very error prone. Circular references, reparenting, etc...
- Gtk already does this for us. Why duplicate code already written?
> :-/ Why are you sure that the python object *can* be deleted when the
> Gtk object is ?
No, which is why we don't delete it. ;)
To clarify: at the C level, calling PyDict_DelItem would decrement the
value and key reference counts. If a reference count ends up being
zero, then the Python library deletes the object. But if it is
referenced elsewhere in your program, then the reference count remains
above zero, and the object stays put. All of this is managed by Python
C interface, and we can be completely unconcerned about it...
> Eg. what happens if the Gtk object should be destroyed
> but the Python object is still refd somewhere else ?
Nothing, except you really couldn't do anything with the reference,
except del() it. But your program wouldn't SEGFAULT, if that's what you
were thinking.
> Or the other
> way round: What happens when I del the python object but the Gtk object
> is still used somewhere in Gtk ?
Not possible. Well, Ok, you could do:
def somefunc():
...
b = GtkButton()
...
return
Upon returning from somefunc, python will decrement the reference count
of b. But, because pygtk still has a reference to b, it is not
deleted. The Gtk object still exists, and is still displayed on the
screen.
> That's true. But I see no harm in storing the Python object in Gtk's
> data field. There must just be a strict order who rules. Either Gtk
> tells Python what to do or the other way round. Currently, it's Gtk
> and that means that it's not possible to use the full power of Python.
It doesn't particularly matter where the pointer to the Python object is
stored, it's just a matter of taste. There are just some additional
INCREF's and DECREF's to perform if it is stored in the data field, but
avoiding the dictionary lookup would be provide a small performance
gain.
Note that just storing a pointer to a Python object in the Gtk object's
data field doesn't buy you anything -- you still need to decide at what
points you take a reference to the object, and when you give up your
reference to the object. If done wrong or inconsistently, you end up
with either memory leaks or SEGFAULTs. Where the pointer is stored is
irrelevant.
--
Richard Fish Enhanced Software Technologies, Inc.
Software Developer 4014 E Broadway Rd Suite 405
[EMAIL PROTECTED] Phoenix, AZ 85040
(602) 470-1115 http://www.estinc.com
To unsubscribe: echo "unsubscribe" | mail [EMAIL PROTECTED]