Skip to content

Commit

Permalink
Merge pull request #2399 from takluyver/indentationerr
Browse files Browse the repository at this point in the history
Fix IndentationError display line numbering not shown.

IndentationError is a subclass of SyntaxError, and they should display in a similar fashion, with the problematic line and line number shown. We were being over-specific with our type checking.
  • Loading branch information
Carreau committed Sep 13, 2012
2 parents a52d09c + b1788e4 commit 6ad4114
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
4 changes: 2 additions & 2 deletions IPython/core/interactiveshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -1716,7 +1716,7 @@ def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None,
self.write_err('No traceback available to show.\n')
return

if etype is SyntaxError:
if issubclass(etype, SyntaxError):
# Though this won't be called by syntax errors in the input
# line, there may be SyntaxError cases with imported code.
self.showsyntaxerror(filename)
Expand Down Expand Up @@ -1769,7 +1769,7 @@ def showsyntaxerror(self, filename=None):
"""
etype, value, last_traceback = self._get_exc_info()

if filename and etype is SyntaxError:
if filename and issubclass(etype, SyntaxError):
try:
value.filename = filename
except:
Expand Down
20 changes: 20 additions & 0 deletions IPython/core/tests/test_ultratb.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,23 @@ def test_iso8859_5(self):
with tt.AssertPrints("ZeroDivisionError"):
with tt.AssertPrints(u'дбИЖ', suppress=False):
ip.run_cell('fail()')

indentationerror_file = """if True:
zoon()
"""

class IndentationErrorTest(unittest.TestCase):
def test_indentationerror_shows_line(self):
# See issue gh-2398
with tt.AssertPrints("IndentationError"):
with tt.AssertPrints("zoon()", suppress=False):
ip.run_cell(indentationerror_file)

with TemporaryDirectory() as td:
fname = os.path.join(td, "foo.py")
with open(fname, "w") as f:
f.write(indentationerror_file)

with tt.AssertPrints("IndentationError"):
with tt.AssertPrints("zoon()", suppress=False):
ip.magic('run %s' % fname)
2 changes: 1 addition & 1 deletion IPython/core/ultratb.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ def _format_exception_only(self, etype, value):
# Not sure if this can still happen in Python 2.6 and above
list.append( py3compat.cast_unicode(stype) + '\n')
else:
if etype is SyntaxError:
if issubclass(etype, SyntaxError):
have_filedata = True
#print 'filename is',filename # dbg
if not value.filename: value.filename = "<string>"
Expand Down

0 comments on commit 6ad4114

Please sign in to comment.