]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-133143: Condense the implementation for ``sys.abi_info`` (#138672)
authorAdam Turner <9087854+AA-Turner@users.noreply.github.com>
Mon, 8 Sep 2025 19:21:28 +0000 (20:21 +0100)
committerGitHub <noreply@github.com>
Mon, 8 Sep 2025 19:21:28 +0000 (19:21 +0000)
Include/internal/pycore_global_objects_fini_generated.h
Include/internal/pycore_global_strings.h
Include/internal/pycore_runtime_init_generated.h
Include/internal/pycore_unicodeobject_generated.h
Lib/test/test_sys.py
Misc/NEWS.d/next/Core_and_Builtins/2025-08-06-16-55-44.gh-issue-133143.l7CI9v.rst
Python/sysmodule.c

index 2078c201b4558d66ed4f0aff360476948b6e132b..ad65d1fda18e3bc3e765401c3b9e2896e907c70d 100644 (file)
@@ -984,6 +984,7 @@ _PyStaticObjects_CheckRefcnt(PyInterpreterState *interp) {
     _PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(format));
     _PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(format_spec));
     _PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(frame_buffer));
+    _PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(free_threaded));
     _PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(from_param));
     _PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(fromlist));
     _PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(fromtimestamp));
@@ -1182,6 +1183,7 @@ _PyStaticObjects_CheckRefcnt(PyInterpreterState *interp) {
     _PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(person));
     _PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(pi_factory));
     _PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(pid));
+    _PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(pointer_bits));
     _PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(policy));
     _PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(pos));
     _PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(pos1));
