8000 ENH: add identity kwarg to frompyfunc by mattharrigan · Pull Request #8255 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: add identity kwarg to frompyfunc #8255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 17, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Merge branch 'master' into frompyfunc-identity
  • Loading branch information
eric-wieser authored Dec 4, 2019
commit 8d09f795bec8486901f24f4e1577b487748a406c
50 changes: 7 additions & 43 deletions numpy/core/src/umath/umathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@ object_ufunc_loop_selector(PyUFuncObject *ufunc,
return 0;
}

static PyObject *
PyObject *
ufunc_frompyfunc(PyObject *NPY_UNUSED(dummy), PyObject *args, PyObject *kwds) {

PyObject *function, *pyname = NULL;
int nin, nout, i, nargs;
PyUFunc_PyFuncData *fdata;
Expand All @@ -81,53 +80,18 @@ ufunc_frompyfunc(PyObject *NPY_UNUSED(dummy), PyObject *args, PyObject *kwds) {
void * ptr, **data;
int offset[2];
int identity = PyUFunc_None;
static char *kwlist[] = {"func", "nin", "nout", "identity", NULL};
static char *kwlist[] = {"", "nin", "nout", "identity", NULL};

if (!PyArg_ParseTupleAndKeywords(args, kwds, "Oii|i", kwlist,
if (!PyArg_ParseTupleAndKeywords(args, kwds, "Oii|$i:frompyfunc", kwlist,
&function, &nin, &nout, &identity)) {
return NULL;
}
if (!PyCallable_Check(function)) {
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 = identity;
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) {
Expand Down Expand Up @@ -184,10 +148,10 @@ ufunc_frompyfunc(PyObject *NPY_UNUSED(dummy), PyObject *args, PyObject *kwds) {
/* Do a better job someday */
doc = "dynamic ufunc based on a python function";

self = (PyUFuncObject *)PyUFunc_FromFuncAndData(
self = (PyUFuncObject *)PyUFunc_FromFuncAndDataAndSignatureAndIdentity(
(PyUFuncGenericFunction *)pyfunc_functions, data,
types, /* ntypes */ 1, nin, nout, PyUFunc_None,
str, doc, /* unused */ 0);
types, /* ntypes */ 1, nin, nout, PyUFunc_IdentityValue,
str, doc, /* unused */ 0, NULL, identity);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A future PR could add signature, which would help vectorize


if (self == NULL) {
PyArray_free(ptr);
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.
0