Unfortunately the answer to this right now is that support for bindings other than Cython are poor/nonexistent. I'd highly recommend using Cython if you plan to integrate deeply with pycapnp. However, I understand if you're not able to just switch frameworks at the drop of a hat :)
If you really want to integrate with SWIG, then we're going to have to dive into Cython just a bit. If you want to follow along, run `python setup.py build` in pycapnp's directory and then open `capnp/lib/capnp.cpp' and look for the definition of the extension class _MessageBuilder. In my file, the name mangling turned it into __pyx_obj_5capnp_3lib_5capnp__MessageBuilder but that may be different for you. Once you find it, you'll see something like:
struct __pyx_obj_5capnp_3lib_5capnp__MessageBuilder {
PyObject_HEAD
struct __pyx_vtabstruct_5capnp_3lib_5capnp__MessageBuilder *__pyx_vtab;
::capnp::MessageBuilder *thisptr;
};
What this says is that the _MessageBuilder PyObject has two extra fields, the cython vtable (which we don't care about), and the Cap'n Proto Message builder. The layout of this struct is guaranteed to be preserved since this is how Cython will access fields across pyx/pxd files. It's a bit sad that there's no way to get Cython to export this in a re-usable header, but for our purposes you can just paste the following struct into your code:
struct pycapnp_MessageBuilder {
PyObject_HEAD
void *__pyx_vtab;
::capnp::MessageBuilder *thisptr;
};
And cast to it whenever you receive a _MessageBuilder PyObject from pycapnp. The easiest way is to obtain one is like so:
addresses = addressbook_capnp.AddressBook.new_message()
addresses._parent # this is always a MessageBuilder when allocated from a new_message like above
Also be very sure to keep the _MessageBuilder's PyObject refcount above 0, otherwise it will be garbage collected and the underlying capnp::MessageBuilder deleted.