##// END OF EJS Templates
Fix display of SyntaxError in Python 3....
Thomas Kluyver -
Show More
@@ -1681,20 +1681,12 b' class InteractiveShell(SingletonConfigurable, Magic):'
1681 sys.last_traceback = last_traceback
1681 sys.last_traceback = last_traceback
1682
1682
1683 if filename and etype is SyntaxError:
1683 if filename and etype is SyntaxError:
1684 # Work hard to stuff the correct filename in the exception
1685 try:
1684 try:
1686 msg, (dummy_filename, lineno, offset, line) = value
1685 value.filename = filename
1687 except:
1686 except:
1688 # Not the format we expect; leave it alone
1687 # Not the format we expect; leave it alone
1689 pass
1688 pass
1690 else:
1689
1691 # Stuff in the right filename
1692 try:
1693 # Assume SyntaxError is a class exception
1694 value = SyntaxError(msg, (filename, lineno, offset, line))
1695 except:
1696 # If that failed, assume SyntaxError is a string
1697 value = msg, (filename, lineno, offset, line)
1698 stb = self.SyntaxTB.structured_traceback(etype, value, [])
1690 stb = self.SyntaxTB.structured_traceback(etype, value, [])
1699 self._showtraceback(etype, value, stb)
1691 self._showtraceback(etype, value, stb)
1700
1692
@@ -548,44 +548,40 b' class ListTB(TBTools):'
548 have_filedata = False
548 have_filedata = False
549 Colors = self.Colors
549 Colors = self.Colors
550 list = []
550 list = []
551 try:
551 stype = Colors.excName + etype.__name__ + Colors.Normal
552 stype = Colors.excName + etype.__name__ + Colors.Normal
553 except AttributeError:
554 stype = etype # String exceptions don't get special coloring
555 if value is None:
552 if value is None:
553 # Not sure if this can still happen in Python 2.6 and above
556 list.append( str(stype) + '\n')
554 list.append( str(stype) + '\n')
557 else:
555 else:
558 if etype is SyntaxError:
556 if etype is SyntaxError:
559 try:
557 have_filedata = True
560 msg, (filename, lineno, offset, line) = value
558 #print 'filename is',filename # dbg
561 except:
559 if not value.filename: value.filename = "<string>"
562 have_filedata = False
560 list.append('%s File %s"%s"%s, line %s%d%s\n' % \
563 else:
561 (Colors.normalEm,
564 have_filedata = True
562 Colors.filenameEm, value.filename, Colors.normalEm,
565 #print 'filename is',filename # dbg
563 Colors.linenoEm, value.lineno, Colors.Normal ))
566 if not filename: filename = "<string>"
564 if value.text is not None:
567 list.append('%s File %s"%s"%s, line %s%d%s\n' % \
565 i = 0
568 (Colors.normalEm,
566 while i < len(value.text) and value.text[i].isspace():
569 Colors.filenameEm, filename, Colors.normalEm,
567 i = i+1
570 Colors.linenoEm, lineno, Colors.Normal ))
568 list.append('%s %s%s\n' % (Colors.line,
571 if line is not None:
569 value.text.strip(),
572 i = 0
570 Colors.Normal))
573 while i < len(line) and line[i].isspace():
571 if value.offset is not None:
574 i = i+1
572 s = ' '
575 list.append('%s %s%s\n' % (Colors.line,
573 for c in value.text[i:value.offset-1]:
576 line.strip(),
574 if c.isspace():
577 Colors.Normal))
575 s = s + c
578 if offset is not None:
576 else:
579 s = ' '
577 s = s + ' '
580 for c in line[i:offset-1]:
578 list.append('%s%s^%s\n' % (Colors.caret, s,
581 if c.isspace():
579 Colors.Normal) )
582 s = s + c
580
583 else:
581 try:
584 s = s + ' '
582 s = value.msg
585 list.append('%s%s^%s\n' % (Colors.caret, s,
583 except Exception:
586 Colors.Normal) )
584 s = self._some_str(value)
587 value = msg
588 s = self._some_str(value)
589 if s:
585 if s:
590 list.append('%s%s:%s %s\n' % (str(stype), Colors.excName,
586 list.append('%s%s:%s %s\n' % (str(stype), Colors.excName,
591 Colors.Normal, s))
587 Colors.Normal, s))
@@ -596,7 +592,7 b' class ListTB(TBTools):'
596 if have_filedata:
592 if have_filedata:
597 ipinst = ipapi.get()
593 ipinst = ipapi.get()
598 if ipinst is not None:
594 if ipinst is not None:
599 ipinst.hooks.synchronize_with_editor(filename, lineno, 0)
595 ipinst.hooks.synchronize_with_editor(value.filename, value.lineno, 0)
600
596
601 return list
597 return list
602
598
@@ -13,7 +13,6 b' import unittest'
13
13
14 # IPython imports
14 # IPython imports
15 from IPython.lib import irunner
15 from IPython.lib import irunner
16 from IPython.testing.decorators import known_failure_py3
17 from IPython.utils.py3compat import doctest_refactor_print
16 from IPython.utils.py3compat import doctest_refactor_print
18
17
19 # Testing code begins
18 # Testing code begins
@@ -58,8 +57,6 b' class RunnerTestCase(unittest.TestCase):'
58 self.assert_(mismatch==0,'Number of mismatched lines: %s' %
57 self.assert_(mismatch==0,'Number of mismatched lines: %s' %
59 mismatch)
58 mismatch)
60
59
61 # The SyntaxError appears differently in Python 3, for some reason.
62 @known_failure_py3
63 def testIPython(self):
60 def testIPython(self):
64 """Test the IPython runner."""
61 """Test the IPython runner."""
65 source = doctest_refactor_print("""
62 source = doctest_refactor_print("""
General Comments 0
You need to be logged in to leave comments. Login now