Question

while following pythons embedding/extending tutorial i came up with following code

#include <boost/filesystem.hpp>
#include <Python.h>
static PyObject *
spam_system(PyObject *self, PyObject *args) {
    const char *command;
    int sts;
    if (!PyArg_ParseTuple(args, "s", &command))
        return NULL;
    sts = system(command);
    return PyLong_FromLong(sts);
}
static char SpamModuleName[] = "spam\000";
int main(int argc, char const *argv[]) {
    Py_SetPath((
        boost::filesystem::canonical("./python_lib.zip").wstring()
    ).c_str());
    PyImport_AppendInittab(SpamModuleName,[](){
        static PyMethodDef SpamMethods[] = {
            {"system", spam_system, METH_VARARGS, "Execute a shell command."},
            {NULL, NULL, 0, NULL}
        };
        static struct PyModuleDef spammodule = {
            PyModuleDef_HEAD_INIT,
            SpamModuleName,
            NULL,
            -1,
            SpamMethods,
            NULL, NULL, NULL, NULL
        };
        return PyModule_Create(&spammodule);
    });
    Py_Initialize();
    PyRun_SimpleString(
        "import spam\n"
        "status = spam.system(\"ls -l\")\n"
    );
    Py_Finalize();
    return 0;
}

the code compiles fine (using g++ -std=c++11 main.cpp -lpython33.64 -lboost_filesystem -lboost_system -s i use x64 native mingw toolchain by Stephan T. Lavavej) yet when running my programm allocates about 4 gig of ram and have 100% cpu usage (procexp screenshot) in PyRun_SimpleString("import spam\n") and more than often crashes with pythons MemoryError.

PyImport_ImportModule(SpamModuleName); crashes the program too, also after allocating a lot of memory (i in fact never had a successfull run with this function).

If i end all other programs and free as much of the ram as possible the program runs fine and produces the expected output yet the resource eatup makes it unsable. Wwhat am i doing wrong/what makes python use that much resources?

edit after discussion on mingw-w64 irc i got it working and will post the solution as an answer in case someone else find themself in my place

Was it helpful?

Solution

thanks to extensive help from users alexey and ktietz i was pointed to the fact python.dll built with 64 bit VC have problems importing x64 binaries built by GCC. The solution is to build the lib yourself, while patching it to compile unter MINGWx64.

one can find those patches here or the prebuilt libraries here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top