[3.11] gh-116404: Handle errors correctly in wait_helper in posixmodule (GH-116405) (#116407)

gh-116404: Handle errors correctly in `wait_helper` in `posixmodule` (GH-116405)
(cherry picked from commit 22ccf13b332902142fe0c52c593f9efc152c7761)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
This commit is contained in:
Miss Islington (bot) 2024-03-06 10:08:35 +01:00 committed by GitHub
parent 23c17f3c1a
commit d69bef6080
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -8392,36 +8392,39 @@ wait_helper(PyObject *module, pid_t pid, int status, struct rusage *ru)
if (!result)
return NULL;
int pos = 0;
#ifndef doubletime
#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
#endif
PyStructSequence_SET_ITEM(result, 0,
PyFloat_FromDouble(doubletime(ru->ru_utime)));
PyStructSequence_SET_ITEM(result, 1,
PyFloat_FromDouble(doubletime(ru->ru_stime)));
#define SET_INT(result, index, value)\
PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
SET_INT(result, 2, ru->ru_maxrss);
SET_INT(result, 3, ru->ru_ixrss);
SET_INT(result, 4, ru->ru_idrss);
SET_INT(result, 5, ru->ru_isrss);
SET_INT(result, 6, ru->ru_minflt);
SET_INT(result, 7, ru->ru_majflt);
SET_INT(result, 8, ru->ru_nswap);
SET_INT(result, 9, ru->ru_inblock);
SET_INT(result, 10, ru->ru_oublock);
SET_INT(result, 11, ru->ru_msgsnd);
SET_INT(result, 12, ru->ru_msgrcv);
SET_INT(result, 13, ru->ru_nsignals);
SET_INT(result, 14, ru->ru_nvcsw);
SET_INT(result, 15, ru->ru_nivcsw);
#undef SET_INT
#define SET_RESULT(CALL) \
do { \
PyObject *item = (CALL); \
if (item == NULL) { \
Py_DECREF(result); \
return NULL; \
} \
PyStructSequence_SET_ITEM(result, pos++, item); \
} while(0)
if (PyErr_Occurred()) {
Py_DECREF(result);
return NULL;
}
SET_RESULT(PyFloat_FromDouble(doubletime(ru->ru_utime)));
SET_RESULT(PyFloat_FromDouble(doubletime(ru->ru_stime)));
SET_RESULT(PyLong_FromLong(ru->ru_maxrss));
SET_RESULT(PyLong_FromLong(ru->ru_ixrss));
SET_RESULT(PyLong_FromLong(ru->ru_idrss));
SET_RESULT(PyLong_FromLong(ru->ru_isrss));
SET_RESULT(PyLong_FromLong(ru->ru_minflt));
SET_RESULT(PyLong_FromLong(ru->ru_majflt));
SET_RESULT(PyLong_FromLong(ru->ru_nswap));
SET_RESULT(PyLong_FromLong(ru->ru_inblock));
SET_RESULT(PyLong_FromLong(ru->ru_oublock));
SET_RESULT(PyLong_FromLong(ru->ru_msgsnd));
SET_RESULT(PyLong_FromLong(ru->ru_msgrcv));
SET_RESULT(PyLong_FromLong(ru->ru_nsignals));
SET_RESULT(PyLong_FromLong(ru->ru_nvcsw));
SET_RESULT(PyLong_FromLong(ru->ru_nivcsw));
#undef SET_RESULT
return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
}