470,821 Members | 2,024 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,821 developers. It's quick & easy.

Validating Syntax only with PyParser_SimpleParseString ?


I am seeking for a method to parse single lines of Python code (infact
they are only formulas, so I generate a line like RESULT=<formula>). I
do only want to know if the syntax is correct and if there is an error
best would be to know where.

I did find PyParser_SimpleParseString which does return a node
structure. If there is an error it seems to return NULL, while I did
not find this documented.

Th eproblem with this is that first I do not get th elopcation of the
syntax error, and second I don't know what is the proper method to free
the node structure after I got it, since I will not use it. Or is there
any better method doing this anyway.

Environment: Windows / Borland C++

Mar 29 '06 #1
3 1515

ga**************@web.de wrote:
I am seeking for a method to parse single lines of Python code (infact
they are only formulas, so I generate a line like RESULT=<formula>). I
do only want to know if the syntax is correct and if there is an error
best would be to know where.

I did find PyParser_SimpleParseString which does return a node
structure. If there is an error it seems to return NULL, while I did
not find this documented.

Th eproblem with this is that first I do not get th elopcation of the
syntax error, and second I don't know what is the proper method to free
the node structure after I got it, since I will not use it. Or is there
any better method doing this anyway.
A better way might be to use the standard library compiler module. A
file called cptools.py (part of the cherrypy project) has a very simple
example called 'unrepr' which shows how to do it.

I only have restricted internet access or I would find the URL for you
(sorry).

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml
Environment: Windows / Borland C++


Mar 29 '06 #2

ga**************@web.de wrote:
I am seeking for a method to parse single lines of Python code (infact
they are only formulas, so I generate a line like RESULT=<formula>). I
do only want to know if the syntax is correct and if there is an error
best would be to know where.

I did find PyParser_SimpleParseString which does return a node
structure. If there is an error it seems to return NULL, while I did
not find this documented.

Th eproblem with this is that first I do not get th elopcation of the
syntax error, and second I don't know what is the proper method to free
the node structure after I got it, since I will not use it. Or is there
any better method doing this anyway.

Environment: Windows / Borland C++


The builtin function compile will throw a SyntaxError if the syntax is
bad.
compile("1=a","","single") Traceback (most recent call last):
File "<interactive input>", line 1, in ?
SyntaxError: can't assign to literal


Mar 29 '06 #3
Thanks for th eanswers so far.

But as you can see from the function I use, I do need an interface
function to do th ejob from C++. I canno timagine there is no clean way
to do this. Calling th epython interpreter to execute a compule
function in python and then get the output parsed to get th eerror
message i snot an option. The strange thing however is that I have
tried to use th ecompile interface function first and this one did
return an error. I supposed this was because th ewhole context did not
exist so I went to the "parse only" function.

The main question I have is what to do with the pointer to the node
structure returned by this function, as freeing th epointer with free()
throws an error.

Mar 30 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Clifford W. Racz | last post: by
reply views Thread by blurz | last post: by
5 posts views Thread by Steve | last post: by
2 posts views Thread by bildad | last post: by
3 posts views Thread by Tom Anderson | last post: by
232 posts views Thread by robert maas, see http://tinyurl.com/uh3t | last post: by
2 posts views Thread by arunairs | last post: by
76 posts views Thread by Michael Stemper | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.