Skip to content

Commit 5e31b65

Browse files
authored
Set Numpy floor to 1.22 and NPY_NO_DEPRECATED_API NPY_1_22_API_VERSION (#167)
* Set Numpy floor to 1.22 and NPY_NO_DEPRECATED_API NPY_1_22_API_VERSION * set PY_ARRAY_UNIQUE_SYMBOL to PDAL_ARRAY_API and only import it in extension initialization. Guard against using the extension against any PDAL verisions < 2.6 * debug PDAL_DRIVER_PATH issues * bug in --print-plugin-path * only one os.path.sep * concat PDAL_DRIVER_PATH with PDAL_PLUGIN_PATH
1 parent 52f94b0 commit 5e31b65

File tree

10 files changed

+53
-28
lines changed

10 files changed

+53
-28
lines changed

.github/workflows/build.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,12 @@ jobs:
5757

5858
- name: Test
5959
run: |
60-
export PDAL_DRIVER_PATH=$(python -m pdal --pdal-plugin-path)
61-
echo $PDAL_DRIVER_PATH
60+
export PDAL_DRIVER_PATH=$(python -m pdal --pdal-driver-path)
61+
export PDAL_PLUGIN_PATH=$(python -m pdal --pdal-plugin-path)
62+
echo "PDAL_DRIVER_PATH $PDAL_DRIVER_PATH"
63+
echo "PDAL_PLUGIN_PATH $PDAL_PLUGIN_PATH"
64+
export PDAL_DRIVER_PATH=$PDAL_PLUGIN_PATH:$PDAL_DRIVER_PATH
65+
python -m pdal
6266
pdal --drivers --debug
6367
py.test -v test/
6468

CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
cmake_minimum_required(VERSION 3.11.0)
2-
project(pdal-python VERSION)
2+
project(pdal-python VERSION ${SKBUILD_PROJECT_VERSION}
3+
DESCRIPTION "PDAL Python bindings"
4+
HOMEPAGE_URL "https://github.com/PDAL/Python")
35

46
set(CMAKE_CXX_STANDARD 17)
57
set(CMAKE_CXX_STANDARD_REQUIRED ON)

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ classifiers = [
2626
]
2727

2828
dependencies = [
29-
"numpy"
29+
"numpy >= 1.22"
3030
]
3131

3232
dynamic = ["version"]
@@ -48,7 +48,7 @@ repository = "https://github.com/PDAL/Python"
4848
changelog = "https://github.com/PDAL/python/blob/main/README.rst"
4949

5050
[build-system]
51-
requires = ["scikit-build-core >= 0.9", "numpy", "pybind11[global]"]
51+
requires = ["scikit-build-core >= 0.9", "numpy >= 1.22", "pybind11[global]"]
5252
build-backend = "scikit_build_core.build"
5353

5454

src/pdal/PyArray.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,6 @@ std::string toString(PyObject *pname)
9292

9393
Array::Array(PyArrayObject* array) : m_array(array), m_rowMajor(true)
9494
{
95-
if (_import_array() < 0)
96-
throw pdal_error("Could not import numpy.core.multiarray.");
97-
9895
Py_XINCREF(array);
9996

10097
PyArray_Descr *dtype = PyArray_DTYPE(m_array);

src/pdal/PyArray.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,17 @@
3535
#pragma once
3636

3737
#include <pdal/PointView.hpp>
38+
39+
#define NPY_TARGET_VERSION NPY_1_22_API_VERSION
40+
#define NPY_NO_DEPRECATED_API NPY_1_22_API_VERSION
41+
42+
#define NO_IMPORT_ARRAY
43+
#define PY_ARRAY_UNIQUE_SYMBOL PDAL_ARRAY_API
44+
3845
#include <pdal/io/MemoryViewReader.hpp>
46+
3947
#include <numpy/ndarraytypes.h>
48+
#include <numpy/arrayobject.h>
4049

4150
#include <vector>
4251
#include <memory>

src/pdal/PyPipeline.cpp

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -211,16 +211,7 @@ std::string PipelineExecutor::getQuickInfo() const
211211

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

@@ -320,8 +311,6 @@ PyObject* buildNumpyDescriptor(PointLayoutPtr layout)
320311

321312
PyArrayObject* viewToNumpyArray(PointViewPtr view)
322313
{
323-
if (_import_array() < 0)
324-
throw pdal_error("Could not import numpy.core.multiarray.");
325314

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

345334
PyArrayObject* meshToNumpyArray(const TriangularMesh* mesh)
346335
{
347-
if (_import_array() < 0)
348-
throw pdal_error("Could not import numpy.core.multiarray.");
349-
350336
// Build up a numpy dtype dictionary
351337
//
352338
// {'formats': ['f8', 'f8', 'f8', 'u2', 'u1', 'u1', 'u1', 'u1', 'u1',

src/pdal/PyPipeline.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@
3535
#pragma once
3636

3737
#include <pdal/PipelineManager.hpp>
38+
39+
#define NPY_TARGET_VERSION NPY_1_22_API_VERSION
40+
#define NPY_NO_DEPRECATED_API NPY_1_22_API_VERSION
41+
42+
#define NO_IMPORT_ARRAY
43+
#define PY_ARRAY_UNIQUE_SYMBOL PDAL_ARRAY_API
44+
3845
#include <numpy/arrayobject.h>
3946

4047
namespace pdal

src/pdal/StreamableExecutor.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
#include "PyPipeline.hpp"
3636
#include "StreamableExecutor.hpp"
3737

38+
#define NO_IMPORT_ARRAY
39+
#define PY_ARRAY_UNIQUE_SYMBOL PDAL_ARRAY_API
40+
3841
#include <Python.h>
3942
#include <numpy/arrayobject.h>
4043

@@ -67,8 +70,7 @@ void PythonPointTable::finalize()
6770

6871
// create dtype
6972
auto gil = PyGILState_Ensure();
70-
if (_import_array() < 0)
71-
std::cerr << "Could not import array!\n";
73+
7274
PyObject *dtype_dict = buildNumpyDescriptor(&m_layout);
7375
if (PyArray_DescrConverter(dtype_dict, &m_dtype) == NPY_FAIL)
7476
throw pdal_error("Unable to create numpy dtype");
@@ -102,8 +104,8 @@ void PythonPointTable::py_resizeArray(point_count_t np)
102104
{
103105
if (src_idx != dest_idx)
104106
{
105-
PyObject* src_item = PyArray_GETITEM(m_curArray, PyArray_GETPTR1(m_curArray, src_idx));
106-
PyArray_SETITEM(m_curArray, PyArray_GETPTR1(m_curArray, dest_idx), src_item);
107+
PyObject* src_item = PyArray_GETITEM(m_curArray, (const char*) PyArray_GETPTR1(m_curArray, src_idx));
108+
PyArray_SETITEM(m_curArray, (char*) PyArray_GETPTR1(m_curArray, dest_idx), src_item);
107109
Py_XDECREF(src_item);
108110
}
109111
dest_idx++;

src/pdal/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def print_driver_path(args):
2323
print (os.environ['PDAL_DRIVER_PATH'])
2424

2525
def print_plugin_path(args):
26-
purelib = sysconfig.get_paths()["purelib"]+os.path.sep+"pdal"
26+
purelib = sysconfig.get_paths()["purelib"]
2727

2828
if sys.platform == "linux" or sys.platform == "linux2":
2929
suffix = 'so'

src/pdal/libpdalpython.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77
#include <pdal/pdal_config.hpp>
88
#include <pdal/StageFactory.hpp>
99

10+
#define NPY_TARGET_VERSION NPY_1_22_API_VERSION
11+
#define NPY_NO_DEPRECATED_API NPY_1_22_API_VERSION
12+
13+
#define PY_ARRAY_UNIQUE_SYMBOL PDAL_ARRAY_API
14+
15+
#include <numpy/arrayobject.h>
16+
1017
#include "PyArray.hpp"
1118
#include "PyDimension.hpp"
1219
#include "PyPipeline.hpp"
@@ -283,8 +290,12 @@ namespace pdal {
283290
int _loglevel;
284291
};
285292

293+
294+
286295
PYBIND11_MODULE(libpdalpython, m)
287296
{
297+
_import_array();
298+
288299
py::class_<PipelineIterator>(m, "PipelineIterator")
289300
.def("__iter__", [](PipelineIterator &it) -> PipelineIterator& { return it; })
290301
.def("__next__", &PipelineIterator::executeNext)
@@ -294,6 +305,7 @@ namespace pdal {
294305
.def_property_readonly("pipeline", &PipelineIterator::getPipeline)
295306
.def_property_readonly("metadata", &PipelineIterator::getMetadata);
296307

308+
297309
py::class_<Pipeline>(m, "Pipeline")
298310
.def(py::init<>())
299311
.def("execute", &Pipeline::execute)
@@ -319,6 +331,12 @@ namespace pdal {
319331
m.def("getDimensions", &getDimensions);
320332
m.def("infer_reader_driver", &getReaderDriver);
321333
m.def("infer_writer_driver", &getWriterDriver);
334+
335+
if (pdal::Config::versionMajor() < 2)
336+
throw pybind11::import_error("PDAL version must be >= 2.6");
337+
338+
if (pdal::Config::versionMajor() == 2 && pdal::Config::versionMinor() < 6)
339+
throw pybind11::import_error("PDAL version must be >= 2.6");
322340
};
323341

324342
}; // namespace pdal

0 commit comments

Comments
 (0)