From 30a9438b012b677af1ed9255e316c4d2571bccdb Mon Sep 17 00:00:00 2001 From: Shaohui Liu Date: Wed, 20 Aug 2025 14:00:12 +0200 Subject: [PATCH] Upgrade pybind11 to 3.0.0. --- _pyceres/core/callbacks.h | 8 +++++--- _pyceres/core/cost_functions.h | 5 +++-- _pyceres/core/covariance.h | 4 ++-- _pyceres/core/crs_matrix.h | 2 +- _pyceres/core/loss_functions.h | 24 ++++++++---------------- _pyceres/core/manifold.h | 14 +++++++------- _pyceres/core/problem.h | 8 ++++---- _pyceres/core/solver.h | 9 ++++----- _pyceres/helpers.h | 4 ++-- _pyceres/logging.h | 2 +- pyproject.toml | 2 +- 11 files changed, 38 insertions(+), 44 deletions(-) diff --git a/_pyceres/core/callbacks.h b/_pyceres/core/callbacks.h index 0fe0aa4..17a712f 100644 --- a/_pyceres/core/callbacks.h +++ b/_pyceres/core/callbacks.h @@ -8,7 +8,8 @@ namespace py = pybind11; // Trampoline class so we can create an EvaluationCallback in Python. -class PyEvaluationCallBack : public ceres::EvaluationCallback { +class PyEvaluationCallBack : public ceres::EvaluationCallback, + py::trampoline_self_life_support { public: /* Inherit the constructors */ using ceres::EvaluationCallback::EvaluationCallback; @@ -25,7 +26,8 @@ class PyEvaluationCallBack : public ceres::EvaluationCallback { } }; -class PyIterationCallback : public ceres::IterationCallback { +class PyIterationCallback : public ceres::IterationCallback, + py::trampoline_self_life_support { public: using ceres::IterationCallback::IterationCallback; @@ -43,7 +45,7 @@ class PyIterationCallback : public ceres::IterationCallback { PYBIND11_MAKE_OPAQUE(std::vector); void BindCallbacks(py::module& m) { - py::class_(m, "EvaluationCallback") .def(py::init<>()); } diff --git a/_pyceres/core/cost_functions.h b/_pyceres/core/cost_functions.h index 6564a41..ba64d0c 100644 --- a/_pyceres/core/cost_functions.h +++ b/_pyceres/core/cost_functions.h @@ -10,7 +10,8 @@ namespace py = pybind11; // Class which we can use to create a ceres::CostFunction in python. // This allows use to create python based cost functions. -class PyCostFunction : public ceres::CostFunction { +class PyCostFunction : public ceres::CostFunction, + py::trampoline_self_life_support { public: // Inherit the constructors. using ceres::CostFunction::CostFunction; @@ -91,7 +92,7 @@ class PyCostFunction : public ceres::CostFunction { }; void BindCostFunctions(py::module& m) { - py::class_( + py::classh( m, "CostFunction") .def(py::init<>()) .def("num_residuals", &ceres::CostFunction::num_residuals) diff --git a/_pyceres/core/covariance.h b/_pyceres/core/covariance.h index 05a9917..83701dc 100644 --- a/_pyceres/core/covariance.h +++ b/_pyceres/core/covariance.h @@ -10,7 +10,7 @@ namespace py = pybind11; void BindCovariance(py::module& m) { using Options = ceres::Covariance::Options; - py::class_ PyOptions(m, "CovarianceOptions"); + py::classh PyOptions(m, "CovarianceOptions"); PyOptions.def(py::init<>()) .def_property( "num_threads", @@ -28,7 +28,7 @@ void BindCovariance(py::module& m) { .def_readwrite("apply_loss_function", &Options::apply_loss_function); MakeDataclass(PyOptions); - py::class_(m, "Covariance") + py::classh(m, "Covariance") .def(py::init()) .def( "compute", diff --git a/_pyceres/core/crs_matrix.h b/_pyceres/core/crs_matrix.h index c21577b..74ba82a 100644 --- a/_pyceres/core/crs_matrix.h +++ b/_pyceres/core/crs_matrix.h @@ -38,7 +38,7 @@ py::tuple ConvertCRSToPyTuple(const ceres::CRSMatrix& crsMatrix) { void BindCRSMatrix(py::module& m) { using CRSMatrix = ceres::CRSMatrix; - py::class_ PyCRSMatrix(m, "CRSMatrix"); + py::classh PyCRSMatrix(m, "CRSMatrix"); PyCRSMatrix.def(py::init<>()) .def_readonly("num_rows", &CRSMatrix::num_rows) .def_readonly("num_cols", &CRSMatrix::num_cols) diff --git a/_pyceres/core/loss_functions.h b/_pyceres/core/loss_functions.h index 5123a74..15d0a21 100644 --- a/_pyceres/core/loss_functions.h +++ b/_pyceres/core/loss_functions.h @@ -15,7 +15,8 @@ namespace py = pybind11; // Trampoline class so that we can create a LossFunction in Python. -class PyLossFunction : public ceres::LossFunction { +class PyLossFunction : public ceres::LossFunction, + py::trampoline_self_life_support { public: /* Inherit the constructors */ using ceres::LossFunction::LossFunction; @@ -86,9 +87,8 @@ std::shared_ptr CreateLossFunctionFromDict(py::dict dict) { } void BindLossFunctions(py::module& m) { - py::class_>(m, "LossFunction") + py::classh( + m, "LossFunction") .def(py::init<>()) .def(py::init(&CreateLossFunctionFromDict)) .def("evaluate", [](ceres::LossFunction& self, float v) { @@ -98,23 +98,15 @@ void BindLossFunctions(py::module& m) { }); py::implicitly_convertible(); - py::class_>(m, "TrivialLoss") + py::classh(m, "TrivialLoss") .def(py::init<>()); - py::class_>(m, "HuberLoss") + py::classh(m, "HuberLoss") .def(py::init()); - py::class_>(m, "SoftLOneLoss") + py::classh(m, "SoftLOneLoss") .def(py::init()); - py::class_>(m, "CauchyLoss") + py::classh(m, "CauchyLoss") .def(py::init()); } diff --git a/_pyceres/core/manifold.h b/_pyceres/core/manifold.h index 8a1115f..d5b8f4c 100644 --- a/_pyceres/core/manifold.h +++ b/_pyceres/core/manifold.h @@ -8,7 +8,7 @@ namespace py = pybind11; -class PyManifold : public ceres::Manifold { +class PyManifold : public ceres::Manifold, py::trampoline_self_life_support { /* Inherit the constructors */ using ceres::Manifold::Manifold; bool Plus(const double* x, @@ -57,23 +57,23 @@ class PyManifold : public ceres::Manifold { }; void BindManifold(py::module& m) { - py::class_(m, "Manifold") + py::classh(m, "Manifold") .def(py::init<>()) .def("ambient_size", &ceres::Manifold::AmbientSize) .def("tangent_size", &ceres::Manifold::TangentSize); - py::class_, ceres::Manifold>( + py::classh, ceres::Manifold>( m, "EuclideanManifold") .def(py::init()); - py::class_(m, "SubsetManifold") + py::classh(m, "SubsetManifold") .def(py::init&>()); - py::class_(m, + py::classh(m, "QuaternionManifold") .def(py::init<>()); - py::class_( + py::classh( m, "EigenQuaternionManifold") .def(py::init<>()); - py::class_, ceres::Manifold>( + py::classh, ceres::Manifold>( m, "SphereManifold") .def(py::init()); } diff --git a/_pyceres/core/problem.h b/_pyceres/core/problem.h index 74e64ca..71eb4ce 100644 --- a/_pyceres/core/problem.h +++ b/_pyceres/core/problem.h @@ -44,7 +44,7 @@ std::unique_ptr CreatePythonProblem() { void BindProblem(py::module& m) { using options = ceres::Problem::Options; - py::class_(m, "ProblemOptions") + py::classh(m, "ProblemOptions") .def(py::init(&CreateProblemOptions)) // Ensures default is that // Python manages memory .def_readonly("cost_function_ownership", @@ -56,7 +56,7 @@ void BindProblem(py::module& m) { .def_readwrite("disable_all_safety_checks", &options::disable_all_safety_checks); - py::class_(m, "EvaluateOptions") + py::classh(m, "EvaluateOptions") .def(py::init<>()) .def( "set_parameter_blocks", @@ -78,9 +78,9 @@ void BindProblem(py::module& m) { .def_readwrite("num_threads", &ceres::Problem::EvaluateOptions::num_threads); - py::class_ residual_block_wrapper(m, "ResidualBlock"); + py::classh residual_block_wrapper(m, "ResidualBlock"); - py::class_>(m, "Problem") + py::classh(m, "Problem") .def(py::init(&CreatePythonProblem)) .def(py::init()) .def("num_parameter_blocks", &ceres::Problem::NumParameterBlocks) diff --git a/_pyceres/core/solver.h b/_pyceres/core/solver.h index 4083939..35de604 100644 --- a/_pyceres/core/solver.h +++ b/_pyceres/core/solver.h @@ -11,8 +11,7 @@ namespace py = pybind11; void BindSolver(py::module& m) { using IterSummary = ceres::IterationSummary; - py::class_> PyIterSummary( - m, "IterationSummary"); + py::classh PyIterSummary(m, "IterationSummary"); PyIterSummary.def(py::init<>()) .def(py::init()) .def_readonly("iteration", &IterSummary::iteration) @@ -43,7 +42,7 @@ void BindSolver(py::module& m) { .def_readonly("cumulative_time_in_seconds", &IterSummary::cumulative_time_in_seconds); - py::class_(m, "IterationCallback") .def(py::init<>()) .def("__call__", &ceres::IterationCallback::operator()); @@ -64,7 +63,7 @@ void BindSolver(py::module& m) { std::vector>(); using Options = ceres::Solver::Options; - py::class_> PyOptions(m, "SolverOptions"); + py::classh PyOptions(m, "SolverOptions"); PyOptions.def(py::init<>()) .def(py::init()) .def("IsValid", &Options::IsValid) @@ -180,7 +179,7 @@ void BindSolver(py::module& m) { MakeDataclass(PyOptions); using Summary = ceres::Solver::Summary; - py::class_> PySummary(m, "SolverSummary"); + py::classh PySummary(m, "SolverSummary"); PySummary.def(py::init<>()) .def(py::init()) .def("BriefReport", &Summary::BriefReport) diff --git a/_pyceres/helpers.h b/_pyceres/helpers.h index c2521d5..dc43289 100644 --- a/_pyceres/helpers.h +++ b/_pyceres/helpers.h @@ -208,7 +208,7 @@ std::string CreateSummary(const T& self, bool write_type) { } template -void AddDefaultsToDocstrings(py::class_ cls) { +void AddDefaultsToDocstrings(py::classh cls) { auto obj = cls(); for (auto& handle : obj.attr("__dir__")()) { const std::string attribute = py::str(handle); @@ -233,7 +233,7 @@ void AddDefaultsToDocstrings(py::class_ cls) { } template -void MakeDataclass(py::class_ cls, +void MakeDataclass(py::classh cls, const std::vector& attributes = {}) { AddDefaultsToDocstrings(cls); if (!py::hasattr(cls, "summary")) { diff --git a/_pyceres/logging.h b/_pyceres/logging.h index 38bfb77..7a79b61 100644 --- a/_pyceres/logging.h +++ b/_pyceres/logging.h @@ -167,7 +167,7 @@ std::pair GetPythonCallFrame() { } void BindLogging(py::module& m) { - py::class_ PyLogging(m, "logging", py::module_local()); + py::classh PyLogging(m, "logging", py::module_local()); py::enum_(PyLogging, "Level", py::module_local()) .value("INFO", Logging::LogSeverity::GLOG_INFO) diff --git a/pyproject.toml b/pyproject.toml index 5455c95..00e1ae9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,5 +1,5 @@ [build-system] -requires = ["scikit-build-core>=0.3.3", "pybind11==2.13.6"] +requires = ["scikit-build-core>=0.3.3", "pybind11==3.0.0"] build-backend = "scikit_build_core.build" [project]