HOWTO : Py_CompileString !?!?!

Luc acid_til at yahoo.com
Fri Jan 31 10:46:51 EST 2003


Mark Charsley wrote:
> In article <OXa_9.4120$nD1.943639 at news20.bellglobal.com>, 
> acid_til at yahoo.com (Luc) wrote:
> 
> 
>>>>I'm trying to embed python in my C++ application. I have a python 
>>
>>source >>file names test.py and what I want to do is first try to 
>>compile it to >>see if any syntax error or import error occurs.
> 
> 
>>Here is the script in question :
>>
>>import FOMath
>>#FOMath has a method called PointValue()
>>def test():
>>	print FOMath.PointdddValue()
>>
>>test()
>>
>>This should't compile because a syntax error is present...
> 
> 
> There isn't a syntax error I can see. Until you run the string, python 
> doesn't know that FOMath hasn't had a function called PointdddValue added 
> to it. 

If I understand correctly, because Python is interpreted anything goes 
for functions names and variables... until runtime. If such syntax error 
is present the runtime will catch it. Right ???

> 
> Now if you left out the colon in "def test():", that _would_ be a syntax 
> error, and Py_CompileString would return NULL.
> 
> 
>>The way I compile the code from C++ is done like so :
>>
>>//str contains the file that just loaded...
>>Py_CompileString((char*)str.c_str(), "<test.py>", Py_file_input);
> 
> 
> That looks about right.
> 
> HTH
> 
> Mark





More information about the Python-list mailing list