Skip to content

Commit

Permalink
#20 Standardize numbers in default args
Browse files Browse the repository at this point in the history
  • Loading branch information
kwabenantim committed Nov 26, 2024
1 parent 3224d57 commit 0109047
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
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

0 comments on commit 0109047

Please sign in to comment.