From 0da65b31c2c72884cf039ab526500a3c115dd0f7 Mon Sep 17 00:00:00 2001 From: handsomeliu-google Date: Tue, 10 Sep 2024 01:23:26 +0800 Subject: [PATCH] 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 --- src/python/m5/SimObject.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/python/m5/SimObject.py b/src/python/m5/SimObject.py index 0da9f7d889..4ba64aedd0 100644 --- a/src/python/m5/SimObject.py +++ b/src/python/m5/SimObject.py @@ -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: