aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/test/test_cmd_line.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_cmd_line.py')
-rw-r--r--Lib/test/test_cmd_line.py52
1 files changed, 43 insertions, 9 deletions
diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py
index 36f87e259e7..c17d749d4a1 100644
--- a/Lib/test/test_cmd_line.py
+++ b/Lib/test/test_cmd_line.py
@@ -39,7 +39,8 @@ class CmdLineTest(unittest.TestCase):
def verify_valid_flag(self, cmd_line):
rc, out, err = assert_python_ok(cmd_line)
- self.assertTrue(out == b'' or out.endswith(b'\n'))
+ if out != b'':
+ self.assertEndsWith(out, b'\n')
self.assertNotIn(b'Traceback', out)
self.assertNotIn(b'Traceback', err)
return out
@@ -89,8 +90,8 @@ class CmdLineTest(unittest.TestCase):
version = ('Python %d.%d' % sys.version_info[:2]).encode("ascii")
for switch in '-V', '--version', '-VV':
rc, out, err = assert_python_ok(switch)
- self.assertFalse(err.startswith(version))
- self.assertTrue(out.startswith(version))
+ self.assertNotStartsWith(err, version)
+ self.assertStartsWith(out, version)
def test_verbose(self):
# -v causes imports to write to stderr. If the write to
@@ -380,7 +381,7 @@ class CmdLineTest(unittest.TestCase):
p.stdin.flush()
data, rc = _kill_python_and_exit_code(p)
self.assertEqual(rc, 0)
- self.assertTrue(data.startswith(b'x'), data)
+ self.assertStartsWith(data, b'x')
def test_large_PYTHONPATH(self):
path1 = "ABCDE" * 100
@@ -972,10 +973,25 @@ class CmdLineTest(unittest.TestCase):
@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)
+ # 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"
+ 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")
@@ -1024,7 +1040,7 @@ class CmdLineTest(unittest.TestCase):
stderr=subprocess.PIPE,
text=True)
err_msg = "Unknown option: --unknown-option\nusage: "
- self.assertTrue(proc.stderr.startswith(err_msg), proc.stderr)
+ self.assertStartsWith(proc.stderr, err_msg)
self.assertNotEqual(proc.returncode, 0)
def test_int_max_str_digits(self):
@@ -1158,6 +1174,24 @@ class CmdLineTest(unittest.TestCase):
res = assert_python_ok('-c', code, PYTHON_CPU_COUNT='default')
self.assertEqual(self.res2int(res), (os.cpu_count(), os.process_cpu_count()))
+ def test_import_time(self):
+ # os is not imported at startup
+ code = 'import os; import os'
+
+ for case in 'importtime', 'importtime=1', 'importtime=true':
+ res = assert_python_ok('-X', case, '-c', code)
+ res_err = res.err.decode('utf-8')
+ self.assertRegex(res_err, r'import time: \s*\d+ \| \s*\d+ \| \s*os')
+ self.assertNotRegex(res_err, r'import time: cached\s* \| cached\s* \| os')
+
+ res = assert_python_ok('-X', 'importtime=2', '-c', code)
+ res_err = res.err.decode('utf-8')
+ self.assertRegex(res_err, r'import time: \s*\d+ \| \s*\d+ \| \s*os')
+ self.assertRegex(res_err, r'import time: cached\s* \| cached\s* \| os')
+
+ assert_python_failure('-X', 'importtime=-1', '-c', code)
+ assert_python_failure('-X', 'importtime=3', '-c', code)
+
def res2int(self, res):
out = res.out.strip().decode("utf-8")
return tuple(int(i) for i in out.split())