index a7ebaf76c908c8eb589aa3da51f7c1151cef8ed6..88283fa625706f3fe6c5c774010c9f238f22a463 100644 (file)
@@ -475,6 +475,7 @@ struct _Py_global_strings {
         STRUCT_FOR_ID(format)
         STRUCT_FOR_ID(format_spec)
         STRUCT_FOR_ID(frame_buffer)
+        STRUCT_FOR_ID(free_threaded)
         STRUCT_FOR_ID(from_param)
         STRUCT_FOR_ID(fromlist)
         STRUCT_FOR_ID(fromtimestamp)
@@ -673,6 +674,7 @@ struct _Py_global_strings {
         STRUCT_FOR_ID(person)
         STRUCT_FOR_ID(pi_factory)
         STRUCT_FOR_ID(pid)
+        STRUCT_FOR_ID(pointer_bits)
         STRUCT_FOR_ID(policy)
         STRUCT_FOR_ID(pos)
         STRUCT_FOR_ID(pos1)
index 27669c50cd6101e08114a666903254a6249e2730..a08d77ab0a06b9b693a06ed9be0feadb72a72deb 100644 (file)
@@ -982,6 +982,7 @@ extern "C" {
     INIT_ID(format), \
     INIT_ID(format_spec), \
     INIT_ID(frame_buffer), \
+    INIT_ID(free_threaded), \
     INIT_ID(from_param), \
     INIT_ID(fromlist), \
     INIT_ID(fromtimestamp), \
@@ -1180,6 +1181,7 @@ extern "C" {
     INIT_ID(person), \
     INIT_ID(pi_factory), \
     INIT_ID(pid), \
+    INIT_ID(pointer_bits), \
     INIT_ID(policy), \
     INIT_ID(pos), \
     INIT_ID(pos1), \
index c9b8a8b50510a23e726bf7752bad0a9cccc1fee4..57c0a7d51f89f3a0cb4f384a3dd411cc8f9d3ab2 100644 (file)
@@ -1688,6 +1688,10 @@ _PyUnicode_InitStaticStrings(PyInterpreterState *interp) {
     _PyUnicode_InternStatic(interp, &string);
     assert(_PyUnicode_CheckConsistency(string, 1));
     assert(PyUnicode_GET_LENGTH(string) != 1);
+    string = &_Py_ID(free_threaded);
+    _PyUnicode_InternStatic(interp, &string);
+    assert(_PyUnicode_CheckConsistency(string, 1));
+    assert(PyUnicode_GET_LENGTH(string) != 1);
     string = &_Py_ID(from_param);
     _PyUnicode_InternStatic(interp, &string);
     assert(_PyUnicode_CheckConsistency(string, 1));
@@ -2480,6 +2484,10 @@ _PyUnicode_InitStaticStrings(PyInterpreterState *interp) {
     _PyUnicode_InternStatic(interp, &string);
     assert(_PyUnicode_CheckConsistency(string, 1));
     assert(PyUnicode_GET_LENGTH(string) != 1);
+    string = &_Py_ID(pointer_bits);
+    _PyUnicode_InternStatic(interp, &string);
+    assert(_PyUnicode_CheckConsistency(string, 1));
+    assert(PyUnicode_GET_LENGTH(string) != 1);
     string = &_Py_ID(policy);
     _PyUnicode_InternStatic(interp, &string);
     assert(_PyUnicode_CheckConsistency(string, 1));
index 42672eb7912ebaa2dcdc6b1c65bdae43098d5bd1..1198c6d35113c81b0689a60528ef65ef30b52807 100644 (file)
@@ -741,17 +741,15 @@ class SysModuleTest(unittest.TestCase):
 
     def test_abi_info(self):
         info = sys.abi_info
-        self.assertEqual(len(info.__dict__), 4)
+        info_keys = {'pointer_bits', 'free_threaded', 'debug', 'byteorder'}
+        self.assertEqual(set(vars(info)), info_keys)
         pointer_bits = 64 if sys.maxsize > 2**32 else 32
         self.assertEqual(info.pointer_bits, pointer_bits)
+        self.assertEqual(info.free_threaded,
+                         bool(sysconfig.get_config_var('Py_GIL_DISABLED')))
+        self.assertEqual(info.debug,
+                         bool(sysconfig.get_config_var('Py_DEBUG')))
         self.assertEqual(info.byteorder, sys.byteorder)
-        for attr, flag in [
-            ("free_threaded", "Py_GIL_DISABLED"),
-            ("debug", "Py_DEBUG"),
-        ]:
-            self.assertEqual(getattr(info, attr, None),
-                             bool(sysconfig.get_config_var(flag)),
-                             f"for {attr}")
 
     @unittest.skipUnless(support.is_emscripten, "only available on Emscripten")
     def test_emscripten_info(self):
index eaffb4022c677010e513630cf728a91be0cbbe2d..b357fbcdb9fbd0b09ca7dd4319fab9c908b77de2 100644 (file)
@@ -1 +1 @@
-Add ``sys.abi_info`` object to make ABI information more easily accessible.
+Add :data:`sys.abi_info` object to make ABI information more easily accessible.
index 95ab87589718ce418eaa3737b3723be12af302a4..a5b42d48e432d7ea3d0394d2f7f7a9447cb0f1a7 100644 (file)
@@ -3643,33 +3643,30 @@ error:
 static PyObject *
 make_abi_info(void)
 {
-    // New entries should be added when needed for a supported platform, or (for
-    // enabling an unsupported one) by core dev consensus.  Entries should be removed
-    // following PEP 387.
-    int res;
-    PyObject *abi_info, *value, *ns;
-    abi_info = PyDict_New();
+    // New entries should be added when needed for a supported platform,
+    // or by core dev consensus for enabling an unsupported one.
+
+    PyObject *value;
+    PyObject *abi_info = PyDict_New();
     if (abi_info == NULL) {
-        goto error;
+        return NULL;
     }
 
     value = PyLong_FromLong(sizeof(void *) * 8);
     if (value == NULL) {
         goto error;
     }
-    res = PyDict_SetItemString(abi_info, "pointer_bits", value);
-    Py_DECREF(value);
-    if (res < 0) {
+    if (PyDict_SetItem(abi_info, &_Py_ID(pointer_bits), value) < 0) {
         goto error;
     }
+    Py_DECREF(value);
 
 #ifdef Py_GIL_DISABLED
     value = Py_True;
 #else
     value = Py_False;
 #endif
-    res = PyDict_SetItemString(abi_info, "free_threaded", value);
-    if (res < 0) {
+    if (PyDict_SetItem(abi_info, &_Py_ID(free_threaded), value) < 0) {
         goto error;
     }
 
@@ -3678,34 +3675,30 @@ make_abi_info(void)
 #else
     value = Py_False;
 #endif
-    res = PyDict_SetItemString(abi_info, "debug", value);
-    if (res < 0) {
+    if (PyDict_SetItem(abi_info, &_Py_ID(debug), value) < 0) {
         goto error;
     }
 
 #if PY_BIG_ENDIAN
-    value = PyUnicode_FromString("big");
+    value = &_Py_ID(big);
 #else
-    value = PyUnicode_FromString("little");
+    value = &_Py_ID(little);
 #endif
-    if (value == NULL) {
-        goto error;
-    }
-    res = PyDict_SetItemString(abi_info, "byteorder", value);
-    Py_DECREF(value);
-    if (res < 0) {
+    if (PyDict_SetItem(abi_info, &_Py_ID(byteorder), value) < 0) {
         goto error;
     }
 
-    ns = _PyNamespace_New(abi_info);
+    PyObject *ns = _PyNamespace_New(abi_info);
     Py_DECREF(abi_info);
     return ns;
 
 error:
     Py_DECREF(abi_info);
+    Py_XDECREF(value);
     return NULL;
 }
 
+
 #ifdef __EMSCRIPTEN__
 
 PyDoc_STRVAR(emscripten_info__doc__,