From cfaa04f81b39db45897275f355371822e7a00b56 Mon Sep 17 00:00:00 2001 From: Sam Gross Date: Thu, 1 Aug 2024 18:31:56 +0000 Subject: [PATCH 1/3] gh-122575: Include `sys.flags.gil` as a sequence attribute Make sure that `sys.flags.gil` is included when `sys.flags` is treated as a tuple or named tuple. --- Doc/library/sys.rst | 6 ++++++ Lib/test/test_sys.py | 3 ++- .../2024-08-01-18-35-54.gh-issue-122575.JTvKx9.rst | 2 ++ Python/sysmodule.c | 2 +- 4 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2024-08-01-18-35-54.gh-issue-122575.JTvKx9.rst diff --git a/Doc/library/sys.rst b/Doc/library/sys.rst index ed809d04167ffd..d0df6d4a0194eb 100644 --- a/Doc/library/sys.rst +++ b/Doc/library/sys.rst @@ -587,6 +587,9 @@ always available. * - .. attribute:: flags.warn_default_encoding - :option:`-X warn_default_encoding <-X>` + * - .. attribute:: flags.gil + - :option:`-X gil <-X>` + .. versionchanged:: 3.2 Added ``quiet`` attribute for the new :option:`-q` flag. @@ -613,6 +616,9 @@ always available. .. versionchanged:: 3.11 Added the ``int_max_str_digits`` attribute. + .. versionchanged:: 3.13 + Added the ``gil`` attribute. + .. data:: float_info diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py index 7e4bc980b390f7..944421b140a045 100644 --- a/Lib/test/test_sys.py +++ b/Lib/test/test_sys.py @@ -803,7 +803,8 @@ def test_sys_flags(self): "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") + "warn_default_encoding", "safe_path", "int_max_str_digits", + "gil") for attr in attrs: self.assertTrue(hasattr(sys.flags, attr), attr) attr_type = bool if attr in ("dev_mode", "safe_path") else int diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2024-08-01-18-35-54.gh-issue-122575.JTvKx9.rst b/Misc/NEWS.d/next/Core_and_Builtins/2024-08-01-18-35-54.gh-issue-122575.JTvKx9.rst new file mode 100644 index 00000000000000..4602a97384f6dd --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2024-08-01-18-35-54.gh-issue-122575.JTvKx9.rst @@ -0,0 +1,2 @@ +Include :attr:`sys.flags.gil` as part of the sequence when :data:`sys.flags` +is treated as a tuple. diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 1fff7e41767398..d86769bf1bc8c4 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -3124,7 +3124,7 @@ static PyStructSequence_Desc flags_desc = { "sys.flags", /* name */ flags__doc__, /* doc */ flags_fields, /* fields */ - 18 + sizeof(flags_fields) / sizeof(flags_fields[0]) - 1 }; static int From 7a41cfc68035214762f637a56b30954ebc5f384c Mon Sep 17 00:00:00 2001 From: Sam Gross Date: Thu, 1 Aug 2024 19:34:02 +0000 Subject: [PATCH 2/3] sys.flags.gil may be None or an int --- Lib/test/test_sys.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py index 944421b140a045..a82334d25623ed 100644 --- a/Lib/test/test_sys.py +++ b/Lib/test/test_sys.py @@ -805,10 +805,15 @@ def test_sys_flags(self): "hash_randomization", "isolated", "dev_mode", "utf8_mode", "warn_default_encoding", "safe_path", "int_max_str_digits", "gil") + attr_types = { + "dev_mode": bool, + "safe_path": bool, + "gil": (int, type(None)), + } for attr in attrs: self.assertTrue(hasattr(sys.flags, attr), attr) - attr_type = bool if attr in ("dev_mode", "safe_path") else int - self.assertEqual(type(getattr(sys.flags, attr)), attr_type, attr) + expected_type = attr_types.get(attr, int) + self.assertIsInstance(getattr(sys.flags, attr), expected_type, attr) self.assertTrue(repr(sys.flags)) self.assertEqual(len(sys.flags), len(attrs)) From dbf5189bd9ef90873e8014e084c8d08b6bf83af9 Mon Sep 17 00:00:00 2001 From: Sam Gross Date: Mon, 2 Sep 2024 13:40:16 -0400 Subject: [PATCH 3/3] Update Python/sysmodule.c MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Python/sysmodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/sysmodule.c b/Python/sysmodule.c index d86769bf1bc8c4..ab1a5a0860e042 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -3124,7 +3124,7 @@ static PyStructSequence_Desc flags_desc = { "sys.flags", /* name */ flags__doc__, /* doc */ flags_fields, /* fields */ - sizeof(flags_fields) / sizeof(flags_fields[0]) - 1 + Py_ARRAY_LENGTH(flags_fields) - 1 }; static int