diff options
author | Nikita Sobolev <mail@sobolevn.me> | 2024-05-05 18:46:37 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-05 18:46:37 (GMT) |
commit | 44f67916dafd3583f482e6d001766581a1a734fc (patch) | |
tree | 6704e583c9ea2580a5c6b5e924b34e1840243418 | |
parent | 5092ea238e28c7d099c662d416b2a96fdbea4790 (diff) | |
download | cpython-44f67916dafd3583f482e6d001766581a1a734fc.zip cpython-44f67916dafd3583f482e6d001766581a1a734fc.tar.gz cpython-44f67916dafd3583f482e6d001766581a1a734fc.tar.bz2 |
gh-117389: Fix `test_compileall.EncodingTest` (#117390)
-rw-r--r-- | Lib/test/test_compileall.py | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/Lib/test/test_compileall.py b/Lib/test/test_compileall.py index 14c2af1..bf0fd05 100644 --- a/Lib/test/test_compileall.py +++ b/Lib/test/test_compileall.py @@ -502,19 +502,25 @@ class EncodingTest(unittest.TestCase): 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: |