Error in Extending/Embedding FAQ, point 16: How do I tell"incomplete input" from "invalid input"?

Discussion in 'Python' started by Dietrich Bollmann, Apr 22, 2008.

  1. Hi,

    Both code examples from paragraph 16 from the Python Extending /
    Embedding FAQ - 'How do I tell "incomplete input" from "invalid input"?'
    ( ) do not work with the current state of Python anymore.

    In the second code example, the error message returned by Python is
    checked in order to differentiate errors caused by an incomplete input
    from other syntax errors:

    if (PyArg_ParseTuple (val, "sO", &msg, &obj) &&
    !strcmp (msg, "unexpected EOF while parsing")) /* E_EOF */

    In the current Python version there are more error messages indicating an
    incomplete Python input and I could make the code work for a while
    by adding the following strings to the condition:

    /* error messages indicating an incomplete input */
    if (PyArg_ParseTuple(error, "sO", &message, &obj) &&
    (!strcmp(message, "unexpected EOF while parsing") ||
    !strcmp(message, "expected an indented block") ||
    !strcmp(message, "EOF while scanning triple-quoted string")
    ) { /* E_EOF */

    but recently there are also cases which generate error messages
    which are too general to be added to this list.

    The following code for example:

    >>> eins = [1,

    ... 2,
    ... 3]

    is accepted without any problem by the Python shell.

    When using the code from the FAQ and entering it line by line
    already the second line causes a simple "invalid syntax" error:

    >>> eins = [1,

    ... 2,
    File "<stdin>", line 2
    SyntaxError: invalid syntax

    which is to general to be integrated into the list of tested
    error messages as it might be caused also by code like:

    >>> one two

    File "<stdin>", line 1
    one two
    SyntaxError: invalid syntax

    which generates an "invalid syntax" error even in the Python shell.

    I also tried the first code example of paragraph
    '16 How do I tell "incomplete input" from "invalid input"?'
    of the FAQ in order to see if it could be used to make the
    difference between syntax errors and incomplete code errors.
    But - as in the case before - the returned error
    code is E_SYNTAX (14 = Syntax error) and not E_EOF (11 = End Of File)
    as should be expected.

    Is there anybody who has an idea how to differentiate the
    first case from the second in order to mimic the behaviour of
    the Python shell from c code?

    If this shouldn't be possible lists split into different lines
    couldn't be accepted anymore or the feature of the Python shell
    to described in paragraph 16 of the faq:

    Sometimes you want to emulate the Python interactive interpreter's
    behavior, where it gives you a continuation prompt when the input
    is incomplete (e.g. you typed the start of an "if" statement or you
    didn't close your parentheses or triple string quotes), but it gives
    you a syntax error message immediately when the input is invalid.

    would have to be given up and every entered line of code would have to
    be terminated by an empty line before evaluation :(

    Thanks for any help, Dietrich
    Dietrich Bollmann, Apr 22, 2008
    1. Advertisements

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. Luke
    Robin Becker
    Oct 24, 2003
  2. Tommy Nordgren

    Extending/Embedding MacPython.

    Tommy Nordgren, Feb 4, 2004, in forum: Python
    Tommy Nordgren
    Feb 4, 2004
  3. Ing Giuseppe Monteleone

    need help for extending and embedding

    Ing Giuseppe Monteleone, Feb 29, 2004, in forum: Python
    Jeff Epler
    Feb 29, 2004
  4. Saraswati lakki
    Saraswati lakki
    Jan 6, 2012
  5. Mateusz Loskot
    Mateusz Loskot
    Jan 11, 2012

Share This Page