Skip to content

Commit

Permalink
STY: Apply clang-format to convert.c
Browse files Browse the repository at this point in the history
This is to provide an example of the result for review.
  • Loading branch information
charris committed Aug 26, 2021
1 parent ecd74af commit 4adb838
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 63 deletions.
2 changes: 2 additions & 0 deletions numpy/core/src/common/array_assign.h
@@ -1,6 +1,8 @@
#ifndef _NPY_PRIVATE__ARRAY_ASSIGN_H_
#define _NPY_PRIVATE__ARRAY_ASSIGN_H_

#include "numpy/arrayobject.h"

/*
* An array assignment function for copying arrays, treating the
* arrays as flat according to their respective ordering rules.
Expand Down
2 changes: 2 additions & 0 deletions numpy/core/src/multiarray/arrayobject.h
Expand Up @@ -5,6 +5,8 @@
#error You should not include this
#endif

#include "numpy/arrayobject.h"

NPY_NO_EXPORT PyObject *
_strings_richcompare(PyArrayObject *self, PyArrayObject *other, int cmp_op,
int rstrip);
Expand Down
128 changes: 65 additions & 63 deletions numpy/core/src/multiarray/convert.c
@@ -1,24 +1,22 @@
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include "structmember.h"

#include <npy_config.h>

#include "structmember.h"

#define NPY_NO_DEPRECATED_API NPY_API_VERSION
#define _MULTIARRAYMODULE
#include "numpy/arrayobject.h"
#include "numpy/arrayscalars.h"
#include "npy_pycompat.h"

#include "common.h"
#include "array_assign.h"
#include "arrayobject.h"
#include "common.h"
#include "convert.h"
#include "ctors.h"
#include "mapping.h"
#include "lowlevel_strided_loops.h"
#include "mapping.h"
#include "npy_pycompat.h"
#include "numpy/arrayobject.h"
#include "numpy/arrayscalars.h"
#include "scalartypes.h"
#include "array_assign.h"

#include "convert.h"

int
fallocate(int fd, int mode, off_t offset, off_t len);
Expand All @@ -30,7 +28,7 @@ fallocate(int fd, int mode, off_t offset, off_t len);
* returns -1 and raises exception on no space, ignores all other errors
*/
static int
npy_fallocate(npy_intp nbytes, FILE * fp)
npy_fallocate(npy_intp nbytes, FILE *fp)
{
/*
* unknown behavior on non-linux so don't try it
Expand Down Expand Up @@ -61,8 +59,11 @@ npy_fallocate(npy_intp nbytes, FILE * fp)
* early exit on no space, other errors will also get found during fwrite
*/
if (r == -1 && errno == ENOSPC) {
PyErr_Format(PyExc_IOError, "Not enough free space to write "
"%"NPY_INTP_FMT" bytes", nbytes);
PyErr_Format(
PyExc_IOError,
"Not enough free space to write "
"%" NPY_INTP_FMT " bytes",
nbytes);
return -1;
}
#endif
Expand Down Expand Up @@ -95,7 +96,7 @@ recursive_tolist(PyArrayObject *self, char *dataptr, int startdim)
}

