From e7c153ffc76bde9f03224564104ed8441087baf6 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Fri, 16 May 2025 15:18:28 +0900 Subject: [PATCH 1/2] fix test_python_legacy_windows_stdio --- Lib/test/test_cmd_line.py | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py index 1b40e0d05fe3bc..c50f340b9d8a0b 100644 --- a/Lib/test/test_cmd_line.py +++ b/Lib/test/test_cmd_line.py @@ -972,10 +972,32 @@ def test_python_legacy_windows_fs_encoding(self): @unittest.skipUnless(support.MS_WINDOWS, 'Test only applicable on Windows') def test_python_legacy_windows_stdio(self): - code = "import sys; print(sys.stdin.encoding, sys.stdout.encoding)" - expected = 'cp' - rc, out, err = assert_python_ok('-c', code, PYTHONLEGACYWINDOWSSTDIO='1') - self.assertIn(expected.encode(), out) + fn = os_helper.TESTFN + # We cannot use PIPE to test ConsoleIO + code = dedent(f""" + import sys + with open({fn!r}, 'w', encoding='utf-8') as f: + print(type(sys.stdout.buffer.raw).__name__, file=f) + """) + + # Test that _WindowsConsoleIO is used when PYTHONLEGACYWINDOWSSTDIO is not set. + subprocess.run([sys.executable, "-c", code], check=True, + creationflags=subprocess.CREATE_NEW_CONSOLE) + with open(fn, "r", encoding="utf-8") as f: + out = f.read() + os.remove(fn) + self.assertEqual(out.strip(), "_WindowsConsoleIO") + + # Test that _FileIO is used when PYTHONLEGACYWINDOWSSTDIO is set. + env = os.environ.copy() + env["PYTHONLEGACYWINDOWSSTDIO"] = "1" + subprocess.run([sys.executable, "-c", code], check=True, + creationflags=subprocess.CREATE_NEW_CONSOLE, + env=env) + with open(fn, "r", encoding="utf-8") as f: + out = f.read() + os.remove(fn) + self.assertEqual(out.strip(), "FileIO") @unittest.skipIf("-fsanitize" in sysconfig.get_config_vars().get('PY_CFLAGS', ()), "PYTHONMALLOCSTATS doesn't work with ASAN") From cc7b61b7976c2c33141027e37ef9ae2545480e2d Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Fri, 16 May 2025 20:50:37 +0900 Subject: [PATCH 2/2] update test based on the review comment --- Lib/test/test_cmd_line.py | 41 ++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py index c50f340b9d8a0b..f540973c11e01c 100644 --- a/Lib/test/test_cmd_line.py +++ b/Lib/test/test_cmd_line.py @@ -972,32 +972,25 @@ def test_python_legacy_windows_fs_encoding(self): @unittest.skipUnless(support.MS_WINDOWS, 'Test only applicable on Windows') def test_python_legacy_windows_stdio(self): - fn = os_helper.TESTFN - # We cannot use PIPE to test ConsoleIO - code = dedent(f""" - import sys - with open({fn!r}, 'w', encoding='utf-8') as f: - print(type(sys.stdout.buffer.raw).__name__, file=f) - """) - - # Test that _WindowsConsoleIO is used when PYTHONLEGACYWINDOWSSTDIO is not set. - subprocess.run([sys.executable, "-c", code], check=True, - creationflags=subprocess.CREATE_NEW_CONSOLE) - with open(fn, "r", encoding="utf-8") as f: - out = f.read() - os.remove(fn) - self.assertEqual(out.strip(), "_WindowsConsoleIO") - - # Test that _FileIO is used when PYTHONLEGACYWINDOWSSTDIO is set. + # Test that _WindowsConsoleIO is used when PYTHONLEGACYWINDOWSSTDIO + # is not set. + # We cannot use PIPE becase it prevents creating new console. + # 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" - subprocess.run([sys.executable, "-c", code], check=True, - creationflags=subprocess.CREATE_NEW_CONSOLE, - env=env) - with open(fn, "r", encoding="utf-8") as f: - out = f.read() - os.remove(fn) - self.assertEqual(out.strip(), "FileIO") + 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', ()), "PYTHONMALLOCSTATS doesn't work with ASAN")