Skip to content

Commit 4b2cd27

Browse files
committed
STY: Small style fixups for never-copy changes
Yes, these may be slightly biased towards my own opinions.
1 parent eccb8df commit 4b2cd27

File tree

5 files changed

+20
-24
lines changed

5 files changed

+20
-24
lines changed

numpy/core/include/numpy/ndarraytypes.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -450,9 +450,9 @@ typedef struct {
450450
} PyArray_Dims;
451451

452452
typedef enum {
453-
NPY_COPY_IF_NEEDED,
454-
NPY_COPY_ALWAYS,
455-
NPY_COPY_NEVER
453+
NPY_COPY_IF_NEEDED = 0,
454+
NPY_COPY_ALWAYS = 1,
455+
NPY_COPY_NEVER = 2,
456456
} _PyArray_CopyMode;
457457

458458
typedef struct {

numpy/core/src/multiarray/conversion_utils.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,30 +171,30 @@ PyArray_CopyConverter(PyObject *obj, _PyArray_CopyMode *copymode) {
171171
return NPY_FAIL;
172172
}
173173

174-
int int_copymode = -1;
174+
int int_copymode;
175175
PyObject* numpy_CopyMode = NULL;
176176
npy_cache_import("numpy", "_CopyMode", &numpy_CopyMode);
177177

178-
if (numpy_CopyMode != NULL && PyObject_Type(obj) == numpy_CopyMode) {
178+
if (numpy_CopyMode != NULL && (PyObject *)Py_TYPE(obj) == numpy_CopyMode) {
179179
PyObject* mode_value = PyObject_GetAttrString(obj, "value");
180180
if (mode_value == NULL) {
181181
return NPY_FAIL;
182182
}
183183

184-
int_copymode = PyLong_AsLong(mode_value);
184+
int_copymode = (int)PyLong_AsLong(mode_value);
185185
if (error_converting(int_copymode)) {
186186
return NPY_FAIL;
187187
}
188188
}
189189
else {
190190
npy_bool bool_copymode;
191-
if( !PyArray_BoolConverter(obj, &bool_copymode) ) {
191+
if (!PyArray_BoolConverter(obj, &bool_copymode)) {
192192
return NPY_FAIL;
193193
}
194-
int_copymode = (int) bool_copymode;
194+
int_copymode = (int)bool_copymode;
195195
}
196196

197-
*copymode = (_PyArray_CopyMode) int_copymode;
197+
*copymode = (_PyArray_CopyMode)int_copymode;
198198
return NPY_SUCCEED;
199199
}
200200

numpy/core/src/multiarray/ctors.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1879,13 +1879,12 @@ PyArray_CheckFromAny(PyObject *op, PyArray_Descr *descr, int min_depth,
18791879
return NULL;
18801880
}
18811881

1882-
if ((requires & NPY_ARRAY_ELEMENTSTRIDES) &&
1883-
!PyArray_ElementStrides(obj)) {
1882+
if ((requires & NPY_ARRAY_ELEMENTSTRIDES)
1883+
&& !PyArray_ElementStrides(obj)) {
18841884
PyObject *ret;
18851885
if (requires & NPY_ARRAY_ENSURENOCOPY) {
18861886
PyErr_SetString(PyExc_ValueError,
1887-
"Unable to avoid copy "
1888-
"while creating a new array.");
1887+
"Unable to avoid copy while creating a new array.");
18891888
return NULL;
18901889
}
18911890
ret = PyArray_NewCopy((PyArrayObject *)obj, NPY_ANYORDER);

numpy/core/src/multiarray/methods.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -875,10 +875,9 @@ array_astype(PyArrayObject *self,
875875
return (PyObject *)self;
876876
}
877877

878-
if( forcecopy == NPY_COPY_NEVER ) {
878+
if (forcecopy == NPY_COPY_NEVER) {
879879
PyErr_SetString(PyExc_ValueError,
880-
"Unable to avoid copy while casting in "
881-
"never copy mode.");
880+
"Unable to avoid copy while casting in never copy mode.");
882881
Py_DECREF(dtype);
883882
return NULL;
884883
}

numpy/core/src/multiarray/multiarraymodule.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,16 +1579,15 @@ _array_fromobject_generic(
15791579
if (PyArray_CheckExact(op) || (subok && PyArray_Check(op))) {
15801580
oparr = (PyArrayObject *)op;
15811581
if (type == NULL) {
1582-
if ((copy == NPY_COPY_IF_NEEDED || copy == NPY_COPY_NEVER) &&
1583-
STRIDING_OK(oparr, order)) {
1582+
if (copy != NPY_COPY_ALWAYS && STRIDING_OK(oparr, order)) {
15841583
ret = oparr;
15851584
Py_INCREF(ret);
15861585
goto finish;
15871586
}
15881587
else {
1589-
if( copy == NPY_COPY_NEVER ) {
1588+
if (copy == NPY_COPY_NEVER) {
15901589
PyErr_SetString(PyExc_ValueError,
1591-
"Unable to avoid copy while creating a new array.");
1590+
"Unable to avoid copy while creating a new array.");
15921591
return NULL;
15931592
}
15941593
ret = (PyArrayObject *)PyArray_NewCopy(oparr, order);
@@ -1598,16 +1597,15 @@ _array_fromobject_generic(
15981597
/* One more chance */
15991598
oldtype = PyArray_DESCR(oparr);
16001599
if (PyArray_EquivTypes(oldtype, type)) {
1601-
if ((copy == NPY_COPY_IF_NEEDED || copy == NPY_COPY_NEVER) &&
1602-
STRIDING_OK(oparr, order)) {
1600+
if (copy != NPY_COPY_ALWAYS && STRIDING_OK(oparr, order)) {
16031601
Py_INCREF(op);
16041602
ret = oparr;
16051603
goto finish;
16061604
}
16071605
else {
1608-
if( copy == NPY_COPY_NEVER ) {
1606+
if (copy == NPY_COPY_NEVER) {
16091607
PyErr_SetString(PyExc_ValueError,
1610-
"Unable to avoid copy while creating a new array.");
1608+
"Unable to avoid copy while creating a new array.");
16111609
return NULL;
16121610
}
16131611
ret = (PyArrayObject *)PyArray_NewCopy(oparr, order);

0 commit comments

Comments
 (0)