[3.14] gh-133374: fix test_python_legacy_windows_stdio (GH-134080) (GH-134314)

(cherry picked from commit 652d6938ef)

Co-authored-by: Inada Naoki <songofacandy@gmail.com>
This commit is contained in:
Miss Islington (bot) 2025-05-20 08:53:44 +02:00 committed by GitHub
parent 07a2033fcf
commit 90aa13ae47
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -972,10 +972,25 @@ class CmdLineTest(unittest.TestCase):
@unittest.skipUnless(support.MS_WINDOWS, 'Test only applicable on Windows') @unittest.skipUnless(support.MS_WINDOWS, 'Test only applicable on Windows')
def test_python_legacy_windows_stdio(self): def test_python_legacy_windows_stdio(self):
code = "import sys; print(sys.stdin.encoding, sys.stdout.encoding)" # Test that _WindowsConsoleIO is used when PYTHONLEGACYWINDOWSSTDIO
expected = 'cp' # is not set.
rc, out, err = assert_python_ok('-c', code, PYTHONLEGACYWINDOWSSTDIO='1') # We cannot use PIPE becase it prevents creating new console.
self.assertIn(expected.encode(), out) # So we use exit code.
code = "import sys; sys.exit(type(sys.stdout.buffer.raw).__name__ != '_WindowsConsoleIO')"
env = os.environ.copy()
env["PYTHONLEGACYWINDOWSSTDIO"] = ""
p = subprocess.run([sys.executable, "-c", code],
creationflags=subprocess.CREATE_NEW_CONSOLE,
env=env)
self.assertEqual(p.returncode, 0)
# Then test that FIleIO is used when PYTHONLEGACYWINDOWSSTDIO is set.
code = "import sys; sys.exit(type(sys.stdout.buffer.raw).__name__ != 'FileIO')"
env["PYTHONLEGACYWINDOWSSTDIO"] = "1"
p = subprocess.run([sys.executable, "-c", code],
creationflags=subprocess.CREATE_NEW_CONSOLE,
env=env)
self.assertEqual(p.returncode, 0)
@unittest.skipIf("-fsanitize" in sysconfig.get_config_vars().get('PY_CFLAGS', ()), @unittest.skipIf("-fsanitize" in sysconfig.get_config_vars().get('PY_CFLAGS', ()),
"PYTHONMALLOCSTATS doesn't work with ASAN") "PYTHONMALLOCSTATS doesn't work with ASAN")