Mixing cairo and boost-python

I've been mixing python and C++ a lot recently, with cairo and pango spanning the both of them. I haven't worked out a good solution for pango yet, but here's a type checking way to pass a cairo.Context into a boost-python wrapper.:

#include <pycairo/pycairo.h> #include <boost/python.hpp> static Pycairo_CAPI_t *Pycairo_CAPI = 0; cairo_t* cairo_t_from_object(boost::python::object cr) { if(!Pycairo_CAPI) Pycairo_IMPORT; PycairoContext* pcc = (PycairoContext*)cr.ptr(); assert(PyObject_TypeCheck(pcc, &PycairoContext_Type)); return PycairoContext_GET(pcc); } void f(object cr_obj) {cairo_t* cr = cairo_t_from_object(cr_obj);...} def("f", f);

And in python: f(cr)