Validating Syntax only with PyParser_SimpleParseString ?

olsongt at verizon.net olsongt at verizon.net
Wed Mar 29 21:47:38 CEST 2006


gaston.gloesener at 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
>>>




More information about the Python-list mailing list