[3.13] gh-128894: Fix TracebackException._format_syntax_error on custom SyntaxError metadata (GH-128946) (#129178)

This commit is contained in:
Miss Islington (bot) 2025-01-22 14:12:39 +01:00 committed by GitHub
parent a379749217
commit 7f6e80a03e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 36 additions and 3 deletions

View file

@ -376,6 +376,30 @@ class TracebackCases(unittest.TestCase):
' ValueError: 0\n',
])
def test_format_exception_group_syntax_error_with_custom_values(self):
# See https://github.com/python/cpython/issues/128894
for exc in [
SyntaxError('error', 'abcd'),
SyntaxError('error', [None] * 4),
SyntaxError('error', (1, 2, 3, 4)),
SyntaxError('error', (1, 2, 3, 4)),
SyntaxError('error', (1, 'a', 'b', 2)),
# with end_lineno and end_offset:
SyntaxError('error', 'abcdef'),
SyntaxError('error', [None] * 6),
SyntaxError('error', (1, 2, 3, 4, 5, 6)),
SyntaxError('error', (1, 'a', 'b', 2, 'c', 'd')),
]:
with self.subTest(exc=exc):
err = traceback.format_exception_only(exc, show_group=True)
# Should not raise an exception:
if exc.lineno is not None:
self.assertEqual(len(err), 2)
self.assertTrue(err[0].startswith(' File'))
else:
self.assertEqual(len(err), 1)
self.assertEqual(err[-1], 'SyntaxError: error\n')
@requires_subprocess()
@force_not_colorized
def test_encoded_file(self):