Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 获取最后一个函数';从回溯中得到的参数是什么?_Python_Exception_Stack Trace - Fatal编程技术网

Python 获取最后一个函数';从回溯中得到的参数是什么?

Python 获取最后一个函数';从回溯中得到的参数是什么?,python,exception,stack-trace,Python,Exception,Stack Trace,我读过,但不够具体,无法回答我的问题 这真的让我很困扰,因为没有调用参数会让我的速度变慢,而且我非常确定可以从Python中获取此类信息 下面是一个例子来说明这个问题: # -*- coding: utf-8 -*- import sys import traceback import inspect import logging as log def fl(x): # exception is happening here y = 5/x return y def

我读过,但不够具体,无法回答我的问题

这真的让我很困扰,因为没有调用参数会让我的速度变慢,而且我非常确定可以从Python中获取此类信息

下面是一个例子来说明这个问题:

# -*- coding: utf-8 -*-
import sys
import traceback
import inspect
import logging as log

def fl(x):
    # exception is happening here
    y = 5/x
    return y


def fm(x):
    return fl(x-3)


def fn(a, b, c=None):
    return fm(c)


def main():

    try:
        print fn(1, 2, c=3)
    except Exception as e:
        log.error('Unexpected problem.')
        log.error(e)
        traceback.print_exc()
        ### what I need to see is are the call arguments of the last / deepest call: ###
        ### def fl(x) was called with arguments: [(x, 3)]                            ###
        # this does not cut it:
        tb = sys.exc_info()[2]
        traceback.print_tb(tb)
        # this is broken:
        #frames = inspect.getinnerframes(tb)
        #log.error('Argvalues: %s', inspect.getargvalues(frames))
        # not sure:
        frames = inspect.trace()
        argvalues = inspect.getargvalues(frames[0][0])
        log.error('Argvalues: %s', inspect.formatargvalues(*argvalues))



if __name__ == '__main__':
    main()
所以我得到了详细信息,但调用参数不包含:

ERROR:root:Unexpected problem.
ERROR:root:integer division or modulo by zero
Traceback (most recent call last):
  File "sample.py", line 24, in main
    print fn(1, 2, c=3)
  File "sample.py", line 18, in fn
    return fm(c)
  File "sample.py", line 14, in fm
    return fl(x-3)
  File "sample.py", line 9, in fl
    y = 5/x
ZeroDivisionError: integer division or modulo by zero
  File "sample.py", line 24, in main
    print fn(1, 2, c=3)
  File "sample.py", line 18, in fn
    return fm(c)
  File "sample.py", line 14, in fm
    return fl(x-3)
  File "sample.py", line 9, in fl
    y = 5/x
ERROR:root:Argvalues: ()

帧[0][0]
表示
main
函数
main
在没有参数的情况下被调用,这就是为什么会得到空元组。将其更改为
frames[-1][0]
以获取最后一帧。

在此上下文中,“不剪切”是什么意思?“破碎”是什么意思?为什么你“不确定”?不确定怎么说才容易理解。。。我需要在日志中包含这个信息“def fl(x)是用参数:[(x,3)]调用的”。我把这最后一个函数称为参数…我只是要求你们解释你们自己的评论;大概您已经运行了该代码,并且可以在识别的三条注释上进行扩展。我发现“不确定”特别令人困惑。我明白了,我运行了代码,日志输出包含在问题的底部,如您所见。样本中的所有三次尝试都是对所引用问题的回答,并且没有提供信息。@DJV:so“argvalues=inspect.getargvalues(frames[-1][0])可以满足我的需要。非常有用-谢谢我刚刚把这个添加到我的代码中,它就像一个符咒。在日志中包含最后一个调用参数非常有用。我想知道为什么它不是更好的广告/更明显。无论如何,再次谢谢你。