Skip to content

gh-133143: Make information about the interpreter ABI more accessible #137476

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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
18 changes: 18 additions & 0 deletions Doc/library/sys.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@ interpreter and to functions that interact strongly with the interpreter. It is
always available. Unless explicitly noted otherwise, all variables are read-only.


.. data:: abi_info

An object containing information about the ABI of the currently running
Python interpreter. The following attributes are available in cpython:

*pointer_bits* is the width of pointers in bits, as an integer, equivalent
to ``8 * sizeof(void *)``, i.e. usually ``32`` or ``64``.

*Py_GIL_DISABLED* is a boolean indicating whether the interpreter was built
with the GIL disabled, i.e. with the :option:`--disable-gil` option,
aka free-threading.

*Py_DEBUG* is a boolean indicating whether the interpreter was built in
debug mode, i.e. with the :option:`--with-pydebug` option.

.. versionadded:: next


.. data:: abiflags

On POSIX systems where Python was built with the standard ``configure``
Expand Down
9 changes: 9 additions & 0 deletions Lib/test/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,15 @@ def test_thread_info(self):
elif sys.platform == "wasi":
self.assertEqual(info.name, "pthread-stubs")

def test_abi_info(self):
info = sys.abi_info
self.assertEqual(len(info.__dict__), 3)
pointer_bits = 64 if sys.maxsize > 2**32 else 32
self.assertEqual(info.pointer_bits, pointer_bits)
for flag in ["Py_GIL_DISABLED", "Py_DEBUG"]:
self.assertEqual(getattr(info, flag, None),
bool(sysconfig.get_config_var(flag)))

@unittest.skipUnless(support.is_emscripten, "only available on Emscripten")
def test_emscripten_info(self):
self.assertEqual(len(sys._emscripten_info), 4)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add ``sys.abi_info`` object to make ABI information more easily accessible.
53 changes: 53 additions & 0 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3260,6 +3260,7 @@ PyDoc_STR(
"\n\
Static objects:\n\
\n\
abi_info -- a named tuple with information about the ABI.\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\
Expand Down Expand Up @@ -3630,6 +3631,56 @@ make_impl_info(PyObject *version_info)
return NULL;
}


static PyObject *
make_abi_info(void)
{
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, "Py_GIL_DISABLED", value);
if (res < 0) {
goto error;
}

#ifdef Py_DEBUG
value = Py_True;
#else
value = Py_False;
#endif
res = PyDict_SetItemString(abi_info, "Py_DEBUG", value);
if (res < 0) {
goto error;
}

ns = _PyNamespace_New(abi_info);
Py_DECREF(abi_info);
return ns;

error:
Py_CLEAR(abi_info);
return NULL;
}

#ifdef __EMSCRIPTEN__

PyDoc_STRVAR(emscripten_info__doc__,
Expand Down Expand Up @@ -3855,6 +3906,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)
Expand Down
Loading