Skip to content

Commit e797d2e

Browse files
committed
gh-127927: Use the supplied source for parser syntax errors
1 parent 0423fc6 commit e797d2e

3 files changed

Lines changed: 24 additions & 8 deletions

File tree

‎Lib/test/test_exceptions.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2710,6 +2710,23 @@ def try_compile(source):
27102710
self.assertEqual(exc.offset, 1)
27112711
self.assertEqual(exc.end_offset, 12)
27122712

2713+
def test_parser_error_uses_supplied_source(self):
2714+
self.addCleanup(unlink, TESTFN)
2715+
with open(TESTFN, 'wb') as f:
2716+
f.write(b'pass\npass\n')
2717+
2718+
line = 'café = 1 + * 2\n'
2719+
source = '# coding: latin-1\n' + line
2720+
for src in (source, source.encode('latin-1')):
2721+
with self.subTest(src=src):
2722+
with self.assertRaises(SyntaxError) as cm:
2723+
compile(src, TESTFN, 'exec')
2724+
exc = cm.exception
2725+
self.assertEqual(exc.filename, TESTFN)
2726+
self.assertEqual(exc.lineno, 2)
2727+
self.assertEqual(exc.text, line)
2728+
self.assertEqual((exc.offset, exc.end_offset), (12, 13))
2729+
27132730
def test_file_source(self):
27142731
self.addCleanup(unlink, TESTFN)
27152732
err = run_script('return "ä"')
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
When parsing source passed to :func:`compile` or :func:`ast.parse`, use the
2+
supplied source for the text and column offsets of parser-generated
3+
:exc:`SyntaxError` exceptions even when the filename names an existing file.

‎Parser/pegen_errors.c‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -321,19 +321,15 @@ _PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,
321321
if (info.is_interactive && _PyTokenizer_RetainedSource(p->tok) != NULL) {
322322
error_line = get_error_line_from_source(p, lineno);
323323
}
324-
else if (p->start_rule == Py_file_input) {
324+
else if (p->start_rule == Py_file_input &&
325+
_PyTokenizer_RetainedSource(p->tok) == NULL) {
325326
error_line = _PyErr_ProgramDecodedTextObject(info.filename,
326327
(int) lineno, info.encoding);
327328
}
328329

329330
if (!error_line) {
330-
/* PyErr_ProgramTextObject was not called or returned NULL. If it was not called,
331-
then we need to find the error line from some other source, because
332-
p->start_rule != Py_file_input. If it returned NULL, then it either unexpectedly
333-
failed or we're parsing from a string or the REPL. There's a third edge case where
334-
we're actually parsing from a file, which has an E_EOF SyntaxError and in that case
335-
`PyErr_ProgramTextObject` fails because lineno points to last_file_line + 1, which
336-
does not physically exist */
331+
/* Use the tokenizer's input. For a file, an EOF error can point
332+
one line past the last line on disk. */
337333
assert(!info.is_file || info.status == E_EOF);
338334

339335
if (location.lineno <= lineno &&

0 commit comments

Comments
 (0)