Skip to content

Commit ee3d5ed

Browse files
[3.14] Fix ignored error returns in sys initialization, _ctypes and _interpchannels (GH-160614)
(cherry picked from commit c5d0978) (cherry picked from commit 65057c5) Co-authored-by: Stan Ulbrych <stan@python.org>
1 parent 6841d98 commit ee3d5ed

3 files changed

Lines changed: 34 additions & 14 deletions

File tree

‎Modules/_ctypes/_ctypes.c‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2497,22 +2497,30 @@ PyCSimpleType_init(PyObject *self, PyObject *args, PyObject *kwds)
24972497
}
24982498
assert(sw_info);
24992499
#ifdef WORDS_BIGENDIAN
2500-
PyObject_SetAttrString(self, "__ctype_le__", swapped);
2501-
PyObject_SetAttrString(self, "__ctype_be__", self);
2502-
PyObject_SetAttrString(swapped, "__ctype_be__", self);
2503-
PyObject_SetAttrString(swapped, "__ctype_le__", swapped);
2500+
if (PyObject_SetAttrString(self, "__ctype_le__", swapped) < 0 ||
2501+
PyObject_SetAttrString(self, "__ctype_be__", self) < 0 ||
2502+
PyObject_SetAttrString(swapped, "__ctype_be__", self) < 0 ||
2503+
PyObject_SetAttrString(swapped, "__ctype_le__", swapped) < 0)
2504+
{
2505+
Py_DECREF(swapped);
2506+
return -1;
2507+
}
25042508
/* We are creating the type for the OTHER endian */
25052509
sw_info->format = _ctypes_alloc_format_string("<", stginfo->format+1);
25062510
#else
2507-
PyObject_SetAttrString(self, "__ctype_be__", swapped);
2508-
PyObject_SetAttrString(self, "__ctype_le__", self);
2509-
PyObject_SetAttrString(swapped, "__ctype_le__", self);
2510-
PyObject_SetAttrString(swapped, "__ctype_be__", swapped);
2511+
if (PyObject_SetAttrString(self, "__ctype_be__", swapped) < 0 ||
2512+
PyObject_SetAttrString(self, "__ctype_le__", self) < 0 ||
2513+
PyObject_SetAttrString(swapped, "__ctype_le__", self) < 0 ||
2514+
PyObject_SetAttrString(swapped, "__ctype_be__", swapped) < 0)
2515+
{
2516+
Py_DECREF(swapped);
2517+
return -1;
2518+
}
25112519
/* We are creating the type for the OTHER endian */
25122520
sw_info->format = _ctypes_alloc_format_string(">", stginfo->format+1);
25132521
#endif
25142522
Py_DECREF(swapped);
2515-
if (PyErr_Occurred()) {
2523+
if (sw_info->format == NULL) {
25162524
return -1;
25172525
}
25182526
};

‎Modules/_interpchannelsmodule.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2638,7 +2638,7 @@ _channelid_shared(PyThreadState *tstate, PyObject *obj, _PyXIData_t *data)
26382638
if (st == NULL) {
26392639
return -1;
26402640
}
2641-
if (!PyObject_IsInstance(obj, (PyObject *)st->ChannelIDType)) {
2641+
if (!PyObject_TypeCheck(obj, st->ChannelIDType)) {
26422642
PyErr_Format(PyExc_TypeError,
26432643
"'_id' attribute must be an %N object, not %T",
26442644
st->ChannelIDType, obj);

‎Python/pylifecycle.c‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2835,8 +2835,14 @@ init_sys_streams(PyThreadState *tstate)
28352835
config->stdio_errors);
28362836
if (std == NULL)
28372837
goto error;
2838-
PySys_SetObject("__stdin__", std);
2839-
_PySys_SetAttr(&_Py_ID(stdin), std);
2838+
if (PySys_SetObject("__stdin__", std) < 0) {
2839+
Py_DECREF(std);
2840+
goto error;
2841+
}
2842+
if (_PySys_SetAttr(&_Py_ID(stdin), std) < 0) {
2843+
Py_DECREF(std);
2844+
goto error;
2845+
}
28402846
Py_DECREF(std);
28412847

28422848
/* Set sys.stdout */
@@ -2846,8 +2852,14 @@ init_sys_streams(PyThreadState *tstate)
28462852
config->stdio_errors);
28472853
if (std == NULL)
28482854
goto error;
2849-
PySys_SetObject("__stdout__", std);
2850-
_PySys_SetAttr(&_Py_ID(stdout), std);
2855+
if (PySys_SetObject("__stdout__", std) < 0) {
2856+
Py_DECREF(std);
2857+
goto error;
2858+
}
2859+
if (_PySys_SetAttr(&_Py_ID(stdout), std) < 0) {
2860+
Py_DECREF(std);
2861+
goto error;
2862+
}
28512863
Py_DECREF(std);
28522864

28532865
#if 1 /* Disable this if you have trouble debugging bootstrap stuff */

0 commit comments

Comments
 (0)