Python 通过 sys.setprofile 和帧检查观察"str"调用



我是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,
)

当我运行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')

我在 python 内置配置文件中看到过字符串调用,所以我试图查看它们如何获取 c 函数名称,以防str委托给 c 调用。

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

if event == "c_call":
self.c_func_name = arg.__name__

有没有人熟悉如何跟踪 pythonstr或内置调用。

当我运行 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

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

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

最新更新