Skip to content

Show line number for IndentationErrors #2398

@cdeil

Description

@cdeil

This is a feature request.

When I execute a script with an IndentationError python and ipython show me the line where the problem is.
Would it be possible to also give show the line where the IndentationError occurs when I execute a multi-line code snippet in ipython via a notebook or qtconsole cell or on the command line via %paste?

I just copy & pasted and executed a 20-line code snippet and got a SyntaxError and couldn't figure out in the notebook where the problem was.

Here's an example:

$ cat test.py 
a = 42
if a == 42:
    #print 'Yes'
else:
    print 'No'

Python shows the line number and text from the correct line:

$ python test.py 
  File "test.py", line 4
    else:
       ^
IndentationError: expected an indented block

ipython mentions the line number, but the text it shows me is useless:

In [1]: run test.py
---------------------------------------------------------------------------
IndentationError                          Traceback (most recent call last)
/Users/deil/Library/Python/2.7/lib/python/site-packages/IPython/utils/py3compat.pyc in execfile(fname, *where)
    177             else:
    178                 filename = fname
--> 179             __builtin__.execfile(filename, *where)

IndentationError: expected an indented block (test.py, line 4)

Copy and paste that code in a notebook cell and execute is and you'll get no line info.
Executing a cell with this content

a = 42
if a == 42:
    1 / 0
else:
    print 'No'

gives a ZeroDivisionError in the notebook with correct line number info:

---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-33-09fdeff38d22> in <module>()
      1 a = 42
      2 if a == 42:
----> 3     1 / 0
      4 else:
      5     print 'No'

ZeroDivisionError: integer division or modulo by zero

So is it possible to give equally good line number info and show the text context info for SyntaxErrors?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions