[C++-sig] Multiple modules in a single pyd

Jérôme Laheurte fraca7 at free.fr
Tue Nov 15 17:16:56 CET 2011


Le 15 nov. 2011 à 16:08, Wichert Akkerman a écrit :

> On 11/15/2011 03:51 PM, Jérôme Laheurte wrote:
>> Le 15 nov. 2011 à 15:24, Jim Bosch a écrit :
>> 
>>> On Nov 15, 2011 8:53 AM, "Olivier Voyer"<olivier.voyer at gmail.com>  wrote:
>>>> Hi everyone,
>>>> 
>>>> Is it possible to have multiple modules linking to one single pyd file? I'm using SWIG with VS2010 and I can't find a way of doing that.
>>>> 
>>> I believe this is not supported by the Python C-API itself, regardless of what wrapper generator or library you use.  The name of the module import function needs to be related to the loadable module file name, and you generally can't have two functions for which that's true.
>> But you can put several submodules in an extension module, as a workaround (see code below). I always wondered if that was possible with boost::python or SWIG ?
> 
> You can. For example:
> 
> 
> void exportModule1() {
>    object module(handle<>(borrowed(PyImport_AddModule("mypkg.module1"))));
>    scope().attr("module1") = module.
>    scope module_scope = module;
> 
>   // put your def/class_/etc things here
> }
> 
> void exportModule2() {
>    object module(handle<>(borrowed(PyImport_AddModule("mypkg.module2"))));
>    scope().attr("module2") = module.
>    scope module_scope = module;
> 
>   // put your def/class_/etc things here
> }
> 
> void export() {
>    object package = scope();
>    package.attr("__path__") = "mypkg";
>    exportModule1();
>    exportModule2();
> }

Nice, thanks. I'll keep this around.



More information about the Cplusplus-sig mailing list