From e797d2e4e77ce9d7213f5506ce7f9a7639646036 Mon Sep 17 00:00:00 2001 From: Christian Sagstetter <39457217+saggl@users.noreply.github.com> Date: Mon, 30 Nov 2026 07:13:49 +0000 Subject: [PATCH] gh-127927: Use the supplied source for parser syntax errors --- Lib/test/test_exceptions.py | 17 +++++++++++++++++ ...26-11-29-23-56-46.gh-issue-127927.xg6Nbw.rst | 3 +++ Parser/pegen_errors.c | 12 ++++-------- 3 files changed, 24 insertions(+), 8 deletions(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-11-29-23-56-46.gh-issue-127927.xg6Nbw.rst diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py index 3883c0efb9ad4fc..8c4ecd949b07ed7 100644 --- a/Lib/test/test_exceptions.py +++ b/Lib/test/test_exceptions.py @@ -2710,6 +2710,23 @@ def try_compile(source): self.assertEqual(exc.offset, 1) self.assertEqual(exc.end_offset, 12) + def test_parser_error_uses_supplied_source(self): + self.addCleanup(unlink, TESTFN) + with open(TESTFN, 'wb') as f: + f.write(b'pass\npass\n') + + line = 'café = 1 + * 2\n' + source = '# coding: latin-1\n' + line + for src in (source, source.encode('latin-1')): + with self.subTest(src=src): + with self.assertRaises(SyntaxError) as cm: + compile(src, TESTFN, 'exec') + exc = cm.exception + self.assertEqual(exc.filename, TESTFN) + self.assertEqual(exc.lineno, 2) + self.assertEqual(exc.text, line) + self.assertEqual((exc.offset, exc.end_offset), (12, 13)) + def test_file_source(self): self.addCleanup(unlink, TESTFN) err = run_script('return "ä"') diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-29-23-56-46.gh-issue-127927.xg6Nbw.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-29-23-56-46.gh-issue-127927.xg6Nbw.rst new file mode 100644 index 000000000000000..470a6b8680c65e8 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-29-23-56-46.gh-issue-127927.xg6Nbw.rst @@ -0,0 +1,3 @@ +When parsing source passed to :func:`compile` or :func:`ast.parse`, use the +supplied source for the text and column offsets of parser-generated +:exc:`SyntaxError` exceptions even when the filename names an existing file. diff --git a/Parser/pegen_errors.c b/Parser/pegen_errors.c index f5dce5ded18eb83..30610c28f6caf1a 100644 --- a/Parser/pegen_errors.c +++ b/Parser/pegen_errors.c @@ -321,19 +321,15 @@ _PyPegen_raise_error_known_location(Parser *p, PyObject *errtype, if (info.is_interactive && _PyTokenizer_RetainedSource(p->tok) != NULL) { error_line = get_error_line_from_source(p, lineno); } - else if (p->start_rule == Py_file_input) { + else if (p->start_rule == Py_file_input && + _PyTokenizer_RetainedSource(p->tok) == NULL) { error_line = _PyErr_ProgramDecodedTextObject(info.filename, (int) lineno, info.encoding); } if (!error_line) { - /* PyErr_ProgramTextObject was not called or returned NULL. If it was not called, - then we need to find the error line from some other source, because - p->start_rule != Py_file_input. If it returned NULL, then it either unexpectedly - failed or we're parsing from a string or the REPL. There's a third edge case where - we're actually parsing from a file, which has an E_EOF SyntaxError and in that case - `PyErr_ProgramTextObject` fails because lineno points to last_file_line + 1, which - does not physically exist */ + /* Use the tokenizer's input. For a file, an EOF error can point + one line past the last line on disk. */ assert(!info.is_file || info.status == E_EOF); if (location.lineno <= lineno &&