python: Ignore *args and **kwargs when generating cxxMethod pybinding script (#1535)

According to the pybind documentation, "When combining *args or **kwargs
with Keyword arguments you should not include py::arg tags for the
py::args and py::kwargs arguments."

In the current implementation of gem5, if you use the cxxMethod
decorator on a function that has *args or **kwargs, gem5 will
incorrectly add these variables to the pybind generated declaration.

I.e., def f(arg1, arg2,  *args, **kwargs): -> .def("f", &f,
py::arg("arg1"), py::arg("arg2"), py::arg("*args"), py::arg("**kwargs"))
which is incorrect pybind code.

To fix this problem, we should ignore variables in the generator if they
are *args or **kwargs. This change skips these variables when creating
the pybind declaration.

Change-Id: I44a1e0eb0b5fc5c1e1d423ba145d456bff92c6b8
This commit is contained in:
handsomeliu-google
2024-09-10 01:23:26 +08:00
committed by GitHub
parent da6ce1d9c2
commit 0da65b31c2

View File

@@ -489,6 +489,12 @@ def cxxMethod(*args, **kwargs):
# We don't cound 'self' as an argument in this case.
continue
param = sig.parameters[param_name]
if param.kind in [
inspect.Parameter.VAR_POSITIONAL,
inspect.Parameter.VAR_KEYWORD,
]:
# *args and **kwargs shouldn't be in generated parameters
continue
if param.default is param.empty:
args.append(param_name)
else: