[Python-ideas] Inconsistent script/console behaviour

Nick Coghlan ncoghlan at gmail.com
Thu Dec 15 12:41:04 CET 2011


(moved thread to python-ideas, where it belongs. Messing with 20 years
of precedent is *not* a topic for python-dev)

On Thu, Dec 15, 2011 at 6:58 PM, anatoly techtonik <techtonik at gmail.com> wrote:
>> You can't fix this without completely changing the way the interactive
>> console treats blank lines.
>
> But the fix doesn't require changing the way interactive console treats
> blank lines at all. It only requires to finish current block when a dedented
> line is encountered and not throwing obviously confusing SyntaxError. At the
> very least it should not say it is SyntaxError, because the code is pretty
> valid Python code. If it appears to be invalid "Python Console code" - the
> error message should say that explicitly. That would be a correct
> user-friendly fix for this UX issue, but I'd still like the behavior to be
> fixed - i.e. "allow dedented lines end current block in console without
> SyntaxError". Right now I don't see the reasons why it is not possible.
>
> Please speak code when replying about use cases/examples that will be broken
> - I didn't quite get the problem with "global scope if" statements.

There are two steps to what the interactive console does:

Step 1: accept a block of code.
Step 2: compile and execute it in single statement mode (in order to
get the result of any expressions for display)

It is the *first* step that is terminated by a blank line. That step
understands almost nothing about Python syntax - just enough to figure
out whether or not it should be asking for a continuation line. (IIRC,
it pretty much just does bracket matching and "if I have seen a ':'
character, ask for continuation lines until one of them is blank".

So yes, fixing example 1 *does* require significant changes to the way
the interactive interpreter works. The interactive interpreter
currently operates on individual statements. For compound statements,
those statements are terminated by a blank line. If you don't
terminate them correctly, then it will attempt to interpret two
statements as one, and hence you will get UnexpectedDedentError.

To fix example 1, the interactive interpreter would have to be
redesigned to use exec mode instead, so it can accept multiple
statements at once. That's not impossible, but is a lot of effort to
address a relatively minor inconvenience (the inability to tolerate
blank lines *within* a suite significantly more annoying in my
experience, but even harder to fix than example 1 would be).

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia



More information about the Python-ideas mailing list