diff --git a/cppwg/info/class_info.py b/cppwg/info/class_info.py index 0056307..f14776b 100644 --- a/cppwg/info/class_info.py +++ b/cppwg/info/class_info.py @@ -189,6 +189,7 @@ def update_from_ns(self, source_ns: "namespace_t") -> None: # noqa: F821 class_decl = typedef_decl.decl_type.declaration logger.info(f"Found {class_decl.name} for {class_cpp_name}") + class_decl.name = class_cpp_name self.decls.append(class_decl) diff --git a/cppwg/utils/utils.py b/cppwg/utils/utils.py index 4b48b62..d3d3aab 100644 --- a/cppwg/utils/utils.py +++ b/cppwg/utils/utils.py @@ -1,6 +1,8 @@ """Utility functions for the cppwg package.""" +import ast import re +from numbers import Number from typing import Any, List, Tuple from cppwg.utils.constants import CPPWG_ALL_STRING, CPPWG_TRUE_STRINGS @@ -170,6 +172,40 @@ def read_source_file( return source +def str_to_num(expr: str) -> Number: + """ + Convert a literal string expression to a number e.g. "(-1)" to -1. + + Parameters + ---------- + expr : str + The string expression to convert + + Returns + ------- + Number + The converted number, or None if the conversion fails + """ + expr = expr.strip() + if expr.isnumeric(): + return int(expr) + + try: + result = float(expr) + return result + except ValueError: + pass + + try: + result = ast.literal_eval(expr) + if isinstance(result, Number): + return result + except (ValueError, SyntaxError): + pass + + return None + + def strip_source( source: str, strip_comments: bool = True, diff --git a/cppwg/writers/constructor_writer.py b/cppwg/writers/constructor_writer.py index e643550..624cf6f 100644 --- a/cppwg/writers/constructor_writer.py +++ b/cppwg/writers/constructor_writer.py @@ -5,6 +5,7 @@ from pygccxml.declarations import type_traits, type_traits_classes +from cppwg.utils import utils from cppwg.writers.base_writer import CppBaseWrapperWriter @@ -185,7 +186,11 @@ def generate_wrapper(self) -> str: arg.default_value is None or self.class_info.hierarchy_attribute("exclude_default_args") ): + # Try to convert "(-1)" to "-1" etc. default_value = str(arg.default_value) + num = utils.str_to_num(default_value) + if num is not None: + default_value = str(num) # Check for template params in default value if self.template_params: diff --git a/cppwg/writers/free_function_writer.py b/cppwg/writers/free_function_writer.py index 4b931cc..39215f1 100644 --- a/cppwg/writers/free_function_writer.py +++ b/cppwg/writers/free_function_writer.py @@ -3,6 +3,7 @@ from typing import Dict, List from cppwg.info.free_function_info import CppFreeFunctionInfo +from cppwg.utils import utils from cppwg.writers.base_writer import CppBaseWrapperWriter @@ -51,7 +52,12 @@ def generate_wrapper(self) -> str: for argument in self.free_function_info.decls[0].arguments: default_args += f', py::arg("{argument.name}")' if argument.default_value is not None: - default_args += f" = {argument.default_value}" + # Try to convert "(-1)" to "-1" etc. + default_value = str(argument.default_value) + num = utils.str_to_num(default_value) + if num is not None: + default_value = str(num) + default_args += f" = {default_value}" # Add the free function wrapper code to the wrapper string func_dict = { diff --git a/cppwg/writers/method_writer.py b/cppwg/writers/method_writer.py index e55298c..4ccc056 100644 --- a/cppwg/writers/method_writer.py +++ b/cppwg/writers/method_writer.py @@ -5,6 +5,7 @@ from pygccxml.declarations import type_traits +from cppwg.utils import utils from cppwg.writers.base_writer import CppBaseWrapperWriter @@ -155,7 +156,11 @@ def generate_wrapper(self) -> str: arg.default_value is None or self.class_info.hierarchy_attribute("exclude_default_args") ): + # Try to convert "(-1)" to "-1" etc. default_value = str(arg.default_value) + num = utils.str_to_num(default_value) + if num is not None: + default_value = str(num) # Check for template params in default value if self.template_params: diff --git a/examples/cells/src/cpp/visualization/Scene.hpp b/examples/cells/src/cpp/visualization/Scene.hpp index 6fe3296..40560b2 100644 --- a/examples/cells/src/cpp/visualization/Scene.hpp +++ b/examples/cells/src/cpp/visualization/Scene.hpp @@ -2,6 +2,7 @@ #define SCENE_HPP_ #include +#include #include #include #include @@ -13,6 +14,7 @@ class Scene { vtkSmartPointer mpRenderer; vtkSmartPointer mpRenderWindow; + vtkSmartPointer mpColorTransferFunction; public: Scene(); diff --git a/examples/shapes/wrapper/math_funcs/_pyshapes_math_funcs.main.cppwg.cpp b/examples/shapes/wrapper/math_funcs/_pyshapes_math_funcs.main.cppwg.cpp index 78ba6d1..b9367a1 100644 --- a/examples/shapes/wrapper/math_funcs/_pyshapes_math_funcs.main.cppwg.cpp +++ b/examples/shapes/wrapper/math_funcs/_pyshapes_math_funcs.main.cppwg.cpp @@ -8,5 +8,5 @@ namespace py = pybind11; PYBIND11_MODULE(_pyshapes_math_funcs, m) { - m.def("add", &add, " ", py::arg("i") = 1., py::arg("j") = 2.); + m.def("add", &add, " ", py::arg("i") = 1.0, py::arg("j") = 2.0); } diff --git a/examples/shapes/wrapper/primitives/Cuboid.cppwg.cpp b/examples/shapes/wrapper/primitives/Cuboid.cppwg.cpp index cfaa752..8927818 100644 --- a/examples/shapes/wrapper/primitives/Cuboid.cppwg.cpp +++ b/examples/shapes/wrapper/primitives/Cuboid.cppwg.cpp @@ -14,6 +14,6 @@ PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr); void register_Cuboid_class(py::module &m) { py::class_, Shape<3>>(m, "Cuboid") - .def(py::init(), py::arg("width") = 2., py::arg("height") = 1., py::arg("depth") = 1.) + .def(py::init(), py::arg("width") = 2.0, py::arg("height") = 1.0, py::arg("depth") = 1.0) ; } diff --git a/examples/shapes/wrapper/primitives/Rectangle.cppwg.cpp b/examples/shapes/wrapper/primitives/Rectangle.cppwg.cpp index 83a286b..2cc8a68 100644 --- a/examples/shapes/wrapper/primitives/Rectangle.cppwg.cpp +++ b/examples/shapes/wrapper/primitives/Rectangle.cppwg.cpp @@ -14,7 +14,7 @@ PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr); void register_Rectangle_class(py::module &m) { py::class_, Shape<2>>(m, "Rectangle") - .def(py::init(), py::arg("width") = 2., py::arg("height") = 1.) + .def(py::init(), py::arg("width") = 2.0, py::arg("height") = 1.0) .def(py::init<::std::vector>> const>(), py::arg("points") = ::std::vector>> {}) ; }