What the heck is with PyRun_File?

Darin Goldstein daring at ecs.fullerton.edu
Thu Jul 19 12:05:47 EDT 2001


I have been told to use this newsgroup as a last resource. I modified
the file Demo/embed/demo.c in order to test out the functionality of
the PyRun_File command. Most likely, I'm making some sort of
boneheaded mistake that will probably be obvious to everyone and their
dog, but I've been banging my head for over 2 hours and I can't figure
out why this won't work.

Everything compiles perfectly and things seem to run as usual. result
does NOT come out NULL but the answer doesn't get parsed correctly.
Ironically, when it prints out the string with the message that it's
not parsing correctly, it only comes up with a smiley face. If someone
could tell me what's wrong, I would VERY much appreciate it. Thanks
for your help in advance.

D

---------------<demo.c>
/* Example of embedding Python in another program */

#include "Python.h"

void initxyzzy(void); /* Forward */

main(int argc, char **argv)
{
	PyObject *dict;
	FILE *fp;
	PyObject *result;
	char *helper; /* should be deleted */
	double answer;


	/* Pass argv[0] to the Python interpreter */
	Py_SetProgramName(argv[0]);

	/* Initialize the Python interpreter.  Required. */
	Py_Initialize();

	/* Add a static module */
	initxyzzy();

	/* Define sys.argv.  It is up to the application if you
	   want this; you can also let it undefined (since the Python 
	   code is generally not a main program it has no business
	   touching sys.argv...) */
	PySys_SetArgv(argc, argv);

	/* Do some application specific code */
	printf("Hello, brave new world\n\n");

	/* Execute some Python statements (in module __main__) */
	PyRun_SimpleString("import sys\n");
	PyRun_SimpleString("print sys.builtin_module_names\n");
	PyRun_SimpleString("print sys.modules.keys()\n");
	PyRun_SimpleString("print sys.executable\n");
	PyRun_SimpleString("print sys.argv\n");

	/* We're going to try and run a Python file here. */
	fp=fopen("costtemp.py","r");
	if (fp==NULL)
	{
		printf("Failed to find the file costtemp.py.\n");
		exit(EXIT_FAILURE);
	};
	result=NULL;
	dict=PyDict_New();
	
	result=PyRun_File(fp,"costtemp.py",Py_file_input,
		dict,dict);
	if (result==NULL) printf("This went wrong.\n");
	if (!PyArg_ParseTuple(result,"d",&answer))
	{
		printf("The answer didn't get parsed correctly:
%s\n",PyObject_Str(result));
	}
	else
	{
		printf("The answer is %.39f\n",answer);
	};

	Py_DECREF(dict);
	Py_DECREF(result);

	fclose(fp);
	/* ********************************************** */

	/* Note that you can call any public function of the Python
	   interpreter here, e.g. call_object(). */

	/* Some more application specific code */
	printf("\nGoodbye, cruel world\n");

	/* Exit, cleaning up the interpreter */
	Py_Exit(0);
	/*NOTREACHED*/
}

/* A static module */

/* 'self' is not used */
static PyObject *
xyzzy_foo(PyObject *self, PyObject* args)
{
	if (!PyArg_ParseTuple(args, ""))
		return NULL;
	return PyInt_FromLong(42L);
}

static PyMethodDef xyzzy_methods[] = {
	{"foo",		xyzzy_foo,	1},
	{NULL,		NULL}		/* sentinel */
};

void
initxyzzy(void)
{
	PyImport_AddModule("xyzzy");
	Py_InitModule("xyzzy", xyzzy_methods);
}

-------------------------------------<costtemp.py>
x=[-9999.999998975394191802479326725006103515625 ,
9989.999999949799530440941452980041503906250 ]
x[0]+2*x[1]



More information about the Python-list mailing list