Skip to content

Commit c0fcade

Browse files
committed
Condense the implementation
1 parent 0506533 commit c0fcade

File tree

3 files changed

+22
-32
lines changed

3 files changed

+22
-32
lines changed

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(list(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: 15 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 *abi_info = PyDict_New();
36523650
if (abi_info == NULL) {
3653-
goto error;
3651+
return NULL;
36543652
}
36553653

3654+
PyObject *value;
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_SetItemString(abi_info, "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_SetItemString(abi_info, "free_threaded", value) < 0) {
36733670
goto error;
36743671
}
36753672

@@ -3678,31 +3675,26 @@ 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_SetItemString(abi_info, "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_SetItemString(abi_info, "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

0 commit comments

Comments
 (0)