From e3f10197d078ee2fd658e512c9f9cbd999ea65b5 Mon Sep 17 00:00:00 2001 From: AN Long Date: Sun, 4 May 2025 20:44:41 +0900 Subject: [PATCH 1/3] Fix test_python_legacy_windows_stdio in test_cmd_line --- Lib/test/test_cmd_line.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py index 36f87e259e794d..3505b2243b3354 100644 --- a/Lib/test/test_cmd_line.py +++ b/Lib/test/test_cmd_line.py @@ -972,10 +972,15 @@ 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) + code = "import sys; print(sys.stdin.encoding, sys.stdout.encoding, file=sys.stderr)" + env = os.environ.copy() + env['PYTHONLEGACYWINDOWSSTDIO'] = '1' + p = subprocess.Popen([sys.executable, '-c', code], + stderr=subprocess.PIPE, env=env) + out = p.stderr.read() + p.stderr.close() + p.wait() + self.assertNotIn(b'utf-8', out) @unittest.skipIf("-fsanitize" in sysconfig.get_config_vars().get('PY_CFLAGS', ()), "PYTHONMALLOCSTATS doesn't work with ASAN") From 63105a20927c740d8e32b5d0562fca6f323b43bf Mon Sep 17 00:00:00 2001 From: AN Long Date: Tue, 6 May 2025 22:50:58 +0900 Subject: [PATCH 2/3] Update Lib/test/test_cmd_line.py Co-authored-by: Steve Dower --- Lib/test/test_cmd_line.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py index 3505b2243b3354..c68dd01a85ae19 100644 --- a/Lib/test/test_cmd_line.py +++ b/Lib/test/test_cmd_line.py @@ -975,6 +975,7 @@ def test_python_legacy_windows_stdio(self): code = "import sys; print(sys.stdin.encoding, sys.stdout.encoding, file=sys.stderr)" env = os.environ.copy() env['PYTHONLEGACYWINDOWSSTDIO'] = '1' + # Use Popen directly to ensure stdin/out are console handles p = subprocess.Popen([sys.executable, '-c', code], stderr=subprocess.PIPE, env=env) out = p.stderr.read() From 8ee72945d2783d12002296a6deedf1b1b54157a8 Mon Sep 17 00:00:00 2001 From: AN Long Date: Thu, 8 May 2025 00:09:42 +0900 Subject: [PATCH 3/3] Skip if there is no real console handle --- Lib/test/test_cmd_line.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py index c68dd01a85ae19..ff94d9d34515b9 100644 --- a/Lib/test/test_cmd_line.py +++ b/Lib/test/test_cmd_line.py @@ -2,6 +2,7 @@ # Most tests are executed with environment variables ignored # See test_cmd_line_script.py for testing of script execution +import io import os import subprocess import sys @@ -971,6 +972,8 @@ def test_python_legacy_windows_fs_encoding(self): self.assertIn(expected.encode(), out) @unittest.skipUnless(support.MS_WINDOWS, 'Test only applicable on Windows') + @unittest.skipUnless(type(sys.stdin.buffer.raw) == io._WindowsConsoleIO, + 'Requires real console handles') def test_python_legacy_windows_stdio(self): code = "import sys; print(sys.stdin.encoding, sys.stdout.encoding, file=sys.stderr)" env = os.environ.copy()