8000 TST: Test dimensions/indices found from parsed gufunc signatures. · numpy/numpy@d0e5e39 · GitHub
[go: up one dir, main page]

Skip to content

Commit d0e5e39

Browse files
mattipmhvk
authored andcommitted
TST: Test dimensions/indices found from parsed gufunc signatures.
1 parent 69458b0 commit d0e5e39

File tree

2 files changed

+67
-12
lines changed

2 files changed

+67
-12
lines changed

numpy/core/src/umath/_umath_tests.c.src

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -317,13 +317,16 @@ addUfuncs(PyObject *dictionary) {
317317
static PyObject *
318318
UMath_Tests_test_signature(PyObject *NPY_UNUSED(dummy), PyObject *args)
319319
{
320-
int nin, nout;
320+
int nin, nout, i;
321321
PyObject *signature, *sig_str;
322-
PyObject *f;
322+
PyUFuncObject *f = NULL;
323+
PyObject *core_num_dims = NULL, *core_dim_ixs = NULL;
323324
int core_enabled;
325+
int core_num_ixs = 0;
324326

325-
if (!PyArg_ParseTuple(args, "iiO", &nin, &nout, &signature)) return NULL;
326-
327+
if (!PyArg_ParseTuple(args, "iiO", &nin, &nout, &signature)) {
328+
return NULL;
329+
}
327330

328331
if (PyString_Check(signature)) {
329332
sig_str = signature;
@@ -334,17 +337,60 @@ UMath_Tests_test_signature(PyObject *NPY_UNUSED(dummy), PyObject *args)
334337
return NULL;
335338
}
336339

337-
f = PyUFunc_FromFuncAndDataAndSignature(NULL, NULL, NULL,
340+
f = (PyUFuncObject*)PyUFunc_FromFuncAndDataAndSignature(
341+
NULL, NULL, NULL,
338342
0, nin, nout, PyUFunc_None, "no name",
339343
"doc:none",
340344
1, PyString_AS_STRING(sig_str));
341345
if (sig_str != signature) {
342346
Py_DECREF(sig_str);
343347
}
344-
if (f == NULL) return NULL;
345-
core_enabled = ((PyUFuncObject*)f)->core_enabled;
348+
if (f == NULL) {
349+
return NULL;
350+
}
351+
core_enabled = f->core_enabled;
352+
/*
353+
* Don't presume core_num_dims and core_dim_ixs are defined;
354+
* they currently are even if core_enabled=0, but there's no real
355+
* reason they should be. So avoid segfaults if we change our mind.
356+
*/
357+
if (f->core_num_dims != NULL) {
358+
core_num_dims = PyTuple_New(f->nargs);
359+
if (core_num_dims == NULL) {
360+
goto fail;
361+
}
362+
for (i = 0; i < f->nargs; i++) {
363+
PyObject *val = PyLong_FromLong(f->core_num_dims[i]);
364+
PyTuple_SET_ITEM(core_num_dims, i, val);
365+
core_num_ixs += f->core_num_dims[i];
366+
}
367+
}
368+
else {
369+
Py_INCREF(Py_None);
370+
core_num_dims = Py_None;
371+
}
372+
if (f->core_dim_ixs != NULL) {
373+
core_dim_ixs = PyTuple_New(core_num_ixs);
374+
if (core_num_dims == NULL) {
375+
goto fail;
376+
}
377+
for (i = 0; i < core_num_ixs; i++) {
378+
PyObject * val = PyLong_FromLong(f->core_dim_ixs[i]);
379+
PyTuple_SET_ITEM(core_dim_ixs, i, val);
380+
}
381+
}
382+
else {
383+
Py_INCREF(Py_None);
384+
core_dim_ixs = Py_None;
385+
}
346386
Py_DECREF(f);
347-
return Py_BuildValue("i", core_enabled);
387+
return Py_BuildValue("iOO", core_enabled, core_num_dims, core_dim_ixs);
388+
389+
fail:
390+
Py_XDECREF(f);
391+
Py_XDECREF(core_num_dims);
392+
Py_XDECREF(core_dim_ixs);
393+
return NULL;
348394
}
349395

350396
static PyMethodDef UMath_TestsMethods[] = {

numpy/core/tests/test_ufunc.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,10 +285,16 @@ def test_all_ufunc(self):
285285
def test_signature(self):
286286
# the arguments to test_signature are: nin, nout, core_signature
287287
# pass
288-
assert_equal(umt.test_signature(2, 1, "(i),(i)->()"), 1)
288+
enabled, num_dims, ixs = umt.test_signature(2, 1, "(i),(i)->()")
289+
assert_equal(enabled, 1)
290+
assert_equal(num_dims, (1, 1, 0))
291+
assert_equal(ixs, (0, 0))
289292

290-
# pass. empty core signature; treat as plain ufunc (with trivial core)
291-
assert_equal(umt.test_signature(2, 1, "(),()->()"), 0)
293+
# empty core signature; treat as plain ufunc (with trivial core)
294+
enabled, num_dims, ixs = umt.test_signature(2, 1, "(),()->()")
295+
assert_equal(enabled, 0)
296+
assert_equal(num_dims, (0, 0, 0))
297+
assert_equal(ixs, ())
292298

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

329335
# more complicated names for variables
330-
assert_equal(umt.test_signature(2, 1, "(i1,i2),(J_1)->(_kAB)"), 1)
336+
enabled, num_dims, ixs = umt.test_signature(2, 1, "(i1,i2),(J_1)->(_kAB)")
337+
assert_equal(enabled, 1)
338+
assert_equal(num_dims, (2, 1, 1))
339+
assert_equal(ixs, (0, 1, 2, 3))
331340

332341
def test_get_signature(self):
333342
assert_equal(umt.inner1d.signature, "(i),(i)->()")

0 commit comments

Comments
 (0)
0