Skip to content

Commit 11594da

Browse files
[3.12] gh-117389: Fix test_compileall.EncodingTest (GH-117390) (#118603)
gh-117389: Fix `test_compileall.EncodingTest` (GH-117390) (cherry picked from commit 44f6791) Co-authored-by: Nikita Sobolev <[email protected]>
1 parent 68316a0 commit 11594da

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

Lib/test/test_compileall.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -477,19 +477,25 @@ def setUp(self):
477477
self.directory = tempfile.mkdtemp()
478478
self.source_path = os.path.join(self.directory, '_test.py')
479479
with open(self.source_path, 'w', encoding='utf-8') as file:
480-
file.write('# -*- coding: utf-8 -*-\n')
481-
file.write('print u"\u20ac"\n')
480+
# Intentional syntax error: bytes can only contain
481+
# ASCII literal characters.
482+
file.write('b"\u20ac"')
482483

483484
def tearDown(self):
484485
shutil.rmtree(self.directory)
485486

486487
def test_error(self):
487-
try:
488-
orig_stdout = sys.stdout
489-
sys.stdout = io.TextIOWrapper(io.BytesIO(),encoding='ascii')
490-
compileall.compile_dir(self.directory)
491-
finally:
492-
sys.stdout = orig_stdout
488+
buffer = io.TextIOWrapper(io.BytesIO(), encoding='ascii')
489+
with contextlib.redirect_stdout(buffer):
490+
compiled = compileall.compile_dir(self.directory)
491+
self.assertFalse(compiled) # should not be successful
492+
buffer.seek(0)
493+
res = buffer.read()
494+
self.assertIn(
495+
'SyntaxError: bytes can only contain ASCII literal characters',
496+
res,
497+
)
498+
self.assertNotIn('UnicodeEncodeError', res)
493499

494500

495501
class CommandLineTestsBase:

0 commit comments

Comments
 (0)