Need Help Differentiating Bad Commands From Incomplete Commands

Discussion in 'Python' started by Tim Stanka, Jul 30, 2004.

  1. Tim Stanka

    Tim Stanka Guest

    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
     
    Tim Stanka, Jul 30, 2004
    #1
    1. Advertising

  2. Tim Stanka

    Jeff Epler Guest

    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)

    iD8DBQFBDaIiJd01MZaTXX0RAqRCAJoCwFbeF3stlmUlKu9qm48//KX2IwCfWnDu
    xSAZAJhbHCMIBlr4MPObM94=
    =kzMv
    -----END PGP SIGNATURE-----
     
    Jeff Epler, Aug 2, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Jaffe
    Replies:
    4
    Views:
    1,198
    John Saunders
    Jul 9, 2004
  2. =?Utf-8?B?U3VtYQ==?=

    Differentiating a normal window from a popup window.

    =?Utf-8?B?U3VtYQ==?=, Feb 28, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    468
    =?Utf-8?B?U3VtYQ==?=
    Feb 28, 2005
  3. Pietro

    differentiating between requests

    Pietro, Aug 31, 2005, in forum: ASP .Net
    Replies:
    8
    Views:
    469
    =?Utf-8?B?UGF1bCBEIE1jTQ==?=
    Sep 1, 2005
  4. Exceedingly
    Replies:
    2
    Views:
    471
    Exceedingly
    Feb 21, 2006
  5. rantingrick
    Replies:
    44
    Views:
    1,235
    Peter Pearson
    Jul 13, 2010
Loading...

Share This Page