Embedded Python implementation

Bengt Richter bokr at oz.net
Mon Sep 9 23:58:38 CEST 2002

On Sat, 07 Sep 2002 04:50:52 GMT, Evan <tagith1394 at hotmail.com> wrote:

>I am currently trying to embed Python in an application I am writing, 
>and all is going well, except for one inexplicable bug..
>When I pass a variable (pointer to a string) to PyRun_String, it will 
>always error on line 2, character 5.. regardless of what is actually on 
>the line in that spot (for that matter, even if there is no line 2)..
>The script works perfectly when I place it in a file, and use 
>PyRun_File, and it also works perfectly when I place the string in 
>quotations, and pass it to PyRun_String directly inside the C code..
>The C code in question is as follows:
>if ( PyRun_String( com_list, Py_file_input, pGlobals, pLocals ) == NULL )
>         PyErr_Print();
>I am confident the problem is not with pGlobals or pLocals, as those 
>arguments are used with PyRun_File, as well as with the quoted string..
Looking at \python-2.1\python\pythonrun.c:
PyRun_SimpleString(char *command)
	PyObject *m, *d, *v;
	m = PyImport_AddModule("__main__");
	if (m == NULL)
		return -1;
	d = PyModule_GetDict(m);
	v = PyRun_String(command, Py_file_input, d, d);
	if (v == NULL) {
		return -1;
	if (Py_FlushLine())
	return 0;

I would model the code after that. You say you have the directories, and Py_file_input
is the  constant from Python.h, so capture v and do the right thing (note the decref), IWG ;-)

Bengt Richter

More information about the Python-list mailing list