Le 15 nov. 2011 à 15:24, Jim Bosch a écrit :

> On Nov 15, 2011 8:53 AM, "Olivier Voyer" <olivier.vo...@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 ?


#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@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig

Reply via email to