Python通过sys.setprofile和框架检查观察`str`调用

dm0*_*514 6 python python-3.x

我是python框架内省的新手,我正在尝试设置分析器或跟踪器以跟踪str函数调用.我已经以各种方式设置了跟踪器,但我认为我缺少关于帧内省的一些关键理解以及如何获得内置函数名称(即str)

def test_trace():
    sys.setprofile(trace_calls)
    hi = str('hellllo')
    stuff = []
    for i in range(10):
        stuff.append(str(random.randrange(0, 10000000000)))
    print(hi)
    os._exit(0)

def trace_calls(frame, event, arg):
    '''
    if event not in ('call', 'c_call'):
        return
    '''
    stack = collections.deque()
    stack.appendleft(_expand_arg(arg) + _expand_frame(frame))

    while frame.f_back is not None:
        stack.appendleft(_expand_arg(arg) + _expand_frame(frame))
        frame = frame.f_back

    print('-' * 100)
    for frame in stack:
        print(frame)

def _expand_arg(arg):
    if arg is None:
        return ()

    return (
        # arg.__name__,
    )

def _expand_frame(frame):
    code = frame.f_code
    c_class = ''
    c_module = ''
    if 'self' in frame.f_locals:
        c_class = frame.f_locals['self'].__class__.__name__
        c_module = frame.f_locals['self'].__class__.__module__

    return (
        code.co_filename,
        frame.f_lineno,
        frame.f_trace,
        code.co_name,
        code.co_firstlineno,
        c_class,
        c_module,
    )
Run Code Online (Sandbox Code Playgroud)

当我运行时,test_trace()我看不到str任何配置事件中的任何调用.我希望看到一些提及str<string>在一些调用堆栈中,但只看到随机调用:

$ ipython
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.4.0 -- An enhanced Interactive Python. Type '?' for help.

