Skip to content

Commit 8ee7e91

Browse files
[3.11] gh-115320: Refactor get_hash_info in sysmodule.c not to swallow errors (GH-115321) (#116324)
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 e559528 commit 8ee7e91

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
@@ -1363,31 +1363,33 @@ get_hash_info(PyThreadState *tstate)
13631363
int field = 0;
13641364
PyHash_FuncDef *hashfunc;
13651365
hash_info = PyStructSequence_New(&Hash_InfoType);
1366-
if (hash_info == NULL)
1367-
return NULL;
1368-
hashfunc = PyHash_GetFuncDef();
1369-
PyStructSequence_SET_ITEM(hash_info, field++,
1370-
PyLong_FromLong(8*sizeof(Py_hash_t)));
1371-
PyStructSequence_SET_ITEM(hash_info, field++,
1372-
PyLong_FromSsize_t(_PyHASH_MODULUS));
1373-
PyStructSequence_SET_ITEM(hash_info, field++,
1374-
PyLong_FromLong(_PyHASH_INF));
1375-
PyStructSequence_SET_ITEM(hash_info, field++,
1376-
PyLong_FromLong(0)); // This is no longer used
1377-
PyStructSequence_SET_ITEM(hash_info, field++,
1378-
PyLong_FromLong(_PyHASH_IMAG));
1379-
PyStructSequence_SET_ITEM(hash_info, field++,
1380-
PyUnicode_FromString(hashfunc->name));
1381-
PyStructSequence_SET_ITEM(hash_info, field++,
1382-
PyLong_FromLong(hashfunc->hash_bits));
1383-
PyStructSequence_SET_ITEM(hash_info, field++,
1384-
PyLong_FromLong(hashfunc->seed_bits));
1385-
PyStructSequence_SET_ITEM(hash_info, field++,
1386-
PyLong_FromLong(Py_HASH_CUTOFF));
1387-
if (_PyErr_Occurred(tstate)) {
1388-
Py_CLEAR(hash_info);
1366+
if (hash_info == NULL) {
13891367
return NULL;
13901368
}
1369+
hashfunc = PyHash_GetFuncDef();
1370+
1371+
#define SET_HASH_INFO_ITEM(CALL) \
1372+
do { \
1373+
PyObject *item = (CALL); \
1374+
if (item == NULL) { \
1375+
Py_CLEAR(hash_info); \
1376+
return NULL; \
1377+
} \
1378+
PyStructSequence_SET_ITEM(hash_info, field++, item); \
1379+
} while(0)
1380+
1381+
SET_HASH_INFO_ITEM(PyLong_FromLong(8 * sizeof(Py_hash_t)));
1382+
SET_HASH_INFO_ITEM(PyLong_FromSsize_t(_PyHASH_MODULUS));
1383+
SET_HASH_INFO_ITEM(PyLong_FromLong(_PyHASH_INF));
1384+
SET_HASH_INFO_ITEM(PyLong_FromLong(0)); // This is no longer used
1385+
SET_HASH_INFO_ITEM(PyLong_FromLong(_PyHASH_IMAG));
1386+
SET_HASH_INFO_ITEM(PyUnicode_FromString(hashfunc->name));
1387+
SET_HASH_INFO_ITEM(PyLong_FromLong(hashfunc->hash_bits));
1388+
SET_HASH_INFO_ITEM(PyLong_FromLong(hashfunc->seed_bits));
1389+
SET_HASH_INFO_ITEM(PyLong_FromLong(Py_HASH_CUTOFF));
1390+
1391+
#undef SET_HASH_INFO_ITEM
1392+
13911393
return hash_info;
13921394
}
13931395
/*[clinic input]

0 commit comments

Comments
 (0)