[英]python introspection; see which method was called, who called it, and which parameters they passed it from the main method
The following is an attempt I have been making at introspection on a "simulated operating system" in python. 以下是我在python中对“模拟操作系统”进行自省的尝试。
def tracefunc(frame, event, arg, indent=[0]):
if event == "call" and frame.f_code.co_name in methods:
if frame.f_code.co_name != 'FetchOperand':
print("====================================")
print("CALL FUNCTION: %s()\nARGUMENTS: %s" % (frame.f_code.co_name, frame.f_code.co_varnames))
print('====================================')
return tracefunc
sys.settrace(tracefunc)
It is contained in a block that looks something like this: 它包含在一个看起来像这样的块中:
if __name__ == '__main__':
vm = OS()
while(interrupt not shutdown):
try:
vm.read_interrupt()
vm.execute_program()
# some case statement
What the ultimate goal is is to print out any method's execution that is incapsulated within my VM object. 最终目标是打印出封装在我的VM对象中的任何方法的执行。 I want to see where it was called, who called it, what the parameters passed in were, and what variables it itself modified.
我想看看它的调用位置,调用者,传入的参数是什么以及它本身修改了哪些变量。 I also need a way to specify these things called
output parameters
, which will be a list. 我还需要一种指定这些东西的方法,称为
output parameters
,它将是一个列表。 Ideally, if it modified an array, it would print the index and the item and that index. 理想情况下,如果修改了数组,则将打印索引以及项目和该索引。 Here is an example
这是一个例子
$my_program: 4
==============================
SYSTEM CALL() method called
called by READ INTERRUPT()
--> Arguments:
arg[0]: 4 <type 'int'>
--> Output Parameters:
gpr[0]: OK
return value: 'OK'
==============================
PUT CHARACTER() method called
called by SYSTEM CALL()
--> Arguments:
None
--> output parameters:
gpr[0]: OK
gpr[1]: 3
--> modified variables:
memory:
index: 4003
old value: 0
new value: 3
return value: 'OK'
==============================
by the way, do not feel too bound to my original method of doing things. 顺便说一句,不要太束缚我原来的做事方法。 I welcome any suggestions you may have.
我欢迎您提出任何建议。
Can anyone suggest a method by which to achieve such introspection? 谁能建议一种实现这种自省的方法?
import traceback
traceback.print_stack()
that should tell you all that great stuff ... 那应该告诉你所有很棒的东西...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.