469,622 Members | 1,462 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Need Help Differentiating Bad Commands From Incomplete Commands

Thanks in advance for any responses.

I have an application which embeds Python. I also have a command window
for using Python interactively. The last piece I have left is to
detect incomplete (ie "for x in range(10):") vs bad ("fer x in ronge(10):")
input. Basically I read a line of input from my command window and
feed it to PyRun_SimpleString(command). This command returns 0 on a
good, complete command ("a = 10") but returns non-zero on bad and incomplete
commands. I need to do something else to differentiate between the last two.

I did find a section in the FAQ docs which gave some sample code:
n = PyParser_ParseString(m_python_command,
&_PyParser_Grammar,Py_file_input, &e);
Problem is _PyParser_Grammar gives me an "unresolved external" error.
I did some searching through the .h files and the actual source code and
I could not find this symbol. I did some searching on the web and found
a little snippit that indicated it was an extern which used to be defined
in a .c file.

The C-API manual doesn't list PyParser_ParseString() as a function. I'm
thinking it might have been a wrapper for another function.

Does anyone have a code snippit I could use for my purpose?

Thanks,
Tim
Jul 18 '05 #1
1 1642
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-----

Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Andrew Ward | last post: by
6 posts views Thread by Eric Smith | last post: by
2 posts views Thread by dave_dp | last post: by
19 posts views Thread by Justin | last post: by
10 posts views Thread by arcadio | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.