Confusing SyntaxError while entering non-indented code in interactiveinterpreter on continuation lin

Discussion in 'Python' started by Colin Howell, Apr 25, 2010.

  1. Colin Howell

    Colin Howell Guest

    [I originally sent this to python-help; the volunteer who responded
    thought it was OK to repost it here.]

    I'm sure this has been discussed somewhere before, but I can't find it
    in the Python issue tracker. The following behavior from the
    interactive interpreter is rather confusing. (I've seen this behavior
    both under Python 2.6.5 on 32-bit Windows XP, installed from the
    standard Windows binary installer available from python.org, and under
    Python 2.4.3 on a Fedora Core 4 32-bit x86 Linux system.)

    The following do-nothing code is valid Python:

    if True:
        pass
    pass

    A script file containing it will execute without error when passed to
    the Python interpreter from the command line, or when run from an IDLE
    edit window using the Run Module command (F5) key.

    However, if the interpreter is run in interactive mode from the
    command line, and I attempt to enter the same code from the
    interactive prompt as follows, I get a SyntaxError at the outer "pass"
    statement:

    >>> if True:

    ....     pass
    .... pass
     File "<stdin>", line 3
       pass
          ^
    SyntaxError: invalid syntax

    The inner "pass" statement can be indented using spaces or with the
    Tab key; it doesn't matter. The problem is that I entered the outer
    "pass" statement on the second continuation line, rather than simply
    hitting return and waiting for a new primary prompt. But since the
    second continuation line is not indented unless you enter the
    indentation yourself, this is actually an easy mistake to make. One
    might think the parser will recognize that the inner block's
    indentation has been removed on the new line and that the line
    therefore represents a new statement.

    The same thing can happen in IDLE, except that IDLE automatically
    indents the continuation line and doesn't print a secondary prompt.
    But if you delete IDLE's indentation on the next continuation line and
    enter a new statement, you get the same SyntaxError as described
    above.

    What led me to this behavior? Example 6.1 on the following page from
    Dive Into Python:

    http://diveintopython.org/file_handling/index.html

    In this case, the code is a try-except statement, followed by a print.
    Note that the print is entered on a continuation line, which led me to
    carelessly try the same thing and then puzzle about it for quite a
    while before it finally hit me what I had been doing.

    I know that Dive Into Python is quite old and there have been many
    improvements in the language since, but I still think this gotcha
    needs to be looked at, at least to add a warning about it in the FAQ
    or tutorial, or to make the nature of the syntax error more obvious.
    Colin Howell, Apr 25, 2010
    #1
    1. Advertising

  2. Re: Confusing SyntaxError while entering non-indented code ininteractive interpreter on continuation line.

    On Apr 25, 3:31 pm, Colin Howell <> wrote:
    > [I originally sent this to python-help; the volunteer who responded
    > thought it was OK to repost it here.]
    >
    > I'm sure this has been discussed somewhere before, but I can't find it
    > in the Python issue tracker. The following behavior from the
    > interactive interpreter is rather confusing. (I've seen this behavior
    > both under Python 2.6.5 on 32-bit Windows XP, installed from the
    > standard Windows binary installer available from python.org, and under
    > Python 2.4.3 on a Fedora Core 4 32-bit x86 Linux system.)
    >
    > The following do-nothing code is valid Python:
    >
    > if True:
    >     pass
    > pass
    >
    > A script file containing it will execute without error when passed to
    > the Python interpreter from the command line, or when run from an IDLE
    > edit window using the Run Module command (F5) key.
    >
    > However, if the interpreter is run in interactive mode from the
    > command line, and I attempt to enter the same code from the
    > interactive prompt as follows, I get a SyntaxError at the outer "pass"
    > statement:
    >
    > >>> if True:

    >
    > ...     pass
    > ... pass
    >  File "<stdin>", line 3
    >    pass
    >       ^
    > SyntaxError: invalid syntax
    >
    > The inner "pass" statement can be indented using spaces or with the
    > Tab key; it doesn't matter. The problem is that I entered the outer
    > "pass" statement on the second continuation line, rather than simply
    > hitting return and waiting for a new primary prompt. But since the
    > second continuation line is not indented unless you enter the
    > indentation yourself, this is actually an easy mistake to make. One
    > might think the parser will recognize that the inner block's
    > indentation has been removed on the new line and that the line
    > therefore represents a new statement.
    >
    > The same thing can happen in IDLE, except that IDLE automatically
    > indents the continuation line and doesn't print a secondary prompt.
    > But if you delete IDLE's indentation on the next continuation line and
    > enter a new statement, you get the same SyntaxError as described
    > above.
    >
    > What led me to this behavior? Example 6.1 on the following page from
    > Dive Into Python:
    >
    > http://diveintopython.org/file_handling/index.html
    >
    > In this case, the code is a try-except statement, followed by a print.
    > Note that the print is entered on a continuation line, which led me to
    > carelessly try the same thing and then puzzle about it for quite a
    > while before it finally hit me what I had been doing.
    >
    > I know that Dive Into Python is quite old and there have been many
    > improvements in the language since, but I still think this gotcha
    > needs to be looked at, at least to add a warning about it in the FAQ
    > or tutorial, or to make the nature of the syntax error more obvious.


    I agree that this is an easy mistake to make. It's particularly easy
    to do when cutting and pasting into an interactive shell. I don't
    think you could call this an error on the part of the python
    interpreter, but it seems like it would be a great feature enhancement
    to make the interpreter work more consistently. After all, you can
    happily unindent to any level except the far left:

    >>> if a:

    .... if b:
    .... c
    .... d
    .... e
    File "<stdin>", line 5
    e
    ^
    SyntaxError: invalid syntax

    Regards,
    Pat
    Patrick Maupin, Apr 25, 2010
    #2
    1. Advertising

  3. Colin Howell

    John Bokma Guest

    Re: Confusing SyntaxError while entering non-indented code in interactive interpreter on continuation line.

    Colin Howell <> writes:

    > I know that Dive Into Python is quite old and there have been many
    > improvements in the language since,


    FYI There is a Dive Into Python 3.

    --
    John Bokma j3b

    Hacking & Hiking in Mexico - http://johnbokma.com/
    http://castleamber.com/ - Perl & Python Development
    John Bokma, Apr 25, 2010
    #3
    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. Replies:
    2
    Views:
    560
    Jim Sizelove
    Dec 3, 2004
  2. oscartheduck
    Replies:
    7
    Views:
    5,340
    Steve Holden
    Apr 4, 2007
  3. Dave W.
    Replies:
    7
    Views:
    904
    Dave W.
    Apr 16, 2010
  4. JB
    Replies:
    2
    Views:
    3,401
    Cubaman
    Aug 2, 2010
  5. Gnarlodious
    Replies:
    5
    Views:
    1,111
    Terry Reedy
    Mar 31, 2011
Loading...

Share This Page