]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-117389: Fix `test_compileall.EncodingTest` (#117390)
authorNikita Sobolev <mail@sobolevn.me>
Sun, 5 May 2024 18:46:37 +0000 (21:46 +0300)
committerGitHub <noreply@github.com>
Sun, 5 May 2024 18:46:37 +0000 (21:46 +0300)
Lib/test/test_compileall.py

index 14c2af19e3eb28b8c187edea2314d158ed1f7a77..bf0fd051672db46ba0e00c092b9c0ec2d0fc79a3 100644 (file)
@@ -502,19 +502,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: