Skip to content

Commit

Permalink
MAINT: respond to review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ngoldbaum committed Mar 20, 2023
1 parent a1339de commit 4ecdd83
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
4 changes: 2 additions & 2 deletions numpy/core/src/multiarray/ctors.c
Original file line number Diff line number Diff line change
Expand Up @@ -1934,14 +1934,14 @@ PyArray_CheckFromAny(PyObject *op, PyArray_Descr *descr, int min_depth,
int res = PyArray_ExtractDTypeAndDescriptor(
descr, &dt_info.descr, &dt_info.dtype);

Py_XDECREF(descr);

if (res < 0) {
Py_XDECREF(dt_info.descr);
Py_XDECREF(dt_info.dtype);
return NULL;
}

Py_XDECREF(descr);

PyObject* ret = PyArray_CheckFromAny_int(
op, dt_info.descr, dt_info.dtype, min_depth, max_depth, requires,
context);
Expand Down
40 changes: 25 additions & 15 deletions numpy/core/src/multiarray/multiarraymodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1635,10 +1635,16 @@ _prepend_ones(PyArrayObject *arr, int nd, int ndmin, NPY_ORDER order)
((order) == NPY_CORDER && PyArray_IS_C_CONTIGUOUS(op)) || \
((order) == NPY_FORTRANORDER && PyArray_IS_F_CONTIGUOUS(op)))

/*
* NOTE: in_descr is passed as a reference to a pointer because there is a
* code path where the pointer can be changed via Py_XSETREF and we need to
* make sure the mutated pointer is available for the caller.
*/

static inline PyObject *
_array_fromobject_generic(
PyObject *op, npy_dtype_info dt_info, _PyArray_CopyMode copy,
NPY_ORDER order, npy_bool subok, int ndmin)
PyObject *op, PyArray_Descr **in_descr, PyArray_DTypeMeta *in_DType,
_PyArray_CopyMode copy, NPY_ORDER order, npy_bool subok, int ndmin)
{
PyArrayObject *oparr = NULL, *ret = NULL;
PyArray_Descr *oldtype = NULL;
Expand All @@ -1658,29 +1664,31 @@ _array_fromobject_generic(
* Skip AdaptDescriptorToArray if the supplied dtype class does not
* match the dtype of the input array or if we have to determine the
* descriptor because only the DType was given. This means we avoid
* inspecting array values twice for example if someone does:
* inspecting array values twice if someone does, e.g.:
*
* >>> arr = np.array(["asdf", "fdsa"], dtype=object)
* >>> np.array(arr, dtype="U")
*/
if ((NPY_DTYPE(PyArray_DESCR(oparr)) == dt_info.dtype) ||
((dt_info.descr == NULL) && (dt_info.dtype != NULL))) {
if ((NPY_DTYPE(PyArray_DESCR(oparr)) == in_DType) ||
((*in_descr == NULL) && (in_DType != NULL))) {
dtype = PyArray_AdaptDescriptorToArray(
oparr, dt_info.dtype, dt_info.descr);
oparr, in_DType, *in_descr);
if (dtype == NULL) {
return NULL;
}
Py_INCREF(dtype);
Py_XSETREF(*in_descr, dtype);
}
else {
if ((dt_info.descr == NULL) && (dt_info.dtype == NULL)) {
if ((*in_descr == NULL) && (in_DType == NULL)) {
dtype = PyArray_DESCR(oparr);
}
else {
dtype = dt_info.descr;
dtype = *in_descr;
}
Py_INCREF(dtype);
}
if ((dt_info.descr == NULL) && (dt_info.dtype == NULL)) {
if ((*in_descr == NULL) && (in_DType == NULL)) {
if (copy != NPY_COPY_ALWAYS && STRIDING_OK(oparr, order)) {
ret = oparr;
Py_INCREF(ret);
Expand Down Expand Up @@ -1772,7 +1780,7 @@ _array_fromobject_generic(
flags |= NPY_ARRAY_FORCECAST;

ret = (PyArrayObject *)PyArray_CheckFromAny_int(
op, dt_info.descr, dt_info.dtype, 0, 0, flags, NULL);
op, *in_descr, in_DType, 0, 0, flags, NULL);

finish:
if (ret == NULL) {
Expand Down Expand Up @@ -1836,7 +1844,7 @@ array_array(PyObject *NPY_UNUSED(ignored),
}

PyObject *res = _array_fromobject_generic(
op, dt_info, copy, order, subok, ndmin);
op, &dt_info.descr, dt_info.dtype, copy, order, subok, ndmin);
Py_XDECREF(dt_info.descr);
Py_XDECREF(dt_info.dtype);
return res;
Expand Down Expand Up @@ -1878,7 +1886,7 @@ array_asarray(PyObject *NPY_UNUSED(ignored),
}

PyObject *res = _array_fromobject_generic(
op, dt_info, NPY_FALSE, order, NPY_FALSE, 0);
op, &dt_info.descr, dt_info.dtype, NPY_FALSE, order, NPY_FALSE, 0);
Py_XDECREF(dt_info.descr);
Py_XDECREF(dt_info.dtype);
return res;
Expand Down Expand Up @@ -1920,7 +1928,7 @@ array_asanyarray(PyObject *NPY_UNUSED(ignored),
}

PyObject *res = _array_fromobject_generic(
op, dt_info, NPY_FALSE, order, NPY_TRUE, 0);
op, &dt_info.descr, dt_info.dtype, NPY_FALSE, order, NPY_TRUE, 0);
Py_XDECREF(dt_info.descr);
Py_XDECREF(dt_info.dtype);
return res;
Expand Down Expand Up @@ -1961,7 +1969,8 @@ array_ascontiguousarray(PyObject *NPY_UNUSED(ignored),
}

PyObject *res = _array_fromobject_generic(
op, dt_info, NPY_FALSE, NPY_CORDER, NPY_FALSE, 1);
op, &dt_info.descr, dt_info.dtype, NPY_FALSE, NPY_CORDER, NPY_FALSE,
1);
Py_XDECREF(dt_info.descr);
Py_XDECREF(dt_info.dtype);
return res;
Expand Down Expand Up @@ -2002,7 +2011,8 @@ array_asfortranarray(PyObject *NPY_UNUSED(ignored),
}

PyObject *res = _array_fromobject_generic(
op, dt_info, NPY_FALSE, NPY_FORTRANORDER, NPY_FALSE, 1);
op, &dt_info.descr, dt_info.dtype, NPY_FALSE, NPY_FORTRANORDER,
NPY_FALSE, 1);
Py_XDECREF(dt_info.descr);
Py_XDECREF(dt_info.dtype);
return res;
Expand Down

0 comments on commit 4ecdd83

Please sign in to comment.