diff options
author | Matti Picus <matti.picus@gmail.com> | 2020-05-18 09:00:39 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-18 09:00:39 +0300 |
commit | 25f64902e9178bf476e2ce002985495ce56a7092 (patch) | |
tree | 99411e7f709de3b6f73dae626a4ff035017b77e5 | |
parent | a6b4b2bdd6baf2afde27284097426e93912452e9 (diff) | |
parent | bf14c1ce748f46df7daf5923cfb79ff905e233e8 (diff) | |
download | numpy-25f64902e9178bf476e2ce002985495ce56a7092.tar.gz |
Merge pull request #16238 from seberg/maint-cached-imports
MAINT: Unify cached (C-level static) imports
-rw-r--r-- | numpy/core/src/common/npy_import.h | 2 | ||||
-rw-r--r-- | numpy/core/src/multiarray/common.h | 9 | ||||
-rw-r--r-- | numpy/core/src/multiarray/dtype_transfer.c | 10 | ||||
-rw-r--r-- | numpy/core/src/multiarray/methods.c | 33 | ||||
-rw-r--r-- | numpy/core/src/umath/scalarmath.c.src | 9 |
5 files changed, 13 insertions, 50 deletions
diff --git a/numpy/core/src/common/npy_import.h b/numpy/core/src/common/npy_import.h index 221e1e645..f485514d1 100644 --- a/numpy/core/src/common/npy_import.h +++ b/numpy/core/src/common/npy_import.h @@ -19,7 +19,7 @@ NPY_INLINE static void npy_cache_import(const char *module, const char *attr, PyObject **cache) { - if (*cache == NULL) { + if (NPY_UNLIKELY(*cache == NULL)) { PyObject *mod = PyImport_ImportModule(module); if (mod != NULL) { diff --git a/numpy/core/src/multiarray/common.h b/numpy/core/src/multiarray/common.h index 4913eb202..78a15a63c 100644 --- a/numpy/core/src/multiarray/common.h +++ b/numpy/core/src/multiarray/common.h @@ -5,6 +5,7 @@ #include <numpy/npy_cpu.h> #include <numpy/ndarraytypes.h> #include <limits.h> +#include "npy_import.h" #define error_converting(x) (((x) == -1) && PyErr_Occurred()) @@ -148,13 +149,9 @@ check_and_adjust_axis_msg(int *axis, int ndim, PyObject *msg_prefix) static PyObject *AxisError_cls = NULL; PyObject *exc; + npy_cache_import("numpy.core._exceptions", "AxisError", &AxisError_cls); if (AxisError_cls == NULL) { - PyObject *mod = PyImport_ImportModule("numpy.core._exceptions"); - - if (mod != NULL) { - AxisError_cls = PyObject_GetAttrString(mod, "AxisError"); - Py_DECREF(mod); - } + return -1; } /* Invoke the AxisError constructor */ diff --git a/numpy/core/src/multiarray/dtype_transfer.c b/numpy/core/src/multiarray/dtype_transfer.c index ecaa680ec..a26426d41 100644 --- a/numpy/core/src/multiarray/dtype_transfer.c +++ b/numpy/core/src/multiarray/dtype_transfer.c @@ -696,17 +696,15 @@ get_nbo_cast_numeric_transfer_function(int aligned, if (PyTypeNum_ISCOMPLEX(src_type_num) && !PyTypeNum_ISCOMPLEX(dst_type_num) && !PyTypeNum_ISBOOL(dst_type_num)) { - PyObject *cls = NULL, *obj = NULL; + static PyObject *cls = NULL; int ret; - obj = PyImport_ImportModule("numpy.core"); - if (obj) { - cls = PyObject_GetAttrString(obj, "ComplexWarning"); - Py_DECREF(obj); + npy_cache_import("numpy.core", "ComplexWarning", &cls); + if (cls == NULL) { + return NPY_FAIL; } ret = PyErr_WarnEx(cls, "Casting complex values to real discards " "the imaginary part", 1); - Py_XDECREF(cls); if (ret < 0) { return NPY_FAIL; } diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c index e2026ec1c..8a659fa69 100644 --- a/numpy/core/src/multiarray/methods.c +++ b/numpy/core/src/multiarray/methods.c @@ -54,33 +54,6 @@ NpyArg_ParseKeywords(PyObject *keys, const char *format, char **kwlist, ...) return ret; } -static PyObject * -get_forwarding_ndarray_method(const char *name) -{ - PyObject *module_methods, *callable; - - /* Get a reference to the function we're calling */ - module_methods = PyImport_ImportModule("numpy.core._methods"); - if (module_methods == NULL) { - return NULL; - } - callable = _PyDict_GetItemStringWithError(PyModule_GetDict(module_methods), name); - if (callable == NULL && PyErr_Occurred()) { - Py_DECREF(module_methods); - return NULL; - } - if (callable == NULL) { - Py_DECREF(module_methods); - PyErr_Format(PyExc_RuntimeError, - "NumPy internal error: could not find function " - "numpy.core._methods.%s", name); - } - else { - Py_INCREF(callable); - } - Py_DECREF(module_methods); - return callable; -} /* * Forwards an ndarray method to a the Python function @@ -121,11 +94,9 @@ forward_ndarray_method(PyArrayObject *self, PyObject *args, PyObject *kwds, */ #define NPY_FORWARD_NDARRAY_METHOD(name) \ static PyObject *callable = NULL; \ + npy_cache_import("numpy.core._methods", name, &callable); \ if (callable == NULL) { \ - callable = get_forwarding_ndarray_method(name); \ - if (callable == NULL) { \ - return NULL; \ - } \ + return NULL; \ } \ return forward_ndarray_method(self, args, kwds, callable) diff --git a/numpy/core/src/umath/scalarmath.c.src b/numpy/core/src/umath/scalarmath.c.src index bb2915e09..90cc7a513 100644 --- a/numpy/core/src/umath/scalarmath.c.src +++ b/numpy/core/src/umath/scalarmath.c.src @@ -16,6 +16,7 @@ #include "numpy/ufuncobject.h" #include "numpy/arrayscalars.h" +#include "npy_import.h" #include "npy_pycompat.h" #include "numpy/halffloat.h" @@ -1339,13 +1340,9 @@ static int emit_complexwarning(void) { static PyObject *cls = NULL; + npy_cache_import("numpy.core", "ComplexWarning", &cls); if (cls == NULL) { - PyObject *mod; - mod = PyImport_ImportModule("numpy.core"); - assert(mod != NULL); - cls = PyObject_GetAttrString(mod, "ComplexWarning"); - assert(cls != NULL); - Py_DECREF(mod); + return -1; } return PyErr_WarnEx(cls, "Casting complex values to real discards the imaginary part", 1); |