Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cppwg/info/class_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
36 changes: 36 additions & 0 deletions cppwg/utils/utils.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions cppwg/writers/constructor_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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:
Expand Down
8 changes: 7 additions & 1 deletion cppwg/writers/free_function_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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 = {
Expand Down
5 changes: 5 additions & 0 deletions cppwg/writers/method_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from pygccxml.declarations import type_traits

from cppwg.utils import utils
from cppwg.writers.base_writer import CppBaseWrapperWriter


Expand Down Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions examples/cells/src/cpp/visualization/Scene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define SCENE_HPP_

#include <vtkAutoInit.h>
#include <vtkColorTransferFunction.h>
#include <vtkOpenGLRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkSmartPointer.h>
Expand All @@ -13,6 +14,7 @@ class Scene
{
vtkSmartPointer<vtkRenderer> mpRenderer;
vtkSmartPointer<vtkRenderWindow> mpRenderWindow;
vtkSmartPointer<vtkColorTransferFunction> mpColorTransferFunction;

public:
Scene();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion examples/shapes/wrapper/primitives/Cuboid.cppwg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>);
void register_Cuboid_class(py::module &m)
{
py::class_<Cuboid, std::shared_ptr<Cuboid>, Shape<3>>(m, "Cuboid")
.def(py::init<double, double, double>(), py::arg("width") = 2., py::arg("height") = 1., py::arg("depth") = 1.)
.def(py::init<double, double, double>(), py::arg("width") = 2.0, py::arg("height") = 1.0, py::arg("depth") = 1.0)
;
}
2 changes: 1 addition & 1 deletion examples/shapes/wrapper/primitives/Rectangle.cppwg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>);
void register_Rectangle_class(py::module &m)
{
py::class_<Rectangle, std::shared_ptr<Rectangle>, Shape<2>>(m, "Rectangle")
.def(py::init<double, double>(), py::arg("width") = 2., py::arg("height") = 1.)
.def(py::init<double, double>(), py::arg("width") = 2.0, py::arg("height") = 1.0)
.def(py::init<::std::vector<std::shared_ptr<Point<2>>> const>(), py::arg("points") = ::std::vector<std::shared_ptr<Point<2>>> {})
;
}