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

Jim Bosch talljimbo at gmail.com
Tue Nov 15 16:20:04 CET 2011


On 11/15/2011 09:51 AM, Jérôme Laheurte wrote:
>
> 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 ?

Interesting, I hadn't thought of that.

Anyhow, I believe it should be possible (but not very useful) with both; 
you can make the inner module, but it's just a Python C-API module and 
you have to manually add SWIG or Boost.Python-wrapped functions and 
types to it.

Jim


>
>
> #include<Python.h>
>
> static PyObject *myfunc(PyObject *self, PyObject *args) { if
> (!PyArg_ParseTuple(args, "|myfunc")) return NULL;
>
> printf("Hello, world\n");
>
> Py_INCREF(Py_None); return Py_None; }
>
> static PyMethodDef functions[] = { { "myfunc", (PyCFunction)myfunc,
> METH_VARARGS, "" }, { NULL } };
>
> static PyMethodDef nofunctions[] = { { NULL } };
>
> void inittestmod() { PyObject *mdl, *submdl;
>
> if (!(mdl = Py_InitModule3("testmod", nofunctions, ""))) return;
>
> if (!(submdl = Py_InitModule3("foo", functions, ""))) return;
>
> Py_INCREF(submdl); PyModule_AddObject(mdl, "foo", submdl); }
>
> After building,
>
>>>> import testmod testmod.foo.myfunc()
> Hello, world
>>>>
>
> _______________________________________________ Cplusplus-sig mailing
> list Cplusplus-sig at python.org
> http://mail.python.org/mailman/listinfo/cplusplus-sig



More information about the Cplusplus-sig mailing list