https://github.com/python/cpython/commit/11594da0461b02a8448dfa3354884ef6e274e587
commit: 11594da0461b02a8448dfa3354884ef6e274e587
branch: 3.12
author: Miss Islington (bot) <[email protected]>
committer: sobolevn <[email protected]>
date: 2024-05-05T19:02:04Z
summary:

[3.12] gh-117389: Fix `test_compileall.EncodingTest` (GH-117390) (#118603)

gh-117389: Fix `test_compileall.EncodingTest` (GH-117390)
(cherry picked from commit 44f67916dafd3583f482e6d001766581a1a734fc)

Co-authored-by: Nikita Sobolev <[email protected]>

files:
M Lib/test/test_compileall.py

diff --git a/Lib/test/test_compileall.py b/Lib/test/test_compileall.py
index 9cd92ad365c5a9..5189637c5cfd6d 100644
--- a/Lib/test/test_compileall.py
+++ b/Lib/test/test_compileall.py
@@ -477,19 +477,25 @@ def setUp(self):
         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:

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to