diff options
| author | Gagandeep Singh <gdp.1807@gmail.com> | 2021-11-02 13:04:51 +0530 |
|---|---|---|
| committer | Gagandeep Singh <gdp.1807@gmail.com> | 2021-11-02 13:04:51 +0530 |
| commit | 790f927fcedb7debc477746ffb7a4a2eb1668693 (patch) | |
| tree | 8bd6df7baa87bfceecda52eb801be6b578a6998e /numpy | |
| parent | c04509e86e97a69a0b5fcbeebdbec66faad3dbe0 (diff) | |
| download | numpy-790f927fcedb7debc477746ffb7a4a2eb1668693.tar.gz | |
Addressed reviews
Diffstat (limited to 'numpy')
| -rw-r--r-- | numpy/core/include/numpy/ndarraytypes.h | 6 | ||||
| -rw-r--r-- | numpy/core/src/multiarray/conversion_utils.c | 25 | ||||
| -rw-r--r-- | numpy/core/src/multiarray/conversion_utils.h | 2 | ||||
| -rw-r--r-- | numpy/core/src/multiarray/ctors.c | 12 | ||||
| -rw-r--r-- | numpy/core/src/multiarray/methods.c | 4 | ||||
| -rw-r--r-- | numpy/core/src/multiarray/multiarraymodule.c | 6 | ||||
| -rw-r--r-- | numpy/core/tests/test_multiarray.py | 15 |
7 files changed, 36 insertions, 34 deletions
diff --git a/numpy/core/include/numpy/ndarraytypes.h b/numpy/core/include/numpy/ndarraytypes.h index a1d1c01dc..566eae357 100644 --- a/numpy/core/include/numpy/ndarraytypes.h +++ b/numpy/core/include/numpy/ndarraytypes.h @@ -449,11 +449,11 @@ typedef struct { int len; } PyArray_Dims; -typedef enum PyNpCopyMode { +typedef enum { NPY_COPY_IF_NEEDED, NPY_COPY_ALWAYS, NPY_COPY_NEVER -} PyArray_CopyMode; +} _PyArray_CopyMode; typedef struct { /* @@ -938,7 +938,7 @@ typedef int (PyArray_FinalizeFunc)(PyArrayObject *, PyObject *); #define NPY_ARRAY_UPDATEIFCOPY 0x1000 /* Deprecated in 1.14 */ #define NPY_ARRAY_WRITEBACKIFCOPY 0x2000 -#define NPY_ARRAY_ENSURENOCOPY 0x4000 +#define _NPY_ARRAY_ENSURENOCOPY 0x4000 /* * NOTE: there are also internal flags defined in multiarray/arrayobject.h, diff --git a/numpy/core/src/multiarray/conversion_utils.c b/numpy/core/src/multiarray/conversion_utils.c index 59e3b4922..983890433 100644 --- a/numpy/core/src/multiarray/conversion_utils.c +++ b/numpy/core/src/multiarray/conversion_utils.c @@ -166,13 +166,10 @@ PyArray_OptionalIntpConverter(PyObject *obj, PyArray_Dims *seq) } NPY_NO_EXPORT int -PyArray_CopyConverter(PyObject *obj, PyArray_CopyMode *copymode) { +PyArray_CopyConverter(PyObject *obj, _PyArray_CopyMode *copymode) { if (obj == Py_None) { PyErr_SetString(PyExc_ValueError, - "NoneType copy mode not allowed. Please choose one of " - "np.array_api._CopyMode.ALWAYS, " - "np.array_api._CopyMode.IF_NEEDED, " - "np.array_api._CopyMode.NEVER."); + "NoneType copy mode not allowed."); return NPY_FAIL; } @@ -180,7 +177,7 @@ PyArray_CopyConverter(PyObject *obj, PyArray_CopyMode *copymode) { PyObject* numpy_CopyMode = NULL; npy_cache_import("numpy", "_CopyMode", &numpy_CopyMode); - if (numpy_CopyMode != NULL && PyObject_IsInstance(obj, numpy_CopyMode)) { + if (numpy_CopyMode != NULL && PyObject_TypeCheck(obj, numpy_CopyMode)) { PyObject* mode_value = PyObject_GetAttrString(obj, "value"); if (mode_value == NULL) { return NPY_FAIL; @@ -188,7 +185,8 @@ PyArray_CopyConverter(PyObject *obj, PyArray_CopyMode *copymode) { if (!PyArray_PythonPyIntFromInt(mode_value, &int_copymode)) { return NPY_FAIL; } - } else { + } + else { npy_bool bool_copymode; if( !PyArray_BoolConverter(obj, &bool_copymode) ) { return NPY_FAIL; @@ -196,18 +194,7 @@ PyArray_CopyConverter(PyObject *obj, PyArray_CopyMode *copymode) { int_copymode = (int) bool_copymode; } - if( int_copymode != NPY_COPY_ALWAYS && - int_copymode != NPY_COPY_IF_NEEDED && - int_copymode != NPY_COPY_NEVER ) { - PyErr_Format(PyExc_ValueError, - "Unrecognized copy mode %d. Please choose one of " - "np._CopyMode.ALWAYS, np._CopyMode.IF_NEEDED, " - "np._CopyMode.NEVER, " - "True/np.True_, False/np.False_", int_copymode); - return NPY_FAIL; - } - - *copymode = (PyArray_CopyMode) int_copymode; + *copymode = (_PyArray_CopyMode) int_copymode; return NPY_SUCCEED; } diff --git a/numpy/core/src/multiarray/conversion_utils.h b/numpy/core/src/multiarray/conversion_utils.h index 4662c6a8b..643b67d59 100644 --- a/numpy/core/src/multiarray/conversion_utils.h +++ b/numpy/core/src/multiarray/conversion_utils.h @@ -10,7 +10,7 @@ NPY_NO_EXPORT int PyArray_OptionalIntpConverter(PyObject *obj, PyArray_Dims *seq); NPY_NO_EXPORT int -PyArray_CopyConverter(PyObject *obj, PyArray_CopyMode *copyflag); +PyArray_CopyConverter(PyObject *obj, _PyArray_CopyMode *copyflag); NPY_NO_EXPORT int PyArray_BufferConverter(PyObject *obj, PyArray_Chunk *buf); diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c index 27fd3a057..fdc393b97 100644 --- a/numpy/core/src/multiarray/ctors.c +++ b/numpy/core/src/multiarray/ctors.c @@ -1305,7 +1305,7 @@ _array_from_array_like(PyObject *op, PyErr_Clear(); } else { - tmp = _array_from_buffer_3118(memoryview); // Assume: never creates a copy + tmp = _array_from_buffer_3118(memoryview); Py_DECREF(memoryview); if (tmp == NULL) { return NULL; @@ -1347,7 +1347,7 @@ _array_from_array_like(PyObject *op, * this should be changed! */ if (!writeable && tmp == Py_NotImplemented) { - tmp = PyArray_FromArrayAttr(op, requested_dtype, context); // Assume: array was copied. + tmp = PyArray_FromArrayAttr(op, requested_dtype, context); if (tmp == NULL) { return NULL; } @@ -1738,7 +1738,7 @@ PyArray_FromAny(PyObject *op, PyArray_Descr *newtype, int min_depth, /* Create a new array and copy the data */ Py_INCREF(dtype); /* hold on in case of a subarray that is replaced */ - if( flags & NPY_ARRAY_ENSURENOCOPY ) { + if( flags & _NPY_ARRAY_ENSURENOCOPY ) { PyErr_SetString(PyExc_ValueError, "Unable to avoid copy while creating " "an array from descriptor."); @@ -1802,7 +1802,7 @@ PyArray_FromAny(PyObject *op, PyArray_Descr *newtype, int min_depth, * NPY_ARRAY_ALIGNED, * NPY_ARRAY_WRITEABLE, * NPY_ARRAY_NOTSWAPPED, - * NPY_ARRAY_ENSURECOPY, + * _NPY_ARRAY_ENSURECOPY, * NPY_ARRAY_UPDATEIFCOPY, * NPY_ARRAY_WRITEBACKIFCOPY, * NPY_ARRAY_FORCECAST, @@ -1872,7 +1872,7 @@ PyArray_CheckFromAny(PyObject *op, PyArray_Descr *descr, int min_depth, if ((requires & NPY_ARRAY_ELEMENTSTRIDES) && !PyArray_ElementStrides(obj)) { PyObject *ret; - if( requires & NPY_ARRAY_ENSURENOCOPY ) { + if( requires & _NPY_ARRAY_ENSURENOCOPY ) { PyErr_SetString(PyExc_ValueError, "Unable to avoid copy while creating a new array."); return NULL; @@ -1952,7 +1952,7 @@ PyArray_FromArray(PyArrayObject *arr, PyArray_Descr *newtype, int flags) if (copy) { - if( flags & NPY_ARRAY_ENSURENOCOPY ) { + if( flags & _NPY_ARRAY_ENSURENOCOPY ) { PyErr_SetString(PyExc_ValueError, "Unable to avoid copy while creating " "an array from given array."); diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c index bb0006e32..7530a2e36 100644 --- a/numpy/core/src/multiarray/methods.c +++ b/numpy/core/src/multiarray/methods.c @@ -833,7 +833,7 @@ array_astype(PyArrayObject *self, */ NPY_CASTING casting = NPY_UNSAFE_CASTING; NPY_ORDER order = NPY_KEEPORDER; - PyArray_CopyMode forcecopy = 1; + _PyArray_CopyMode forcecopy = 1; int subok = 1; NPY_PREPARE_ARGPARSER; if (npy_parse_arguments("astype", args, len_args, kwnames, @@ -877,7 +877,7 @@ array_astype(PyArrayObject *self, if( forcecopy == NPY_COPY_NEVER ) { PyErr_SetString(PyExc_ValueError, "Unable to avoid copy while casting in " - "np._CopyMode.NEVER"); + "never copy mode."); Py_DECREF(dtype); return NULL; } diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index c00f14045..bf60314ad 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -1560,7 +1560,7 @@ _prepend_ones(PyArrayObject *arr, int nd, int ndmin, NPY_ORDER order) static NPY_INLINE PyObject * _array_fromobject_generic( - PyObject *op, PyArray_Descr *type, PyArray_CopyMode copy, NPY_ORDER order, + PyObject *op, PyArray_Descr *type, _PyArray_CopyMode copy, NPY_ORDER order, npy_bool subok, int ndmin) { PyArrayObject *oparr = NULL, *ret = NULL; @@ -1623,7 +1623,7 @@ _array_fromobject_generic( if (copy == NPY_COPY_ALWAYS) { flags = NPY_ARRAY_ENSURECOPY; } else if( copy == NPY_COPY_NEVER ) { - flags = NPY_ARRAY_ENSURENOCOPY; + flags = _NPY_ARRAY_ENSURENOCOPY; } if (order == NPY_CORDER) { flags |= NPY_ARRAY_C_CONTIGUOUS; @@ -1668,7 +1668,7 @@ array_array(PyObject *NPY_UNUSED(ignored), { PyObject *op; npy_bool subok = NPY_FALSE; - PyArray_CopyMode copy = NPY_COPY_ALWAYS; + _PyArray_CopyMode copy = NPY_COPY_ALWAYS; int ndmin = 0; PyArray_Descr *type = NULL; NPY_ORDER order = NPY_KEEPORDER; diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index fa7f254a6..3919f31d8 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -7816,6 +7816,11 @@ class TestNewBufferProtocol: class TestArrayCreationCopyArgument(object): + class RaiseOnBool: + + def __bool__(self): + raise ValueError + true_vals = [True, np._CopyMode.ALWAYS, np.True_] false_vals = [False, np._CopyMode.IF_NEEDED, np.False_] @@ -7831,6 +7836,10 @@ class TestArrayCreationCopyArgument(object): copy=np._CopyMode.NEVER) assert_raises(ValueError, np.array, pyscalar, copy=np._CopyMode.NEVER) + assert_raises(ValueError, np.array, pyscalar, + copy=None) + assert_raises(ValueError, np.array, pyscalar, + copy=self.RaiseOnBool()) def test_compatible_cast(self): @@ -7874,6 +7883,9 @@ class TestArrayCreationCopyArgument(object): assert_raises(ValueError, np.array, arr, copy=np._CopyMode.NEVER, dtype=int2) + assert_raises(ValueError, np.array, + arr, copy=None, + dtype=int2) def test_buffer_interface(self): @@ -7988,6 +8000,9 @@ class TestArrayCreationCopyArgument(object): assert_raises(ValueError, np.array, view, copy=np._CopyMode.NEVER, order=order2) + assert_raises(ValueError, np.array, + view, copy=None, + order=order2) class TestArrayAttributeDeletion: |
