Skip to content

Commit 727bed6

Browse files
[3.8] bpo-34463: Make python tracebacks identical to C tracebacks for (python#23899)
* [3.8] bpo-34463: Make python tracebacks identical to C tracebacks for SyntaxErrors without a lineno (pythonGH-23427) (cherry picked from commit 069560b) Co-authored-by: Irit Katriel <iritkatriel@yahoo.com> * 📜🤖 Added by blurb_it. * added missing newline in test Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
1 parent e1203e8 commit 727bed6

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

Lib/test/test_traceback.py

+24
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,30 @@ def test_message_none(self):
679679
err = self.get_report(Exception(''))
680680
self.assertIn('Exception\n', err)
681681

682+
def test_syntax_error_no_lineno(self):
683+
# See #34463.
684+
685+
# Without filename
686+
e = SyntaxError('bad syntax')
687+
msg = self.get_report(e).splitlines()
688+
self.assertEqual(msg,
689+
['SyntaxError: bad syntax'])
690+
e.lineno = 100
691+
msg = self.get_report(e).splitlines()
692+
self.assertEqual(msg,
693+
[' File "<string>", line 100', 'SyntaxError: bad syntax'])
694+
695+
# With filename
696+
e = SyntaxError('bad syntax')
697+
e.filename = 'myfile.py'
698+
699+
msg = self.get_report(e).splitlines()
700+
self.assertEqual(msg,
701+
['SyntaxError: bad syntax (myfile.py)'])
702+
e.lineno = 100
703+
msg = self.get_report(e).splitlines()
704+
self.assertEqual(msg,
705+
[' File "myfile.py", line 100', 'SyntaxError: bad syntax'])
682706

683707
class PyExcReportingTests(BaseExceptionReportingTests, unittest.TestCase):
684708
#

Lib/traceback.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,8 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None,
515515
if exc_type and issubclass(exc_type, SyntaxError):
516516
# Handle SyntaxError's specially
517517
self.filename = exc_value.filename
518-
self.lineno = str(exc_value.lineno)
518+
lno = exc_value.lineno
519+
self.lineno = str(lno) if lno is not None else None
519520
self.text = exc_value.text
520521
self.offset = exc_value.offset
521522
self.msg = exc_value.msg
@@ -569,9 +570,12 @@ def format_exception_only(self):
569570
return
570571

571572
# It was a syntax error; show exactly where the problem was found.
572-
filename = self.filename or "<string>"
573-
lineno = str(self.lineno) or '?'
574-
yield ' File "{}", line {}\n'.format(filename, lineno)
573+
filename_suffix = ''
574+
if self.lineno is not None:
575+
yield ' File "{}", line {}\n'.format(
576+
self.filename or "<string>", self.lineno)
577+
elif self.filename is not None:
578+
filename_suffix = ' ({})'.format(self.filename)
575579

576580
badline = self.text
577581
offset = self.offset
@@ -585,7 +589,7 @@ def format_exception_only(self):
585589
caretspace = ((c.isspace() and c or ' ') for c in caretspace)
586590
yield ' {}^\n'.format(''.join(caretspace))
587591
msg = self.msg or "<no detail available>"
588-
yield "{}: {}\n".format(stype, msg)
592+
yield "{}: {}{}\n".format(stype, msg, filename_suffix)
589593

590594
def format(self, *, chain=True):
591595
"""Format the exception.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed discrepancy between :mod:`traceback` and the interpreter in formatting of SyntaxError with lineno not set (:mod:`traceback` was changed to match interpreter).

0 commit comments

Comments
 (0)