Skip to content

Latest commit

 

History

History
45 lines (31 loc) · 2.25 KB

NOTES.md

File metadata and controls

45 lines (31 loc) · 2.25 KB

You need to have the _whatever.so in the same dir as the whatever.py file that gets generated by SWIG for importing the .py file to work

If it's cpp files getting wrapped, you need to pass swig_opts=['-c++'] as a kwarg to the Extension constructor Otherwise you get:

ImportError: dlopen(/Users/db/Desktop/swig_scratch/pyWrapper/_inplace.so, 2): Symbol not found: _inplace

Referenced from: /Users/db/Desktop/swig_scratch/pyWrapper/_inplace.so Expected in: dynamic lookup

You can tell if this is missing because the swig command will be:

swig -python -o inplace_wrap.c inplace.i

instead of:

swig -python -c++ -o inplace_wrap.cpp inplace.i

Note also that language='c++' wasn't necessary as a kwarg to the extension

If you want to define stuff in a .c file, you HAVE to extern "C" the header or you'll get something like:

Traceback (most recent call last):
  File "test_example.py", line 4, in <module>
    import example
  File "/Users/db/Desktop/swig_scratch/pyWrapper/example.py", line 26, in <module>
    _example = swig_import_helper()
  File "/Users/db/Desktop/swig_scratch/pyWrapper/example.py", line 22, in swig_import_helper
    _mod = imp.load_module('_example', fp, pathname, description)
ImportError: dlopen(/Users/db/Desktop/swig_scratch/pyWrapper/_example.so, 2): Symbol not found: __Z9util_funcv
  Referenced from: /Users/db/Desktop/swig_scratch/pyWrapper/_example.so
  Expected in: dynamic lookup

SWIG is really weird about having both input and output arrays...you have to supply the size of the input (or maybe output?) array as an arg... -EDIT: see next point

An important subtlety: ARGOUT_ARRAYs don't eat up the length parameter--they replace the T* with the numpy T array, but the "int len" part still needs to get passed in. -if it looks easy, maybe figure out how to typemap this so that it eats up the length param -Way easier approach: see if we can just pass in a default arg -can't actually get this to work with numpy array lengths... -Related subtlety: needs the length arg to actually be passed in by python, because the SWIG func uses it when determining how long an array to return -so we could probably wrap it in another func that has a default arg in python, but not in cpp -basically, try to avoid using argout arrays...