Skip to content

Commit 5443b9e

Browse files
authored
gh-133143: Condense the implementation for sys.abi_info (#138672)
1 parent 49b351e commit 5443b9e

File tree

7 files changed

+37
-32
lines changed

7 files changed

+37
-32
lines changed

Include/internal/pycore_global_objects_fini_generated.h

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_global_strings.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ struct _Py_global_strings {
475475
STRUCT_FOR_ID(format)
476476
STRUCT_FOR_ID(format_spec)
477477
STRUCT_FOR_ID(frame_buffer)
478+
STRUCT_FOR_ID(free_threaded)
478479
STRUCT_FOR_ID(from_param)
479480
STRUCT_FOR_ID(fromlist)
480481
STRUCT_FOR_ID(fromtimestamp)
@@ -673,6 +674,7 @@ struct _Py_global_strings {
673674
STRUCT_FOR_ID(person)
674675
STRUCT_FOR_ID(pi_factory)
675676
STRUCT_FOR_ID(pid)
677+
STRUCT_FOR_ID(pointer_bits)
676678
STRUCT_FOR_ID(policy)
677679
STRUCT_FOR_ID(pos)
678680
STRUCT_FOR_ID(pos1)

Include/internal/pycore_runtime_init_generated.h

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_unicodeobject_generated.h

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/test/test_sys.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -741,17 +741,15 @@ def test_thread_info(self):
741741

742742
def test_abi_info(self):
743743
info = sys.abi_info
744-
self.assertEqual(len(info.__dict__), 4)
744+
info_keys = {'pointer_bits', 'free_threaded', 'debug', 'byteorder'}
745+
self.assertEqual(set(vars(info)), info_keys)
745746
pointer_bits = 64 if sys.maxsize > 2**32 else 32
746747
self.assertEqual(info.pointer_bits, pointer_bits)
748+
self.assertEqual(info.free_threaded,
749+
bool(sysconfig.get_config_var('Py_GIL_DISABLED')))
750+
self.assertEqual(info.debug,
751+
bool(sysconfig.get_config_var('Py_DEBUG')))
747752
self.assertEqual(info.byteorder, sys.byteorder)
748-
for attr, flag in [
749-
("free_threaded", "Py_GIL_DISABLED"),
750-
("debug", "Py_DEBUG"),
751-
]:
752-
self.assertEqual(getattr(info, attr, None),
753-
bool(sysconfig.get_config_var(flag)),
754-
f"for {attr}")
755753

756754
@unittest.skipUnless(support.is_emscripten, "only available on Emscripten")
757755
def test_emscripten_info(self):
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Add ``sys.abi_info`` object to make ABI information more easily accessible.
1+
Add :data:`sys.abi_info` object to make ABI information more easily accessible.

Python/sysmodule.c

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3643,33 +3643,30 @@ make_impl_info(PyObject *version_info)
36433643
static PyObject *
36443644
make_abi_info(void)
36453645
{
3646-
// New entries should be added when needed for a supported platform, or (for
3647-
// enabling an unsupported one) by core dev consensus. Entries should be removed
3648-
// following PEP 387.
3649-
int res;
3650-
PyObject *abi_info, *value, *ns;
3651-
abi_info = PyDict_New();
3646+
// New entries should be added when needed for a supported platform,
3647+
// or by core dev consensus for enabling an unsupported one.
3648+
3649+
PyObject *value;
3650+
PyObject *abi_info = PyDict_New();
36523651
if (abi_info == NULL) {
3653-
goto error;
3652+
return NULL;
36543653
}
36553654

36563655
value = PyLong_FromLong(sizeof(void *) * 8);
36573656
if (value == NULL) {
36583657
goto error;
36593658
}
3660-
res = PyDict_SetItemString(abi_info, "pointer_bits", value);
3661-
Py_DECREF(value);
3662-
if (res < 0) {
3659+
if (PyDict_SetItem(abi_info, &_Py_ID(pointer_bits), value) < 0) {
36633660
goto error;
36643661
}
3662+
Py_DECREF(value);
36653663

36663664
#ifdef Py_GIL_DISABLED
36673665
value = Py_True;
36683666
#else
36693667
value = Py_False;
36703668
#endif
3671-
res = PyDict_SetItemString(abi_info, "free_threaded", value);
3672-
if (res < 0) {
3669+
if (PyDict_SetItem(abi_info, &_Py_ID(free_threaded), value) < 0) {
36733670
goto error;
36743671
}
36753672

@@ -3678,34 +3675,30 @@ make_abi_info(void)
36783675
#else
36793676
value = Py_False;
36803677
#endif
3681-
res = PyDict_SetItemString(abi_info, "debug", value);
3682-
if (res < 0) {
3678+
if (PyDict_SetItem(abi_info, &_Py_ID(debug), value) < 0) {
36833679
goto error;
36843680
}
36853681

36863682
#if PY_BIG_ENDIAN
3687-
value = PyUnicode_FromString("big");
3683+
value = &_Py_ID(big);
36883684
#else
3689-
value = PyUnicode_FromString("little");
3685+
value = &_Py_ID(little);
36903686
#endif
3691-
if (value == NULL) {
3692-
goto error;
3693-
}
3694-
res = PyDict_SetItemString(abi_info, "byteorder", value);
3695-
Py_DECREF(value);
3696-
if (res < 0) {
3687+
if (PyDict_SetItem(abi_info, &_Py_ID(byteorder), value) < 0) {
36973688
goto error;
36983689
}
36993690

3700-
ns = _PyNamespace_New(abi_info);
3691+
PyObject *ns = _PyNamespace_New(abi_info);
37013692
Py_DECREF(abi_info);
37023693
return ns;
37033694

37043695
error:
37053696
Py_DECREF(abi_info);
3697+
Py_XDECREF(value);
37063698
return NULL;
37073699
}
37083700

3701+
37093702
#ifdef __EMSCRIPTEN__
37103703

37113704
PyDoc_STRVAR(emscripten_info__doc__,

0 commit comments

Comments
 (0)