From 3f7fbba0aad5a8571f2e50a1edbaffe9ffe9bdd7 Mon Sep 17 00:00:00 2001 From: Eyck Jentzsch Date: Tue, 1 Jan 2019 14:48:37 +0100 Subject: [PATCH] Added pythonization of some SystemC classes and cleanup --- pysysc/sccppyy.py | 33 ++++++++++++++++++++++++++------- setup.py | 2 +- tests/router_example.py | 14 ++++++++------ 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/pysysc/sccppyy.py b/pysysc/sccppyy.py index bf81076..e8ab4e9 100644 --- a/pysysc/sccppyy.py +++ b/pysysc/sccppyy.py @@ -1,11 +1,9 @@ import json import cppyy import os.path -import os import sys import tempfile import conans.client.conan_api as conan -from conans.client.command import main from contextlib import (redirect_stdout, redirect_stderr) import io @@ -54,7 +52,7 @@ def read_config_from_conan(conanfile): # set include pathes and load libraries for d in data['dependencies']: for p in d['include_paths']: - add_sys_include_path(p) + add_sys_include_path(p) if d['name'] == 'SystemC': for l in d['lib_paths']: if os.path.exists(l+'/'+'libsystemc.so'): @@ -94,8 +92,29 @@ def add_sys_include_path(incl): sysIncludeDirs.add(incl) cppyy.add_include_path(incl) +# prepare a pythonizor +def _pythonizor(klass, name): + # A pythonizor receives the freshly prepared bound C++ class, and a name stripped down to + # the namespace the pythonizor is applied. Also accessible are klass.__name__ (for the + # Python name) and klass.__cppname__ (for the C++ name) + if name == 'sc_time': + klass.__repr__ = lambda self: repr(self.to_string()) + klass.__str__ = lambda self: self.to_string() + elif name in ['sc_object', 'sc_module']: + klass.__repr__ = lambda self: repr(self.name()) + elif len(name) > 8 and name[:7] == 'sc_port<': + klass.__repr__ = lambda self: repr(self.name()) + elif len(name) > 10 and name[:9] == 'sc_export<': + klass.__repr__ = lambda self: repr(self.name()) +# else: +# print('not pythonizing', name) + +# install the pythonizor as a callback on namespace 'Math' (default is the global namespace) +cppyy.py.add_pythonization(_pythonizor, 'sc_core') + + # reflection methods -def get_members(object): +def get_members(sc_object): def is_cpp_data_type(name, module): matches = [x for x in ['int', 'char', 'float', 'double'] if name == x] if len(matches) > 0 or module[:10] == "cppyy.gbl.": @@ -103,11 +122,11 @@ def get_members(object): else: return False - members = [(e, getattr(object, e)) for e in dir(object)] + members = [(e, getattr(sc_object, e)) for e in dir(sc_object)] return [(k,v) for k,v in members if is_cpp_data_type(type(v).__name__, type(v).__module__)] -def get_methods(object): - members = [(e, getattr(object, e)) for e in dir(object)] +def get_methods(sc_object): + members = [(e, getattr(sc_object, e)) for e in dir(sc_object)] return [(k,v) for k,v in members if type(v).__name__=='CPPOverload'] def get_ports(module): diff --git a/setup.py b/setup.py index 5396d9f..55bf85a 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ def readme(): return f.read() -setup(name='PySysC-UI', +setup(name='PySysC', version='0.1', description='Python SystemC binding', long_description=readme(), diff --git a/tests/router_example.py b/tests/router_example.py index c759d3a..fb59d21 100644 --- a/tests/router_example.py +++ b/tests/router_example.py @@ -3,11 +3,12 @@ import cppyy import os.path import pysysc as scpy from cppyy import gbl as cpp - -conan_err = scpy.read_config_from_conan('../conanfile.txt') + +proj_home='../../PySysC-SC' +conan_res = scpy.read_config_from_conan(os.path.join(proj_home, 'conanfile.txt')) scpy.load_systemc() -scpy.add_include_path('..') -scpy.add_library('components.h', '../build/Debug/lib/libcomponents.so') +scpy.add_include_path(os.path.join(proj_home, 'components')) +scpy.add_library('components.h', os.path.join(proj_home, 'build/Debug/lib/libcomponents.so')) initiator = cpp.Initiator(cpp.sc_core.sc_module_name("initiator")) memories = [cpp.Memory(cpp.sc_core.sc_module_name(name)) for name in ["mem0", "mem1", "mem2", "mem3"]] @@ -24,7 +25,8 @@ childs = scpy.get_submodules(initiator) initiator.socket.bind(router.target_socket) for idx,m in enumerate(memories): router.initiator_socket.at(idx).bind(m.socket) - +time = cpp.sc_core.sc_time_stamp() +print(intors[0].name(), "connects to sc_object:", isinstance(intors[0][0], cpp.sc_core.sc_object)) cpp.sc_core.sc_in_action=True cpp.sc_core.sc_start() -print("Done") +print("Done at", cpp.sc_core.sc_time_stamp())