Skip to content

Commit 0f70c14

Browse files
[3.12] gh-115320: Refactor get_hash_info in sysmodule.c not to swallow errors (GH-115321) (#116323)
gh-115320: Refactor `get_hash_info` in `sysmodule.c` not to swallow errors (GH-115321) (cherry picked from commit 207030f) Co-authored-by: Nikita Sobolev <[email protected]>
1 parent 19aa557 commit 0f70c14

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

Python/sysmodule.c

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,31 +1439,33 @@ get_hash_info(PyThreadState *tstate)
14391439
int field = 0;
14401440
PyHash_FuncDef *hashfunc;
14411441
hash_info = PyStructSequence_New(&Hash_InfoType);
1442-
if (hash_info == NULL)
1443-
return NULL;
1444-
hashfunc = PyHash_GetFuncDef();
1445-
PyStructSequence_SET_ITEM(hash_info, field++,
1446-
PyLong_FromLong(8*sizeof(Py_hash_t)));
1447-
PyStructSequence_SET_ITEM(hash_info, field++,
1448-
PyLong_FromSsize_t(_PyHASH_MODULUS));
1449-
PyStructSequence_SET_ITEM(hash_info, field++,
1450-
PyLong_FromLong(_PyHASH_INF));
1451-
PyStructSequence_SET_ITEM(hash_info, field++,
1452-
PyLong_FromLong(0)); // This is no longer used
1453-
PyStructSequence_SET_ITEM(hash_info, field++,
1454-
PyLong_FromLong(_PyHASH_IMAG));
1455-
PyStructSequence_SET_ITEM(hash_info, field++,
1456-
PyUnicode_FromString(hashfunc->name));
1457-
PyStructSequence_SET_ITEM(hash_info, field++,
1458-
PyLong_FromLong(hashfunc->hash_bits));
1459-
PyStructSequence_SET_ITEM(hash_info, field++,
1460-
PyLong_FromLong(hashfunc->seed_bits));
1461-
PyStructSequence_SET_ITEM(hash_info, field++,
1462-
PyLong_FromLong(Py_HASH_CUTOFF));
1463-
if (_PyErr_Occurred(tstate)) {
1464-
Py_CLEAR(hash_info);
1442+
if (hash_info == NULL) {
14651443
return NULL;
14661444
}
1445+
hashfunc = PyHash_GetFuncDef();
1446+
1447+
#define SET_HASH_INFO_ITEM(CALL) \
1448+
do { \
1449+
PyObject *item = (CALL); \
1450+
if (item == NULL) { \
1451+
Py_CLEAR(hash_info); \
1452+
return NULL; \
1453+
} \
1454+
PyStructSequence_SET_ITEM(hash_info, field++, item); \
1455+
} while(0)
1456+
1457+
SET_HASH_INFO_ITEM(PyLong_FromLong(8 * sizeof(Py_hash_t)));
1458+
SET_HASH_INFO_ITEM(PyLong_FromSsize_t(_PyHASH_MODULUS));
1459+
SET_HASH_INFO_ITEM(PyLong_FromLong(_PyHASH_INF));
1460+
SET_HASH_INFO_ITEM(PyLong_FromLong(0)); // This is no longer used
1461+
SET_HASH_INFO_ITEM(PyLong_FromLong(_PyHASH_IMAG));
1462+
SET_HASH_INFO_ITEM(PyUnicode_FromString(hashfunc->name));
1463+
SET_HASH_INFO_ITEM(PyLong_FromLong(hashfunc->hash_bits));
1464+
SET_HASH_INFO_ITEM(PyLong_FromLong(hashfunc->seed_bits));
1465+
SET_HASH_INFO_ITEM(PyLong_FromLong(Py_HASH_CUTOFF));
1466+
1467+
#undef SET_HASH_INFO_ITEM
1468+
14671469
return hash_info;
14681470
}
14691471
/*[clinic input]

0 commit comments

Comments
 (0)