Added pythonization of some SystemC classes and cleanup

This commit is contained in:
2019-01-01 14:48:37 +01:00
parent 796d8bf69f
commit 3f7fbba0aa
3 changed files with 35 additions and 14 deletions

View File

@ -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):