8000 TST: Test dimensions/indices found from parsed gufunc signatures. by mhvk · Pull Request #11178 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

TST: Test dimensions/indices found from parsed gufunc signatures. #11178

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 1 commit into from
May 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
TST: Test dimensions/indices found from parsed gufunc signatures.
  • Loading branch information
mattip authored and mhvk committed May 28, 2018
commit d0e5e394d9174d3d5651aacc98b1cb6fcdcc75b2
62 changes: 54 additions & 8 deletions numpy/core/src/umath/_umath_tests.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -317,13 +317,16 @@ addUfuncs(PyObject *dictionary) {
static PyObject *
UMath_Tests_test_signature(PyObject *NPY_UNUSED(dummy), PyObject *args)
{
int nin, nout;
int nin, nout, i;
PyObject *signature, *sig_str;
PyObject *f;
PyUFuncObject *f = NULL;
PyObject *core_num_dims = NULL, *core_dim_ixs = NULL;
int core_enabled;
int core_num_ixs = 0;

if (!PyArg_ParseTuple(args, "iiO", &nin, &nout, &signature)) return NULL;

if (!PyArg_ParseTuple(args, "iiO", &nin, &nout, &signature)) {
return NULL;
}

if (PyString_Check(signature)) {
sig_str = signature;
Expand All @@ -334,17 +337,60 @@ UMath_Tests_test_signature(PyObject *NPY_UNUSED(dummy), PyObject *args)
return NULL;
}

f = PyUFunc_FromFuncAndDataAndSignature(NULL, NULL, NULL,
f = (PyUFuncObject*)PyUFunc_FromFuncAndDataAndSignature(
NULL, NULL, NULL,
0, nin, nout, PyUFunc_None, "no name",
"doc:none",
1, PyString_AS_STRING(sig_str));
if (sig_str != signature) {
Py_DECREF(sig_str);
}
if (f == NULL) return NULL;
core_enabled = ((PyUFuncObject*)f)->core_enabled;
if (f == NULL) {
return NULL;
}
core_enabled = f->core_enabled;
/*
* Don't presume core_num_dims and core_dim_ixs are defined;
* they currently are even if core_enabled=0, but there's no real
* reason they should be. So avoid segfaults if we change our mind.
*/
if (f->core_num_dims != NULL) {
core_num_dims = PyTuple_New(f->nargs);
if (core_num_dims == NULL) {
goto fail;
}
for (i = 0; i < f->nargs; i++) {
PyObject *val = PyLong_FromLong(f->core_num_dims[i]);
PyTuple_SET_ITEM(core_num_dims, i, val);
core_num_ixs += f->core_num_dims[i];
}
}
else {
Py_INCREF(Py_None);
core_num_dims = Py_None;
}
if (f->core_dim_ixs != NULL) {
core_dim_ixs = PyTuple_New(core_num_ixs);
if (core_num_dims == NULL) {
goto fail;
}
for (i = 0; i < core_num_ixs; i++) {
PyObject * val = PyLong_FromLong(f->core_dim_ixs[i]);
PyTuple_SET_ITEM(core_dim_ixs, i, val);
}
}
else {
Py_INCREF(Py_None);
core_dim_ixs = Py_None;
}
Py_DECREF(f);
return Py_BuildValue("i", core_enabled);
return Py_BuildValue("iOO", core_enabled, core_num_dims, core_dim_ixs);

fail:
Py_XDECREF(f);
Py_XDECREF(core_num_dims);
Py_XDECREF(core_dim_ixs);
return NULL;
}

static PyMethodDef UMath_TestsMethods[] = {
Expand Down
17 changes: 13 additions & 4 deletions numpy/core/tests/test_ufunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,16 @@ def test_all_ufunc(self):
def test_signature(self):
# the arguments to test_signature are: nin, nout, core_signature
# pass
assert_equal(umt.test_signature(2, 1, "(i),(i)->()"), 1)
enabled, num_dims, ixs = umt.test_signature(2, 1, "(i),(i)->()")
assert_equal(enabled, 1)
assert_equal(num_dims, (1, 1, 0))
assert_equal(ixs, (0, 0))

# pass. empty core signature; treat as plain ufunc (with trivial core)
assert_equal(umt.test_signature(2, 1, "(),()->()"), 0)
# empty core signature; treat as plain ufunc (with trivial core)
enabled, num_dims, ixs = umt.test_signature(2, 1, "(),()->()")
assert_equal(enabled, 0)
assert_equal(num_dims, (0, 0, 0))
assert_equal(ixs, ())

# in the following calls, a ValueError should be raised because
# of error in core signature
Expand Down Expand Up @@ -327,7 +333,10 @@ def test_signature(self):
pass

# more complicated names for variables
assert_equal(umt.test_signature(2, 1, "(i1,i2),(J_1)->(_kAB)"), 1)
enabled, num_dims, ixs = umt.test_signature(2, 1, "(i1,i2),(J_1)->(_kAB)")
assert_equal(enabled, 1)
assert_equal(num_dims, (2, 1, 1))
assert_equal(ixs, (0, 1, 2, 3))
Copy link
Member

Choose a reason for hiding this comment

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

Might be nicer to build a nested tuple here, and I don't envisage it being any harder


def test_get_signature(self):
assert_equal(umt.inner1d.signature, "(i),(i)->()")
Expand Down
0