diff options
author | Sebastian Berg <sebastian@sipsolutions.net> | 2021-06-21 22:17:14 -0500 |
---|---|---|
committer | Sebastian Berg <sebastian@sipsolutions.net> | 2021-06-21 22:18:37 -0500 |
commit | ef7dd36dbb0c890f1157bd9a761830954cbcb12d (patch) | |
tree | 05efdc7ba90719bf8dfee55f1e4e06fc840d47be | |
parent | 80b387c66e4f10faafdb1a21c011c173f77b0942 (diff) | |
download | numpy-ef7dd36dbb0c890f1157bd9a761830954cbcb12d.tar.gz |
MAINT: Add simple tuple creation helper and use it
The new dispatching code requires a few more places where a similar
tuple is created, so add the helper and use it in three places
only. But more will be added (especially more that require
the NULL -> None logic).
-rw-r--r-- | numpy/core/src/multiarray/array_method.c | 8 | ||||
-rw-r--r-- | numpy/core/src/multiarray/common.h | 28 | ||||
-rw-r--r-- | numpy/core/src/umath/ufunc_object.c | 7 | ||||
-rw-r--r-- | numpy/core/src/umath/ufunc_type_resolution.c | 18 |
4 files changed, 35 insertions, 26 deletions
diff --git a/numpy/core/src/multiarray/array_method.c b/numpy/core/src/multiarray/array_method.c index 3ecc20d1d..ab992a3ae 100644 --- a/numpy/core/src/multiarray/array_method.c +++ b/numpy/core/src/multiarray/array_method.c @@ -36,6 +36,7 @@ #include "dtypemeta.h" #include "common_dtype.h" #include "convert_datatype.h" +#include "common.h" /* @@ -471,14 +472,11 @@ static PyObject * boundarraymethod_repr(PyBoundArrayMethodObject *self) { int nargs = self->method->nin + self->method->nout; - PyObject *dtypes = PyTuple_New(nargs); + PyObject *dtypes = PyArray_TupleFromItems( + nargs, (PyObject **)self->dtypes, 0); if (dtypes == NULL) { return NULL; } - for (int i = 0; i < nargs; i++) { - Py_INCREF(self->dtypes[i]); - PyTuple_SET_ITEM(dtypes, i, (PyObject *)self->dtypes[i]); - } return PyUnicode_FromFormat( "<np._BoundArrayMethod `%s` for dtypes %S>", self->method->name, dtypes); diff --git a/numpy/core/src/multiarray/common.h b/numpy/core/src/multiarray/common.h index 83209cd38..203decaa0 100644 --- a/numpy/core/src/multiarray/common.h +++ b/numpy/core/src/multiarray/common.h @@ -291,6 +291,34 @@ npy_memchr(char * haystack, char needle, return p; } + +/* + * Simple helper to create a tuple from an array of items. The `make_null_none` + * flag means that NULL entries are replaced with None, which is occasionally + * useful. + */ +static NPY_INLINE PyObject * +PyArray_TupleFromItems(int n, PyObject *const *items, int make_null_none) +{ + PyObject *tuple = PyTuple_New(n); + if (tuple == NULL) { + return NULL; + } + for (int i = 0; i < n; i ++) { + PyObject *tmp; + if (!make_null_none || items[i] != NULL) { + tmp = items[i]; + } + else { + tmp = Py_None; + } + Py_INCREF(tmp); + PyTuple_SET_ITEM(tuple, i, tmp); + } + return tuple; +} + + #include "ucsnarrow.h" /* diff --git a/numpy/core/src/umath/ufunc_object.c b/numpy/core/src/umath/ufunc_object.c index 37e297ed5..cdb5b720d 100644 --- a/numpy/core/src/umath/ufunc_object.c +++ b/numpy/core/src/umath/ufunc_object.c @@ -4686,15 +4686,10 @@ ufunc_generic_fastcall(PyUFuncObject *ufunc, } /* Fetch input arguments. */ - full_args.in = PyTuple_New(ufunc->nin); + full_args.in = PyArray_TupleFromItems(ufunc->nin, args, 0); if (full_args.in == NULL) { return NULL; } - for (int i = 0; i < ufunc->nin; i++) { - PyObject *tmp = args[i]; - Py_INCREF(tmp); - PyTuple_SET_ITEM(full_args.in, i, tmp); - } /* * If there are more arguments, they define the out args. Otherwise diff --git a/numpy/core/src/umath/ufunc_type_resolution.c b/numpy/core/src/umath/ufunc_type_resolution.c index 2834235e4..211d837dd 100644 --- a/numpy/core/src/umath/ufunc_type_resolution.c +++ b/numpy/core/src/umath/ufunc_type_resolution.c @@ -94,9 +94,6 @@ raise_no_loop_found_error( PyUFuncObject *ufunc, PyArray_Descr **dtypes) { static PyObject *exc_type = NULL; - PyObject *exc_value; - PyObject *dtypes_tup; - npy_intp i; npy_cache_import( "numpy.core._exceptions", "_UFuncNoLoopError", @@ -105,22 +102,13 @@ raise_no_loop_found_error( return -1; } - /* convert dtypes to a tuple */ - dtypes_tup = PyTuple_New(ufunc->nargs); + PyObject *dtypes_tup = PyArray_TupleFromItems( + ufunc->nargs, (PyObject **)dtypes, 1); if (dtypes_tup == NULL) { return -1; } - for (i = 0; i < ufunc->nargs; ++i) { - PyObject *tmp = Py_None; - if (dtypes[i] != NULL) { - tmp = (PyObject *)dtypes[i]; - } - Py_INCREF(tmp); - PyTuple_SET_ITEM(dtypes_tup, i, tmp); - } - /* produce an error object */ - exc_value = PyTuple_Pack(2, ufunc, dtypes_tup); + PyObject *exc_value = PyTuple_Pack(2, ufunc, dtypes_tup); Py_DECREF(dtypes_tup); if (exc_value == NULL) { return -1; |