Effective Python 读书笔记: 第57条: 考虑用pdb实现交互调试

# -*- encoding: utf-8 -*-

import os

'''
第57条: 考虑用pdb实现交互调试

关键:
1 pdb
含义: python交互调试器
调试方法: 修改程序,在需要调试的代码前一行加上如下内容
import pdb;pdb.set_trace()
调试命令:
1) bt: 打印当前执行点的调用栈信息,查看程序是如何从开始到达当前pdb处的
示例输出:
.> /home/discovery/test_utils.py(10)test_toUnicode_by_str()
-> self.assertEqual(u'hello', toUnicode('hello'))
(Pdb) bt
  /home/discovery/test_utils.py(24)<module>()
-> process()
  /home/discovery/test_utils.py(20)process()
-> main()
  /usr/lib64/python2.7/unittest/main.py(95)__init__()
-> self.runTests()
  /usr/lib64/python2.7/unittest/main.py(232)runTests()
-> self.result = testRunner.run(self.test)
  /usr/lib64/python2.7/unittest/runner.py(151)run()
-> test(result)
  /usr/lib64/python2.7/unittest/suite.py(70)__call__()
-> return self.run(*args, **kwds)
  /usr/lib64/python2.7/unittest/suite.py(108)run()
-> test(result)
  /usr/lib64/python2.7/unittest/suite.py(70)__call__()
-> return self.run(*args, **kwds)
  /usr/lib64/python2.7/unittest/suite.py(108)run()
-> test(result)
  /usr/lib64/python2.7/unittest/case.py(433)__call__()
-> return self.run(*args, **kwds)
  /usr/lib64/python2.7/unittest/case.py(369)run()
-> testMethod()
> /home/discovery/test_dirver56.py(10)test_toUnicode_by_str()
-> self.assertEqual(u'hello', toUnicode('hello'))

2) up
作用:回到函数调用栈的上一层,这样可以查看当前函数调用者信息
样例输出:
> /home/discovery/test_dirver56.py(10)test_toUnicode_by_str()
-> self.assertEqual(u'hello', toUnicode('hello'))
(Pdb) up
> /usr/lib64/python2.7/unittest/case.py(369)run()
-> testMethod()
(Pdb) l
364  	                raise
365  	            except:
366  	                result.addError(self, sys.exc_info())
367  	            else:
368  	                try:
369  ->	                    testMethod()
370  	                except KeyboardInterrupt:
371  	                    raise
372  	                except self.failureException:
373  	                    result.addFailure(self, sys.exc_info())
374  	                except _ExpectedFailure as e:

3) down: 把调试范围沿着函数调用栈下移一层
4) step: 跳入函数内部
5) next: 执行下一行代码,可直接用n代替
6) return: 直接运行到当前函数的return语句开头
7) continue: 运行到下一个断点

2 总结
通过在需要调试代码的前一行加上
import pdb;pdb.set_trace()就可以进行调试
通过bt查看函数调用栈信息,通过up查看函数调用栈上一层的变量信息,
通过down返回函数调用栈的下一层


参考:
Effectiv Python 编写高质量Python代码的59个有效方法
'''

def process():
    pass


if __name__ == "__main__":
    process() 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值