Nikos Nikoleris has submitted this change and it was merged. ( https://gem5-review.googlesource.com/c/public/gem5/+/19168 )

Change subject: python: Add support for exporting static class methods from c++
......................................................................

python: Add support for exporting static class methods from c++

This change adds support for exporting static methods in a c++
SimObject from the coressponsing python wrapper class. This will allow
us to define and use c++ methods without the need to instantiate an
object of the corresponding class.

Change-Id: Iaf24c1aa6f20feb5c91241f46ec8db005a6a0c0c
Signed-off-by: Nikos Nikoleris <[email protected]>
Reviewed-by: Andreas Sandberg <[email protected]>
Signed-off-by: Nikos Nikoleris <[email protected]>
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/19168
Reviewed-by: Jason Lowe-Power <[email protected]>
Tested-by: kokoro <[email protected]>
Maintainer: Andreas Sandberg <[email protected]>
---
M src/python/m5/SimObject.py
M src/python/m5/util/pybind.py
2 files changed, 21 insertions(+), 12 deletions(-)

Approvals:
  Jason Lowe-Power: Looks good to me, but someone else must approve
  Andreas Sandberg: Looks good to me, approved; Looks good to me, approved
  Nikos Nikoleris: Looks good to me, approved
  kokoro: Regressions pass



diff --git a/src/python/m5/SimObject.py b/src/python/m5/SimObject.py
index dca7d40..317ae6c 100644
--- a/src/python/m5/SimObject.py
+++ b/src/python/m5/SimObject.py
@@ -1,4 +1,4 @@
-# Copyright (c) 2017-2018 ARM Limited
+# Copyright (c) 2017-2019 ARM Limited
 # All rights reserved.
 #
 # The license below extends only to copyright in the software and shall
@@ -655,18 +655,27 @@
         if attr == 'cxx_namespaces':
             return cls.cxx_class_path[:-1]

+        if attr == 'pybind_class':
+            return  '_COLONS_'.join(cls.cxx_class_path)
+
         if attr in cls._values:
             return cls._values[attr]

         if attr in cls._children:
             return cls._children[attr]

-        raise AttributeError(
-              "object '%s' has no attribute '%s'" % (cls.__name__, attr))
+        try:
+            return getattr(cls.getCCClass(), attr)
+        except AttributeError:
+            raise AttributeError(
+                "object '%s' has no attribute '%s'" % (cls.__name__, attr))

     def __str__(cls):
         return cls.__name__

+    def getCCClass(cls):
+        return getattr(m5.internal.params, cls.pybind_class)
+
     # See ParamValue.cxx_predecls for description.
     def cxx_predecls(cls, code):
         code('#include "params/$cls.hh"')
@@ -675,10 +684,7 @@
         code('#include "${{cls.cxx_header}}"')

     def pybind_decl(cls, code):
-        class_path = cls.cxx_class.split('::')
-        namespaces, classname = class_path[:-1], class_path[-1]
-        py_class_name = '_COLONS_'.join(class_path) if namespaces else \
-                        classname;
+        py_class_name = cls.pybind_class

         # The 'local' attribute restricts us to the params declared in
         # the object itself, not including inherited params (which
@@ -952,6 +958,7 @@
         override = kwargs.get("override", False)
         cxx_name = kwargs.get("cxx_name", name)
         return_value_policy = kwargs.get("return_value_policy", None)
+        static = kwargs.get("static", False)

         args, varargs, keywords, defaults = inspect.getargspec(func)
         if varargs or keywords:
@@ -968,7 +975,7 @@

         @wraps(func)
         def cxx_call(self, *args, **kwargs):
-            ccobj = self.getCCObject()
+            ccobj = self.getCCClass() if static else self.getCCObject()
             return getattr(ccobj, name)(*args, **kwargs)

         @wraps(func)
@@ -977,7 +984,8 @@

         f = py_call if override else cxx_call
         f.__pybind = PyBindMethod(name, cxx_name=cxx_name, args=args,
-                                  return_value_policy=return_value_policy)
+                                  return_value_policy=return_value_policy,
+                                  static=static)

         return f

diff --git a/src/python/m5/util/pybind.py b/src/python/m5/util/pybind.py
index 4664c16..e194e37 100644
--- a/src/python/m5/util/pybind.py
+++ b/src/python/m5/util/pybind.py
@@ -1,4 +1,4 @@
-# Copyright (c) 2017 ARM Limited
+# Copyright (c) 2017, 2019 ARM Limited
 # All rights reserved.
 #
 # The license below extends only to copyright in the software and shall
@@ -59,11 +59,12 @@

 class PyBindMethod(PyBindExport):
     def __init__(self, name, cxx_name=None, args=None,
-                 return_value_policy=None):
+                 return_value_policy=None, static=False):
         self.name = name
         self.cxx_name = cxx_name if cxx_name else name
         self.args = args
         self.return_value_policy = return_value_policy
+        self.method_def = 'def_static' if static else 'def'

     def _conv_arg(self, value):
         if isinstance(value, bool):
@@ -88,4 +89,4 @@
                     return 'py::arg("%s")' % arg

             arguments.extend(list([ get_arg_decl(a) for a in self.args ]))
-        code('.def(' + ', '.join(arguments) + ')')
+        code('.' + self.method_def + '(' + ', '.join(arguments) + ')')

--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/19168
To unsubscribe, or for help writing mail filters, visit https://gem5-review.googlesource.com/settings

Gerrit-Project: public/gem5
Gerrit-Branch: master
Gerrit-Change-Id: Iaf24c1aa6f20feb5c91241f46ec8db005a6a0c0c
Gerrit-Change-Number: 19168
Gerrit-PatchSet: 3
Gerrit-Owner: Nikos Nikoleris <[email protected]>
Gerrit-Reviewer: Andreas Sandberg <[email protected]>
Gerrit-Reviewer: Gabe Black <[email protected]>
Gerrit-Reviewer: Jason Lowe-Power <[email protected]>
Gerrit-Reviewer: Nikos Nikoleris <[email protected]>
Gerrit-Reviewer: kokoro <[email protected]>
Gerrit-MessageType: merged
_______________________________________________
gem5-dev mailing list
[email protected]
http://m5sim.org/mailman/listinfo/gem5-dev

Reply via email to