我不知道如何使用inspect / inspect_shell检查当前正在执行的函数
我猜它涉及使用 getinnerframe 和 getouterframe 遍历框架层次结构,但我对几个问题感到困惑。
给定这个例子九.py:
import inspect_shell
import time
def number_nine():
x = 9
while x==9:
time.sleep(1)
number_nine()
print x
我想检查x
甚至可能更改它的值以使函数返回并打印新值。
首先我启动 Nine.py,然后在一个单独的命令窗口中,使用inspect_shell,我看到它getinnerframes
在当前帧上不起作用(它需要一个跟踪(也许?))并且当前帧没有“跟踪”。并且getouterframes
(以防我倒退)似乎只得到与我的功能无关的帧。
>> Inspect Shell v1.0
>> https://github.com/amoffat/Inspect-Shell
localhost:1234> import inspect
localhost:1234> f = inspect.currentframe()
localhost:1234> inspect.getinnerframes(f)
Traceback (most recent call last):
File "C:\Users\Paul\Desktop\inspect_shell.py", line 143, in run_repl
try: exec compile(data, "<dummy>", "single") in f_globals, f_globals
File "<dummy>", line 1, in <module>
File "C:\Python26\lib\inspect.py", line 942, in getinnerframes
framelist.append((tb.tb_frame,) + getframeinfo(tb, context))
AttributeError: 'frame' object has no attribute 'tb_frame'
localhost:1234> dir(f)
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__',
'__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'f_back', 'f_builtins',
'f_code', 'f_exc_traceback', 'f_exc_type', 'f_exc_value', 'f_glo
bals', 'f_lasti', 'f_lineno', 'f_locals', 'f_restricted', 'f_trace']
localhost:1234> print f.f_trace
None
localhost:1234> inspect.getouterframes(f)
[(<frame object at 0x0237D470>, '<dummy>', 1, '<module>', None, None),
(<frame object at 0x02375938>, 'C:\\Users\\Paul\\Desktop\\inspect_shell.py', 14
3, 'run_repl', [' try: exec compile(data, "<dummy>", "single") in
f_globals, f_globals\n'], 0), (<frame object at 0x023A2C30>, 'C:\
\Python26\\lib\\threading.py', 484, 'run',
[' self.__target(*self.__args, **self.__kwargs)\n'], 0),
(<frame object at 0x02397F28>, 'C:\
\Python26\\lib\\threading.py', 532, '__bootstrap_inner',
[' self.run()\n'], 0), (<frame object at 0x023A9D68>,
'C:\\Python26\\lib\\thre
ading.py', 504, '__bootstrap', [' self.__bootstrap_inner()\n'], 0)]