Skip to content

Commit

Permalink
Add options.fn_params_type_replacements
Browse files Browse the repository at this point in the history
  • Loading branch information
pthom committed Nov 22, 2024
1 parent f507cf2 commit 6eb5fd3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
14 changes: 14 additions & 0 deletions src/codemanip/code_replacements.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ def apply(self, s: str) -> str:
r, nb = self.replace_what_re.subn(self.by_what, s)
return r

@staticmethod
def replace_whole_word(s: str) -> RegexReplacement:
return RegexReplacement(r"\b" + s + r"\b", s)

@staticmethod
def from_string(line: str) -> RegexReplacement:
"""
Expand Down Expand Up @@ -67,6 +71,16 @@ def add_last_replacement(self, replace_what_regex: str, by_what: str) -> None:
r = RegexReplacement(replace_what_regex, by_what)
self.add_last_regex_replacement(r)

def add_replacement(self, replace_what_regex: str, by_what: str) -> None:
self.add_last_replacement(replace_what_regex, by_what)

def add_replacements(self, replacements: list[RegexReplacement | tuple[str, str]]) -> None:
for replacement in replacements:
if isinstance(replacement, tuple):
self.add_last_replacement(replacement[0], replacement[1])
else:
self.add_last_regex_replacement(replacement)

def add_first_regex_replacement(self, replacement: RegexReplacement) -> None:
self.replacements = [replacement] + self.replacements

Expand Down
26 changes: 14 additions & 12 deletions src/litgen/internal/adapted_types/adapted_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -1243,19 +1243,21 @@ def _stub_params_list_signature(self) -> list[str]:
param_type_cpp = param_decl.cpp_type.str_code()

# Handle *args and **kwargs
param_type_cpp_simplified = (
param_type_cpp.replace("const ", "")
.replace("pybind11::", "py::")
.replace(" &", "")
.replace("nanobind::", "nb::")
)
if param_type_cpp_simplified == "py::args" or param_type_cpp_simplified == "nb::args":
r.append("*args")
continue
if param_type_cpp_simplified == "py::kwargs" or param_type_cpp_simplified == "nb::kwargs":
r.append("**kwargs")
continue
if True:
param_type_cpp_simplified = (
param_type_cpp.replace("const ", "")
.replace("pybind11::", "py::")
.replace(" &", "")
.replace("nanobind::", "nb::")
)
if param_type_cpp_simplified == "py::args" or param_type_cpp_simplified == "nb::args":
r.append("*args")
continue
if param_type_cpp_simplified == "py::kwargs" or param_type_cpp_simplified == "nb::kwargs":
r.append("**kwargs")
continue

param_type_cpp = self.options.fn_params_type_replacements.apply(param_type_cpp)
param_type_python = cpp_to_python.type_to_python(self.lg_context, param_type_cpp)
# Add Optional to param_type_python if cpp_type is a pointer with default = nullptr or NULL
if "*" in param_decl.cpp_type.modifiers and param_decl.initial_value_code in ["NULL", "nullptr"]:
Expand Down
3 changes: 3 additions & 0 deletions src/litgen/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,15 @@ class LitgenOptions:
# - by default, the others are empty
# - type_replacements, var_names_replacements and function_names_replacements enable you
# to modify the outputted python code
# - fn_parameters_type_replacements can be used to modify types when used as function parameters
type_replacements: RegexReplacementList # = cpp_to_python.standard_type_replacements() by default
var_names_replacements: RegexReplacementList # = RegexReplacementList() by default (i.e. empty)
namespace_names_replacements: RegexReplacementList # = RegexReplacementList() by default (i.e. empty)
function_names_replacements: RegexReplacementList # = RegexReplacementList() by default (i.e. empty)
value_replacements: RegexReplacementList # = cpp_to_python.standard_value_replacements() by default
comments_replacements: RegexReplacementList # = cpp_to_python.standard_comment_replacements() by default
macro_name_replacements: RegexReplacementList # = RegexReplacementList() by default (i.e. empty)
fn_params_type_replacements: RegexReplacementList # = RegexReplacementList() by default (i.e. empty)

################################################################################
# <functions and method adaptations>
Expand Down Expand Up @@ -695,6 +697,7 @@ def __init__(self) -> None:
self.var_names_replacements = RegexReplacementList()
self.macro_name_replacements = RegexReplacementList()
self.namespace_names_replacements = RegexReplacementList()
self.fn_params_type_replacements = RegexReplacementList()

self.fn_template_options = TemplateFunctionsOptions()
self.class_template_options = TemplateClassOptions()
Expand Down

0 comments on commit 6eb5fd3

Please sign in to comment.