如何在 Python3 中的 trace.Trace 模块中存储跟踪调用的结果

How to store the result of trace calls in trace.Trace module in Python3

我想将跟踪函数调用的结果存储在变量(数据结构)中。我能够在 shell 中得到结果,但无法存储在变量中。

我尝试阅读 official documentation but it does not address it, nor I was able to figure this out using the source code

这是我的代码:

    from driver import main
    from trace import Trace
    import importlib

    class GenerateSequenceDiagram:
        def __init__(self, driver_module):
            self.driver_module = __import__(driver_module)

        def get_functions_called(self, driver_function):
            self.driver_function = getattr(self.driver_module, driver_function)
            print(dir(self.driver_function))
            print(self.driver_function.__name__)
            tracer = Trace(countfuncs=1)
            try:
                tracer.run('{}()'.format(self.driver_function.__name__))
            except Exception as e:
                print(e)
            results = tracer.results()
            results.write_results()
            # results.write_results_file('one.txt',)


    ob = GenerateSequenceDiagram('driver')
    ob.get_functions_called('main')



看了源码,原来calledfuncs是必须要用的api

class GenerateSequenceDiagram:
    def __init__(self, driver_module):
        self.driver_module = __import__(driver_module)

    def get_functions_called(self, driver_function):
        self.driver_function = getattr(self.driver_module, driver_function)
        self.driver_function()
        # print(dir(self.driver_function))
        # print(self.driver_function.__name__)
        tracer = Trace(countfuncs=1)
        tracer.run('{}()'.format(self.driver_function.__name__))
        results = tracer.results()
        called_functions = results.calledfuncs
        for filename, modulename, funcname in sorted(called_functions):
            print('filename: {}, modulename: {}, funcname: {}'.format(
                filename, modulename, funcname))
        results.write_results()