Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
37 changes: 27 additions & 10 deletions Lib/test/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -858,24 +858,35 @@ def test_subinterp_intern_singleton(self):
'''))
self.assertTrue(sys._is_interned(s))

def test_sys_flags(self):
def test_sys_flags_indexable_attributes(self):
self.assertTrue(sys.flags)
attrs = ("debug",
# We've stopped assigning sequence indices to new sys.flags attributes:
# https://github.com/python/cpython/issues/122575#issuecomment-2416497086
indexable_attrs = ("debug",
"inspect", "interactive", "optimize",
"dont_write_bytecode", "no_user_site", "no_site",
"ignore_environment", "verbose", "bytes_warning", "quiet",
"hash_randomization", "isolated", "dev_mode", "utf8_mode",
"warn_default_encoding", "safe_path", "int_max_str_digits",
"lazy_imports")
for attr in attrs:
"warn_default_encoding", "safe_path", "int_max_str_digits")
for attr_idx, attr in enumerate(indexable_attrs):
self.assertHasAttr(sys.flags, attr)
attr_type = bool if attr in ("dev_mode", "safe_path") else int
self.assertEqual(type(getattr(sys.flags, attr)), attr_type, attr)
attr_value = getattr(sys.flags, attr)
self.assertEqual(sys.flags[attr_idx], attr_value,
msg=f"sys.flags .{attr} vs [{attr_idx}]")
self.assertTrue(repr(sys.flags))
self.assertEqual(len(sys.flags), len(attrs))
self.assertEqual(len(sys.flags), 18, msg="Do not increase, see GH-122575")

self.assertIn(sys.flags.utf8_mode, {0, 1, 2})

def test_sys_flags_name_only_attributes(self):
# non-tuple sequence fields (name only sys.flags attributes)
self.assertIsInstance(sys.flags.gil, int|type(None))
self.assertIsInstance(sys.flags.thread_inherit_context, int|type(None))
self.assertIsInstance(sys.flags.context_aware_warnings, int|type(None))
self.assertIsInstance(sys.flags.lazy_imports, int|type(None))

def assert_raise_on_new_sys_type(self, sys_attr):
# Users are intentionally prevented from creating new instances of
# sys.flags, sys.version_info, and sys.getwindowsversion.
Expand Down Expand Up @@ -1908,10 +1919,16 @@ def test_pythontypes(self):
# symtable entry
# XXX
# sys.flags
# FIXME: The +3 is for the 'gil', 'thread_inherit_context' and
# 'context_aware_warnings' flags and will not be necessary once
# gh-122575 is fixed
check(sys.flags, vsize('') + self.P + self.P * (3 + len(sys.flags)))
# FIXME: The non_sequence_fields adjustment is for these flags:
# - 'gil'
# - 'thread_inherit_context'
# - 'context_aware_warnings'
# - 'lazy_imports'
# Not needing to increment this every time we add a new field
# per GH-122575 would be nice...
# Q: What is the actual point of this sys.flags C size derived from PyStructSequence_Field array assertion?
non_sequence_fields = 4
check(sys.flags, vsize('') + self.P + self.P * (non_sequence_fields + len(sys.flags)))

def test_asyncgen_hooks(self):
old = sys.get_asyncgen_hooks()
Expand Down
9 changes: 6 additions & 3 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3495,11 +3495,12 @@ static PyStructSequence_Field flags_fields[] = {
{"dev_mode", "-X dev"},
{"utf8_mode", "-X utf8"},
{"warn_default_encoding", "-X warn_default_encoding"},
{"safe_path", "-P"},
{"safe_path", "-P"},
{"int_max_str_digits", "-X int_max_str_digits"},
// Fields below are only usable by sys.flags attribute name, not index:
{"gil", "-X gil"},
{"thread_inherit_context", "-X thread_inherit_context"},
{"context_aware_warnings", "-X context_aware_warnings"},
{"context_aware_warnings", "-X context_aware_warnings"},
{"lazy_imports", "-X lazy_imports"},
{0}
};
Expand All @@ -3510,7 +3511,9 @@ static PyStructSequence_Desc flags_desc = {
"sys.flags", /* name */
flags__doc__, /* doc */
flags_fields, /* fields */
19
18 /* NB - do not increase beyond 3.13's value of 18. */
// New sys.flags fields should NOT be tuple addressable per
// https://github.com/python/cpython/issues/122575#issuecomment-2416497086
};

static void
Expand Down
Loading