Python bindings

Armin Bauer armin.bauer at
Sat Apr 10 20:57:16 EST 2004


im working on an application where i also wrote python bindings for.
Soon i had to set a c variable on a class, so i borrowed some of the
code from dbus:

cdef class Message:
    cdef DBusMessage *msg

    def _set_msg(self, msg):
        self.msg = <DBusMessage*>msg

which is used for example here:

    def borrow_message(self):
    	m = Message(_create=0)
        return m

your approach gave me unpredictable segfaults but i found no other way
so i asked the pyrex list. Here is the answer:

It looks like you're trying to smuggle a non-Python pointer into the
_set_struct1 method by pretending it's a Python object. That won't

You should NEVER cast a pointer to a Python type unless it really and
truly points to a Python object. Crashing and burning is guaranteed to

Here is how it should look like for the message example above:

cdef class Message:
    cdef DBusMessage *msg

    #get rid of the _set method here

the other code would look like this:

    def borrow_message(self):
	cdef Message m
    	m = Message(_create=0)
        m.msg  = dbus_connection_borrow_message(self.conn)
        return m

This fix will probably also get rid of errors like these:

# FIXME: this is a major major hack. We use this because casting values
# python objects and returning seemed to be corrupting them. This is a
"global variable" :-(

Best Regards, Armin Bauer

More information about the dbus mailing list