Skip to content

Commit da8d481

Browse files
committed
fix test_python_legacy_windows_stdio
1 parent 7a504b3 commit da8d481

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

Lib/test/test_cmd_line.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -972,10 +972,32 @@ def test_python_legacy_windows_fs_encoding(self):
972972

973973
@unittest.skipUnless(support.MS_WINDOWS, 'Test only applicable on Windows')
974974
def test_python_legacy_windows_stdio(self):
975-
code = "import sys; print(sys.stdin.encoding, sys.stdout.encoding)"
976-
expected = 'cp'
977-
rc, out, err = assert_python_ok('-c', code, PYTHONLEGACYWINDOWSSTDIO='1')
978-
self.assertIn(expected.encode(), out)
975+
fn = os_helper.TESTFN
976+
# We cannot use PIPE to test ConsoleIO
977+
code = dedent(f"""
978+
import sys
979+
with open({fn!r}, 'w', encoding='utf-8') as f:
980+
print(sys.stdout.buffer.raw.__type__.__name__, file=f)
981+
""")
982+
983+
# Test that _WindowsConsoleIO is used when PYTHONLEGACYWINDOWSSTDIO is not set.
984+
subprocess.run([sys.executable, '-c', code], check=True,
985+
creationflags=subprocess.CREATE_NEW_CONSOLE)
986+
with open(fn, 'r', encoding='utf-8') as f:
987+
out = f.read()
988+
os.remove(fn)
989+
self.assertEqual(out.strip(), '_io._WindowsConsoleIO')
990+
991+
# Test that _FileIO is used when PYTHONLEGACYWINDOWSSTDIO is set.
992+
env = os.environ.copy()
993+
env["PYTHONLEGACYWINDOWSSTDIO"] = "1"
994+
subprocess.run([sys.executable, '-c', code], check=True,
995+
creationflags=subprocess.CREATE_NEW_CONSOLE,
996+
env=env)
997+
with open(fn, 'r', encoding='utf-8') as f:
998+
out = f.read()
999+
os.remove(fn)
1000+
self.assertEqual(out.strip(), '_io._FileIO')
9791001

9801002
@unittest.skipIf("-fsanitize" in sysconfig.get_config_vars().get('PY_CFLAGS', ()),
9811003
"PYTHONMALLOCSTATS doesn't work with ASAN")

0 commit comments

Comments
 (0)