mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-25 23:11:02 +08:00
Translating 7 steps for hunting down Python code bugs.
This commit is contained in:
parent
339456790e
commit
6ddc8fcf8b
@ -61,9 +61,9 @@ Pdb, 一个 Python 调试器。
|
||||
|
||||
#### 一句题外话
|
||||
|
||||
为什么不使用 print 语句呢?我曾经依赖于 print 语句。他们有时候仍然派得上用场。但当我开始处理复杂的代码库,尤其是有网络调用的代码库,print 语句就变得太慢了。我最终得到所有打印出来的数据,但我没法追踪他们的位置和原因,而且他们变得复杂了。 But there is a more important reason to mostly use pdb. Let's say you put a print statement in and discover that something is wrong—and must have gone wrong earlier. But looking at the function where you put the print statement, you have no idea how you got there. Looking at code is a great way to see where you are going, but it is terrible for learning where you've been. And yes, I have done a grep of my code base looking for where a function is called, but this can get tedious and doesn't narrow it down much with a popular function. Pdb can be very helpful.
|
||||
为什么不使用 print 语句呢?我曾经依赖于 print 语句。他们有时候仍然派得上用场。但当我开始处理复杂的代码库,尤其是有网络调用的代码库,print 语句就变得太慢了。我最终得到所有打印出来的数据,但我没法追踪他们的位置和原因,而且他们变得复杂了。但是主要使用 pdb 还有一个更重要的原因。假设你添加一条 print 语句去发现错误问题,而且 print 语句必须早于错误出现的地方。但是,看看你放 print 语句的函数,你不知道你是怎么调用它的。查看代码是寻找的好方法,但看你以前写的代码是恐怖的。是的,我会用 grep 处理我的代码库以寻找调用函数的地方,但这会变得乏味而且匹配一个通用函数时不能缩小范围。Pdb 就变得非常有用。
|
||||
|
||||
You follow my advice, and put in a pdb break and run your test. And it whooshes on by and fails again, with no break at all. Leave your breakpoint in, and run a test already in your test suite that does something very similar to the broken test. If you have a decent test suite, you should be able to find a test that is hitting the same code you think your failed test should hit. Run that test, and when it gets to your breakpoint, do a `w` and look at the stack. If you have no idea by looking at the stack how/where the other call may have gone haywire, then go about halfway up the stack, find some code that belongs to you, and put a breakpoint in that file, one line above the one in the stack trace. Try again with the new test. Keep going back and forth, moving up the stack to figure out where your call went off the rails. If you get all the way up to the top of the trace without hitting a breakpoint, then congratulations, you have found the issue: Your app was spelled wrong. No experience here, nope, none at all.
|
||||
你遵循我的建议,打上 pdb 断点并运行你的测试。然后测试再次失败,但是没有任何一个断点被打到。保留你的断点,并运行测试套件中一个同这个失败的测试非常相似的测试。如果你有个不错的测试,你应该能够找到一个测试。它会击中了你认为你的失败测试应该击中的代码。Run that test, and when it gets to your breakpoint, do a `w` and look at the stack. If you have no idea by looking at the stack how/where the other call may have gone haywire, then go about halfway up the stack, find some code that belongs to you, and put a breakpoint in that file, one line above the one in the stack trace. Try again with the new test. Keep going back and forth, moving up the stack to figure out where your call went off the rails. If you get all the way up to the top of the trace without hitting a breakpoint, then congratulations, you have found the issue: Your app was spelled wrong. No experience here, nope, none at all.
|
||||
|
||||
### 4. Change things
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user