Saturday, September 19, 2009

Python extensions, wrapping with swig and bug hunting

If you use swig to wrap your c/c++ code into a python extension, you will sometimes come across bugs when you try to import your freshly minted module.

Errors which go something like,

_mod, attribute referenced before assignment.

They can be pretty nasty to hunt, since you don't know if the bug is in your code, python interpreter (HOLY COW! that's not where I tweeted ;) ), swig (please no!!) or your own code (damn, I'll have debug it instead of passing it off on someone else..).

Chances are that the error is in your code. And if you get error like the one showed above, then python interpreter is throwing an error while importing your extension. To debug it, try the following,

1) if your module is named FOO, then do

import _FOO

as you have to prepend the underscore for the real module while swig sugarcoats it to make it look a little less ugly.

There is a very good chance that it is happening because there is a function in your code which is unknown to swig or the python interpreter is attempting to call a function that does not exist.

The path from now on is a little harder.

2) do

objdump -dS _FOO.so > some-text-file

And dive into that disassembly looking for the offending function. This isn't for the faint-hearted, but it pretty straightforward, quick and simple if you are used to it. :)

Happy bug hunting

No comments: