10000 BUG/ENH: Switch to simplified __numpy_ufunc__/binop interaction by njsmith · Pull Request #6001 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG/ENH: Switch to simplified __numpy_ufunc__/binop interaction #6001

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
Next Next commit
MAINT: move PyArray_GetAttrString_SuppressException into private/get_…
…attr_string.h

This is an ugly kluge, but until we merge multiarray.so and umath.so
moving stuff into private/*.h serves as a reasonable workaround.
  • Loading branch information
njsmith committed Jun 24, 2015
commit 65cae2b3758e71488adb903432e1d2c2237be4a8
57 changes: 2 additions & 55 deletions numpy/core/src/multiarray/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include "common.h"
#include "buffer.h"

#include "get_attr_string.h"

/*
* The casting to use for implicit assignment operations resulting from
* in-place operations (like +=) and out= arguments. (Notice that this
Expand All @@ -29,61 +31,6 @@
* warning (that people's code will be broken in a future release.)
*/

/*
* PyArray_GetAttrString_SuppressException:
*
* Stripped down version of PyObject_GetAttrString,
* avoids lookups for None, tuple, and List objects,
* and doesn't create a PyErr since this code ignores it.
*
* This can be much faster then PyObject_GetAttrString where
* exceptions are not used by caller.
*
* 'obj' is the object to search for attribute.
*
* 'name' is the attribute to search for.
*
* Returns attribute value on success, 0 on failure.
*/
PyObject *
PyArray_GetAttrString_SuppressException(PyObject *obj, char *name)
{
PyTypeObject *tp = Py_TYPE(obj);
PyObject *res = (PyObject *)NULL;

/* We do not need to check for special attributes on trivial types */
if (_is_basic_python_type(obj)) {
return NULL;
}

/* Attribute referenced by (char *)name */
if (tp->tp_getattr != NULL) {
res = (*tp->tp_getattr)(obj, name);
if (res == NULL) {
PyErr_Clear();
}
}
/* Attribute referenced by (PyObject *)name */
8000 else if (tp->tp_getattro != NULL) {
#if defined(NPY_PY3K)
PyObject *w = PyUnicode_InternFromString(name);
#else
PyObject *w = PyString_InternFromString(name);
#endif
if (w == NULL) {
return (PyObject *)NULL;
}
res = (*tp->tp_getattro)(obj, w);
Py_DECREF(w);
if (res == NULL) {
PyErr_Clear();
}
}
return res;
}



NPY_NO_EXPORT NPY_CASTING NPY_DEFAULT_ASSIGN_CASTING = NPY_SAME_KIND_CASTING;


Expand Down
31 changes: 0 additions & 31 deletions numpy/core/src/multiarray/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ NPY_NO_EXPORT int
PyArray_DTypeFromObjectHelper(PyObject *obj, int maxdims,
PyArray_Descr **out_dtype, int string_status);

NPY_NO_EXPORT PyObject *
PyArray_GetAttrString_SuppressException(PyObject *v, char *name);

/*
* Returns NULL without setting an exception if no scalar is matched, a
* new dtype reference otherwise.
Expand Down Expand Up @@ -188,34 +185,6 @@ npy_memchr(char * haystack, char needle,
return p;
}

static NPY_INLINE int
_is_basic_python_type(PyObject * obj)
{
if (obj == Py_None ||
PyBool_Check(obj) ||
/* Basic number types */
#if !defined(NPY_PY3K)
PyInt_CheckExact(obj) ||
PyString_CheckExact(obj) ||
#endif
PyLong_CheckExact(obj) ||
PyFloat_CheckExact(obj) ||
PyComplex_CheckExact(obj) ||
/* Basic sequence types */
PyList_CheckExact(obj) ||
PyTuple_CheckExact(obj) ||
PyDict_CheckExact(obj) ||
PyAnySet_CheckExact(obj) ||
PyUnicode_CheckExact(obj) ||
PyBytes_CheckExact(obj) ||
PySlice_Check(obj)) {

return 1;
}

return 0;
}

