On May 1, 4:04 am, Jason <jason.hee...@gmail.com> wrote: > I'm having a bit of trouble with C/Python bindings. Particularly, > trying to set an instance variable from C when the object is > initialised using PyObject_SetAttrString, but nothing seems to happen. > The C initialisation code is: > > static void > nautilus_python_object_instance_init (NautilusPythonObject *object) > { > > fprintf(stderr, "nautilus_python_object_instance_init called\n"); > > NautilusPythonObjectClass *class; > debug_enter(); > > class = (NautilusPythonObjectClass*)(((GTypeInstance*)object)- > > >g_class); > > object->instance = PyObject_CallObject(class->type, NULL); > > PyObject* test_int = PyInt_FromLong(42); > > if (object->instance == NULL) > { > PyErr_Print(); > } > else > { > fprintf(stderr, "Setting magic parameter\n"); > fprintf(stderr, "From C: "); > PyObject_Print(object->instance, stderr, 0); > fprintf(stderr, "\n"); > int retval = PyObject_SetAttrString(object->instance, > "super_happy_magic", test_int); > fprintf(stderr, "Result: %i\n", retval); > } > > Py_DECREF(test_int); > > }
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. > ...and the Python module contains: > > class MenuProviderTest(nautilus.MenuProvider): > > def __init__(self): > print "From Python: %s" % self > > try: > print getattr(self, "super_happy_magic") > except AttributeError: > print "Didn't work!" > > When the MenuProviderTest is created, the output is: > > nautilus_python_object_instance_init called > Setting magic parameter > From C: <MenuProvTest.MenuProviderTest object at 0x7faee6a9fcd0> > Result: 0 > From Python: <MenuProvTest.MenuProviderTest object at 0x7faee6a9fcd0> > Didn't work! > > (I've tried getattr and self.super_happy_magic, with the same effect.) > > Where am I going wrong? 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". Carl Banks -- http://mail.python.org/mailman/listinfo/python-list