Need Help Differentiating Bad Commands From Incomplete Commands

T

Tim Stanka

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
 
J

Jeff Epler

You may want to look at the codeop module
[http://docs.python.org/lib/module-codeop.html]
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)

iD8DBQFBDaIiJd01MZaTXX0RAqRCAJoCwFbeF3stlmUlKu9qm48//KX2IwCfWnDu
xSAZAJhbHCMIBlr4MPObM94=
=kzMv
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top