@@ -892,7 +892,8 @@ discover_dimensions(PyObject *obj, int *maxndim, npy_intp *d, int check_it,
892
892
* Generic new array creation routine.
893
893
* Internal variant with calloc argument for PyArray_Zeros.
894
894
*
895
- * steals a reference to descr (even on failure)
895
+ * steals a reference to descr. On failure or descr->subarray, descr will
896
+ * be decrefed.
896
897
*/
897
898
NPY_NO_EXPORT PyObject *
898
899
PyArray_NewFromDescr_int (PyTypeObject * subtype , PyArray_Descr * descr , int nd ,
@@ -1128,7 +1129,8 @@ PyArray_NewFromDescr_int(PyTypeObject *subtype, PyArray_Descr *descr, int nd,
1128
1129
/*NUMPY_API
1129
1130
* Generic new array creation routine.
1130
1131
*
1131
- * steals a reference to descr (even on failure)
1132
+ * steals a reference to descr. On failure or when dtype->subarray is
1133
+ * true, dtype will be decrefed.
1132
1134
*/
1133
1135
NPY_NO_EXPORT PyObject *
1134
1136
PyArray_NewFromDescr (PyTypeObject * subtype , PyArray_Descr * descr , int nd ,
@@ -1153,7 +1155,8 @@ PyArray_NewFromDescr(PyTypeObject *subtype, PyArray_Descr *descr, int nd,
1153
1155
* subok - If 1, use the prototype's array subtype, otherwise
1154
1156
* always create a base-class array.
1155
1157
*
1156
- * NOTE: If dtype is not NULL, steals the dtype reference.
1158
+ * NOTE: If dtype is not NULL, steals the dtype reference. On failure or when
1159
+ * dtype->subarray is true, dtype will be decrefed.
1157
1160
*/
1158
1161
NPY_NO_EXPORT PyObject *
1159
1162
PyArray_NewLikeArray (PyArrayObject * prototype , NPY_ORDER order ,
@@ -2842,7 +2845,8 @@ PyArray_CheckAxis(PyArrayObject *arr, int *axis, int flags)
2842
2845
/*NUMPY_API
2843
2846
* Zeros
2844
2847
*
2845
- * steal a reference
2848
+ * steals a reference to type. On failure or when dtype->subarray is
2849
+ * true, dtype will be decrefed.
2846
2850
* accepts NULL type
2847
2851
*/
2848
2852
NPY_NO_EXPORT PyObject *
@@ -3260,19 +3264,20 @@ array_fromfile_binary(FILE *fp, PyArray_Descr *dtype, npy_intp num, size_t *nrea
3260
3264
}
3261
3265
num = numbytes / dtype -> elsize ;
3262
3266
}
3267
+ /*
3268
+ * When dtype->subarray is true, PyArray_NewFromDescr will decref dtype
3269
+ * even on success, so make sure it stays around until exit.
3270
+ */
3263
3271
Py_INCREF (dtype );
3264
- r = (PyArrayObject * )PyArray_NewFromDescr (& PyArray_Type ,
3265
- dtype ,
3266
- 1 , & num ,
3267
- NULL , NULL ,
3268
- 0 , NULL );
3272
+ r = (PyArrayObject * )PyArray_NewFromDescr (& PyArray_Type , dtype , 1 , & num ,
3273
+ NULL , NULL , 0 , NULL );
3269
3274
if (r == NULL ) {
3270
- goto fail ;
3275
+ Py_DECREF (dtype );
3276
+ return NULL ;
3271
3277
}
3272
3278
NPY_BEGIN_ALLOW_THREADS ;
3273
3279
* nread = fread (PyArray_DATA (r ), dtype -> elsize , num , fp );
3274
3280
NPY_END_ALLOW_THREADS ;
3275
- fail :
3276
3281
Py_DECREF (dtype );
3277
3282
return r ;
3278
3283
}
@@ -3297,13 +3302,14 @@ array_from_text(PyArray_Descr *dtype, npy_intp num, char *sep, size_t *nread,
3297
3302
3298
3303
size = (num >= 0 ) ? num : FROM_BUFFER_SIZE ;
3299
3304
3305
+ /*
3306
+ * When dtype->subarray is true, PyArray_NewFromDescr will decref dtype
3307
+ * even on success, so make sure it stays around until exit.
3308
+ */
3300
3309
Py_INCREF (dtype );
3301
3310
r = (PyArrayObject * )
3302
- PyArray_NewFromDescr (& PyArray_Type ,
3303
- dtype ,
3304
- 1 , & size ,
3305
- NULL , NULL ,
3306
- 0 , NULL );
3311
+ PyArray_NewFromDescr (& PyArray_Type , dtype , 1 , & size ,
3312
+ NULL , NULL , 0 , NULL );
3307
3313
if (r == NULL ) {
3308
3314
Py_DECREF (dtype );
3309
3315
return NULL ;
@@ -3372,7 +3378,8 @@ array_from_text(PyArray_Descr *dtype, npy_intp num, char *sep, size_t *nread,
3372
3378
* array corresponding to the data encoded in that file.
3373
3379
*
3374
3380
* If the dtype is NULL, the default array type is used (double).
3375
- * If non-null, the reference is stolen.
3381
+ * If non-null, the reference is stolen and if dtype->subarray is true dtype
3382
+ * will be decrefed even on success.
3376
3383
*
3377
3384
* The number of elements to read is given as ``num``; if it is < 0, then
3378
3385
* then as many as possible are read.
0 commit comments