How to evaluate the code object returned by PyParser_SimplePa rseString function?

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Thu Nov 15 05:25:37 CET 2007


En Wed, 14 Nov 2007 23:20:14 -0300, Borse, Ganesh  
<ganesh.borse at credit-suisse.com> escribió:

> Py_CompileString takes the source code from file, isn't it?
> As can be seen from the syntax of this function: PyObject*  
> Py_CompileString(char *str, char *filename, int start)
>
> I want to parse the code which is in memory - loaded from database.
> In that case, may I know, how to use the Py_CompileString?

The first argument is the actual source code string, a char*; the filename  
argument is only used to generate pretty error messages, you may pass in  
some meaningful name or '<string>'.

> If it is mandatory to read from file for this function?
> Reading from file increases startup time of my application.
> So, I was thinking of using PyParser_SimpleParseString, which takes the  
> code to be parsed in the "char*" format. Quit suitable to my need.

No, both Py_CompileString and PyParser_SimpleParseString take the same  
first argument, a char* with the source code.

> Can I use the output of the function PyParser_SimpleParseString as input  
> to PyEval_EvalCode?

Not directly, you must compile the parsed input first. See pythonrun.c for  
examples, but you will end duplicating what Py_CompileString already does.

-- 
Gabriel Genellina




More information about the Python-list mailing list