Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Confusing SyntaxError while entering non-indented code in interactiveinterpreter on continuation line.

Reply
Thread Tools

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

 
 
Colin Howell
Guest
Posts: n/a
 
      04-25-2010
[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.
 
Reply With Quote
 
 
 
 
Patrick Maupin
Guest
Posts: n/a
 
      04-25-2010
On Apr 25, 3:31*pm, Colin Howell <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
John Bokma
Guest
Posts: n/a
 
      04-25-2010
Colin Howell <(E-Mail Removed)> 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
 
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
Suppress output to stdout/stderr in InteractiveInterpreter Dave W. Python 7 04-16-2010 04:46 PM
try... except SyntaxError: unexpected EOF while parsing oscartheduck Python 7 04-04-2007 06:46 PM
How to determine if a line of python code is a continuation of the line above it Sandra-24 Python 5 04-09-2006 08:28 PM
Error when entering wrong data type in do-while loop Jeff C++ 3 02-05-2005 06:56 PM
Help with use of code.InteractiveInterpreter for multiline code rick_muller@yahoo.com Python 2 12-03-2004 05:06 PM



Advertisments