summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
Diffstat (limited to 'numpy')
-rw-r--r--numpy/core/src/umath/ufunc_object.c2
-rw-r--r--numpy/core/src/umath/umathmodule.c91
2 files changed, 35 insertions, 58 deletions
diff --git a/numpy/core/src/umath/ufunc_object.c b/numpy/core/src/umath/ufunc_object.c
index c1e8e5a77..9c9837a3e 100644
--- a/numpy/core/src/umath/ufunc_object.c
+++ b/numpy/core/src/umath/ufunc_object.c
@@ -4521,7 +4521,7 @@ PyUFunc_FromFuncAndData(PyUFuncGenericFunction *func, void **data,
const char *name, const char *doc, int unused)
{
return PyUFunc_FromFuncAndDataAndSignature(func, data, types, ntypes,
- nin, nout, identity, name, doc, 0, NULL);
+ nin, nout, identity, name, doc, unused, NULL);
}
/*UFUNC_API*/
diff --git a/numpy/core/src/umath/umathmodule.c b/numpy/core/src/umath/umathmodule.c
index 15da831b2..5567b9bbf 100644
--- a/numpy/core/src/umath/umathmodule.c
+++ b/numpy/core/src/umath/umathmodule.c
@@ -87,11 +87,12 @@ ufunc_frompyfunc(PyObject *NPY_UNUSED(dummy), PyObject *args, PyObject *NPY_UNUS
/* Keywords are ignored for now */
PyObject *function, *pyname = NULL;
- int nin, nout, i;
+ int nin, nout, i, nargs;
PyUFunc_PyFuncData *fdata;
PyUFuncObject *self;
- char *fname, *str;
+ char *fname, *str, *types, *doc;
Py_ssize_t fname_len = -1;
+ void * ptr, **data;
int offset[2];
if (!PyArg_ParseTuple(args, "Oii:frompyfunc", &function, &nin, &nout)) {
@@ -101,43 +102,7 @@ ufunc_frompyfunc(PyObject *NPY_UNUSED(dummy), PyObject *args, PyObject *NPY_UNUS
PyErr_SetString(PyExc_TypeError, "function must be callable");
return NULL;
}
- if (nin + nout > NPY_MAXARGS) {
- PyErr_Format(PyExc_ValueError,
- "Cannot construct a ufunc with more than %d operands "
- "(requested number were: inputs = %d and outputs = %d)",
- NPY_MAXARGS, nin, nout);
- return NULL;
- }
- self = PyArray_malloc(sizeof(PyUFuncObject));
- if (self == NULL) {
- return NULL;
- }
- PyObject_Init((PyObject *)self, &PyUFunc_Type);
-
- self->userloops = NULL;
- self->nin = nin;
- self->nout = nout;
- self->nargs = nin + nout;
- self->identity = PyUFunc_None;
- self->functions = pyfunc_functions;
- self->ntypes = 1;
-
- /* generalized ufunc */
- self->core_enabled = 0;
- self->core_num_dim_ix = 0;
- self->core_num_dims = NULL;
- self->core_dim_ixs = NULL;
- self->core_offsets = NULL;
- self->core_signature = NULL;
- self->op_flags = PyArray_malloc(sizeof(npy_uint32)*self->nargs);
- if (self->op_flags == NULL) {
- return PyErr_NoMemory();
- }
- memset(self->op_flags, 0, sizeof(npy_uint32)*self->nargs);
- self->iter_flags = 0;
-
- self->type_resolver = &object_ufunc_type_resolver;
- self->legacy_inner_loop_selector = &object_ufunc_loop_selector;
+ nargs = nin + nout;
pyname = PyObject_GetAttrString(function, "__name__");
if (pyname) {
@@ -150,7 +115,7 @@ ufunc_frompyfunc(PyObject *NPY_UNUSED(dummy), PyObject *args, PyObject *NPY_UNUS
}
/*
- * self->ptr holds a pointer for enough memory for
+ * ptr will be assigned to self->ptr, holds a pointer for enough memory for
* self->data[0] (fdata)
* self->data
* self->name
@@ -164,39 +129,51 @@ ufunc_frompyfunc(PyObject *NPY_UNUSED(dummy), PyObject *args, PyObject *NPY_UNUS
if (i) {
offset[0] += (sizeof(void *) - i);
}
- offset[1] = self->nargs;
- i = (self->nargs % sizeof(void *));
+ offset[1] = nargs;
+ i = (nargs % sizeof(void *));
if (i) {
offset[1] += (sizeof(void *)-i);
}
- self->ptr = PyArray_malloc(offset[0] + offset[1] + sizeof(void *) +
+ ptr = PyArray_malloc(offset[0] + offset[1] + sizeof(void *) +
(fname_len + 14));
- if (self->ptr == NULL) {
+ if (ptr == NULL) {
Py_XDECREF(pyname);
return PyErr_NoMemory();
}
- Py_INCREF(function);
- self->obj = function;
- fdata = (PyUFunc_PyFuncData *)(self->ptr);
+ fdata = (PyUFunc_PyFuncData *)(ptr);
+ fdata->callable = function;
fdata->nin = nin;
fdata->nout = nout;
- fdata->callable = function;
- self->data = (void **)(((char *)self->ptr) + offset[0]);
- self->data[0] = (void *)fdata;
- self->types = (char *)self->data + sizeof(void *);
- for (i = 0; i < self->nargs; i++) {
- self->types[i] = NPY_OBJECT;
+ data = (void **)(((char *)ptr) + offset[0]);
+ data[0] = (void *)fdata;
+ types = (char *)data + sizeof(void *);
+ for (i = 0; i < nargs; i++) {
+ types[i] = NPY_OBJECT;
}
- str = self->types + offset[1];
+ str = types + offset[1];
memcpy(str, fname, fname_len);
memcpy(str+fname_len, " (vectorized)", 14);
- self->name = str;
-
Py_XDECREF(pyname);
/* Do a better job someday */
- self->doc = "dynamic ufunc based on a python function";
+ doc = "dynamic ufunc based on a python function";
+
+ self = (PyUFuncObject *)PyUFunc_FromFuncAndData(
+ (PyUFuncGenericFunction *)pyfunc_functions, data,
+ types, /* ntypes */ 1, nin, nout, PyUFunc_None,
+ str, doc, /* unused */ 0);
+
+ if (self == NULL) {
+ PyArray_free(ptr);
+ return NULL;
+ }
+ Py_INCREF(function);
+ self->obj = function;
+ self->ptr = ptr;
+
+ self->type_resolver = &object_ufunc_type_resolver;
+ self->legacy_inner_loop_selector = &object_ufunc_loop_selector;
return (PyObject *)self;
}