8000 ENH: Strict checking of ufunc keyword argument names by jaimefrio · Pull Request #5659 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: Strict checking of ufunc keyword argument names #5659

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
Mar 12, 2015
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
2 changes: 1 addition & 1 deletion doc/source/reference/ufuncs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ advanced usage and will not typically be used.
Defaults to true. If set to false, the output will always be a strict
array, not a subtype.

*sig*
*sig* or *signature*

Either a data-type, a tuple of data-types, or a special signature
string indicating the input and output types of a ufunc. This argument
Expand Down
25 changes: 16 additions & 9 deletions numpy/core/src/umath/ufunc_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,7 @@ get_ufunc_arguments(PyUFuncObject *ufunc,
int type_num;

int any_flexible = 0, any_object = 0, any_flexible_userloops = 0;
int has_sig = 0;

ufunc_name = ufunc->name ? ufunc->name : "<unnamed ufunc>";

Expand Down Expand Up @@ -945,7 +946,7 @@ get_ufunc_arguments(PyUFuncObject *ufunc,
switch (str[0]) {
case 'c':
/* Provides a policy for allowed casting */
if (strncmp(str, "casting", 7) == 0) {
if (strcmp(str, "casting") == 0) {
if (!PyArray_CastingConverter(value, out_casting)) {
goto fail;
}
Expand All @@ -954,7 +955,7 @@ get_ufunc_arguments(PyUFuncObject *ufunc,
break;
case 'd':
/* Another way to specify 'sig' */
if (strncmp(str, "dtype", 5) == 0) {
if (strcmp(str, "dtype") == 0) {
/* Allow this parameter to be None */
PyArray_Descr *dtype;
if (!PyArray_DescrConverter2(value, &dtype)) {
Expand All @@ -976,7 +977,7 @@ get_ufunc_arguments(PyUFuncObject *ufunc,
* Overrides the global parameters buffer size,
* error mask, and error object
*/
if (strncmp(str, "extobj", 6) == 0) {
if (strcmp(str, "extobj") == 0) {
*out_extobj = value;
bad_arg = 0;
}
Expand All @@ -987,7 +988,7 @@ get_ufunc_arguments(PyUFuncObject *ufunc,
* either as a single array or None for single output
* ufuncs, or as a tuple of arrays and Nones.
*/
if (strncmp(str, "out", 3) == 0) {
if (strcmp(str, "out") == 0) {
if (nargs > nin) {
PyErr_SetString(PyExc_ValueError,
"cannot specify 'out' as both a "
Expand Down Expand Up @@ -1048,7 +1049,7 @@ get_ufunc_arguments(PyUFuncObject *ufunc,
bad_arg = 0;
}
/* Allows the default output layout to be overridden */
else if (strncmp(str,"order",5) == 0) {
else if (strcmp(str, "order") == 0) {
if (!PyArray_OrderConverter(value, out_order)) {
goto fail;
}
Expand All @@ -1057,7 +1058,13 @@ get_ufunc_arguments(PyUFuncObject *ufunc,
break;
case 's':
/* Allows a specific function inner loop to be selected */
if (strncmp(str,"sig",3) == 0) {
if (strcmp(str, "sig") == 0 ||
strcmp(str, "signature") == 0) {
if (has_sig == 1) {
PyErr_SetString(PyExc_ValueError,
"cannot specify both 'sig' and 'signature'");
goto fail;
}
if (*out_typetup != NULL) {
PyErr_SetString(PyExc_RuntimeError,
"cannot specify both 'sig' and 'dtype'");
Expand All @@ -1066,8 +1073,9 @@ get_ufunc_arguments(PyUFuncObject *ufunc,
*out_typetup = value;
Py_INCREF(value);
bad_arg = 0;
has_sig = 1;
}
else if (strncmp(str,"subok",5) == 0) {
else if (strcmp(str, "subok") == 0) {
if (!PyBool_Check(value)) {
PyErr_SetString(PyExc_TypeError,
"'subok' must be a boolean");
Expand All @@ -1082,8 +1090,7 @@ get_ufunc_arguments(PyUFuncObject *ufunc,
* Provides a boolean array 'where=' mask if
* out_wheremask is supplied.
*/
if (out_wheremask != NULL &&
strncmp(str,"where",5) == 0) {
if (out_wheremask != NULL && strcmp(str, "where") == 0) {
PyArray_Descr *dtype;
dtype = PyArray_DescrFromType(NPY_BOOL);
if (dtype == NULL) {
Expand Down
23 changes: 23 additions & 0 deletions numpy/core/tests/test_ufunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,29 @@
from numpy.compat import asbytes
from numpy.core.test_rational import *


class TestUfuncKwargs(TestCase):
def test_kwarg_exact(self):
assert_raises(TypeError, np.add, 1, 2, castingx='safe')
assert_raises(TypeError, np.add, 1, 2, dtypex=np.int)
assert_raises(TypeError, np.add, 1, 2, extobjx=[4096])
assert_raises(TypeError, np.add, 1, 2, outx=None)
assert_raises(TypeError, np.add, 1, 2, sigx='ii->i')
assert_raises(TypeError, np.add, 1, 2, signaturex='ii->i')
assert_raises(TypeError, np.add, 1, 2, subokx=False)
assert_raises(TypeError, np.add, 1, 2, wherex=[True])

def test_sig_signature(self):
assert_raises(ValueError, np.add, 1, 2, sig='ii->i',
signature='ii->i')

def test_sig_dtype(self):
assert_raises(RuntimeError, np.add, 1, 2, sig='ii->i',
dtype=np.int)
assert_raises(RuntimeError, np.add, 1, 2, signature='ii->i',
dtype=np.int)


class TestUfunc(TestCase):
def test_pickle(self):
import pickle
Expand Down
0