Skip to content

Commit

Permalink
Add error checking to Object loops.
Browse files Browse the repository at this point in the history
Fix reference leak in Sign Object loop.
Define a binary version of PyNumber_Power so that the generic
object loop doesn't have to check for that function.
Clean up generic object loops.
  • Loading branch information
charris committed Nov 16, 2008
1 parent 357672c commit 6132a51
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
22 changes: 3 additions & 19 deletions numpy/core/src/ufuncobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -268,12 +268,7 @@ PyUFunc_O_O(char **args, intp *dimensions, intp *steps, void *func)
UNARY_LOOP {
PyObject *in1 = *(PyObject **)ip1;
PyObject **out = (PyObject **)op1;
PyObject *ret;

if (in1 == NULL) {
return;
}
ret = f(in1);
PyObject *ret = f(in1);
if ((ret == NULL) || PyErr_Occurred()) {
return;
}
Expand All @@ -291,7 +286,6 @@ PyUFunc_O_O_method(char **args, intp *dimensions, intp *steps, void *func)
PyObject *in1 = *(PyObject **)ip1;
PyObject **out = (PyObject **)op1;
PyObject *ret = PyObject_CallMethod(in1, meth, NULL);

if (ret == NULL) {
return;
}
Expand All @@ -304,21 +298,12 @@ PyUFunc_O_O_method(char **args, intp *dimensions, intp *steps, void *func)
static void
PyUFunc_OO_O(char **args, intp *dimensions, intp *steps, void *func)
{
binaryfunc f = (binaryfunc)func;
BINARY_LOOP {
PyObject *in1 = *(PyObject **)ip1;
PyObject *in2 = *(PyObject **)ip2;
PyObject **out = (PyObject **)op1;
PyObject *ret;

if ((in1 == NULL) || (in2 == NULL)) {
return;
}
if ( (void *) func == (void *) PyNumber_Power) {
ret = ((ternaryfunc)func)(in1, in2, Py_None);
}
else {
ret = ((binaryfunc)func)(in1, in2);
}
PyObject *ret = f(in1, in2);
if (PyErr_Occurred()) {
return;
}
Expand All @@ -337,7 +322,6 @@ PyUFunc_OO_O_method(char **args, intp *dimensions, intp *steps, void *func)
PyObject *in2 = *(PyObject **)ip2;
PyObject **out = (PyObject **)op1;
PyObject *ret = PyObject_CallMethod(in1, meth, "(O)", in2);

if (ret == NULL) {
return;
}
Expand Down
24 changes: 22 additions & 2 deletions numpy/core/src/umathmodule.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ Py_reciprocal(PyObject *o)
return result;
}

/*
* Define numpy version of PyNumber_Power as binary function.
*/
static PyObject *
npy_PyNumber_Power(PyObject *x, PyObject *y)
{
PyNumber_Power(x, y, Py_None);
}
#define PyNumber_Power npy_PyNumber_Power

/**begin repeat
* #Kind = Max, Min#
* #OP = >=, <=#
Expand Down Expand Up @@ -1520,7 +1530,11 @@ OBJECT_@kind@(char **args, intp *dimensions, intp *steps, void *NPY_UNUSED(func)
BINARY_LOOP {
PyObject *in1 = *(PyObject **)ip1;
PyObject *in2 = *(PyObject **)ip2;
*((Bool *)op1) = (Bool) PyObject_RichCompareBool(in1, in2, Py_@OP@);
int ret = PyObject_RichCompareBool(in1, in2, Py_@OP@);
if (ret == -1) {
return;
}
*((Bool *)op1) = (Bool)ret;
}
}
/**end repeat**/
Expand All @@ -1531,7 +1545,13 @@ OBJECT_sign(char **args, intp *dimensions, intp *steps, void *NPY_UNUSED(func))
PyObject *zero = PyInt_FromLong(0);
UNARY_LOOP {
PyObject *in1 = *(PyObject **)ip1;
*((PyObject **)op1) = PyInt_FromLong(PyObject_Compare(in1, zero));
PyObject **out = (PyObject **)op1;
PyObject *ret = PyInt_FromLong(PyObject_Compare(in1, zero));
if (PyErr_Occurred()) {
return;
}
Py_XDECREF(*out);
*out = ret;
}
Py_DECREF(zero);
}
Expand Down

0 comments on commit 6132a51

Please sign in to comment.