Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f909e98

Browse files
committed
Add round method to array scalars and fix a few reference count issues.
1 parent adc2173 commit f909e98

3 files changed

Lines changed: 23 additions & 10 deletions

File tree

THANKS.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ Travis Vaught and Joe Cooper for administration of numpy.org web site and SVN
1414
Eric Firing for bugfixes.
1515
Arnd Baecker for 64-bit testing
1616
David Cooke for many code improvements including the auto-generated C-API
17-
Alexander Belopolsky (sasha) for Masked array bug-fixes and tests and rank-0 array improvements
17+
Alexander Belopolsky (Sasha) for Masked array bug-fixes and tests and rank-0 array improvements and other code additions
1818
Francesc Altet for unicode and nested record tests and help with nested records
1919
Tim Hochberg for getting the build working on MSVC

numpy/core/src/multiarraymodule.c

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ PyArray_Ravel(PyArrayObject *a, int fortran)
195195
return PyArray_Flatten(a, fortran);
196196
}
197197

198-
double
198+
static double
199199
power_of_ten(int n)
200200
{
201201
static const double p10[] = {1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8};
@@ -218,46 +218,57 @@ PyArray_Round(PyArrayObject *a, int decimals)
218218
{
219219
/* do the most common case first */
220220
if (decimals == 0) {
221-
if (PyTypeNum_ISINTEGER(PyArray_TYPE(a))) {
221+
if (PyArray_ISINTEGER(a)) {
222222
Py_INCREF(a);
223223
return (PyObject *)a;
224224
}
225225
return PyArray_GenericUnaryFunction((PyAO *)a, n_ops.rint);
226226
}
227227
if (decimals > 0) {
228228
PyObject *f, *ret;
229-
if (PyTypeNum_ISINTEGER(PyArray_TYPE(a))) {
229+
if (PyArray_ISINTEGER(a)) {
230230
Py_INCREF(a);
231231
return (PyObject *)a;
232232
}
233233
f = PyFloat_FromDouble(power_of_ten(decimals));
234+
if (f==NULL) return NULL;
234235
ret = PyNumber_Multiply((PyObject *)a, f);
236+
if (ret==NULL) {Py_DECREF(f); return NULL;}
235237
if (PyArray_IsScalar(ret, Generic)) {
236238
/* array scalars cannot be modified inplace */
237239
PyObject *tmp;
238240
tmp = PyObject_CallFunction(n_ops.rint, "O", ret);
239241
Py_DECREF(ret);
240-
ret = PyObject_CallFunction(n_ops.divide, "OO", tmp, f);
242+
ret = PyObject_CallFunction(n_ops.divide, "OO",
243+
tmp, f);
244+
Py_DECREF(tmp);
241245
} else {
242246
PyObject_CallFunction(n_ops.rint, "OO", ret, ret);
243-
PyObject_CallFunction(n_ops.divide, "OOO", ret, f, ret);
247+
PyObject_CallFunction(n_ops.divide, "OOO", ret,
248+
f, ret);
244249
}
245250
Py_DECREF(f);
246251
return ret;
247-
} else {
252+
}
253+
else {
248254
/* remaining case: decimals < 0 */
249255
PyObject *f, *ret;
250256
f = PyFloat_FromDouble(power_of_ten(-decimals));
257+
if (f==NULL) return NULL;
251258
ret = PyNumber_Divide((PyObject *)a, f);
259+
if (ret==NULL) {Py_DECREF(f); return NULL;}
252260
if (PyArray_IsScalar(ret, Generic)) {
253261
/* array scalars cannot be modified inplace */
254262
PyObject *tmp;
255263
tmp = PyObject_CallFunction(n_ops.rint, "O", ret);
256264
Py_DECREF(ret);
257-
ret = PyObject_CallFunction(n_ops.multiply, "OO", tmp, f);
265+
ret = PyObject_CallFunction(n_ops.multiply, "OO",
266+
tmp, f);
267+
Py_DECREF(tmp);
258268
} else {
259269
PyObject_CallFunction(n_ops.rint, "OO", ret, ret);
260-
PyObject_CallFunction(n_ops.multiply, "OOO", ret, f, ret);
270+
PyObject_CallFunction(n_ops.multiply, "OOO", ret,
271+
f, ret);
261272
}
262273
Py_DECREF(f);
263274
return ret;

numpy/core/src/scalartypes.inc.src

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1124,7 +1124,7 @@ gentype_byteswap(PyObject *self, PyObject *args)
11241124

11251125
/**begin repeat
11261126
1127-
#name=take, getfield, put, putmask, repeat, tofile, mean, trace, diagonal, clip, std, var, sum, cumsum, prod, cumprod, compress, sort, argsort#
1127+
#name=take, getfield, put, putmask, repeat, tofile, mean, trace, diagonal, clip, std, var, sum, cumsum, prod, cumprod, compress, sort, argsort, round#
11281128
*/
11291129

11301130
static PyObject *
@@ -1391,6 +1391,8 @@ static PyMethodDef gentype_methods[] = {
13911391
METH_VARARGS, NULL},
13921392
{"ravel", (PyCFunction)gentype_ravel,
13931393
METH_VARARGS, NULL},
1394+
{"round", (PyCFunction)gentype_round,
1395+
METH_VARARGS|METH_KEYWORDS, NULL},
13941396
{"setflags", (PyCFunction)gentype_setflags,
13951397
METH_VARARGS|METH_KEYWORDS, NULL},
13961398
{"newbyteorder", (PyCFunction)gentype_newbyteorder,

0 commit comments

Comments
 (0)