Skip to content

Commit c7628b0

Browse files
[3.13] gh-118998: Handle errors correctly in tmtotuple in timemodule (GH-118999) (#119018)
gh-118998: Handle errors correctly in `tmtotuple` in `timemodule` (GH-118999) (cherry picked from commit fc75792) Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
1 parent d1aac22 commit c7628b0

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

Modules/timemodule.c

+17-10
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,18 @@ tmtotuple(time_module_state *state, struct tm *p
462462
if (v == NULL)
463463
return NULL;
464464

465-
#define SET(i,val) PyStructSequence_SET_ITEM(v, i, PyLong_FromLong((long) val))
465+
#define SET_ITEM(INDEX, CALL) \
466+
do { \
467+
PyObject *obj = (CALL); \
468+
if (obj == NULL) { \
469+
Py_DECREF(v); \
470+
return NULL; \
471+
} \
472+
PyStructSequence_SET_ITEM(v, (INDEX), obj); \
473+
} while (0)
474+
475+
#define SET(INDEX, VAL) \
476+
SET_ITEM((INDEX), PyLong_FromLong((long) (VAL)))
466477

467478
SET(0, p->tm_year + 1900);
468479
SET(1, p->tm_mon + 1); /* Want January == 1 */
@@ -474,19 +485,15 @@ tmtotuple(time_module_state *state, struct tm *p
474485
SET(7, p->tm_yday + 1); /* Want January, 1 == 1 */
475486
SET(8, p->tm_isdst);
476487
#ifdef HAVE_STRUCT_TM_TM_ZONE
477-
PyStructSequence_SET_ITEM(v, 9,
478-
PyUnicode_DecodeLocale(p->tm_zone, "surrogateescape"));
488+
SET_ITEM(9, PyUnicode_DecodeLocale(p->tm_zone, "surrogateescape"));
479489
SET(10, p->tm_gmtoff);
480490
#else
481-
PyStructSequence_SET_ITEM(v, 9,
482-
PyUnicode_DecodeLocale(zone, "surrogateescape"));
483-
PyStructSequence_SET_ITEM(v, 10, _PyLong_FromTime_t(gmtoff));
491+
SET_ITEM(9, PyUnicode_DecodeLocale(zone, "surrogateescape"));
492+
SET_ITEM(10, _PyLong_FromTime_t(gmtoff));
484493
#endif /* HAVE_STRUCT_TM_TM_ZONE */
494+
485495
#undef SET
486-
if (PyErr_Occurred()) {
487-
Py_XDECREF(v);
488-
return NULL;
489-
}
496+
#undef SET_ITEM
490497

491498
return v;
492499
}

0 commit comments

Comments
 (0)