Voo
Voo

Reputation: 30216

Embedding Python into C

Win7 x64, Python3.3 32bit, Visual Studio 2010/2012 (same behavior). The following code compiles and runs just fine (i.e. prints current date):

extern "C"{ // not having it doesn't make any difference either
#include <Python.h>
}

int main() {
    Py_Initialize();
    PyRun_SimpleString("from time import time,ctime\n"
        "print('Today is', ctime(time()))\n");
    Py_Finalize();
    return 0;
}

while this here fails with a MessageBox saying The application was unable to start correctly (0xc0000005). Click OK to close the application. before main executed (so no breakpoint possible).

extern "C"{ // not having it doesn't make any difference either
#include <Python.h>
}

int main() {
    Py_Initialize();
    PyObject *p = PyUnicode_FromString("test");
    Py_Finalize();
    return 0;
}

Upvotes: 2

Views: 361

Answers (3)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798706

Your broken program is linking against a build of Python that uses UCS-2 as the internal unicode representation, but the installed Python uses UCS-4, and therefore the PyUnicodeUCS2_* imports can't be resolved. You'll need to link against the UCS-4 build instead.

Upvotes: 0

Voo
Voo

Reputation: 30216

So the problem seems to have been the following: I was linking with python3.lib but since the string functions were completely overworked with Python3.3 there seemed to have been some problem with correctly linking them. (can't really explain why this would be so, since PyUnicode_FromString existed obviously in earlier versions of python3 too).

Why I couldn't get a useful error message about that fact is also beyond me, but there we go: linking against python33.lib solved the problem perfectly.

Upvotes: 1

KrisSodroski
KrisSodroski

Reputation: 2842

I think this could be for 2 reasons, but I'm pretty sure its this one:

http://docs.python.org/2/c-api/unicode.html

You need to null terminate y our constant string "test" by making it "test\0". If that doesn't work, it might have to do with the fact that c files are ansi and not utf8.

Upvotes: 0

Related Questions