diff --git a/src/pdal/io/NumpyReader.cpp b/src/pdal/io/NumpyReader.cpp index 98e18b9..b30eb8a 100644 --- a/src/pdal/io/NumpyReader.cpp +++ b/src/pdal/io/NumpyReader.cpp @@ -43,7 +43,7 @@ #include #include -#include "../plang/Environment.hpp" + #if NPY_ABI_VERSION < 0x02000000 #define PyDataType_FIELDS(descr) ((descr)->fields) @@ -138,6 +138,7 @@ void NumpyReader::setArray(PyObject* array) Py_XINCREF(m_array); } + PyArrayObject* load_npy_file(std::string const& filename) { @@ -167,10 +168,11 @@ PyArrayObject* load_npy_file(std::string const& filename) if (!array) throw pdal::pdal_error(plang::getTraceback()); - PyArrayObject* output = reinterpret_cast(array); - if (!output) - throw pdal_error(plang::getTraceback()); - return output; + + PyArrayObject* nparray = reinterpret_cast(array); + if (!PyArray_Check(array)) + throw pdal_error("Numpy file did not return an array!"); + return nparray; } PyArrayObject* load_npy_script(std::string const& source, @@ -201,10 +203,10 @@ PyArrayObject* load_npy_script(std::string const& source, Py_XDECREF(scriptArgs); - PyArrayObject* output = reinterpret_cast(array); - if (!output) - throw pdal_error(plang::getTraceback()); - return output; + if (!PyArray_Check(array)) + throw pdal_error("Numpy script did not return an array!"); + + return reinterpret_cast(array); } void NumpyReader::initialize() diff --git a/src/pdal/io/NumpyReader.hpp b/src/pdal/io/NumpyReader.hpp index ebffbb6..e9f19a6 100644 --- a/src/pdal/io/NumpyReader.hpp +++ b/src/pdal/io/NumpyReader.hpp @@ -37,13 +37,11 @@ #include #include +#include "../plang/Environment.hpp" #include "../plang/Invocation.hpp" -#include -#define NPY_NO_DEPRECATED_API NPY_1_22_API_VERSION -#define PY_ARRAY_UNIQUE_SYMBOL PDAL_ARRAY_API -#define NO_IMPORT_ARRAY -#include +#define NO_IMPORT_ARRAY // Already have it from Environment.hpp +#include #include diff --git a/src/pdal/plang/Environment.cpp b/src/pdal/plang/Environment.cpp index 210aa64..85bb78c 100644 --- a/src/pdal/plang/Environment.cpp +++ b/src/pdal/plang/Environment.cpp @@ -39,12 +39,8 @@ #include "Environment.hpp" #include "Redirector.hpp" -#define NPY_TARGET_VERSION NPY_1_22_API_VERSION -#define NPY_NO_DEPRECATED_API NPY_1_22_API_VERSION -#define PY_ARRAY_UNIQUE_SYMBOL PDAL_ARRAY_API - -#include +#include #include #include @@ -147,7 +143,12 @@ Environment::Environment() { // #undef NUMPY_IMPORT_ARRAY_RETVAL // #define NUMPY_IMPORT_ARRAY_RETVAL VOID - _import_array(); + +#if NPY_ABI_VERSION < 0x02000000 + import_array(); +#else + PyArray_ImportNumPyAPI(); +#endif return ; }; @@ -165,7 +166,6 @@ Environment::Environment() throw pdal_error("unable to add redirector module!"); } - initNumpy(); PyImport_ImportModule("redirector"); diff --git a/src/pdal/plang/Environment.hpp b/src/pdal/plang/Environment.hpp index fed4fd6..c5ab5a1 100644 --- a/src/pdal/plang/Environment.hpp +++ b/src/pdal/plang/Environment.hpp @@ -49,6 +49,11 @@ #include "Script.hpp" #include "gil.hpp" +#define PY_ARRAY_UNIQUE_SYMBOL PDAL_ARRAY_API +#define NPY_TARGET_VERSION NPY_1_22_API_VERSION +#define NPY_API_SYMBOL_ATTRIBUTE +#define NPY_NO_DEPRECATED_API NPY_1_22_API_VERSION + namespace pdal { namespace plang diff --git a/src/pdal/plang/Invocation.cpp b/src/pdal/plang/Invocation.cpp index ee9342f..808fe5f 100644 --- a/src/pdal/plang/Invocation.cpp +++ b/src/pdal/plang/Invocation.cpp @@ -36,10 +36,8 @@ #include -#define NPY_NO_DEPRECATED_API NPY_1_22_API_VERSION #define NO_IMPORT_ARRAY -#define PY_ARRAY_UNIQUE_SYMBOL PDAL_ARRAY_API -#include +#include namespace { diff --git a/src/pdal/test/data/1.2-with-color.npy b/src/pdal/test/data/1.2-with-color.npy index ee9b0fd..5930c18 100644 Binary files a/src/pdal/test/data/1.2-with-color.npy and b/src/pdal/test/data/1.2-with-color.npy differ