@@ -195,7 +195,7 @@ PyArray_Ravel(PyArrayObject *a, int fortran)
195195 return PyArray_Flatten (a , fortran );
196196}
197197
198- double
198+ static double
199199power_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 ;
0 commit comments