def currentframe():
"""Return the frame object for the caller's stack frame."""
try:
raise Exception
except:
return sys.exc_info()[2].tb_frame.f_back
def findCaller(self):
"""
Find the stack frame of the caller so that we can note the source
file name, line number and function name.
"""
f = currentframe()
#On some versions of IronPython, currentframe() returns None if
#IronPython isn't run with -X:Frames.
if f is not None:
f = f.f_back
rv = "(unknown file)", 0, "(unknown function)"
while hasattr(f, "f_code"):
co = f.f_code
filename = os.path.normcase(co.co_filename)
if filename == _srcfile:
f = f.f_back
continue
rv = (co.co_filename, f.f_lineno, co.co_name)
break
return rv
def _log(self, level, msg, args, exc_info=None, extra=None):
"""
Low-level logging routine which creates a LogRecord and then calls
all the handlers of this logger to handle the record.
"""
if _srcfile:
#IronPython doesn't track Python frames, so findCaller throws an
#exception on some versions of IronPython. We trap it here so that
#IronPython can use logging.
try:
fn, lno, func = self.findCaller()
except ValueError:
fn, lno, func = "(unknown file)", 0, "(unknown function)"
else:
fn, lno, func = "(unknown file)", 0, "(unknown function)"
if exc_info:
if not isinstance(exc_info, tuple):
exc_info = sys.exc_info()
record = self.makeRecord(self.name, level, fn, lno, msg, args, exc_info, func, extra)
self.handle(record)
我简单解释一下,实际上是通过在currentframe函数中抛出一个异常,然后通过向上查找的方式,找到调用的信息。其中
复制代码 代码如下:
rv = (co.co_filename, f.f_lineno, co.co_name)
的三个值分别为文件名,行号,函数名。(可以去http://docs.python.org/library/sys.html来看一下代码中几个系统函数的说明)
OK,如果已经看懂了源码,那获取当前位置的行号和函数名相信也非常清楚了,代码如下:
复制代码 代码如下:
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
#=============================================================================
# FileName: xf.py
# Description: 获取当前位置的行号和函数名
# Version: 1.0
#=============================================================================
'''
import sys
def get_cur_info():
"""Return the frame object for the caller's stack frame."""
try:
raise Exception
except:
f = sys.exc_info()[2].tb_frame.f_back
return (f.f_code.co_name, f.f_lineno)
def callfunc():
print get_cur_info()
if __name__ == '__main__':
callfunc()
输入结果是:
复制代码 代码如下:
('callfunc', 24)
符合预期~~
哈哈,OK!现在应该不用再抱怨取不到行号和函数名了吧~
=========================================== === ==============================
後で、実際には次のようなもっと簡単な方法があることを発見しました。 :
コードをコピー コードは次のとおりです:
import sys
def get_cur_info():
print sys._getframe ().f_code.co_name
Print sys._getframe().f_back.f_code.co_name
get_cur_info()
呼び出し結果は次のとおりです:
コードをコピーします コードは次のとおりです:
get_cur_info