Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Need Help Differentiating Bad Commands From Incomplete Commands

Reply
Thread Tools

Need Help Differentiating Bad Commands From Incomplete Commands

 
 
Tim Stanka
Guest
Posts: n/a
 
      07-30-2004
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
 
Reply With Quote
 
 
 
 
Jeff Epler
Guest
Posts: n/a
 
      08-02-2004
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-----

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
integer >= 1 == True and integer.0 == False is bad, bad, bad!!! rantingrick Python 44 07-13-2010 06:33 PM
Bad media, bad files or bad Nero? John Computer Information 23 01-08-2008 09:17 PM
ActiveX apologetic Larry Seltzer... "Sun paid for malicious ActiveX code, and Firefox is bad, bad bad baad. please use ActiveX, it's secure and nice!" (ok, the last part is irony on my part) fernando.cassia@gmail.com Java 0 04-16-2005 10:05 PM
24 Season 3 Bad Bad Bad (Spoiler) nospam@nospam.com DVD Video 12 02-23-2005 03:28 AM
24 Season 3 Bad Bad Bad (Spoiler) nospam@nospam.com DVD Video 0 02-19-2005 01:10 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57