Skip to content

Commit 207030f

Browse files
authored
gh-115320: Refactor get_hash_info in sysmodule.c not to swallow errors (#115321)
1 parent 01440d3 commit 207030f

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
@@ -1498,31 +1498,33 @@ get_hash_info(PyThreadState *tstate)
14981498
int field = 0;
14991499
PyHash_FuncDef *hashfunc;
15001500
hash_info = PyStructSequence_New(&Hash_InfoType);
1501-
if (hash_info == NULL)
1502-
return NULL;
1503-
hashfunc = PyHash_GetFuncDef();
1504-
PyStructSequence_SET_ITEM(hash_info, field++,
1505-
PyLong_FromLong(8*sizeof(Py_hash_t)));
1506-
PyStructSequence_SET_ITEM(hash_info, field++,
1507-
PyLong_FromSsize_t(_PyHASH_MODULUS));
1508-
PyStructSequence_SET_ITEM(hash_info, field++,
1509-
PyLong_FromLong(_PyHASH_INF));
1510-
PyStructSequence_SET_ITEM(hash_info, field++,
1511-
PyLong_FromLong(0)); // This is no longer used
1512-
PyStructSequence_SET_ITEM(hash_info, field++,
1513-
PyLong_FromLong(_PyHASH_IMAG));
1514-
PyStructSequence_SET_ITEM(hash_info, field++,
1515-
PyUnicode_FromString(hashfunc->name));
1516-
PyStructSequence_SET_ITEM(hash_info, field++,
1517-
PyLong_FromLong(hashfunc->hash_bits));
1518-
PyStructSequence_SET_ITEM(hash_info, field++,
1519-
PyLong_FromLong(hashfunc->seed_bits));
1520-
PyStructSequence_SET_ITEM(hash_info, field++,
1521-
PyLong_FromLong(Py_HASH_CUTOFF));
1522-
if (_PyErr_Occurred(tstate)) {
1523-
Py_CLEAR(hash_info);
1501+
if (hash_info == NULL) {
15241502
return NULL;
15251503
}
1504+
hashfunc = PyHash_GetFuncDef();
1505+
1506+
#define SET_HASH_INFO_ITEM(CALL) \
1507+
do { \
1508+
PyObject *item = (CALL); \
1509+
if (item == NULL) { \
1510+
Py_CLEAR(hash_info); \
1511+
return NULL; \
1512+
} \
1513+
PyStructSequence_SET_ITEM(hash_info, field++, item); \
1514+
} while(0)
1515+
1516+
SET_HASH_INFO_ITEM(PyLong_FromLong(8 * sizeof(Py_hash_t)));
1517+
SET_HASH_INFO_ITEM(PyLong_FromSsize_t(_PyHASH_MODULUS));
1518+
SET_HASH_INFO_ITEM(PyLong_FromLong(_PyHASH_INF));
1519+
SET_HASH_INFO_ITEM(PyLong_FromLong(0)); // This is no longer used
1520+
SET_HASH_INFO_ITEM(PyLong_FromLong(_PyHASH_IMAG));
1521+
SET_HASH_INFO_ITEM(PyUnicode_FromString(hashfunc->name));
1522+
SET_HASH_INFO_ITEM(PyLong_FromLong(hashfunc->hash_bits));
1523+
SET_HASH_INFO_ITEM(PyLong_FromLong(hashfunc->seed_bits));
1524+
SET_HASH_INFO_ITEM(PyLong_FromLong(Py_HASH_CUTOFF));
1525+
1526+
#undef SET_HASH_INFO_ITEM
1527+
15261528
return hash_info;
15271529
}
15281530
/*[clinic input]

0 commit comments

Comments
 (0)