8000 API: Introduce copy argument for np.asarray · numpy/numpy@1e023d3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1e023d3

Browse files
committed
API: Introduce copy argument for np.asarray
1 parent b3d71f7 commit 1e023d3

File tree

5 files changed

+17
-8
lines changed

5 files changed

+17
-8
lines changed

numpy/_core/_add_newdocs.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,13 @@
931931
'A' (any) means 'F' if `a` is Fortran contiguous, 'C' otherwise
932932
'K' (keep) preserve input order
933933
Defaults to 'K'.
934+
copy : bool, optional
935+
If true, then the object is copied. If false then the object is
936+
copied only if needed, i.e. if ``__array__`` returns a copy, if obj
937+
is a nested sequence, or if a copy is needed to satisfy any of
938+
the other requirements (``dtype``, ``order``, etc.).
939+
For ``np._CopyMode.NEVER`` it raises a ``ValueError`` if a copy
940+
cannot be avoided. Default: false.
934941
${ARRAY_FUNCTION_LIKE}
935942
936943
.. versionadded:: 1.20.0

numpy/_core/src/multiarray/conversion_utils.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ PyArray_OptionalIntpConverter(PyObject *obj, PyArray_Dims *seq)
226226
}
227227

228228
NPY_NO_EXPORT int
229-
PyArray_CopyConverter(PyObject *obj, _PyArray_CopyMode *copymode) {
229+
PyArray_CopyConverter(PyObject *obj, NPY_COPYMODE *copymode) {
230230
if (obj == Py_None) {
231231
PyErr_SetString(PyExc_ValueError,
232232
"NoneType copy mode not allowed.");
@@ -257,7 +257,7 @@ PyArray_CopyConverter(PyObject *obj, _PyArray_CopyMode *copymode) {
257257
int_copymode = (int)bool_copymode;
258258
}
259259

260-
*copymode = (_PyArray_CopyMode)int_copymode;
260+
*copymode = (NPY_COPYMODE)int_copymode;
261261
return NPY_SUCCEED;
262262
}
263263

numpy/_core/src/multiarray/conversion_utils.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ typedef enum {
1616
NPY_COPY_IF_NEEDED = 0,
1717
NPY_COPY_ALWAYS = 1,
1818
NPY_COPY_NEVER = 2,
19-
} _PyArray_CopyMode;
19+
} NPY_COPYMODE;
2020

2121
NPY_NO_EXPORT int
22-
PyArray_CopyConverter(PyObject *obj, _PyArray_CopyMode *copyflag);
22+
PyArray_CopyConverter(PyObject *obj, NPY_COPYMODE *copyflag);
2323

2424
NPY_NO_EXPORT int
2525
PyArray_BufferConverter(PyObject *obj, PyArray_Chunk *buf);

numpy/_core/src/multiarray/methods.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,7 @@ array_astype(PyArrayObject *self,
782782
npy_dtype_info dt_info = {NULL, NULL};
783783
NPY_CASTING casting = NPY_UNSAFE_CASTING;
784784
NPY_ORDER order = NPY_KEEPORDER;
785-
_PyArray_CopyMode forcecopy = 1;
785+
NPY_COPYMODE forcecopy = 1;
786786
int subok = 1;
787787

788788
NPY_PREPARE_ARGPARSER;

numpy/_core/src/multiarray/multiarraymodule.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,7 +1555,7 @@ _prepend_ones(PyArrayObject *arr, int nd, int ndmin, NPY_ORDER order)
15551555
static inline PyObject *
15561556
_array_fromobject_generic(
15571557
PyObject *op, PyArray_Descr *in_descr, PyArray_DTypeMeta *in_DType,
1558-
_PyArray_CopyMode copy, NPY_ORDER order, npy_bool subok, int ndmin)
1558+
NPY_COPYMODE copy, NPY_ORDER order, npy_bool subok, int ndmin)
15591559
{
15601560
PyArrayObject *oparr = NULL, *ret = NULL;
15611561
PyArray_Descr *oldtype = NULL;
@@ -1704,7 +1704,7 @@ array_array(PyObject *NPY_UNUSED(ignored),
17041704
{
17051705
PyObject *op;
17061706
npy_bool subok = NPY_FALSE;
1707-
_PyArray_CopyMode copy = NPY_COPY_ALWAYS;
1707+
NPY_COPYMODE copy = NPY_COPY_ALWAYS;
17081708
int ndmin = 0;
17091709
npy_dtype_info dt_info = {NULL, NULL};
17101710
NPY_ORDER order = NPY_KEEPORDER;
@@ -1752,6 +1752,7 @@ array_asarray(PyObject *NPY_UNUSED(ignored),
17521752
PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames)
17531753
{
17541754
PyObject *op;
1755+
NPY_COPYMODE copy = NPY_COPY_IF_NEEDED;
17551756
npy_dtype_info dt_info = {NULL, NULL};
17561757
NPY_ORDER order = NPY_KEEPORDER;
17571758
PyObject *like = Py_None;
@@ -1762,6 +1763,7 @@ array_asarray(PyObject *NPY_UNUSED(ignored),
17621763
"a", NULL, &op,
17631764
"|dtype", &PyArray_DTypeOrDescrConverterOptional, &dt_info,
17641765
"|order", &PyArray_OrderConverter, &order,
1766+
"$copy", &PyArray_CopyConverter, &copy,
17651767
"$like", NULL, &like,
17661768
NULL, NULL, NULL) < 0) {
17671769
Py_XDECREF(dt_info.descr);
@@ -1783,7 +1785,7 @@ array_asarray(PyObject *NPY_UNUSED(ignored),
17831785
}
17841786

17851787
PyObject *res = _array_fromobject_generic(
1786-
op, dt_info.descr, dt_info.dtype, NPY_FALSE, order, NPY_FALSE, 0);
1788+
op, dt_info.descr, dt_info.dtype, copy, order, NPY_FALSE, 0);
17871789
Py_XDECREF(dt_info.descr);
17881790
Py_XDECREF(dt_info.dtype);
17891791
return res;

0 commit comments

Comments
 (0)
0