@@ -3574,6 +3574,7 @@ PyArray_ArangeObj(PyObject *start, PyObject *stop, PyObject *step, PyArray_Descr
3574
3574
return NULL ;
3575
3575
}
3576
3576
3577
+ /* This array creation function steals the reference to dtype. */
3577
3578
static PyArrayObject *
3578
3579
array_fromfile_binary (FILE * fp , PyArray_Descr * dtype , npy_intp num , size_t * nread )
3579
3580
{
@@ -3605,27 +3606,24 @@ array_fromfile_binary(FILE *fp, PyArray_Descr *dtype, npy_intp num, size_t *nrea
3605
3606
}
3606
3607
num = numbytes / dtype -> elsize ;
3607
3608
}
3608
- /*
3609
- * When dtype->subarray is true, PyArray_NewFromDescr will decref dtype
3610
- * even on success, so make sure it stays around until exit.
3611
- */
3612
- Py_INCREF (dtype );
3613
3609
r = (PyArrayObject * )PyArray_NewFromDescr (& PyArray_Type , dtype , 1 , & num ,
3614
3610
NULL , NULL , 0 , NULL );
3615
3611
if (r == NULL ) {
3616
- Py_DECREF (dtype );
3617
3612
return NULL ;
3618
3613
}
3614
+ /* In some cases NewFromDescr can replace the dtype, so fetch new one */
3615
+ dtype = PyArray_DESCR (r );
3616
+
3619
3617
NPY_BEGIN_ALLOW_THREADS ;
3620
3618
* nread = fread (PyArray_DATA (r ), dtype -> elsize , num , fp );
3621
3619
NPY_END_ALLOW_THREADS ;
3622
- Py_DECREF (dtype );
3623
3620
return r ;
3624
3621
}
3625
3622
3626
3623
/*
3627
3624
* Create an array by reading from the given stream, using the passed
3628
3625
* next_element and skip_separator functions.
3626
+ * As typical for array creation functions, it steals the reference to dtype.
3629
3627
*/
3630
3628
#define FROM_BUFFER_SIZE 4096
3631
3629
static PyArrayObject *
@@ -3644,18 +3642,15 @@ array_from_text(PyArray_Descr *dtype, npy_intp num, char *sep, size_t *nread,
3644
3642
3645
3643
size = (num >= 0 ) ? num : FROM_BUFFER_SIZE ;
3646
3644
3647
- /*
3648
- * When dtype->subarray is true, PyArray_NewFromDescr will decref dtype
3649
- * even on success, so make sure it stays around until exit.
3650
- */
3651
- Py_INCREF (dtype );
3652
3645
r = (PyArrayObject * )
3653
3646
PyArray_NewFromDescr (& PyArray_Type , dtype , 1 , & size ,
3654
3647
NULL , NULL , 0 , NULL );
3655
3648
if (r == NULL ) {
3656
- Py_DECREF (dtype );
3657
3649
return NULL ;
3658
3650
}
3651
+ /* In some cases NewFromDescr can replace the dtype, so fetch new one */
3652
+ dtype = PyArray_DESCR (r );
3653
+
3659
3654
clean_sep = swab_separator (sep );
3660
3655
if (clean_sep == NULL ) {
3661
3656
err = 1 ;
@@ -3726,7 +3721,6 @@ array_from_text(PyArray_Descr *dtype, npy_intp num, char *sep, size_t *nread,
3726
3721
free (clean_sep );
3727
3722
3728
3723
fail :
3729
- Py_DECREF (dtype );
3730
3724
if (err == 1 ) {
3731
3725
PyErr_NoMemory ();
3732
3726
}
@@ -3743,9 +3737,8 @@ array_from_text(PyArray_Descr *dtype, npy_intp num, char *sep, size_t *nread,
3743
3737
* Given a ``FILE *`` pointer ``fp``, and a ``PyArray_Descr``, return an
3744
3738
* array corresponding to the data encoded in that file.
3745
3739
*
3746
- * If the dtype is NULL, the default array type is used (double).
3747
- * If non-null, the reference is stolen and if dtype->subarray is true dtype
3748
- * will be decrefed even on success.
3740
+ * The reference to `dtype` is stolen (it is possible that the passed in
3741
+ * dtype is not held on to).
3749
3742
*
3750
3743
* The number of elements to read is given as ``num``; if it is < 0, then
3751
3744
* then as many as possible are read.
@@ -3793,7 +3786,6 @@ PyArray_FromFile(FILE *fp, PyArray_Descr *dtype, npy_intp num, char *sep)
3793
3786
(skip_separator ) fromfile_skip_separator , NULL );
3794
3787
}
3795
3788
if (ret == NULL ) {
3796
- Py_DECREF (dtype );
3797
3789
return NULL ;
3798
3790
}
3799
3791
if (((npy_intp ) nread ) < num ) {
0 commit comments