pdb (debugger)

簡単な使い方

pdbを使うのは,pdbを使うためのコードの変更と,pdbの実行の2段階になる.コードの変更では,デバッグを行うスクリプトの,デバッグを開始するよりも前の方の行に

import pdb

を書いておき,詳しく調べる行の直前に

pdb.set_trace()

を書く.読み出される関数の中にも置くことができるし,次のようにif文と組み合わせて設定することもできる.

for x in range(100):

if x==98:

import pdb

pdb.set_trace()

pdbの実行では,以下のコマンドを適宜用いる.

    • s: step in. 1行づつスクリプトを実行する.呼び出す関数についても,関数の中に入って1行づつ実行する.関数の中に入って,情報を表示できることは,他人が作ったスクリプトの中でエラーが出る場合に特に有効である.

    • n: next. 1行づつスクリプトを実行する.ただし,関数を呼び出した場合は,関数の中には入らず,1行として実行される.

    • r: return. 関数から戻る.

    • l: list. 現在実行中のソースコードを表示する.

    • p: print. p xで変数xの内容を,p type(x)で変数xの型を表示する.

    • a: argument (引数)を表示する.

使ってみよう

簡単な使い方の例として,簡単なサンプルスクリプト(main.py, main_wpdb.py, func.py)を用意したので,これらでpdbを使ってみよう.ここで,wpdbはwith pdbの意味である.ただし,main_wpdb.pyでpdbを呼び出す行は最初コメントアウトしてある

    1. 全てのファイルをダウンロードして同じディレクトリに格納する..

    2. 各ファイルを見て,何をしているのかを理解する.

    3. main.py を実行し,出力結果を見て何が行われているかを理解する.このファイルは正常に実行せれるはずである.

    4. main_wpdb.pyを実行する.このファイルでは,エラーが生じる.

    5. main_wpdb.pyのpdbを呼び出している行がコメントアウトされているので,その行から#を除いて実行する.すると関数に入る直前でdebugger が呼び出されるので,sで関数に入って,関数の中で,a, p xin, p type(xin)を行い,関数内部で変数の型および数値を確認する.

    6. main_wpdb.pyのエラーを修正する.

pdbの詳しい使いかたは,次のpython 2, python 3の公式ドキュメントに記載されている.

https://docs.python.jp/2/library/pdb.html

https://docs.python.jp/3/library/pdb.html

概要

pdbはpython 標準のデバッガーで,これを使うことで,print文を入れるよりもはるかに効率よくデバッグができる.

Overview

pdb is a python standard debugger, which allows you to debug much more efficiently than putting a print statement. The detailed usage of pdb is described in the official document of python 2 and python 3. https://docs.python.org/2/library/pdb.html https://docs.python.org/3/library/pdb.html

Getting started

Using pdb is a two-step process of 1) changing code to use pdb and 2) executing pdb. In the code change, you will include a line

import pdb

somewhere before you actually use the pdb, and insert

pdb.set_trace ()

just before you start using pdb.

In executing pdb, beginners may start using the following commands.

    • s: step in. Execute the script line by line. For the function to be called also enter the function and execute it line by line. The ability to enter the function and display the information is particularly effective when an error occurs in a script made by another person.

    • n: next. Execute the script line by line. However, when a function is called, it is executed as one line without entering the function.

    • r: return. Return from function.

    • l: list. Display the current source code.

    • p: print. p Display the contents of the variable x with x and the type of the variable x with p type (x).

    • a: argument. Display arguments for the function to which you are in.

Let's try

As an simple example, I prepared sample scripts (main.py, main_wpdb.py, func.py), so please try to use pdb with these. Where wpdb stands for with pdb. However, the line that calls pdb in main_wpdb.py is commented out at the beginning, and you will remove # from that line in the 5th step in the followings:

    1. Download all files and store them in the same directory.

    2. Look at each file and understand them.

    3. Execute main.py, see the output result, and understand what is being done. This file should be executed without an error.

    4. Execute main_wpdb.py. You will get an error.

    5. Since the line calling pdb of main_wpdb.py has been commented out, remove "#" from that line, and execute it. Then, since debugger is called immediately before entering the function, enter the function with "s", perform "a", "p xin", "p type (xin)" inside the function to know the type and value of the variable inside the function.

    6. Correct the error in main_wpdb.py.