From 2cad40113e4aa75c5c2c6d6705975c8a22929871 Mon Sep 17 00:00:00 2001 From: Klaus Zimmermann Date: Fri, 14 Mar 2025 19:18:04 +0100 Subject: [PATCH 01/12] Add abi_features to sys --- Python/sysmodule.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/Python/sysmodule.c b/Python/sysmodule.c index adaa5ee1c74aa5..94f23b2701a872 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -3479,6 +3479,64 @@ make_impl_info(PyObject *version_info) return NULL; } +static PyObject* +make_abi_features(void) +{ + int res; + PyObject *features = PyList_New(0); + if (features == NULL) { + goto error; + } + +#if defined(Py_GIL_DISABLED) + PyObject *free_threading = PyUnicode_FromString("free-threading"); + if (free_threading == NULL) { + goto error; + } + res = PyList_Append(features, free_threading); + Py_DECREF(free_threading); + if (res < 0) { + goto error; + } +#endif + +#if defined(Py_DEBUG) || (defined(MS_WINDOWS) && defined(_DEBUG)) + PyObject *debug = PyUnicode_FromString("debug"); + if (debug == NULL) { + goto error; + } + res = PyList_Append(features, debug); + Py_DECREF(debug); + if (res < 0) { + goto error; + } +#endif + + bool is64bit = PY_SSIZE_T_MAX > (1L << 32); + PyObject *bitness; + if (is64bit) { + bitness = PyUnicode_FromString("64-bit"); + } else { + bitness = PyUnicode_FromString("32-bit"); + } + if (bitness == NULL) { + goto error; + } + res = PyList_Append(features, bitness); + Py_DECREF(bitness); + if (res < 0) { + goto error; + } + + PyObject *frozen = PyFrozenSet_New(features); + Py_DECREF(features); + return frozen; + +error: + Py_XDECREF(features); + return NULL; +} + #ifdef __EMSCRIPTEN__ PyDoc_STRVAR(emscripten_info__doc__, @@ -3665,6 +3723,7 @@ _PySys_InitCore(PyThreadState *tstate, PyObject *sysdict) #ifdef ABIFLAGS SET_SYS_FROM_STRING("abiflags", ABIFLAGS); #endif + SET_SYS("abi_features", make_abi_features()); #define ENSURE_INFO_TYPE(TYPE, DESC) \ do { \ From e24cc365bfe9839088d83d643f9bb4a1848ee357 Mon Sep 17 00:00:00 2001 From: Klaus Zimmermann Date: Fri, 14 Mar 2025 19:32:44 +0100 Subject: [PATCH 02/12] Add docs --- Doc/library/sys.rst | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Doc/library/sys.rst b/Doc/library/sys.rst index 2bdfb337f02056..229523fee870ce 100644 --- a/Doc/library/sys.rst +++ b/Doc/library/sys.rst @@ -11,6 +11,28 @@ 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_features + + A frozen set of ABI features. + Currently supported features are: + + ``free-threading`` + The Python interpreter is free-threading. On Unix systems, this corresponds + to ``"t" in sys.abiflags``. + + ``debug`` + The Python interpreter has debugging capabilities. On Unix systems, this + corresponds to ``"d" in sys.abiflags``. + + ``32-bit`` or ``64-bit`` + The bitness of the interpreter, that is, whether it is a 32-bit or 64-bit + build. + + .. versionadded:: 3.14 + + .. availability:: Unix, Windows + + .. data:: abiflags On POSIX systems where Python was built with the standard ``configure`` From cf7dd251173f2eb6dfefc50168b1c1c8fc955bc0 Mon Sep 17 00:00:00 2001 From: Klaus Zimmermann Date: Mon, 17 Mar 2025 17:04:14 +0100 Subject: [PATCH 03/12] Add gil-enabled threading feature --- Python/sysmodule.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 94f23b2701a872..3e369b5c9a1ba5 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -3489,16 +3489,18 @@ make_abi_features(void) } #if defined(Py_GIL_DISABLED) - PyObject *free_threading = PyUnicode_FromString("free-threading"); - if (free_threading == NULL) { + PyObject *threading_feature = PyUnicode_FromString("free-threading"); +#else + PyObject *threading_feature = PyUnicode_FromString("gil-enabled"); +#endif + if (threading_feature == NULL) { goto error; } - res = PyList_Append(features, free_threading); - Py_DECREF(free_threading); + res = PyList_Append(features, threading_feature); + Py_DECREF(threading_feature); if (res < 0) { goto error; } -#endif #if defined(Py_DEBUG) || (defined(MS_WINDOWS) && defined(_DEBUG)) PyObject *debug = PyUnicode_FromString("debug"); From 7c4a39917da27041dc6eebf0c246a456408238e3 Mon Sep 17 00:00:00 2001 From: Klaus Zimmermann Date: Tue, 18 Mar 2025 14:28:40 +0100 Subject: [PATCH 04/12] Apply suggestions from code review Co-authored-by: Lysandros Nikolaou --- Doc/library/sys.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Doc/library/sys.rst b/Doc/library/sys.rst index 229523fee870ce..1e2d791a8fb63e 100644 --- a/Doc/library/sys.rst +++ b/Doc/library/sys.rst @@ -16,12 +16,12 @@ always available. Unless explicitly noted otherwise, all variables are read-only A frozen set of ABI features. Currently supported features are: - ``free-threading`` - The Python interpreter is free-threading. On Unix systems, this corresponds + ``free-threading`` or ``gil_enabled`` + The Python interpreter is free-threaded. On POSIX systems, this corresponds to ``"t" in sys.abiflags``. ``debug`` - The Python interpreter has debugging capabilities. On Unix systems, this + The Python interpreter has debugging capabilities. On POSIX systems, this corresponds to ``"d" in sys.abiflags``. ``32-bit`` or ``64-bit`` From 28013cdb58acee926a3d33ac9c3b9ac20e555185 Mon Sep 17 00:00:00 2001 From: Klaus Zimmermann Date: Tue, 18 Mar 2025 14:32:43 +0100 Subject: [PATCH 05/12] Remove superfluous availability flag in docs --- Doc/library/sys.rst | 2 -- 1 file changed, 2 deletions(-) diff --git a/Doc/library/sys.rst b/Doc/library/sys.rst index 1e2d791a8fb63e..f84ce67bd52030 100644 --- a/Doc/library/sys.rst +++ b/Doc/library/sys.rst @@ -30,8 +30,6 @@ always available. Unless explicitly noted otherwise, all variables are read-only .. versionadded:: 3.14 - .. availability:: Unix, Windows - .. data:: abiflags From 1e70c5dbca8dbda299886676e806af320a06b2fc Mon Sep 17 00:00:00 2001 From: Klaus Zimmermann Date: Tue, 18 Mar 2025 16:23:02 +0100 Subject: [PATCH 06/12] Remove superfluous list --- Python/sysmodule.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 3e369b5c9a1ba5..4a22f0a34f5112 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -3483,7 +3483,7 @@ static PyObject* make_abi_features(void) { int res; - PyObject *features = PyList_New(0); + PyObject *features = PyFrozenSet_New(NULL); if (features == NULL) { goto error; } @@ -3496,7 +3496,7 @@ make_abi_features(void) if (threading_feature == NULL) { goto error; } - res = PyList_Append(features, threading_feature); + res = PySet_Add(features, threading_feature); Py_DECREF(threading_feature); if (res < 0) { goto error; @@ -3507,7 +3507,7 @@ make_abi_features(void) if (debug == NULL) { goto error; } - res = PyList_Append(features, debug); + res = PySet_Add(features, debug); Py_DECREF(debug); if (res < 0) { goto error; @@ -3524,15 +3524,13 @@ make_abi_features(void) if (bitness == NULL) { goto error; } - res = PyList_Append(features, bitness); + res = PySet_Add(features, bitness); Py_DECREF(bitness); if (res < 0) { goto error; } - PyObject *frozen = PyFrozenSet_New(features); - Py_DECREF(features); - return frozen; + return features; error: Py_XDECREF(features); From 4ddb002319ed405a1f937f3aed274f70567af20f Mon Sep 17 00:00:00 2001 From: Klaus Zimmermann Date: Tue, 18 Mar 2025 16:45:04 +0100 Subject: [PATCH 07/12] Improve bitness detection --- Python/sysmodule.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 4a22f0a34f5112..2211a78cfce9fd 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -3516,15 +3516,23 @@ make_abi_features(void) bool is64bit = PY_SSIZE_T_MAX > (1L << 32); PyObject *bitness; - if (is64bit) { - bitness = PyUnicode_FromString("64-bit"); - } else { + switch (PY_SSIZE_T_MAX) { + case 0x7FFFFFFFL: bitness = PyUnicode_FromString("32-bit"); + break; + case 0x7FFFFFFFFFFFFFFFL: + bitness = PyUnicode_FromString("64-bit"); + break; + default: + bitness = Py_NewRef(Py_None); + break; } if (bitness == NULL) { goto error; } - res = PySet_Add(features, bitness); + if (bitness != Py_None) { + res = PySet_Add(features, bitness); + } Py_DECREF(bitness); if (res < 0) { goto error; From f69a8f82820b7b4ab4494b1b3d05108eaf1f91ff Mon Sep 17 00:00:00 2001 From: Klaus Zimmermann Date: Tue, 18 Mar 2025 17:18:50 +0100 Subject: [PATCH 08/12] Simplify debug check --- Python/sysmodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 2211a78cfce9fd..6d74389ee74c37 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -3502,7 +3502,7 @@ make_abi_features(void) goto error; } -#if defined(Py_DEBUG) || (defined(MS_WINDOWS) && defined(_DEBUG)) +#if defined(Py_DEBUG) PyObject *debug = PyUnicode_FromString("debug"); if (debug == NULL) { goto error; From 123e81071b1521bfe3677a593a302c431ff06c72 Mon Sep 17 00:00:00 2001 From: Klaus Zimmermann Date: Tue, 18 Mar 2025 17:22:49 +0100 Subject: [PATCH 09/12] Fix typo --- Doc/library/sys.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/sys.rst b/Doc/library/sys.rst index f84ce67bd52030..e9026d4340b031 100644 --- a/Doc/library/sys.rst +++ b/Doc/library/sys.rst @@ -16,7 +16,7 @@ always available. Unless explicitly noted otherwise, all variables are read-only A frozen set of ABI features. Currently supported features are: - ``free-threading`` or ``gil_enabled`` + ``free-threading`` or ``gil-enabled`` The Python interpreter is free-threaded. On POSIX systems, this corresponds to ``"t" in sys.abiflags``. From 29f1ea08b405d94fe1589a52cdc0e2a11ac45182 Mon Sep 17 00:00:00 2001 From: Klaus Zimmermann Date: Tue, 18 Mar 2025 18:47:46 +0100 Subject: [PATCH 10/12] Remove obsolete code --- Python/sysmodule.c | 1 - 1 file changed, 1 deletion(-) diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 6d74389ee74c37..309b05914dce21 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -3514,7 +3514,6 @@ make_abi_features(void) } #endif - bool is64bit = PY_SSIZE_T_MAX > (1L << 32); PyObject *bitness; switch (PY_SSIZE_T_MAX) { case 0x7FFFFFFFL: From d92e16c48957455ed04265733ada6daf57fdc630 Mon Sep 17 00:00:00 2001 From: Klaus Zimmermann Date: Tue, 18 Mar 2025 18:49:20 +0100 Subject: [PATCH 11/12] Apply suggestions from code review Co-authored-by: Lysandros Nikolaou --- Python/sysmodule.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 309b05914dce21..21d6a880c42dde 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -3488,7 +3488,7 @@ make_abi_features(void) goto error; } -#if defined(Py_GIL_DISABLED) +#ifdef Py_GIL_DISABLED PyObject *threading_feature = PyUnicode_FromString("free-threading"); #else PyObject *threading_feature = PyUnicode_FromString("gil-enabled"); @@ -3502,7 +3502,7 @@ make_abi_features(void) goto error; } -#if defined(Py_DEBUG) +#ifdef Py_DEBUG PyObject *debug = PyUnicode_FromString("debug"); if (debug == NULL) { goto error; From 22fbaa69117e20a6d55f6133d58d54442592cd5f Mon Sep 17 00:00:00 2001 From: Klaus Zimmermann Date: Fri, 4 Apr 2025 17:23:33 -0600 Subject: [PATCH 12/12] Update Doc/library/sys.rst Co-authored-by: Xuehai Pan --- Doc/library/sys.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/sys.rst b/Doc/library/sys.rst index e9026d4340b031..58bd1a4df0e4ca 100644 --- a/Doc/library/sys.rst +++ b/Doc/library/sys.rst @@ -13,7 +13,7 @@ always available. Unless explicitly noted otherwise, all variables are read-only .. data:: abi_features - A frozen set of ABI features. + A frozen set of strings that represent ABI features. Currently supported features are: ``free-threading`` or ``gil-enabled``