for (i = 0; i < n; ++i) {
item = recursive_tolist(self, dataptr, startdim+1);
item = recursive_tolist(self, dataptr, startdim + 1);
if (item == NULL) {
Py_DECREF(ret);
return NULL;
Expand Down Expand Up @@ -138,7 +139,8 @@ PyArray_ToFile(PyArrayObject *self, FILE *fp, char *sep, char *format)
if (n3 == 0) {
/* binary data */
if (PyDataType_FLAGCHK(PyArray_DESCR(self), NPY_LIST_PICKLE)) {
PyErr_SetString(PyExc_IOError,
PyErr_SetString(
PyExc_IOError,
"cannot write object arrays to a file in binary mode");
return -1;
}
Expand All @@ -154,7 +156,7 @@ PyArray_ToFile(PyArrayObject *self, FILE *fp, char *sep, char *format)
size = PyArray_SIZE(self);
NPY_BEGIN_ALLOW_THREADS;

#if defined (_MSC_VER) && defined(_WIN64)
#if defined(_MSC_VER) && defined(_WIN64)
/* Workaround Win64 fwrite() bug. Ticket #1660 */
{
npy_intp maxsize = 2147483648 / PyArray_DESCR(self)->elsize;
Expand All @@ -163,10 +165,11 @@ PyArray_ToFile(PyArrayObject *self, FILE *fp, char *sep, char *format)
n = 0;
while (size > 0) {
chunksize = (size > maxsize) ? maxsize : size;
n2 = fwrite((const void *)
((char *)PyArray_DATA(self) + (n * PyArray_DESCR(self)->elsize)),
(size_t) PyArray_DESCR(self)->elsize,
(size_t) chunksize, fp);
n2 = fwrite(
(const void *)((char *)PyArray_DATA(self) +
(n * PyArray_DESCR(self)->elsize)),
(size_t)PyArray_DESCR(self)->elsize,
(size_t)chunksize, fp);
if (n2 < chunksize) {
break;
}
Expand All @@ -176,31 +179,32 @@ PyArray_ToFile(PyArrayObject *self, FILE *fp, char *sep, char *format)
size = PyArray_SIZE(self);
}
#else
n = fwrite((const void *)PyArray_DATA(self),
(size_t) PyArray_DESCR(self)->elsize,
(size_t) size, fp);
n = fwrite(
(const void *)PyArray_DATA(self),
(size_t)PyArray_DESCR(self)->elsize, (size_t)size, fp);
#endif
NPY_END_ALLOW_THREADS;
if (n < size) {
PyErr_Format(PyExc_IOError,
"%ld requested and %ld written",
(long) size, (long) n);
PyErr_Format(
PyExc_IOError, "%ld requested and %ld written",
(long)size, (long)n);
return -1;
}
}
else {
NPY_BEGIN_THREADS_DEF;

it = (PyArrayIterObject *) PyArray_IterNew((PyObject *)self);
it = (PyArrayIterObject *)PyArray_IterNew((PyObject *)self);
NPY_BEGIN_THREADS;
while (it->index < it->size) {
if (fwrite((const void *)it->dataptr,
(size_t) PyArray_DESCR(self)->elsize,
1, fp) < 1) {
(size_t)PyArray_DESCR(self)->elsize, 1, fp) < 1) {
NPY_END_THREADS;
PyErr_Format(PyExc_IOError,
PyErr_Format(
PyExc_IOError,
"problem writing element %" NPY_INTP_FMT
" to file", it->index);
" to file",
it->index);
Py_DECREF(it);
return -1;
}
Expand All @@ -215,8 +219,7 @@ PyArray_ToFile(PyArrayObject *self, FILE *fp, char *sep, char *format)
* text data
*/

it = (PyArrayIterObject *)
PyArray_IterNew((PyObject *)self);
it = (PyArrayIterObject *)PyArray_IterNew((PyObject *)self);
n4 = (format ? strlen((const char *)format) : 0);
while (it->index < it->size) {
obj = PyArray_GETITEM(self, it->dataptr);
Expand Down Expand Up @@ -244,7 +247,7 @@ PyArray_ToFile(PyArrayObject *self, FILE *fp, char *sep, char *format)
Py_DECREF(it);
return -1;
}
PyTuple_SET_ITEM(tupobj,0,obj);
PyTuple_SET_ITEM(tupobj, 0, obj);
obj = PyUnicode_FromString((const char *)format);
if (obj == NULL) {
Py_DECREF(tupobj);
Expand All @@ -266,17 +269,19 @@ PyArray_ToFile(PyArrayObject *self, FILE *fp, char *sep, char *format)
NPY_END_ALLOW_THREADS;
Py_DECREF(byteobj);
if (n < n2) {
PyErr_Format(PyExc_IOError,
"problem writing element %" NPY_INTP_FMT
" to file", it->index);
PyErr_Format(
PyExc_IOError,
"problem writing element %" NPY_INTP_FMT " to file",
it->index);
Py_DECREF(strobj);
Py_DECREF(it);
return -1;
}
/* write separator for all but last one */
if (it->index != it->size-1) {
if (it->index != it->size - 1) {
if (fwrite(sep, 1, n3, fp) < n3) {
PyErr_Format(PyExc_IOError,
PyErr_Format(
PyExc_IOError,
"problem writing separator to file");
Py_DECREF(strobj);
Py_DECREF(it);
Expand Down Expand Up @@ -313,9 +318,10 @@ PyArray_ToString(PyArrayObject *self, NPY_ORDER order)
*/

numbytes = PyArray_NBYTES(self);
if ((PyArray_IS_C_CONTIGUOUS(self) && (order == NPY_CORDER))
|| (PyArray_IS_F_CONTIGUOUS(self) && (order == NPY_FORTRANORDER))) {
ret = PyBytes_FromStringAndSize(PyArray_DATA(self), (Py_ssize_t) numbytes);
if ((PyArray_IS_C_CONTIGUOUS(self) && (order == NPY_CORDER)) ||
(PyArray_IS_F_CONTIGUOUS(self) && (order == NPY_FORTRANORDER))) {
ret = PyBytes_FromStringAndSize(
PyArray_DATA(self), (Py_ssize_t)numbytes);
}
else {
PyObject *new;
Expand All @@ -335,7 +341,7 @@ PyArray_ToString(PyArrayObject *self, NPY_ORDER order)
if (it == NULL) {
return NULL;
}
ret = PyBytes_FromStringAndSize(NULL, (Py_ssize_t) numbytes);
ret = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)numbytes);
if (ret == NULL) {
Py_DECREF(it);
return NULL;
Expand Down Expand Up @@ -368,8 +374,7 @@ PyArray_FillWithScalar(PyArrayObject *arr, PyObject *obj)
* the element in that array instead.
*/
if (PyArray_DESCR(arr)->type_num == NPY_OBJECT &&
!(PyArray_Check(obj) &&
PyArray_NDIM((PyArrayObject *)obj) == 0)) {
!(PyArray_Check(obj) && PyArray_NDIM((PyArrayObject *)obj) == 0)) {
value = (char *)&obj;

dtype = PyArray_DescrFromType(NPY_OBJECT);
Expand Down Expand Up @@ -469,8 +474,8 @@ PyArray_FillWithScalar(PyArrayObject *arr, PyObject *obj)
/* Use the value pointer we got if possible */
if (value != NULL) {
/* TODO: switch to SAME_KIND casting */
retcode = PyArray_AssignRawScalar(arr, dtype, value,
NULL, NPY_UNSAFE_CASTING);
retcode = PyArray_AssignRawScalar(
arr, dtype, value, NULL, NPY_UNSAFE_CASTING);
Py_DECREF(dtype);
return retcode;
}
Expand All @@ -484,14 +489,15 @@ PyArray_FillWithScalar(PyArrayObject *arr, PyObject *obj)
* recognized as a struct scalar of the required type.
*/
Py_INCREF(PyArray_DTYPE(arr));
src_arr = (PyArrayObject *)PyArray_FromAny(obj,
PyArray_DTYPE(arr), 0, 0, 0, NULL);
src_arr = (PyArrayObject *)PyArray_FromAny(
obj, PyArray_DTYPE(arr), 0, 0, 0, NULL);
if (src_arr == NULL) {
return -1;
}

if (PyArray_NDIM(src_arr) != 0) {
PyErr_SetString(PyExc_ValueError,
PyErr_SetString(
PyExc_ValueError,
"Input object to FillWithScalar is not a scalar");
Py_DECREF(src_arr);
return -1;
Expand All @@ -513,8 +519,7 @@ PyArray_FillWithScalar(PyArrayObject *arr, PyObject *obj)
* Returns 0 on success, -1 on failure.
*/
NPY_NO_EXPORT int
PyArray_AssignZero(PyArrayObject *dst,
PyArrayObject *wheremask)
PyArray_AssignZero(PyArrayObject *dst, PyArrayObject *wheremask)
{
npy_bool value;
PyArray_Descr *bool_dtype;
Expand All @@ -527,14 +532,13 @@ PyArray_AssignZero(PyArrayObject *dst,
}
value = 0;

retcode = PyArray_AssignRawScalar(dst, bool_dtype, (char *)&value,
wheremask, NPY_SAFE_CASTING);
retcode = PyArray_AssignRawScalar(
dst, bool_dtype, (char *)&value, wheremask, NPY_SAFE_CASTING);

Py_DECREF(bool_dtype);
return retcode;
}


/*NUMPY_API
* Copy an array.
*/
Expand Down Expand Up @@ -580,19 +584,17 @@ PyArray_View(PyArrayObject *self, PyArray_Descr *type, PyTypeObject *pytype)

Py_INCREF(dtype);
ret = (PyArrayObject *)PyArray_NewFromDescr_int(
subtype, dtype,
PyArray_NDIM(self), PyArray_DIMS(self), PyArray_STRIDES(self),
PyArray_DATA(self),
flags, (PyObject *)self, (PyObject *)self,
0, 1);
subtype, dtype, PyArray_NDIM(self), PyArray_DIMS(self),
PyArray_STRIDES(self), PyArray_DATA(self), flags, (PyObject *)self,
(PyObject *)self, 0, 1);
if (ret == NULL) {
Py_XDECREF(type);
return NULL;
}

if (type != NULL) {
if (PyObject_SetAttrString((PyObject *)ret, "dtype",
(PyObject *)type) < 0) {
if (PyObject_SetAttrString(
(PyObject *)ret, "dtype", (PyObject *)type) < 0) {
Py_DECREF(ret);
Py_DECREF(type);
return NULL;
Expand Down

0 comments on commit 4adb838

Please sign in to comment.