File indexing completed on 2026-09-22 08:03:38
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 import traceback
0013 import sys
0014 from ROOT import gSystem
0015
0016 import os
0017 import logging
0018 import platform
0019 if platform.system() == "Darwin":
0020 gSystem.SetDynamicPath(os.environ['DD4HEP_LIBRARY_PATH'])
0021 gSystem.Load('libglapi')
0022 gSystem.Load('libDDPython')
0023 from ROOT import dd4hep as Core
0024
0025 logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO)
0026 logger = logging.getLogger(__name__)
0027
0028 name_space = __import__(__name__)
0029
0030
0031 def import_namespace_item(ns, nam):
0032 scope = getattr(name_space, ns)
0033 attr = getattr(scope, nam)
0034 setattr(name_space, nam, attr)
0035 return attr
0036
0037
0038 def a_func():
0039 logger.info('Hello world')
0040 return 1
0041
0042
0043 class a_class:
0044 def __init__(self):
0045 pass
0046
0047 def fcn(self):
0048 logger.info('Hello world from member function fcn')
0049 return 1
0050
0051 def fcn_except(self, args, aa):
0052 logger.info('Hello world from member function fcn1 a1=%s a2=%s', str(args), str(aa))
0053 raise RuntimeError('Except from python test object a_class')
0054 return 6
0055
0056
0057 py = import_namespace_item('Core', 'DDPython')
0058
0059 logger.info('+++++ Test: Execute statements in python with C++ indirection')
0060 py.instance().execute(str('import sys, logging'))
0061 py.instance().execute(str('logging.info("Arguments: %s", str(sys.argv))'))
0062 logger.info('\n')
0063
0064 obj = a_class()
0065
0066 logger.info('+++++ Test: simple function call')
0067 ret = py.instance().call(a_func, None)
0068 logger.info('ret: %s', str(ret))
0069 logger.info('\n')
0070
0071 logger.info('+++++ Test: object method call')
0072 ret = py.instance().call(obj.fcn, None)
0073 logger.info('ret: %s', str(ret))
0074 logger.info('\n')
0075
0076 logger.info('+++++ Test: object method call with non callable')
0077 try:
0078 ret = py.instance().call(1, None)
0079 logger.info('ret: %s', str(ret))
0080 except Exception:
0081 traceback.print_exc()
0082 logger.info('\n')
0083
0084 logger.info('+++++ Test: object method call with exception in python callback')
0085 try:
0086 ret = py.instance().call(obj.fcn_except, (1, [1, 2, 3, 4, 5, 6],))
0087 logger.info('ret: %s', str(ret))
0088 except Exception:
0089 traceback.print_exc()
0090 logger.info('\n')
0091 logger.info('+++++ All Done....\n\n')
0092 logger.info('TEST_PASSED')
0093
0094
0095
0096 sys.exit(0)