After using pdb for three years, I didn't expect to be able to debug like this? Netizen: I learned

Regarding code debugging skills, I have written a lot of articles before, and there are nearly 10 articles in total. Students who paid attention to earlier should have read them.

One of them is about pdb debugging skills: use pdb for interfaceless debugging

It introduces two kinds of pdb debugging entrances, which are also well-known to most of them.

Let's take a look back

The first : specify -m pdb to open

$ python -m pdb pdb_demo.py

The second : use pdb.set_trace() to set breakpoints in the code

import pdb

pdb.set_trace()

But in fact, pdb also has two other debugging methods. The first method may not be used by 99% of developers, or even seen.

These two methods are implemented in conjunction with the interactive interface of the Python Console.

First, I prepare a Python file named utils.py, which defines a tool function of sum (for demonstration purposes only).

def sum(*args):
    result = 0
    for arg in args:
        result += arg

    return result

Then type Python in the terminal to enter the Console mode, import this module, and call the sum function. Under normal circumstances, the function can work normally.

>>> import utils
>>> utils.sum(1,2,3)
6

But if your parameter type is converted to str, the function will report an error~

>>> utils.sum(1,2,"3")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/MING/utils.py", line 4, in sum
    result += arg
TypeError: unsupported operand type(s) for +=: 'int' and 'str'

Since the error report here is deliberately triggered by me, it is easy to locate from the error report.

However, in practical applications, it is inevitable to encounter some situations where the bug cannot be directly judged from the error message.

At this time, if you can switch to the debug mode of pdb after the error is reported~

In fact, pdb supports this usage.

As long as you import pdb in the current session, and then execute pdb.pm(), you can switch to the familiar pdb debugging interface, and set a breakpoint at the wrong place, and then you can view the runtime variables at will information.

If you don't want to wait for an error to be reported before debugging, but want to enter the debug mode from the beginning, you can use the pdb.runcall() function

Some students may also think of the two functions pdb.run() and pdb.runeval(), but for these two methods, you need to debug the breakpoints in the function in advance, which is more troublesome, and generally not recommended use.

In summary, there are six main pdb debugging methods:

  1. python -m pdb: enter debug mode directly when running python file
  2. pdb.set_trace(): Set a breakpoint in advance, and then run the python file directly
  3. pdb.run(): Set breakpoints in advance, and then run the python module directly
  4. pdb.runeval(): Set breakpoints in advance, and then run the python module directly (similar to pdb.run)
  5. pdb.pm(): Switch to debug mode directly after error in Console mode, and locate the error position.
  6. pdb.runcall(): You can directly debug code snippets without setting breakpoints.

Among them, pdb.pm() is the focus of this article. Although it is unpopular, it is very useful and I recommend it to everyone.

The above is my sharing today. I hope it will be useful to you. If you also have useful debugging tips, please leave a message to share and learn from each other.

I still want to recommend the P ython learning group I built by myself : 705933274. The group is all learning Python. If you want to learn or are learning Python, you are welcome to join. Everyone is a software development party and share dry goods from time to time ( Only related to Python software development), including a copy of the latest Python advanced materials and zero-based teaching compiled by myself in 2021. Welcome to the advanced and friends interested in Python to join!

 

Guess you like

Origin blog.csdn.net/pyjishu/article/details/114824272