('/home/ubuntu/.virtualenvs/papm/lib/python3.5/site-packages/IPython/__init__.py', 125, None, 'start_ipython', 99, '', '')
('/home/ubuntu/.virtualenvs/papm/lib/python3.5/site-packages/traitlets/config/application.py', 658, None, 'launch_instance', 650, '', '')
('/home/ubuntu/.virtualenvs/papm/lib/python3.5/site-packages/IPython/terminal/ipapp.py', 356, None, 'start', 350, 'TerminalIPythonApp', 'IPython.terminal.ipapp')
('/home/ubuntu/.virtualenvs/papm/lib/python3.5/site-packages/IPython/terminal/interactiveshell.py', 485, None, 'mainloop', 478, 'TerminalInteractiveShell', 'IPython.terminal.in
teractiveshell')
('/home/ubuntu/.virtualenvs/papm/lib/python3.5/site-packages/IPython/terminal/interactiveshell.py', 476, None, 'interact', 458, 'TerminalInteractiveShell', 'IPython.terminal.in
teractiveshell')
('/home/ubuntu/.virtualenvs/papm/lib/python3.5/site-packages/IPython/core/interactiveshell.py', 2662, None, 'run_cell', 2636, 'TerminalInteractiveShell', 'IPython.terminal.inte
ractiveshell')
('/home/ubuntu/.virtualenvs/papm/lib/python3.5/site-packages/IPython/core/interactiveshell.py', 2785, None, '_run_cell', 2669, 'TerminalInteractiveShell', 'IPython.terminal.int
eractiveshell')
('/home/ubuntu/.virtualenvs/papm/lib/python3.5/site-packages/IPython/core/interactiveshell.py', 2909, None, 'run_ast_nodes', 2835, 'TerminalInteractiveShell', 'IPython.terminal
.interactiveshell')
('/home/ubuntu/.virtualenvs/papm/lib/python3.5/site-packages/IPython/core/interactiveshell.py', 2963, None, 'run_code', 2933, 'TerminalInteractiveShell', 'IPython.terminal.inte
ractiveshell')
('<ipython-input-2-a29a88804d82>', 1, None, '<module>', 1, '', '')
('/vagrant_data/github.com/dm03514/python-apm/pythonapm/instruments/monkey.py', 35, None, 'test_trace', 30, '', '')
('/home/ubuntu/.virtualenvs/papm/lib/python3.5/random.py', 194, None, 'randrange', 170, 'Random', 'random')
('/home/ubuntu/.virtualenvs/papm/lib/python3.5/random.py', 232, None, '_randbelow', 220, 'Random', 'random')
('/home/ubuntu/.virtualenvs/papm/lib/python3.5/random.py', 232, None, '_randbelow', 220, 'Random', 'random')

('/home/ubuntu/.virtualenvs/papm/lib/python3.5/site-packages/IPython/__init__.py', 125, None, 'start_ipython', 99, '', '')
('/home/ubuntu/.virtualenvs/papm/lib/python3.5/site-packages/traitlets/config/application.py', 658, None, 'launch_instance', 650, '', '')
('/home/ubuntu/.virtualenvs/papm/lib/python3.5/site-packages/IPython/terminal/ipapp.py', 356, None, 'start', 350, 'TerminalIPythonApp', 'IPython.terminal.ipapp')
('/home/ubuntu/.virtualenvs/papm/lib/python3.5/site-packages/IPython/terminal/interactiveshell.py', 485, None, 'mainloop', 478, 'TerminalInteractiveShell', 'IPython.terminal.in
teractiveshell')
('/home/ubuntu/.virtualenvs/papm/lib/python3.5/site-packages/IPython/terminal/interactiveshell.py', 476, None, 'interact', 458, 'TerminalInteractiveShell', 'IPython.terminal.in
teractiveshell')
('/home/ubuntu/.virtualenvs/papm/lib/python3.5/site-packages/IPython/core/interactiveshell.py', 2662, None, 'run_cell', 2636, 'TerminalInteractiveShell', 'IPython.terminal.inte
ractiveshell')
('/home/ubuntu/.virtualenvs/papm/lib/python3.5/site-packages/IPython/core/interactiveshell.py', 2785, None, '_run_cell', 2669, 'TerminalInteractiveShell', 'IPython.terminal.int
eractiveshell')
('/home/ubuntu/.virtualenvs/papm/lib/python3.5/site-packages/IPython/core/interactiveshell.py', 2909, None, 'run_ast_nodes', 2835, 'TerminalInteractiveShell', 'IPython.terminal
.interactiveshell')
('/home/ubuntu/.virtualenvs/papm/lib/python3.5/site-packages/IPython/core/interactiveshell.py', 2963, None, 'run_code', 2933, 'TerminalInteractiveShell', 'IPython.terminal.inte
ractiveshell')
('<ipython-input-2-a29a88804d82>', 1, None, '<module>', 1, '', '')
('/vagrant_data/github.com/dm03514/python-apm/pythonapm/instruments/monkey.py', 35, None, 'test_trace', 30, '', '')
('/home/ubuntu/.virtualenvs/papm/lib/python3.5/random.py', 194, None, 'randrange', 170, 'Random', 'random')
('/home/ubuntu/.virtualenvs/papm/lib/python3.5/random.py', 194, None, 'randrange', 170, 'Random', 'random')
Run Code Online (Sandbox Code Playgroud)

我已经在python内置配置文件中看到了字符串调用,所以我试着看看他们如何获得c函数名称,以防str委托给ac调用.

我试图通过检查来获取c函数调用arg(使用setprofilesettrace但它没有显示任何字符串调用

    if event == "c_call":
        self.c_func_name = arg.__name__
Run Code Online (Sandbox Code Playgroud)

是否有人熟悉如何跟踪python str或内置调用.

Mar*_*ers 3

当我运行 test_trace() 时,我在任何分析事件中都没有看到任何 str 调用。

这是因为str()是一个完全用 C 定义的类型对象。仅跟踪函数调用,而不跟踪类型调用,因此不会c_call发出任何事件。

str您可以通过替换内置的来解决这个问题;builtins你可以通过模块改变Python的内置函数:

import builtins

orig_str = builtins.str
def traceable_str(*args):
    return orig_str(*args)
builtins.str = traceable_str
Run Code Online (Sandbox Code Playgroud)

您现在可以看到traceable_str传递给分析函数的调用。

但是,请注意,只会跟踪来自 Python 代码的调用str()!任何用 C 实现的东西,比如print()函数,甚至不调用str()内置函数,它只是使用 C API 来实现相同的结果。