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);
}

...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?

— Jason
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to