You may want to look at the codeop module
[
http://docs.python.org/lib/module-codeop.html]
>>> from codeop import compile_command
>>> compile_command("a = 3") # Complete code
<code object ? at 0xf7054920, file "<input>", line 1>
>>> print compile_command("for x in range(10):") # incomplete code
None
>>> print compile_command("fer x in ronge(10):") # syntax error
Traceback (most recent call last):
[...]
File "<input>", line 1
fer x in ronge(10):
^
SyntaxError: invalid syntax
Note that this can detect invalid syntax ("fer" instead of "for") but
not undefined names (ronge instead of range)---that will only happen
when the code is executed, and range isn't found in anywhere.
Jeff
PS _PyParser_Grammar is an internal symbol, so whoever wrote that FAQ
should be given 20 lashes with a short piece of string
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
iD8DBQFBDaIiJd01MZaTXX0RAqRCAJoCwFbeF3stlmUlKu9qm4 8//KX2IwCfWnDu
xSAZAJhbHCMIBlr4MPObM94=
=kzMv
-----END PGP SIGNATURE-----