Skip to content

[3.11] gh-117389: Fix test_compileall.EncodingTest (GH-117390) #118604

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions Lib/test/test_compileall.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading