]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-117389: Fix `test_compileall.EncodingTest` (GH-117390) (#118603)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sun, 5 May 2024 19:02:04 +0000 (21:02 +0200)
committerGitHub <noreply@github.com>
Sun, 5 May 2024 19:02:04 +0000 (19:02 +0000)
gh-117389: Fix `test_compileall.EncodingTest` (GH-117390)
(cherry picked from commit 44f67916dafd3583f482e6d001766581a1a734fc)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Lib/test/test_compileall.py

index 9cd92ad365c5a90f1a569927bfa717917c5b23ca..5189637c5cfd6d37f002f62099662936696a2a3f 100644 (file)
@@ -477,19 +477,25 @@ class EncodingTest(unittest.TestCase):
         self.directory = tempfile.mkdtemp()
         self.source_path = os.path.join(self.directory, '_test.py')
         with open(self.source_path, 'w', encoding='utf-8') as file:
-            file.write('# -*- coding: utf-8 -*-\n')
-            file.write('print u"\u20ac"\n')
+            # Intentional syntax error: bytes can only contain
+            # ASCII literal characters.
+            file.write('b"\u20ac"')
 
     def tearDown(self):
         shutil.rmtree(self.directory)
 
     def test_error(self):
-        try:
-            orig_stdout = sys.stdout
-            sys.stdout = io.TextIOWrapper(io.BytesIO(),encoding='ascii')
-            compileall.compile_dir(self.directory)
-        finally:
-            sys.stdout = orig_stdout
+        buffer = io.TextIOWrapper(io.BytesIO(), encoding='ascii')
+        with contextlib.redirect_stdout(buffer):
+            compiled = compileall.compile_dir(self.directory)
+        self.assertFalse(compiled)  # should not be successful
+        buffer.seek(0)
+        res = buffer.read()
+        self.assertIn(
+            'SyntaxError: bytes can only contain ASCII literal characters',
+            res,
+        )
+        self.assertNotIn('UnicodeEncodeError', res)
 
 
 class CommandLineTestsBase: