Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Test infrastructure upgrade to 3.13.2 #5517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 20 commits into
base: main
Choose a base branch
Loading
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update test_cmd_line.py to 3.13.2
Signed-off-by: Ashwin Naren <arihant2math@gmail.com>
  • Loading branch information
arihant2math committed Feb 17, 2025
commit 697a8476ebf66ee4ec95d2a8c1d4d7dfd103fa86
171 changes: 126 additions & 45 deletions 171 Lib/test/test_cmd_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import unittest
from test import support
from test.support import os_helper
from test.support import force_not_colorized
from test.support.script_helper import (
spawn_python, kill_python, assert_python_ok, assert_python_failure,
interpreter_requires_environment
Expand Down Expand Up @@ -38,8 +39,7 @@ def verify_valid_flag(self, cmd_line):
self.assertNotIn(b'Traceback', err)
return out

# TODO: RUSTPYTHON
@unittest.expectedFailure
@support.cpython_only
def test_help(self):
self.verify_valid_flag('-h')
self.verify_valid_flag('-?')
Expand All @@ -50,20 +50,17 @@ def test_help(self):
self.assertNotIn(b'-X dev', out)
self.assertLess(len(lines), 50)

# TODO: RUSTPYTHON
@unittest.expectedFailure
@support.cpython_only
def test_help_env(self):
out = self.verify_valid_flag('--help-env')
self.assertIn(b'PYTHONHOME', out)

# TODO: RUSTPYTHON
@unittest.expectedFailure
@support.cpython_only
def test_help_xoptions(self):
out = self.verify_valid_flag('--help-xoptions')
self.assertIn(b'-X dev', out)

# TODO: RUSTPYTHON
@unittest.expectedFailure
@support.cpython_only
def test_help_all(self):
out = self.verify_valid_flag('--help-all')
lines = out.splitlines()
Expand All @@ -82,15 +79,16 @@ def test_optimize(self):
def test_site_flag(self):
self.verify_valid_flag('-S')

# NOTE: RUSTPYTHON version never starts with Python
@unittest.expectedFailure
@support.cpython_only
def test_version(self):
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))

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_verbose(self):
# -v causes imports to write to stderr. If the write to
# stderr itself causes an import to happen (for the output
Expand Down Expand Up @@ -149,8 +147,7 @@ def run_python(*args):
else:
self.assertEqual(err, b'')

# TODO: RUSTPYTHON
@unittest.expectedFailure
@support.cpython_only
def test_xoption_frozen_modules(self):
tests = {
('=on', 'FrozenImporter'),
Expand All @@ -165,6 +162,18 @@ def test_xoption_frozen_modules(self):
res = assert_python_ok(*cmd)
self.assertRegex(res.out.decode('utf-8'), expected)

@support.cpython_only
def test_env_var_frozen_modules(self):
tests = {
('on', 'FrozenImporter'),
('off', 'SourceFileLoader'),
}
for raw, expected in tests:
cmd = ['-c', 'import os; print(os.__spec__.loader, end="")']
with self.subTest(raw):
res = assert_python_ok(*cmd, PYTHON_FROZEN_MODULES=raw)
self.assertRegex(res.out.decode('utf-8'), expected)

def test_run_module(self):
# Test expected operation of the '-m' switch
# Switch needs an argument
Expand Down Expand Up @@ -226,8 +235,6 @@ def test_coding(self):
# command line, but how subprocess does decode bytes to unicode. Python
# doesn't decode the command line because Windows provides directly the
# arguments as unicode (using wmain() instead of main()).
# TODO: RUSTPYTHON
@unittest.expectedFailure
@unittest.skipIf(sys.platform == 'win32',
'Windows has a native unicode API')
def test_undecodable_code(self):
Expand Down Expand Up @@ -263,7 +270,6 @@ def test_undecodable_code(self):
if not stdout.startswith(pattern):
raise AssertionError("%a doesn't start with %a" % (stdout, pattern))

@unittest.skip("TODO: RUSTPYTHON, thread 'main' panicked at 'unexpected invalid UTF-8 code point'")
@unittest.skipIf(sys.platform == 'win32',
'Windows has a native unicode API')
def test_invalid_utf8_arg(self):
Expand All @@ -275,20 +281,17 @@ def test_invalid_utf8_arg(self):
# Test with default config, in the C locale, in the Python UTF-8 Mode.
code = 'import sys, os; s=os.fsencode(sys.argv[1]); print(ascii(s))'

# TODO: RUSTPYTHON
def run_default(arg):
cmd = [sys.executable, '-c', code, arg]
return subprocess.run(cmd, stdout=subprocess.PIPE, text=True)

# TODO: RUSTPYTHON
def run_c_locale(arg):
cmd = [sys.executable, '-c', code, arg]
env = dict(os.environ)
env['LC_ALL'] = 'C'
return subprocess.run(cmd, stdout=subprocess.PIPE,
text=True, env=env)

# TODO: RUSTPYTHON
def run_utf8_mode(arg):
cmd = [sys.executable, '-X', 'utf8', '-c', code, arg]
return subprocess.run(cmd, stdout=subprocess.PIPE, text=True)
Expand Down Expand Up @@ -402,8 +405,7 @@ def test_empty_PYTHONPATH_issue16309(self):
path = ":".join(sys.path)
path = path.encode("ascii", "backslashreplace")
sys.stdout.buffer.write(path)"""
# TODO: RUSTPYTHON we must unset RUSTPYTHONPATH as well
rc1, out1, err1 = assert_python_ok('-c', code, PYTHONPATH="", RUSTPYTHONPATH="")
rc1, out1, err1 = assert_python_ok('-c', code, PYTHONPATH="")
rc2, out2, err2 = assert_python_ok('-c', code, __isolated=False)
# regarding to Posix specification, outputs should be equal
# for empty and unset PYTHONPATH
Expand Down Expand Up @@ -491,8 +493,9 @@ def test_stdout_flush_at_shutdown(self):
rc, out, err = assert_python_failure('-c', code)
self.assertEqual(b'', out)
self.assertEqual(120, rc)
self.assertRegex(err.decode('ascii', 'ignore'),
'Exception ignored in.*\nOSError: .*')
self.assertIn(b'Exception ignored on flushing sys.stdout:\n'
b'OSError: '.replace(b'\n', os.linesep.encode()),
err)

def test_closed_stdout(self):
# Issue #13444: if stdout has been explicitly closed, we should
Expand Down Expand Up @@ -600,8 +603,7 @@ def test_del___main__(self):
print("del sys.modules['__main__']", file=script)
assert_python_ok(filename)

# TODO: RUSTPYTHON
@unittest.expectedFailure
@support.cpython_only
def test_unknown_options(self):
rc, out, err = assert_python_failure('-E', '-z')
self.assertIn(b'Unknown option: -z', err)
Expand Down Expand Up @@ -648,6 +650,8 @@ def test_isolatedmode(self):
cwd=tmpdir)
self.assertEqual(out.strip(), b"ok")

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_sys_flags_set(self):
# Issue 31845: a startup refactoring broke reading flags from env vars
for value, expected in (("", 0), ("1", 1), ("text", 1), ("2", 2)):
Expand All @@ -657,15 +661,13 @@ def test_sys_flags_set(self):
PYTHONDONTWRITEBYTECODE=value,
PYTHONVERBOSE=value,
)
dont_write_bytecode = int(bool(value))
expected_bool = int(bool(value))
code = (
"import sys; "
"sys.stderr.write(str(sys.flags)); "
f"""sys.exit(not (
sys.flags.debug == sys.flags.optimize ==
sys.flags.verbose ==
{expected}
and sys.flags.dont_write_bytecode == {dont_write_bytecode}
sys.flags.optimize == sys.flags.verbose == {expected}
and sys.flags.debug == sys.flags.dont_write_bytecode == {expected_bool}
))"""
)
with self.subTest(envar_value=value):
Expand Down Expand Up @@ -718,8 +720,7 @@ def run_xdev(self, *args, check_exitcode=True, xdev=True):
self.assertEqual(proc.returncode, 0, proc)
return proc.stdout.rstrip()

# TODO: RUSTPYTHON
@unittest.expectedFailure
@support.cpython_only
def test_xdev(self):
# sys.flags.dev_mode
code = "import sys; print(sys.flags.dev_mode)"
Expand Down Expand Up @@ -756,15 +757,17 @@ def test_xdev(self):

# Memory allocator debug hooks
try:
import _testcapi
import _testinternalcapi
except ImportError:
pass
else:
code = "import _testcapi; print(_testcapi.pymem_getallocatorsname())"
code = "import _testinternalcapi; print(_testinternalcapi.pymem_getallocatorsname())"
with support.SuppressCrashReport():
out = self.run_xdev("-c", code, check_exitcode=False)
if support.with_pymalloc():
alloc_name = "pymalloc_debug"
elif support.Py_GIL_DISABLED:
alloc_name = "mimalloc_debug"
else:
alloc_name = "malloc_debug"
self.assertEqual(out, alloc_name)
Expand Down Expand Up @@ -824,7 +827,7 @@ def test_warnings_filter_precedence(self):
self.assertEqual(out, expected_filters)

def check_pythonmalloc(self, env_var, name):
code = 'import _testcapi; print(_testcapi.pymem_getallocatorsname())'
code = 'import _testinternalcapi; print(_testinternalcapi.pymem_getallocatorsname())'
env = dict(os.environ)
env.pop('PYTHONDEVMODE', None)
if env_var is not None:
Expand All @@ -840,12 +843,16 @@ def check_pythonmalloc(self, env_var, name):
self.assertEqual(proc.stdout.rstrip(), name)
self.assertEqual(proc.returncode, 0)

# TODO: RUSTPYTHON
@unittest.expectedFailure
@support.cpython_only
def test_pythonmalloc(self):
# Test the PYTHONMALLOC environment variable
malloc = not support.Py_GIL_DISABLED
pymalloc = support.with_pymalloc()
if pymalloc:
mimalloc = support.with_mimalloc()
if support.Py_GIL_DISABLED:
default_name = 'mimalloc_debug' if support.Py_DEBUG else 'mimalloc'
default_name_debug = 'mimalloc_debug'
elif pymalloc:
default_name = 'pymalloc_debug' if support.Py_DEBUG else 'pymalloc'
default_name_debug = 'pymalloc_debug'
else:
Expand All @@ -855,14 +862,22 @@ def test_pythonmalloc(self):
tests = [
(None, default_name),
('debug', default_name_debug),
('malloc', 'malloc'),
('malloc_debug', 'malloc_debug'),
]
if malloc:
tests.extend([
('malloc', 'malloc'),
('malloc_debug', 'malloc_debug'),
])
if pymalloc:
tests.extend((
('pymalloc', 'pymalloc'),
('pymalloc_debug', 'pymalloc_debug'),
))
if mimalloc:
tests.extend((
('mimalloc', 'mimalloc'),
('mimalloc_debug', 'mimalloc_debug'),
))

for env_var, name in tests:
with self.subTest(env_var=env_var, name=name):
Expand All @@ -888,6 +903,51 @@ def test_pythondevmode_env(self):
self.assertEqual(proc.stdout.rstrip(), 'True')
self.assertEqual(proc.returncode, 0, proc)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_python_gil(self):
cases = [
# (env, opt, expected, msg)
('1', None, '1', "PYTHON_GIL=1"),
(None, '1', '1', "-X gil=1"),
]

if support.Py_GIL_DISABLED:
cases.extend(
[
(None, None, 'None', "no options set"),
('0', None, '0', "PYTHON_GIL=0"),
('1', '0', '0', "-X gil=0 overrides PYTHON_GIL=1"),
(None, '0', '0', "-X gil=0"),
]
)
else:
cases.extend(
[
(None, None, '1', '-X gil=0 (unsupported by this build)'),
('1', None, '1', 'PYTHON_GIL=0 (unsupported by this build)'),
]
)
code = "import sys; print(sys.flags.gil)"
environ = dict(os.environ)

for env, opt, expected, msg in cases:
with self.subTest(msg, env=env, opt=opt):
environ.pop('PYTHON_GIL', None)
if env is not None:
environ['PYTHON_GIL'] = env
extra_args = []
if opt is not None:
extra_args = ['-X', f'gil={opt}']

proc = subprocess.run([sys.executable, *extra_args, '-c', code],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True, env=environ)
self.assertEqual(proc.returncode, 0, proc)
self.assertEqual(proc.stdout.rstrip(), expected)
self.assertEqual(proc.stderr, '')

@unittest.skipUnless(sys.platform == 'win32',
'bpo-32457 only applies on Windows')
def test_argv0_normalization(self):
Expand All @@ -900,8 +960,7 @@ def test_argv0_normalization(self):
self.assertEqual(proc.returncode, 0, proc)
self.assertEqual(proc.stdout.strip(), b'0')

# TODO: RUSTPYTHON
@unittest.expectedFailure
@support.cpython_only
def test_parsing_error(self):
args = [sys.executable, '-I', '--unknown-option']
proc = subprocess.run(args,
Expand All @@ -924,11 +983,8 @@ def test_int_max_str_digits(self):
assert_python_failure('-c', code, PYTHONINTMAXSTRDIGITS='foo')
assert_python_failure('-c', code, PYTHONINTMAXSTRDIGITS='100')

def res2int(res):
out = res.out.strip().decode("utf-8")
return tuple(int(i) for i in out.split())

res = assert_python_ok('-c', code)
res2int = self.res2int
current_max = sys.get_int_max_str_digits()
self.assertEqual(res2int(res), (current_max, current_max))
res = assert_python_ok('-X', 'int_max_str_digits=0', '-c', code)
Expand All @@ -948,6 +1004,30 @@ def res2int(res):
)
self.assertEqual(res2int(res), (6000, 6000))

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_cpu_count(self):
code = "import os; print(os.cpu_count(), os.process_cpu_count())"
res = assert_python_ok('-X', 'cpu_count=4321', '-c', code)
self.assertEqual(self.res2int(res), (4321, 4321))
res = assert_python_ok('-c', code, PYTHON_CPU_COUNT='1234')
self.assertEqual(self.res2int(res), (1234, 1234))

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_cpu_count_default(self):
code = "import os; print(os.cpu_count(), os.process_cpu_count())"
res = assert_python_ok('-X', 'cpu_count=default', '-c', code)
self.assertEqual(self.res2int(res), (os.cpu_count(), os.process_cpu_count()))
res = assert_python_ok('-X', 'cpu_count=default', '-c', code, PYTHON_CPU_COUNT='1234')
self.assertEqual(self.res2int(res), (os.cpu_count(), os.process_cpu_count()))
es = assert_python_ok('-c', code, PYTHON_CPU_COUNT='default')
self.assertEqual(self.res2int(res), (os.cpu_count(), os.process_cpu_count()))

def res2int(self, res):
out = res.out.strip().decode("utf-8")
return tuple(int(i) for i in out.split())


@unittest.skipIf(interpreter_requires_environment(),
'Cannot run -I tests when PYTHON env vars are required.')
Expand Down Expand Up @@ -990,6 +1070,7 @@ def test_sys_flags_not_set(self):


class SyntaxErrorTests(unittest.TestCase):
@force_not_colorized
def check_string(self, code):
proc = subprocess.run([sys.executable, "-"], input=code,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.