[C++-sig] Re: please help with simple program crashing

David Abrahams dave at boost-consulting.com
Thu Jul 1 15:52:59 CEST 2004

Drew Ferraro <gfilla at gmail.com> writes:

> hi,
> ive recently trying to add a scripting system to a game i am making..
> anyway, ive started to learn python and am trying to figure out how to
> embed it in my game. i have gotten a C++ function to work from inside
> C++ but called from python via PyRun_SimpleString(),,, i can send
> commands directly to the intepreter with that function, but im having
> a problem
>  with PyRun_SimpleFile(). im trying to execute a python script with
> this function, but its crashing when it gets to this line:
>  PyRun_SimpleFile(fp,"test.py");
> i cant figure out what im doing wrong. here is the source. any help is
> greatly appreciated!!

One likely possibility is that your Python was built with a different
C/C++ standard library than your program, so they have different
ideas of what a FILE is supposed to be.  Also, Boost.Python is
incompatible with Py_Finalize... though it's not clear why you're
linking with Boost.Python anyway, since you're not using it.

> #include <boost/python.hpp>
> #include <iostream>
> #include <stdio.h>
> using namespace std;
> using namespace boost::python;
> #ifdef _WIN32
> #pragma comment(lib,"boost_python.lib");
> #endif
> int main()
> {
>    Py_Initialize();
>    FILE *fp = NULL;
>    fp = fopen("test.py", "r");
>    if(fp == NULL)
> 	  return 1;
>    PyRun_SimpleFile(fp,"test.py");
>    Py_Finalize();
>    system("PAUSE");
>    return 0;
> }
> thanks for any help! also, i tried not opening the file myself, and
> just sending PR_SF() a NULL FILE*... it didnt crash! instead, in the
> console it said there was a syntax error and printed out some garbage
> characters... i dont think theres a syntax error, "text.py" entire
> file just says print "hello world"
> thanks again!

Dave Abrahams
Boost Consulting

More information about the Cplusplus-sig mailing list