How to access C variables in Python code object generated by Py_C ompileString

Duncan Booth duncan.booth at invalid.invalid
Tue Nov 20 05:19:30 EST 2007


"Borse, Ganesh" <ganesh.borse at credit-suisse.com> wrote:

> 2) my this code got compiled but when running, I got an error from
> Py_CompileString, as below. Why is it so? 
>   File "<string>", line 1
>     if ( (size < 1000) & (vol < (0.001 * ADV)) & (prod=="Stock")):
>     print "OK" ^
> SyntaxError: invalid syntax
> 
>   But when I executed the same code in python process, it worked fine.

You passed Py_eval_input as the start token to Py_CompileString. That means 
you want to evaluate a single expression and an 'if' statement isn't an 
expression. Use Py_file_input to compile a module or Py_single_input for a 
single statement.

Have you looked at using Pyrex? It makes this kind of task really easy: you 
just define a Pyrex function callable from C which imports and calls 
whatever Python code you want. All the work of allocating and releasing 
Python objects gets done for you.




More information about the Python-list mailing list