Python调试:获取调用函数的文件名和行号? [英] Python debugging: get filename and line number from which a function is called?

查看:484
本文介绍了Python调试:获取调用函数的文件名和行号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在用Python构建一个相当复杂的系统,并且在调试时,我经常在几个脚本中放置简单的打印语句。为了保持概述,我经常也想打印出打印语句所在的文件名和行号。我当然可以手动执行此操作,或使用类似这样的方法:

  from inspect import currentframe,getframeinfo 
print getframeinfo (currentframe())。文件名+':'+ str(getframeinfo(currentframe())。lineno)+'-','我实际上要在此处打印的内容'

会打印出类似这样的内容:


filenameX.py:273-我实际上想要在此处打印出的内容


为了使其更简单,我希望能够执行以下操作:

  print debuginfo(),我实际要在此处打印的内容 

所以我将其放在某个函数中并尝试执行以下操作:

 从debugutil导入debuginfo 
打印debuginfo(),我实际上要在此处打印的内容
打印debuginfo(),还有其他内容

不幸的是,我得到:

  debugutil.py :3-什么我实际上想在这里打印
debugutil.py:3-还有其他东西在这里

它打印出我定义函数的文件名和行号,而不是我调用debuginfo()的行。这很明显,因为代码位于debugutil.py文件中。



所以我的问题实际上是:如何获取调用debuginfo()函数的文件名和行号?

解决方案

函数 inspect.stack() 返回帧记录,从调用方开始并移出,您可以使用这些记录来获取所需的信息:

  from inspect import getframeinfo,stack 

def debuginfo(message):
调用者= getframeinfo(堆栈()[1] [0])
打印%s:%d-%s; %(caller.filename,caller.lineno,message)

def grr(arg):
debuginfo(arg)#<-stack()[1] [0]为此

grr( aargh)行#<-该行

输出

  example.py:8-aargh 


I'm currently building quite a complex system in Python, and when I'm debugging I often put simple print statements in several scripts. To keep an overview I often also want to print out the file name and line number where the print statement is located. I can of course do that manually, or with something like this:

from inspect import currentframe, getframeinfo
print getframeinfo(currentframe()).filename + ':' + str(getframeinfo(currentframe()).lineno) + ' - ', 'what I actually want to print out here'

which prints something like:

filenameX.py:273 - what I actually want to print out here

To make it more simple, I want to be able to do something like:

print debuginfo(), 'what I actually want to print out here'

So I put it into a function somewhere and tried doing:

from debugutil import debuginfo
print debuginfo(), 'what I actually want to print out here'
print debuginfo(), 'and something else here'

unfortunately, I get:

debugutil.py:3 - what I actually want to print out here
debugutil.py:3 - and something else here

It prints out the file name and line number on which I defined the function, instead of the line on which I call debuginfo(). This is obvious, because the code is located in the debugutil.py file.

So my question is actually: How can I get the filename and line number from which this debuginfo() function is called? All tips are welcome!

解决方案

The function inspect.stack() returns a list of frame records, starting with the caller and moving out, which you can use to get the information you want:

from inspect import getframeinfo, stack

def debuginfo(message):
    caller = getframeinfo(stack()[1][0])
    print "%s:%d - %s" % (caller.filename, caller.lineno, message)

def grr(arg):
    debuginfo(arg)      # <-- stack()[1][0] for this line

grr("aargh")            # <-- stack()[2][0] for this line

Output:

example.py:8 - aargh

这篇关于Python调试:获取调用函数的文件名和行号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