diff options
| author | Klaus Zimmermann <klaus.zimmermann@quansight.com> | 2025-09-08 14:35:44 (GMT) |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-09-08 14:35:44 (GMT) |
| commit | 1acb718ea215da7ff030f5457bdfa42c31ef39c1 (patch) | |
| tree | 39bfc14a28f193162a9cead615622acea9b0b63c /Python/sysmodule.c | |
| parent | c006a623e73d2368ec653e19c769706885161053 (diff) | |
| download | cpython-1acb718ea215da7ff030f5457bdfa42c31ef39c1.zip cpython-1acb718ea215da7ff030f5457bdfa42c31ef39c1.tar.gz cpython-1acb718ea215da7ff030f5457bdfa42c31ef39c1.tar.bz2 | |
gh-133143: Add sys.abi_info (GH-137476)
This makes information about the interpreter ABI more accessible.
Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
Diffstat (limited to 'Python/sysmodule.c')
| -rw-r--r-- | Python/sysmodule.c | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/Python/sysmodule.c b/Python/sysmodule.c index cc798f6..95ab875 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -3268,6 +3268,7 @@ PyDoc_STR( "\n\ Static objects:\n\ \n\ +abi_info -- Python ABI information.\n\ builtin_module_names -- tuple of module names built into this interpreter\n\ copyright -- copyright notice pertaining to this interpreter\n\ exec_prefix -- prefix used to find the machine-specific Python library\n\ @@ -3638,6 +3639,73 @@ error: return NULL; } + +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(); + if (abi_info == NULL) { + goto error; + } + + 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) { + goto error; + } + +#ifdef Py_GIL_DISABLED + value = Py_True; +#else + value = Py_False; +#endif + res = PyDict_SetItemString(abi_info, "free_threaded", value); + if (res < 0) { + goto error; + } + +#ifdef Py_DEBUG + value = Py_True; +#else + value = Py_False; +#endif + res = PyDict_SetItemString(abi_info, "debug", value); + if (res < 0) { + goto error; + } + +#if PY_BIG_ENDIAN + value = PyUnicode_FromString("big"); +#else + value = PyUnicode_FromString("little"); +#endif + if (value == NULL) { + goto error; + } + res = PyDict_SetItemString(abi_info, "byteorder", value); + Py_DECREF(value); + if (res < 0) { + goto error; + } + + ns = _PyNamespace_New(abi_info); + Py_DECREF(abi_info); + return ns; + +error: + Py_DECREF(abi_info); + return NULL; +} + #ifdef __EMSCRIPTEN__ PyDoc_STRVAR(emscripten_info__doc__, @@ -3863,6 +3931,8 @@ _PySys_InitCore(PyThreadState *tstate, PyObject *sysdict) SET_SYS("thread_info", PyThread_GetInfo()); + SET_SYS("abi_info", make_abi_info()); + /* initialize asyncgen_hooks */ if (_PyStructSequence_InitBuiltin(interp, &AsyncGenHooksType, &asyncgen_hooks_desc) < 0) |
