Skip to content

Commit cc7b61b

Browse files
committed
update test based on the review comment
1 parent e7c153f commit cc7b61b

File tree

1 file changed

+17
-24
lines changed

1 file changed

+17
-24
lines changed

Lib/test/test_cmd_line.py

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -972,32 +972,25 @@ 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-
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(type(sys.stdout.buffer.raw).__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(), "_WindowsConsoleIO")
990-
991-
# Test that _FileIO is used when PYTHONLEGACYWINDOWSSTDIO is set.
975+
# Test that _WindowsConsoleIO is used when PYTHONLEGACYWINDOWSSTDIO
976+
# is not set.
977+
# We cannot use PIPE becase it prevents creating new console.
978+
# So we use exit code.
979+
code = "import sys; sys.exit(type(sys.stdout.buffer.raw).__name__ != '_WindowsConsoleIO')"
992980
env = os.environ.copy()
981+
env["PYTHONLEGACYWINDOWSSTDIO"] = ""
982+
p = subprocess.run([sys.executable, "-c", code],
983+
creationflags=subprocess.CREATE_NEW_CONSOLE,
984+
env=env)
985+
self.assertEqual(p.returncode, 0)
986+
987+
# Then test that FIleIO is used when PYTHONLEGACYWINDOWSSTDIO is set.
988+
code = "import sys; sys.exit(type(sys.stdout.buffer.raw).__name__ != 'FileIO')"
993989
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(), "FileIO")
990+
p = subprocess.run([sys.executable, "-c", code],
991+
creationflags=subprocess.CREATE_NEW_CONSOLE,
992+
env=env)
993+
self.assertEqual(p.returncode, 0)
1001994

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

0 commit comments

Comments
 (0)