@@ -895,7 +895,7 @@ PyArray_Clip(PyArrayObject *self, PyObject *min, PyObject *max, PyArrayObject *o
895
895
PyArrayObject * maxa = NULL ;
896
896
PyArrayObject * mina = NULL ;
897
897
PyArrayObject * newout = NULL , * newin = NULL ;
898
- PyArray_Descr * indescr , * newdescr ;
898
+ PyArray_Descr * indescr = NULL , * newdescr = NULL ;
899
899
char * max_data , * min_data ;
900
900
PyObject * zero ;
901
901
@@ -922,23 +922,24 @@ PyArray_Clip(PyArrayObject *self, PyObject *min, PyObject *max, PyArrayObject *o
922
922
/* Use the fast scalar clip function */
923
923
924
924
/* First we need to figure out the correct type */
925
- indescr = NULL ;
926
925
if (min != NULL ) {
927
926
indescr = PyArray_DescrFromObject (min , NULL );
928
927
if (indescr == NULL ) {
929
- return NULL ;
928
+ goto fail ;
930
929
}
931
930
}
932
931
if (max != NULL ) {
933
932
newdescr = PyArray_DescrFromObject (max , indescr );
934
933
Py_XDECREF (indescr );
934
+ indescr = NULL ;
935
935
if (newdescr == NULL ) {
936
- return NULL ;
936
+ goto fail ;
937
937
}
938
938
}
939
939
else {
940
940
/* Steal the reference */
941
941
newdescr = indescr ;
942
+ indescr = NULL ;
942
943
}
943
944
944
945
@@ -950,8 +951,12 @@ PyArray_Clip(PyArrayObject *self, PyObject *min, PyObject *max, PyArrayObject *o
950
951
if (PyArray_ScalarKind (newdescr -> type_num , NULL ) >
951
952
PyArray_ScalarKind (PyArray_DESCR (self )-> type_num , NULL )) {
952
953
indescr = PyArray_PromoteTypes (newdescr , PyArray_DESCR (self ));
954
+ if (indescr == NULL ) {
955
+ goto fail ;
956
+ }
953
957
func = indescr -> f -> fastclip ;
954
958
if (func == NULL ) {
959
+ Py_DECREF (indescr );
955
960
return _slow_array_clip (self , min , max , out );
956
961
}
957
962
}
@@ -960,11 +965,13 @@ PyArray_Clip(PyArrayObject *self, PyObject *min, PyObject *max, PyArrayObject *o
960
965
Py_INCREF (indescr );
961
966
}
962
967
Py_DECREF (newdescr );
968
+ newdescr = NULL ;
963
969
964
970
if (!PyDataType_ISNOTSWAPPED (indescr )) {
965
971
PyArray_Descr * descr2 ;
966
972
descr2 = PyArray_DescrNewByteorder (indescr , '=' );
967
973
Py_DECREF (indescr );
974
+ indescr = NULL ;
968
975
if (descr2 == NULL ) {
969
976
goto fail ;
970
977
}
@@ -973,16 +980,13 @@ PyArray_Clip(PyArrayObject *self, PyObject *min, PyObject *max, PyArrayObject *o
973
980
974
981
/* Convert max to an array */
975
982
if (max != NULL ) {
983
+ Py_INCREF (indescr );
976
984
maxa = (PyArrayObject * )PyArray_FromAny (max , indescr , 0 , 0 ,
977
985
NPY_ARRAY_DEFAULT , NULL );
978
986
if (maxa == NULL ) {
979
- return NULL ;
987
+ goto fail ;
980
988
}
981
989
}
982
- else {
983
- /* Side-effect of PyArray_FromAny */
984
- Py_DECREF (indescr );
985
- }
986
990
987
991
/*
988
992
* If we are unsigned, then make sure min is not < 0
@@ -1147,6 +1151,8 @@ PyArray_Clip(PyArrayObject *self, PyObject *min, PyObject *max, PyArrayObject *o
1147
1151
func (PyArray_DATA (newin ), PyArray_SIZE (newin ), min_data , max_data , PyArray_DATA (newout ));
1148
1152
1149
1153
/* Clean up temporary variables */
1154
+ Py_XDECREF (indescr );
1155
+ Py_XDECREF (newdescr );
1150
1156
Py_XDECREF (mina );
1151
1157
Py_XDECREF (maxa );
1152
1158
Py_DECREF (newin );
@@ -1155,6 +1161,8 @@ PyArray_Clip(PyArrayObject *self, PyObject *min, PyObject *max, PyArrayObject *o
1155
1161
return (PyObject * )out ;
1156
1162
1157
1163
fail :
1164
+ Py_XDECREF (indescr );
1165
+ Py_XDECREF (newdescr );
1158
1166
Py_XDECREF (maxa );
1159
1167
Py_XDECREF (mina );
1160
1168
Py_XDECREF (newin );
0 commit comments