[C++-sig] Alternate packaging technique for bindings + pure python

David Abrahams dave at boostpro.com
Fri Aug 22 02:51:14 CEST 2008


on Mon Aug 18 2008, "troy d. straszheim" <troy-AT-resophonic.com> wrote:

> def load_pybindings(name, path):
>     """
>     Merges python bindings from shared library 'name' into module 'name'.
>     Use when you have a directory structure::
>
>       lib/
>          foo.so
>          foo/
>            __init__.py
>            something.py
>
>     Here, inside ``foo/__init__.py`` call ``load_pybindings(__name__, __path__)``
>
>     this assumes that the first entry in list ``__path__`` is where
>     you want the wrapped classes to merge to.
>
>     """
>     import imp, sys
>     m = imp.load_dynamic(name, __path__[0] + ".so")
>     thismod = sys.modules[name]
>
>     for (k,v) in m.__dict__.items():
>         if not k.startswith("_"):
>             thismod.__dict__[k] = v
>
> Wherein you just use the __name__ and __path__ of the current module being imported to
> locate and load the associated shared library.  Then you copy the symbols into the
> current library.
>
> So the call to it in each __init__.py would be:
>
>   load_pybindings(__name__, __path__)
>
> Nice thing is that everything looks quite homogenous to the user.   It appears to work
> quite well, but I just now came up with this and thought I ask if anybody
> sees any problems.

As Hans said, I'd want to check that it interacts well with everything
else before recommending it, but also I'd want to check that it works
properly on Windows as well as *nix.

If it does continue to work well, I would be very happy if you would
volunteer to rewrite that part of the tutorial :-)

-- 
Dave Abrahams
BoostPro Computing
http://www.boostpro.com



More information about the Cplusplus-sig mailing list