[Xcb] xpyb and other Python modules

Eamon Walsh efw at eamonwalsh.com
Mon Dec 21 21:02:57 PST 2009

Julien Danjou wrote:
> Hi,
> xpyb is still not widely used, and is not that useful because it lacks
> support from other Python modules.
> Challenged accepted: I decided to add XCB support to pycairo.
> But there's a big problem. To create an XCB surface, cairo requires:
> - a xcb_connection_t
> - a xcb_drawable_t
> - a xcb_visualtype or a xcb_screen_t
> For the 2 first parameters, it's not a real problem since
> xcb_connection_t are used by xpyb, and a drawable is just an integer.
> But xpyb declares its own Python class for visualtype and screem, so
> they are not connected to the libxcb type xcb_visualtype_t and
> xcb_screen_t. That means there is currently no way to bind both modules
> together.
> This is gonna be a real problem for Python modules based upon a library
> using libxcb (cairo, evas, etc).
> xpyb seems a nice implementation of a native xcb-proto based
> lib, but it's totally unusable finally in the Real World.
> I don't know what's the status of other language bindings and if they
> suffers from the same problem (?).
> The only solution I see it writing a lot of code to map a Python
> object from a xpyb Python classm to a C struct from libxcb. That seems
> unpleasant to write and to maintain.
> I'm seeing this situation as a dead end, so any hint would be
> appreciated.

const struct foo *
xpyb2struct(PyObject *obj)
    const void *data;
    Py_ssize_t length;

    if (PyObject_AsReadBuffer(self, &data, &length) < 0)

    if (length < sizeof(struct foo))

    return (const struct foo *)data;

This works for request, response, struct, error, and event objects and any derivatives thereof (including xcb.xproto.VISUALTYPE and xcb.xproto.SCREEN).  It won't work for the List type that wraps lists in responses.  For that, you need to call the "buf" method on the List object to get an object that can be passed to the code above.

See http://docs.python.org/c-api/objbuffer.html  (I'm aware that Python 3.0 has a new buffer interface and I will be looking at that going forward).

Also, the above code is a little cavalier about reference counting.  The data will be freed if the Python object gets garbage collected, so if you need it to remain valid after your entry point returns you need to make a copy of it or take a reference to the object.

Hope this helps!  Making xpyb work with pycairo would be awesome.

--Eamon W.

More information about the Xcb mailing list