2

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;
}
asked Jun 17, 2013 at 17:00
11
  • I don't see anything obviously wrong with that, and it works for me, albeit on Linux. Try rebuilding from clean. Commented Jun 17, 2013 at 17:09
  • @Aya I even created a completely new solution with just that code in it without any help. Obviously the usual clean solution; rebuild solution stuff too. No idea what else I could try there.. Commented Jun 17, 2013 at 17:10
  • If you've done a debug build, it might be looking for a python33_d.dll which doesn't exist. Try a release build. There are other possibilities if you're linking with a python33.lib which you didn't build yourself. I found it to be most reliable if you build your own copy of python33.lib and python33.dll, and link against those. Commented Jun 17, 2013 at 17:11
  • @Aya Same problem for release build (actually for the debug build I just undef _DEBUG before including the python header to avoid exactly that problem). Anyway if it can't find the dll it just tells me that it can't find it, but not this kind of error. Commented Jun 17, 2013 at 17:18
  • If it's a 64-bit OS, but you're linking to a 32-bit Python, maybe you have to build a 32-bit binary? I dunno. I always built my own copy of Python for embedding purposes and used the same build settings for building the embedding binary, and never had any major issues. Commented Jun 17, 2013 at 17:23

3 Answers 3

1

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.

answered Jun 17, 2013 at 17:56
Sign up to request clarification or add additional context in comments.

Comments

0

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 "test0円". If that doesn't work, it might have to do with the fact that c files are ansi and not utf8.

answered Jun 17, 2013 at 17:09

10 Comments

String literals have an implicit NUL at the end, and those characters are encoded the same way in UTF-8 and "ANSI".
Maybe in python, but there is no implicit NULL attached to C strings.
I don't think there was a single C compiler ever (and C89 certainly had that included as well) that didn't specify that char *foo = "foo" will be null terminated. Otherwise printf("Hello World\n") would be horribly broken..
1. A.2.6, "String Literals" 2. String literal concatenation has been performed by the compiler ever since ANSI C was ratified.
|
0

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.

answered Jun 18, 2013 at 1:23

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.