Skip to content

Commit 44f6791

Browse files
authored
gh-117389: Fix test_compileall.EncodingTest (#117390)
1 parent 5092ea2 commit 44f6791

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
@@ -502,19 +502,25 @@ def setUp(self):
502502
self.directory = tempfile.mkdtemp()
503503
self.source_path = os.path.join(self.directory, '_test.py')
504504
with open(self.source_path, 'w', encoding='utf-8') as file:
505-
file.write('# -*- coding: utf-8 -*-\n')
506-
file.write('print u"\u20ac"\n')
505+
# Intentional syntax error: bytes can only contain
506+
# ASCII literal characters.
507+
file.write('b"\u20ac"')
507508

508509
def tearDown(self):
509510
shutil.rmtree(self.directory)
510511

511512
def test_error(self):
512-
try:
513-
orig_stdout = sys.stdout
514-
sys.stdout = io.TextIOWrapper(io.BytesIO(),encoding='ascii')
515-
compileall.compile_dir(self.directory)
516-
finally:
517-
sys.stdout = orig_stdout
513+
buffer = io.TextIOWrapper(io.BytesIO(), encoding='ascii')
514+
with contextlib.redirect_stdout(buffer):
515+
compiled = compileall.compile_dir(self.directory)
516+
self.assertFalse(compiled) # should not be successful
517+
buffer.seek(0)
518+
res = buffer.read()
519+
self.assertIn(
520+
'SyntaxError: bytes can only contain ASCII literal characters',
521+
res,
522+
)
523+
self.assertNotIn('UnicodeEncodeError', res)
518524

519525

520526
class CommandLineTestsBase:

0 commit comments

Comments
 (0)