Sunday, August 06, 2006

Word of caution: Distutils, SWIG, STL

This one made me waste some precious time so here it is in chance Google picks it up.

Python's Distutils got a little smart and can now run Swig for you but if you have any STL datatypes (vectors, maps, etc) on your wrapped functions, remember to add the undocumented swig_opts=['-c++'] option to your Extension module definition on setup.py.

Otherwise, you may end up with Swig spitting errors like

/usr/share/swig1.3/std/std_common.i:109: Error: Syntax error in input(1).

which if you take a look at the mentioned line of std_common.i, it's an unrecognized "%}" swig-pre-processor-thingy.

Here's how a module definition would look like:

setup(name='pyCOMHook',
#standard setup stuff left out
ext_modules = [Extension('pyCOMHook._mouse', ['mouse.i'],
libraries=libs, include_dirs=includes, define_macros=macros,
extra_compile_args=compilerArgs, language=lang, swig_opts=['-c++'])],
)

Other than that, if you get the dreaded

ImportError: dynamic module does not define init function (initimymodule)

error when importing your module, try renaming mymodule.so to _mymodule.so.


No comments: