[3.13] gh-124188: Fix PyErr_ProgramTextObject() (GH-124189) (GH-124423)

* Detect source file encoding.
* Use the "replace" error handler even for UTF-8 (default) encoding.
* Remove the BOM.
* Fix detection of too long lines if they contain NUL.
* Return the head rather than the tail for truncated long lines.
(cherry picked from commit e2f710792b)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
Miss Islington (bot) 2024-10-07 23:23:49 +02:00 committed by GitHub
parent 167d8d2f07
commit 03ae82d0d3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 328 additions and 117 deletions

View file

@ -152,5 +152,8 @@ class IsolatedCodeGenTests(CodegenTestCase):
def test_syntax_error__return_not_in_function(self):
snippet = "return 42"
with self.assertRaisesRegex(SyntaxError, "'return' outside function"):
with self.assertRaisesRegex(SyntaxError, "'return' outside function") as cm:
self.codegen_test(snippet, None)
self.assertIsNone(cm.exception.text)
self.assertEqual(cm.exception.offset, 1)
self.assertEqual(cm.exception.end_offset, 10)