Skip to content

Commit

Permalink
set PY_ARRAY_UNIQUE_SYMBOL to PDAL_ARRAY_API and only import it in ex…
Browse files Browse the repository at this point in the history
…tension initialization. Guard against using the extension against any PDAL verisions < 2.6
  • Loading branch information
hobu committed Apr 25, 2024
1 parent af859ec commit f1243ca
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 20 deletions.
3 changes: 0 additions & 3 deletions src/pdal/PyArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,6 @@ std::string toString(PyObject *pname)

Array::Array(PyArrayObject* array) : m_array(array), m_rowMajor(true)
{
if (_import_array() < 0)
throw pdal_error("Could not import numpy.core.multiarray.");

Py_XINCREF(array);

PyArray_Descr *dtype = PyArray_DTYPE(m_array);
Expand Down
4 changes: 4 additions & 0 deletions src/pdal/PyArray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@
#define NPY_TARGET_VERSION NPY_1_22_API_VERSION
#define NPY_NO_DEPRECATED_API NPY_1_22_API_VERSION

#define NO_IMPORT_ARRAY
#define PY_ARRAY_UNIQUE_SYMBOL PDAL_ARRAY_API

#include <pdal/io/MemoryViewReader.hpp>

#include <numpy/ndarraytypes.h>
#include <numpy/arrayobject.h>

#include <vector>
#include <memory>
Expand Down
16 changes: 1 addition & 15 deletions src/pdal/PyPipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,16 +211,7 @@ std::string PipelineExecutor::getQuickInfo() const

void PipelineExecutor::addArrayReaders(std::vector<std::shared_ptr<Array>> arrays)
{
// Make the symbols in pdal_base global so that they're accessible
// to PDAL plugins. Python dlopen's this extension with RTLD_LOCAL,
// which means that without this, symbols in libpdal_base aren't available
// for resolution of symbols on future runtime linking. This is an issue
// on Alpine and other Linux variants that don't use UNIQUE symbols
// for C++ template statics only. Without this, you end up with multiple
// copies of template statics.
#ifndef _WIN32
::dlopen("libpdal_base.so", RTLD_NOLOAD | RTLD_GLOBAL);
#endif

if (arrays.empty())
return;

Expand Down Expand Up @@ -320,8 +311,6 @@ PyObject* buildNumpyDescriptor(PointLayoutPtr layout)

PyArrayObject* viewToNumpyArray(PointViewPtr view)
{
if (_import_array() < 0)
throw pdal_error("Could not import numpy.core.multiarray.");

PyObject* dtype_dict = buildNumpyDescriptor(view->layout());
PyArray_Descr *dtype = nullptr;
Expand All @@ -344,9 +333,6 @@ PyArrayObject* viewToNumpyArray(PointViewPtr view)

PyArrayObject* meshToNumpyArray(const TriangularMesh* mesh)
{
if (_import_array() < 0)
throw pdal_error("Could not import numpy.core.multiarray.");

// Build up a numpy dtype dictionary
//
// {'formats': ['f8', 'f8', 'f8', 'u2', 'u1', 'u1', 'u1', 'u1', 'u1',
Expand Down
3 changes: 3 additions & 0 deletions src/pdal/PyPipeline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
#define NPY_TARGET_VERSION NPY_1_22_API_VERSION
#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>

namespace pdal
Expand Down
6 changes: 4 additions & 2 deletions src/pdal/StreamableExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
#include "PyPipeline.hpp"
#include "StreamableExecutor.hpp"

#define NO_IMPORT_ARRAY
#define PY_ARRAY_UNIQUE_SYMBOL PDAL_ARRAY_API

#include <Python.h>
#include <numpy/arrayobject.h>

Expand Down Expand Up @@ -67,8 +70,7 @@ void PythonPointTable::finalize()

// create dtype
auto gil = PyGILState_Ensure();
if (_import_array() < 0)
std::cerr << "Could not import array!\n";

PyObject *dtype_dict = buildNumpyDescriptor(&m_layout);
if (PyArray_DescrConverter(dtype_dict, &m_dtype) == NPY_FAIL)
throw pdal_error("Unable to create numpy dtype");
Expand Down
18 changes: 18 additions & 0 deletions src/pdal/libpdalpython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
#include <pdal/pdal_config.hpp>
#include <pdal/StageFactory.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 "PyArray.hpp"
#include "PyDimension.hpp"
#include "PyPipeline.hpp"
Expand Down Expand Up @@ -283,8 +290,12 @@ namespace pdal {
int _loglevel;
};



PYBIND11_MODULE(libpdalpython, m)
{
_import_array();

py::class_<PipelineIterator>(m, "PipelineIterator")
.def("__iter__", [](PipelineIterator &it) -> PipelineIterator& { return it; })
.def("__next__", &PipelineIterator::executeNext)
Expand All @@ -294,6 +305,7 @@ namespace pdal {
.def_property_readonly("pipeline", &PipelineIterator::getPipeline)
.def_property_readonly("metadata", &PipelineIterator::getMetadata);


py::class_<Pipeline>(m, "Pipeline")
.def(py::init<>())
.def("execute", &Pipeline::execute)
Expand All @@ -319,6 +331,12 @@ namespace pdal {
m.def("getDimensions", &getDimensions);
m.def("infer_reader_driver", &getReaderDriver);
m.def("infer_writer_driver", &getWriterDriver);

if (pdal::Config::versionMajor() < 2)
throw pybind11::import_error("PDAL version must be >= 2.6");

if (pdal::Config::versionMajor() == 2 && pdal::Config::versionMinor() < 6)
throw pybind11::import_error("PDAL version must be >= 2.6");
};

}; // namespace pdal

0 comments on commit f1243ca

Please sign in to comment.