diff --git a/.github/workflows/ubuntu.yml b/.github/workflows/ubuntu.yml index 772681c29b..fdc87e9b56 100644 --- a/.github/workflows/ubuntu.yml +++ b/.github/workflows/ubuntu.yml @@ -32,7 +32,7 @@ jobs: steps: - name: Check Out Repo - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Login to Docker Hub uses: docker/login-action@v2 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 11302f6a89..bd0018d779 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -29,7 +29,7 @@ repos: - id: isort args: ["--profile", "black", "--filter-files"] - repo: https://github.com/asottile/pyupgrade - rev: v3.6.0 + rev: v3.21.2 hooks: - id: pyupgrade - repo: https://github.com/PyCQA/flake8 diff --git a/Makefile.in b/Makefile.in index 8c10866c76..f3e4972a47 100755 --- a/Makefile.in +++ b/Makefile.in @@ -340,7 +340,7 @@ endif @cd main; $(MAKE) clean @rm -fr bin lib -clean: clean-light clean-test python_clean clean-debian +clean: clean-light clean-test clean-debian @echo " CLEAN libvtkfortran" @cd libvtkfortran; $(MAKE) clean @echo " CLEAN libjudy" @@ -547,7 +547,7 @@ install: default fltools cp -R schemas $(DESTDIR)$(datadir)/fluidity/ mkdir -p $(DESTDIR)$(datadir)/diamond/schemata $(SED) 's/$${datadir}/$(subst /,\/,$(datadir))/g' schemas/flml.in > $(DESTDIR)$(datadir)/diamond/schemata/flml - cd python ; python3 setup.py install --root=$(shell echo ${DESTDIR} | sed 's/^$$/\//') --prefix="$(prefix)" $$FLUIDITY_PYTHON_INSTALL_ARGS + cd python ; python3 -m pip install --root=$(shell echo ${DESTDIR} | sed 's/^$$/\//') --prefix="$(prefix)" $$FLUIDITY_PYTHON_INSTALL_ARGS . cp -R examples $(DESTDIR)$(docdir)/fluidity find $(DESTDIR)$(docdir)/fluidity/examples -type f -exec $(SED) -i "s/\.\.\/\.\.\/\.\.\/bin\///" '{}' \; find $(DESTDIR)$(docdir)/fluidity/examples -type f -exec $(SED) -i "s/\.\.\/\.\.\/bin\///" '{}' \; diff --git a/climatology/Makefile.in b/climatology/Makefile.in index 98eef68ae6..186af045e6 100644 --- a/climatology/Makefile.in +++ b/climatology/Makefile.in @@ -32,7 +32,7 @@ LINKER = $(CXX) CXXFLAGS = -I../include @CXXFLAGS@ @CPPFLAGS@ LFLAGS = @LDFLAGS@ -LIBS = -L../lib/ @LIBS@ @FLIBS@ @LAPACK_LIBS@ +LIBS = -L../lib/ @LIBS@ .SUFFIXES: .cpp .o diff --git a/femtools/embed_python.c b/femtools/embed_python.c index 0de35faac6..1050044337 100644 --- a/femtools/embed_python.c +++ b/femtools/embed_python.c @@ -43,6 +43,17 @@ USA #ifdef HAVE_NUMPY #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION #include "numpy/arrayobject.h" + +static int ensure_numpy_api(void) { + if (PyArray_API != NULL) { + return 0; + } +#if NPY_ABI_VERSION < 0x02000000 + return _import_array(); +#else + return PyArray_ImportNumPyAPI(); +#endif +} #endif void deallocate_c_array(void *ptr) { @@ -448,7 +459,11 @@ void set_tensor_field_from_python(char *function, int function_len, int dim, *stat = 1; return; #else - import_array(); + if (ensure_numpy_api() < 0) { + PyErr_Print(); + *stat = 1; + return; + } set_field_from_python(function, function_len, dim, nodes, NULL, x, y, z, t, NULL, stat, result_dim, (void**)&result, set_tensor_result_double, NULL); @@ -637,7 +652,11 @@ void set_field_from_python_fields(char *function, int function_len, int dim, int #else PyObject *pLocals, *pFunc, *pNames, *pT, *pdT, *pPos, *pArgs, *pKwArgs, *pResult; - import_array(); + if (ensure_numpy_api() < 0) { + PyErr_Print(); + *stat = 1; + return; + } // load the user's function as a Python object -- borrows locals pLocals = PyDict_New(); diff --git a/femtools/python_statec.c b/femtools/python_statec.c index bf584012e0..f3e46af589 100644 --- a/femtools/python_statec.c +++ b/femtools/python_statec.c @@ -1,6 +1,19 @@ #define ALLOW_IMPORT_ARRAY #include "python_statec.h" +#ifdef HAVE_NUMPY +static int ensure_numpy_api(void) { + if (PyArray_API != NULL) { + return 0; + } +#if NPY_ABI_VERSION < 0x02000000 + return _import_array(); +#else + return PyArray_ImportNumPyAPI(); +#endif +} +#endif + #if PY_MAJOR_VERSION >= 3 #define PyInt_FromLong PyLong_FromLong #define PyString_FromString PyUnicode_FromString @@ -47,7 +60,11 @@ void python_init_(void){ #endif #ifdef HAVE_NUMPY // Enable use of NumPy arrays in C - import_array(); + if (ensure_numpy_api() < 0) { + PyErr_Print(); + fprintf(stderr, "Error: Initializing the NumPy C API failed.\n"); + return; + } // Import the NumPy module in our Python interpreter if(PyRun_SimpleString("import numpy") == -1)