diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py index 36f87e259e794d..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,11 +972,19 @@ 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)" - 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' + # 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() + 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")