8000 address review: check for failures in test code before PyTuple_Pack'ing · python/cpython@f73c907 · GitHub
[go: up one dir, main page]

Skip to content

Commit f73c907

Browse files
committed
address review: check for failures in test code before PyTuple_Pack'ing
1 parent 2f63950 commit f73c907

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

Modules/_testcapi/complex.c

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,19 +104,27 @@ _py_c_neg(PyObject *Py_UNUSED(module), PyObject *num)
104104
static PyObject * \
105105
_py_c_##suffix(PyObject *Py_UNUSED(module), PyObject *args) \
106106
{ \
107-
Py_complex num, exp, res; \
107+
Py_complex num, exp; \
108+
PyObject *res, *err; \
108109
\
109110
if (!PyArg_ParseTuple(args, "DD", &num, &exp)) { \
110111
return NULL; \
111112
} \
112113
\
113114
errno = 0; \
114-
res = _Py_c_##suffix(num, exp); \
115-
_Py_ADJUST_ERANGE2(res.real, res.imag); \
115+
num = _Py_c_##suffix(num, exp); \
116+
_Py_ADJUST_ERANGE2(num.real, num.imag); \
116117
\
117-
return PyTuple_Pack(2, \
118-
PyComplex_FromCComplex(res), \
119-
PyLong_FromLong(errno)); \
118+
res = PyComplex_FromCComplex(num); \
119+
if (!res) { \
120+
return NULL; \
121+
} \
122+
err = PyLong_FromLong(errno); \
123+
if (!err) { \
124+
return NULL; \
125+
} \
126+
\
127+
return PyTuple_Pack(2, res, err); \
120128
};
121129

122130
_PY_C_FUNC2(sum)
@@ -129,7 +137,8 @@ static PyObject*
129137
_py_c_abs(PyObject *Py_UNUSED(module), PyObject* obj)
130138
{
131139
Py_complex complex;
132-
double res;
140+
PyObject *res, *err;
141+
double val;
133142

134143
NULLABLE(obj);
135144
complex = PyComplex_AsCComplex(obj);
@@ -139,9 +148,18 @@ _py_c_abs(PyObject *Py_UNUSED(module), PyObject* obj)
139148
}
140149

141150
errno = 0;
142-
res = _Py_c_abs(complex);
143-
return PyTuple_Pack(2, PyFloat_FromDouble(res),
144-
PyLong_FromLong(errno));
151+
val = _Py_c_abs(complex);
152+
153+
res = PyFloat_FromDouble(val);
154+
if (!res) {
155+
return NULL;
156+
}
157+
err = PyLong_FromLong(errno);
158+
if (!err) {
159+
return NULL;
160+
}
161+
162+
return PyTuple_Pack(2, res, err);
145163
}
146164

147165

0 commit comments

Comments
 (0)
0