首页 > 代码库 > Python 显示调用栈

Python 显示调用栈

Python调试不如强类型的语言方便,显示调用栈有时非常必要,inspect模块很好用

import inspectinspect.stack()

inspect.stack()返回的是一个函数栈帧列表如(已经做了一个for e in inspect(): print e 转化)

(<frame object at 0x7f6ec27b2050>, ‘/usr/lib/python2.7/dist-packages/oslo/config/cfg.py‘, 495, ‘_is_opt_registered‘, [‘    st = inspect.stack()\n‘], 0)(<frame object at 0x7f6ec2807d00>, ‘/usr/lib/python2.7/dist-packages/oslo/config/cfg.py‘, 1163, ‘_register_opt‘, [‘        if _is_opt_registered(self._opts, opt):\n‘], 0)(<frame object at 0x7f6ec280bb90>, ‘/usr/lib/python2.7/dist-packages/oslo/config/cfg.py‘, 1710, ‘register_opt‘, [‘            return group._register_opt(opt, cli)\n‘], 0)(<frame object at 0x1a4f180>, ‘/usr/lib/python2.7/dist-packages/oslo/config/cfg.py‘, 1591, ‘__inner‘, [‘                return f(self, *args, **kwargs)\n‘], 0)(<frame object at 0x1a805f0>, ‘/usr/lib/python2.7/dist-packages/oslo/config/cfg.py‘, 1723, ‘register_opts‘, [‘            self.register_opt(opt, group, clear_cache=False)\n‘], 0)(<frame object at 0x1a941f0>, ‘/usr/lib/python2.7/dist-packages/oslo/config/cfg.py‘, 1587, ‘__inner‘, [‘                result = f(self, *args, **kwargs)\n‘], 0)(<frame object at 0x1ac0bb0>, ‘/usr/lib/python2.7/dist-packages/nova/openstack/common/db/options.py‘, 131, ‘<module>‘, ["CONF.register_opts(database_opts, ‘database‘)\n"], 0)(<frame object at 0x7f6ec2870aa0>, ‘/usr/lib/python2.7/dist-packages/nova/config.py‘, 21, ‘<module>‘, [‘from nova.openstack.common.db import options\n‘], 0)(<frame object at 0x7f6ec381c050>, ‘/usr/lib/python2.7/dist-packages/nova/cmd/api.py‘, 27, ‘<module>‘, [‘from nova import config\n‘], 0)(<frame object at 0x7f6ec4f4cc20>, ‘/usr/bin/nova-api‘, 6, ‘<module>‘, [‘from nova.cmd.api import main\n‘], 0)