PyObject_SetAttrString - doesn't set instance attribute

Carl Banks pavlovevidence at gmail.com
Sun May 2 17:19:08 EDT 2010


On May 2, 3:26 am, Jason <jason.hee... at gmail.com> wrote:
> On May 2, 5:52 pm, Carl Banks <pavlovevide... at gmail.com> wrote:
>
> > Not sure what you're doing here.  It looks like you are being passed
> > an object of a given type, then you get the type object, call it to
> > create another object of that type, and assign it to object->instance.
>
> Sorry, I should have noted that the "NautilusPythonObject" type in the
> code is a struct defined as:
>
> struct _NautilusPythonObject {
>   GObject parent_slot;
>   PyObject *instance;
>
> };
> > You are assigning the attirbute the the object that the C code refers
> > to as "object->instance", but it seems that in the Python snippet you
> > are calling getattr on the object that the C code refers to as
> > "object".
>
> object->instance is the PyObject, and I gathered that it was the
> correct thing to assign to from the fact that the address is identical
> as seen from C and Python.

We'd have to see more code, I'd think.  What you posted needs more
context.  For instance, what type, exactly, is class->type?  This
would help us understand better.

I don't understand how object->instance and self could be the same
object.  If they have the same address it's possible (and, I'm
inclined to think, likely) that you're creating an object, it's
getting destroyed, then you are creating another one.

Here's what's really odd.  You are calling getattr(self,
"super_happy_magic") inside __init__, which is the class's
initializer.  How could you have had occasion to call
PyObject_SetAttrString on that same object at that point?  The only
possible way it could have happened is if
nautilus_python_object_instance_init is invoked by
MenuProviderTest.__new__ somehow, but that doesn't make sense either.
You run PyObject_CallObject(class->type,NULL) to create object-
>instance, but calling a type also calls the type's __init__ method.
So how is it that later the __init__ method is being called again on
the same object?  Unless you're doing something very weird, it could
only mean it's a different object.

I doubt it'll fix all your problems, but one thing to try is to
replace "PyObject_CallObject(class->type, NULL);" with "class->type-
>tp_new(class->type);".

But you probably have to go back to the drawing board and rethink the
whole thing.  What you've posted is quite unusual.


You should choose more descriptive variable names, too.


Carl Banks



More information about the Python-list mailing list