Skip to content

Commit

Permalink
restructure defines and undef NPY_API_SYMBOL_ATTRIBUTE
Browse files Browse the repository at this point in the history
  • Loading branch information
hobu committed Sep 23, 2024
1 parent c39e593 commit 5ff8640
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 24 deletions.
20 changes: 11 additions & 9 deletions src/pdal/io/NumpyReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
#include <pdal/util/Algorithm.hpp>
#include <pdal/util/Extractor.hpp>

#include "../plang/Environment.hpp"


#if NPY_ABI_VERSION < 0x02000000
#define PyDataType_FIELDS(descr) ((descr)->fields)
Expand Down Expand Up @@ -138,6 +138,7 @@ void NumpyReader::setArray(PyObject* array)
Py_XINCREF(m_array);
}


PyArrayObject* load_npy_file(std::string const& filename)
{

Expand Down Expand Up @@ -167,10 +168,11 @@ PyArrayObject* load_npy_file(std::string const& filename)
if (!array)
throw pdal::pdal_error(plang::getTraceback());

PyArrayObject* output = reinterpret_cast<PyArrayObject*>(array);
if (!output)
throw pdal_error(plang::getTraceback());
return output;

PyArrayObject* nparray = reinterpret_cast<PyArrayObject*>(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,
Expand Down Expand Up @@ -201,10 +203,10 @@ PyArrayObject* load_npy_script(std::string const& source,

Py_XDECREF(scriptArgs);

PyArrayObject* output = reinterpret_cast<PyArrayObject*>(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<PyArrayObject*>(array);
}

void NumpyReader::initialize()
Expand Down
8 changes: 3 additions & 5 deletions src/pdal/io/NumpyReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,11 @@
#include <pdal/Reader.hpp>
#include <pdal/Streamable.hpp>

#include "../plang/Environment.hpp"
#include "../plang/Invocation.hpp"

#include <Python.h>
#define NPY_NO_DEPRECATED_API NPY_1_22_API_VERSION
#define PY_ARRAY_UNIQUE_SYMBOL PDAL_ARRAY_API
#define NO_IMPORT_ARRAY
#include <numpy/arrayobject.h>
#define NO_IMPORT_ARRAY // Already have it from Environment.hpp
#include <numpy/ndarrayobject.h>

#include <memory>

Expand Down
14 changes: 7 additions & 7 deletions src/pdal/plang/Environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <numpy/arrayobject.h>
#include <numpy/ndarrayobject.h>
#include <pdal/util/FileUtils.hpp>
#include <pdal/util/Utils.hpp>

Expand Down Expand Up @@ -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 ;
};

Expand All @@ -165,7 +166,6 @@ Environment::Environment()
throw pdal_error("unable to add redirector module!");
}


initNumpy();
PyImport_ImportModule("redirector");

Expand Down
5 changes: 5 additions & 0 deletions src/pdal/plang/Environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions src/pdal/plang/Invocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@

#include <pdal/util/Algorithm.hpp>

#define NPY_NO_DEPRECATED_API NPY_1_22_API_VERSION
#define NO_IMPORT_ARRAY
#define PY_ARRAY_UNIQUE_SYMBOL PDAL_ARRAY_API
#include <numpy/arrayobject.h>
#include <numpy/ndarrayobject.h>

namespace
{
Expand Down
Binary file modified src/pdal/test/data/1.2-with-color.npy
Binary file not shown.

0 comments on commit 5ff8640

Please sign in to comment.