8000 MAINT: Add comments and small cleanup ctors.c · numpy/numpy@b79cc49 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit b79cc49

Browse files
committed
MAINT: Add comments and small cleanup ctors.c
- Do not use fail exit in array_fromfile_binary. - Add comment explaining why we Py_INCREF dtype before call to PyArray_NewFromDescr in array_fromfile_binary and array_from_text.
1 parent f14ad75 commit b79cc49

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

numpy/core/src/multiarray/ctors.c

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,8 @@ discover_dimensions(PyObject *obj, int *maxndim, npy_intp *d, int check_it,
892892
* Generic new array creation routine.
893893
* Internal variant with calloc argument for PyArray_Zeros.
894894
*
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.
896897
*/
897898
NPY_NO_EXPORT PyObject *
898899
PyArray_NewFromDescr_int(PyTypeObject *subtype, PyArray_Descr *descr, int nd,
@@ -1128,7 +1129,8 @@ PyArray_NewFromDescr_int(PyTypeObject *subtype, PyArray_Descr *descr, int nd,
11281129
/*NUMPY_API
11291130
* Generic new array creation routine.
11301131
*
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.
11321134
*/
11331135
NPY_NO_EXPORT PyObject *
11341136
PyArray_NewFromDescr(PyTypeObject *subtype, PyArray_Descr *descr, int nd,
@@ -1153,7 +1155,8 @@ PyArray_NewFromDescr(PyTypeObject *subtype, PyArray_Descr *descr, int nd,
11531155
* subok - If 1, use the prototype's array subtype, otherwise
11541156
* always create a base-class array.
11551157
*
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.
11571160
*/
11581161
NPY_NO_EXPORT PyObject *
11591162
PyArray_NewLikeArray(PyArrayObject *prototype, NPY_ORDER order,
@@ -2842,7 +2845,8 @@ PyArray_CheckAxis(PyArrayObject *arr, int *axis, int flags)
28422845
/*NUMPY_API
28432846
* Zeros
28442847
*
2845-
* steal a reference
2848+
* steals a reference to type. On failure or when dtype->subarray is
2849+
* true, dtype will be decrefed.
28462850
* accepts NULL type
28472851
*/
28482852
NPY_NO_EXPORT PyObject *
@@ -3260,19 +3264,20 @@ array_fromfile_binary(FILE *fp, PyArray_Descr *dtype, npy_intp num, size_t *nrea
32603264
}
32613265
num = numbytes / dtype->elsize;
32623266
}
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+
*/
32633271
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);
32693274
if (r == NULL) {
3270-
goto fail;
3275+
Py_DECREF(dtype);
3276+
return NULL;
32713277
}
32723278
NPY_BEGIN_ALLOW_THREADS;
32733279
*nread = fread(PyArray_DATA(r), dtype->elsize, num, fp);
32743280
NPY_END_ALLOW_THREADS;
3275-
fail:
32763281
Py_DECREF(dtype);
32773282
return r;
32783283
}
@@ -3297,13 +3302,14 @@ array_from_text(PyArray_Descr *dtype, npy_intp num, char *sep, size_t *nread,
32973302

32983303
size = (num >= 0) ? num : FROM_BUFFER_SIZE;
32993304

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+
*/
33003309
Py_INCREF(dtype);
33013310
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);
33073313
if (r == NULL) {
33083314
Py_DECREF(dtype);
33093315
return NULL;
@@ -3372,7 +3378,8 @@ array_from_text(PyArray_Descr *dtype, npy_intp num, char *sep, size_t *nread,
33723378
* array corresponding to the data encoded in that file.
33733379
*
33743380
* 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.
33763383
*
33773384
* The number of elements to read is given as ``num``; if it is < 0, then
33783385
* then as many as possible are read.

0 commit comments

Comments
 (0)
0