Skip to content

Commit e4ca0cc

Browse files
committed
gh-117389: Fix test_compileall.EncodingTest
1 parent bfc57d4 commit e4ca0cc

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

Lib/test/test_compileall.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -502,19 +502,19 @@ 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+
file.write('print(b"\u20ac")\n') # intentional error
507506

508507
def tearDown(self):
509508
shutil.rmtree(self.directory)
510509

511510
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
511+
buffer = io.StringIO()
512+
with contextlib.redirect_stdout(buffer):
513+
compiled = compileall.compile_dir(self.directory))
514+
self.assertFalse(compiled) # should not be successful
515+
res = buffer.getvalue()
516+
self.assertIn('SyntaxError', res)
517+
self.assertIn('print(b"€")', res)
518518

519519

520520
class CommandLineTestsBase:

0 commit comments

Comments
 (0)