前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >从Python调用堆栈获取行号等信息

从Python调用堆栈获取行号等信息

作者头像
职场亮哥
发布2020-10-10 15:07:13
2.6K0
发布2020-10-10 15:07:13
举报
文章被收录于专栏:职场亮哥

程序中的日志打印,或者消息上传,比如kafka消息等等。经常上传的消息中需要上传堆栈信息中的文件名、行号、上层调用者等具体用于定位的消息。Python提供了以下两种方法:

  • sys._getframe, 基础方法
  • inspect.currentframe, 推荐方法,提供除了sys._getframe方法之外更多的frame相关的方法

具体使用如下

使用sys._getframe私有方法

具体使用方法如下:

代码语言:javascript
复制
import os
import sys


def get_cur_info():
	"""
        获取调用时的文件名,行号,上层调用者的名称
        :return: 文件名,行号,上层调用者名称
    """
	try:
		current_frame = sys._getframe(2)
		return os.path.basename(current_frame.f_code.co_filename), current_frame.f_lineno, current_frame.f_code.co_name
	except ValueError:
		return 'unknown', 0, 'unknown'

具体的函数输出结果演示可以参见下面的inspect模块结果

使用inspect模块(推荐)

相比于sys的内置私有方法,更推荐inspect模块。inspect模块的具体使用方法如下

代码语言:javascript
复制
import os
import inspect

def get_cur_info():
	try:
		current_frame = inspect.currentframe(2)
		return os.path.basename(current_frame.f_code.co_filename), current_frame.f_lineno, current_frame.f_code.co_name
	except ValueError:
		return 'unknown', 0, 'unknown'


def produce():
	return get_cur_info()


def business():
	return produce()


if __name__ == '__main__':
	print(get_cur_info())  # 输出 ('unknown', 0, 'unknown')

	print(produce())  # 输出 ('a.py', 22, '<module>')

	print(business())  # 输出 ('a.py', 16, 'business')

主要依赖inspect.currentframe方法,关于inspect.currentframe方法的使用见帮助文档

代码语言:javascript
复制
>>> help(inspect.currentframe)
Help on built-in function _getframe in module sys:

_getframe(...)
    _getframe([depth]) -> frameobject
    
    Return a frame object from the call stack.  If optional integer depth is
    given, return the frame object that many calls below the top of the stack.
    If that is deeper than the call stack, ValueError is raised.  The default
    for depth is zero, returning the frame at the top of the call stack.
    
    This function should be used for internal and specialized
    purposes only.

从调用堆栈返回一个帧对象。深度为整数,默认为0,返回调用堆栈顶部的帧。如果指定深度比调用堆栈深,会抛出ValueError异常。该功能应该只用于内部和专业目的。

inspect.currentframe方法的实现见内置库inspect.py

代码语言:javascript
复制
if hasattr(sys, '_getframe'):
    currentframe = sys._getframe
else:
    currentframe = lambda _=None: None

所以本质上inspect.currentframe方法等同于sys._getframe方法

currentframe = lambda _=None: None 等同于 currentframe = lambda _: None ,即lambda函数接收一个参数,返回None

参考:

  1. Python frame hack
  2. StackOverFlow-In Python, how do I obtain the current frame?
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-10-31,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 使用sys._getframe私有方法
  • 使用inspect模块(推荐)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档