/*
* Convert NumPy stride to BLAS stride. Returns 0 if conversion cannot be done
* (BLAS won't handle negative or zero strides the way we want).
Expand Down
2 changes: 2 additions & 0 deletions numpy/core/src/multiarray/ctors.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include "alloc.h"
#include <assert.h>

#include "get_attr_string.h"

/*
* Reading from a file or a string.
*
Expand Down
2 changes: 2 additions & 0 deletions numpy/core/src/multiarray/multiarraymodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ NPY_NO_EXPORT int NPY_NUMUSERTYPES = 0;
#include "templ_common.h" /* for npy_mul_with_overflow_intp */
#include "compiled_base.h"

#include "get_attr_string.h"

/* Only here for API compatibility */
NPY_NO_EXPORT PyTypeObject PyBigArray_Type;

Expand Down
85 changes: 85 additions & 0 deletions numpy/core/src/private/get_attr_string.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#ifndef __GET_ATTR_STRING_H
#define __GET_ATTR_STRING_H

static NPY_INLINE int
_is_basic_python_type(PyObject * obj)
{
if (obj == Py_None ||
PyBool_Check(obj) ||
/* Basic number types */
#if !defined(NPY_PY3K)
PyInt_CheckExact(obj) ||
PyString_CheckExact(obj) ||
#endif
PyLong_CheckExact(obj) ||
PyFloat_CheckExact(obj) ||
PyComplex_CheckExact(obj) ||
/* Basic sequence types */
PyList_CheckExact(obj) ||
PyTuple_CheckExact(obj) ||
PyDict_CheckExact(obj) ||
PyAnySet_CheckExact(obj) ||
PyUnicode_CheckExact(obj) ||
PyBytes_CheckExact(obj) ||
PySlice_Check(obj)) {

return 1;
}

return 0;
}

/*
* PyArray_GetAttrString_SuppressException:
*
* Stripped down version of PyObject_GetAttrString,
* avoids lookups for None, tuple, and List objects,
* and doesn't create a PyErr since this code ignores it.
*
* This can be much faster then PyObject_GetAttrString where
* exceptions are not used by caller.
*
* 'obj' is the object to search for attribute.
*
* 'name' is the attribute to search for.
*
* Returns attribute value on success, 0 on failure.
*/
static PyObject *
PyArray_GetAttrString_SuppressException(PyObject *obj, char *name)
{
PyTypeObject *tp = Py_TYPE(obj);
PyObject *res = (PyObject *)NULL;

/* We do not need to check for special attributes on trivial types */
if (_is_basic_python_type(obj)) {
return NULL;
}

/* Attribute referenced by (char *)name */
if (tp->tp_getattr != NULL) {
res = (*tp->tp_getattr)(obj, name);
if (res == NULL) {
PyErr_Clear();
}
}
/* Attribute referenced by (PyObject *)name */
else if (tp->tp_getattro != NULL) {
#if defined(NPY_PY3K)
PyObject *w = PyUnicode_InternFromString(name);
#else
PyObject *w = PyString_InternFromString(name);
#endif
if (w == NULL) {
return (PyObject *)NULL;
}
res = (*tp->tp_getattro)(obj, w);
Py_DECREF(w);
if (res == NULL) {
PyErr_Clear();
}
}
return res;
}

#endif
15 changes: 6 additions & 9 deletions numpy/core/src/private/ufunc_override.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <string.h>
#include "numpy/ufuncobject.h"

#include "get_attr_string.h"

static void
normalize___call___args(PyUFuncObject *ufunc, PyObject *args,
PyObject **normal_args, PyObject **normal_kwds,
Expand Down Expand Up @@ -213,16 +215,11 @@ PyUFunc_CheckOverride(PyUFuncObject *ufunc, char *method,
}

for (i = 0; i < nargs; ++i) {
PyObject *tmp;
obj = PyTuple_GET_ITEM(args, i);
/*
* TODO: could use PyArray_GetAttrString_SuppressException if it
* weren't private to multiarray.so
*/
if (PyArray_CheckExact(obj) || PyArray_IsScalar(obj, Generic) ||
_is_basic_python_type(obj)) {
continue;
}
if (PyObject_HasAttrString(obj, "__numpy_ufunc__")) {
tmp = PyArray_GetAttrString_SuppressException(obj, "__numpy_ufunc__");
if (tmp) {
P 482E y_DECREF(tmp);
with_override[noa] = obj;
with_override_pos[noa] = i;
++noa;
Expand Down
0