Python um C++ Methode erweitern

Python in C/C++ embedden, C-Module, ctypes, Cython, SWIG, SIP etc sind hier richtig.
Antworten
alexp
User
Beiträge: 6
Registriert: Mittwoch 10. März 2010, 15:36

Hi,

ich habe eine C++ Methode (do_something).
Jetzt würde ich gerne Python um diese Methode erweitern.
Wenn ich die C++ Datei mit den Disutils kompilieren ("python setup.py build --compiler=mingw32"), klappt das einwandfrei.
Versuch ich jedoch das Module "module.pyd" einzubinden, bekomm ich folgenden Fehler:
"SystemError: dynamic module not initialized properly"

Hab leider von C++ nicht so die Ahnung, nur Erfahrung mit C#.

Hoffe mir kann jemand helfe, Danke schonmal.

Hier noch der Code:

module.cpp

Code: Alles auswählen

#include <Python.h>

double **do_something(unsigned N, unsigned D, char *dir_file)
{
  double **result;
  [..]
  return result;
}

static PyObject *module_do_something(PyObject *self, PyObject *args)
{
    double **sts;
	unsigned N;
	unsigned D;
	char *dir_file;

    if (!PyArg_ParseTuple(args, "iis", &N, &D, &dir_file))
        return NULL;
    sts = do_something(N, D, dir_file);
    return Py_BuildValue("[[d]]", sts);
}

static PyMethodDef Methods[] = {
    {"do_something",  module_do_something, METH_VARARGS,
     "Execute a shell command."},
	{NULL, NULL, 0, NULL}
};

extern "C" void initmodule() {
PyObject *m;
m = Py_InitModule("do_something", Methods);
}
setup.py

Code: Alles auswählen

from distutils.core import setup, Extension

module1 = Extension('module',
                    sources = ['module.cpp'])

setup (name = 'Module',
       version = '1.0',
       description = 'This is a demo package',
       ext_modules = [module1])
Antworten