Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit92081fe

Browse files
committed
API: Introduce copy argument for np.asarray
1 parent06d7bdf commit92081fe

File tree

7 files changed

+17
-22
lines changed

7 files changed

+17
-22
lines changed

‎doc/source/reference/array_api.rst‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -585,9 +585,6 @@ Creation functions differences
585585
* - Feature
586586
- Type
587587
- Notes
588-
* - ``copy`` keyword argument to ``asarray``
589-
- **Compatible**
590-
-
591588
* - New ``device`` keyword argument to all array creation functions
592589
(``asarray``, ``arange``, ``empty``, ``empty_like``, ``eye``, ``full``,
593590
``full_like``, ``linspace``, ``ones``, ``ones_like``, ``zeros``, and

‎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_EXPORTint
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
returnNPY_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_EXPORTint
22-
PyArray_CopyConverter(PyObject*obj,_PyArray_CopyMode*copyflag);
22+
PyArray_CopyConverter(PyObject*obj,NPY_COPYMODE*copyflag);
2323

2424
NPY_NO_EXPORTint
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_infodt_info= {NULL,NULL};
783783
NPY_CASTINGcasting=NPY_UNSAFE_CASTING;
784784
NPY_ORDERorder=NPY_KEEPORDER;
785-
_PyArray_CopyModeforcecopy=1;
785+
NPY_COPYMODEforcecopy=1;
786786
intsubok=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
staticinlinePyObject*
15561556
_array_fromobject_generic(
15571557
PyObject*op,PyArray_Descr*in_descr,PyArray_DTypeMeta*in_DType,
1558-
_PyArray_CopyModecopy,NPY_ORDERorder,npy_boolsubok,intndmin)
1558+
NPY_COPYMODEcopy,NPY_ORDERorder,npy_boolsubok,intndmin)
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_boolsubok=NPY_FALSE;
1707-
_PyArray_CopyModecopy=NPY_COPY_ALWAYS;
1707+
NPY_COPYMODEcopy=NPY_COPY_ALWAYS;
17081708
intndmin=0;
17091709
npy_dtype_infodt_info= {NULL,NULL};
17101710
NPY_ORDERorder=NPY_KEEPORDER;
@@ -1752,6 +1752,7 @@ array_asarray(PyObject *NPY_UNUSED(ignored),
17521752
PyObject*const*args,Py_ssize_tlen_args,PyObject*kwnames)
17531753
{
17541754
PyObject*op;
1755+
NPY_COPYMODEcopy=NPY_COPY_IF_NEEDED;
17551756
npy_dtype_infodt_info= {NULL,NULL};
17561757
NPY_ORDERorder=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
returnres;

‎tools/ci/array-api-skips.txt‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# copy not implemented
2-
array_api_tests/test_creation_functions.py::test_asarray_arrays
3-
41
# waiting on NumPy to allow/revert distinct NaNs for np.unique
52
# https://github.com/numpy/numpy/issues/20326#issuecomment-1012380448
63
array_api_tests/test_set_functions.py
@@ -28,11 +25,6 @@ array_api_tests/test_special_cases.py::test_iop[__ifloordiv__(x1_i is -infinity
2825
array_api_tests/test_special_cases.py::test_iop[__ifloordiv__(isfinite(x1_i) and x1_i > 0 and x2_i is -infinity) -> -0]
2926
array_api_tests/test_special_cases.py::test_iop[__ifloordiv__(isfinite(x1_i) and x1_i < 0 and x2_i is +infinity) -> -0]
3027

31-
# asarray() got an unexpected keyword argument 'copy'
32-
array_api_tests/test_array_object.py::test_setitem
33-
# array_api_tests/test_array_object.py::test_setitem_masking
34-
array_api_tests/test_creation_functions.py::test_asarray_scalars
35-
3628
# fft test suite is buggy as of 83f0bcdc
3729
array_api_tests/test_fft.py
3830

@@ -148,9 +140,6 @@ array_api_tests/test_sorting_functions.py::test_sort
148140
array_api_tests/test_special_cases.py::test_unary
149141
array_api_tests/test_special_cases.py::test_binary
150142

151-
# asarray() got an unexpected keyword argument 'copy'
152-
array_api_tests/test_special_cases.py::test_iop
153-
154143
# got an unexpected keyword argument 'correction'
155144
array_api_tests/test_statistical_functions.py::test_std
156145
array_api_tests/test_statistical_functions.py::test_var

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp