diff options
author | Jaime <jaime.frio@gmail.com> | 2015-06-04 15:47:57 -0700 |
---|---|---|
committer | Jaime <jaime.frio@gmail.com> | 2015-06-04 15:47:57 -0700 |
commit | b1cfccd6d78be622ee85f658e8f9b4a2d5fc9ddb (patch) | |
tree | 71da0b78e413453f325fafd2998086f325520b4f | |
parent | 36b940497f194b1b90e9ff48c58a01a6e74c09de (diff) | |
parent | 403d61d1448eb8ee6a2ddc81865d9ebaf1ef73df (diff) | |
download | numpy-b1cfccd6d78be622ee85f658e8f9b4a2d5fc9ddb.tar.gz |
Merge pull request #5940 from charris/add-python-func-import
ENH: Add inline C function to import and cache Python functions.
-rw-r--r-- | numpy/core/src/private/npy_import.h | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/numpy/core/src/private/npy_import.h b/numpy/core/src/private/npy_import.h new file mode 100644 index 000000000..e618e121e --- /dev/null +++ b/numpy/core/src/private/npy_import.h @@ -0,0 +1,33 @@ +#ifndef NPY_IMPORT_H +#define NPY_IMPORT_H + +#include <Python.h> +#include <assert.h> + +/*! \brief Fetch and cache Python function. + * + * Import a Python function and cache it for use. The function checks if + * cache is NULL, and if not NULL imports the Python function specified by + * \a module and \a function, increments its reference count, and stores + * the result in \a cache. Usually \a cache will be a static variable and + * should be initialized to NULL. On error \a cache will contain NULL on + * exit, + * + * @param module Absolute module name. + * @param function Function name. + * @param cache Storage location for imported function. + */ +NPY_INLINE void +npy_cache_pyfunc(const char *module, const char *function, PyObject **cache) +{ + if (*cache == NULL) { + PyObject *mod = PyImport_ImportModule(module); + + if (mod != NULL) { + *cache = PyObject_GetAttrString(mod, function); + Py_DECREF(mod); + } + } +} + +#endif |