The idea is to use this trace-it function to build your own interactive debugger.
We're going to call this my spyder for my simple Python debugger
with the idea of a spider being something that catches all the bugs.
My spyder is a super simple debugger as it says in the name.
My spyder is a command line debugger, so it prompts you to enter a command
and the command can be something like break in line 70.
Run the program and when it reaches line 70, it stops and as it has stopped,
we can print individual variables--for instance, an x and x saying has a value of 42.
How do we do that? We need the number of variables that controls how the debugger works.
First, a variable name stepping which tells us whether we are
stepping line by line through the program or whether we're just running the program.
We also have a list of breakpoints, actually it's a mapping
of individual line numbers to arbitrary values, but if this mapping say has an entry for the number 7,
this means that whenever the debugger reaches line 7, then execution should stop.
For our current setting, we'll make a lot simple. We'll have a number of predefined breakpoints.
We will have for instance a breakpoint in line 9, and possibly another one in line 14.
Our trace-it function is being called for every single line and of course we want our debugger to stop
whenever either a breakpoint is reached or when we're stepping from one line to the next.
We want to access these global variables, so we need to declare them accordingly.
If the event is that we reached a new line, we check if we're stepping
or if our breakpoints dictionary has a value for the current line
and if this actually is the case, we print out a proper diagnostic information
which may be helpful for the debugger--what has happened, where we currently are,
the functions that executed as well as the local variables.
L'idea e' quella di usare questa funzione trace-it per costruire il nostro debugger interatttivo.
Lo chiameremo "my-spyder" che sta per MY Simple PYthon DEbuggeR
con l'idea di un ragno (spider) che cattura tutti gli insetti (bugs).
My-spider e' un debugger super-semplice, come dice il nome.
My-spyder e' un debugger a riga di comando, che ti chiedera' di inserire un comando
e il comando puo' essere una cosa tipo: interrompi a riga 70,
lancia il programma e quando raggiunge riga 70 si interrompe e quando si interrompe
possiamo visualizzare singole variabili, per esempio una x con x che ha valore 42.
Come lo possiamo fare? Ci servono delle variabili che regolino il funzionamento del debugger.
Prima, una variabile di nome "stepping" che ci dica se stiamo
percorrendo riga dopo riga il programma, o se lo stiamo semplicemente eseguendo.
Abbiamo anche un elenco di breakpoint, in effetti e' il mapping
di singoli numeri di riga per valori qualsiasi, ma se in questa voce c'e', diciamo, il numero 7,
significa che quando il debugger arriva a riga 7, l'esecuzione dovrebbe stopparsi.
Per quello che ci serve, lo faremo molto semplice: abbiamo un certo numero di breakpoints predefiniti.
Abbiamo per esempio un breakpoint a riga 9 e magari uno a riga 14.
La nostra funzione trace-in viene chiamata per ogni singola riga e ovviamente vogliamo che si stoppi
quando sia raggiunto un breakpoint o quando passiamo da una riga alla successiva.
Vogliamo accedere a queste variabili globali quindi le dichiareremo di conseguenza.
Nell'eventualita' in cui passiamo ad una nuova riga, controlliamo se siamo in stepping
o se il nostro dizionario dei breakpoint ha il valore di riga attuale
e, se e' effettivamente il caso, stampiamo a video opportune infomazioni diagnostiche
che possono essere utili al debugging: cosa e' successo, dove siamo attualmente,
le funzioni che sono in esecuzione cosi' come le variabili locali.
このtraceit関数を使って
自分の対話型デバッガを作成してください
このスパイダーをmy-spyder
つまり"自分のシンプルなPythonデバッガ"と名づけ
すべてのバグを捕らえることができるものにしましょう
名前が示すようにmy-spyderは
非常にシンプルなデバッガです
my-spyderはコマンドラインのデバッガですので
使用者がコマンドを入力します
つまり"break 70"と"run"をコマンドとして入力すると
プログラムの70行目で実行が停止します
"print x"と入力しておけば変数xを出力し
例えば変数xは42という値を持つことが分かります
どのようにやるのでしょう?
デバッガの機能を制御する多くの変数が必要です
まず変数steppingを定義しましょう
この変数でコードをラインごとに見ているかどうか
または単にプログラムを実行しているのかどうかという
状態を判断します
配列breakpointsもあります
各ラインから任意の値までのマッピングです
このマッピングの7番目が指定されたとしましょう
するとコードの7行目にデバッガが達した時に
プログラムの実行が中断されることになります
私たちが行う設定ではもっとシンプルにしましょう
ブレイクポイントをたくさん定義しておくのです
例えばコードの9行目と14行目に
ブレイクポイントを設定しておくことができます
traceit関数はラインごとに呼び出され
ブレイクポイントでプログラムの実行を止めたり
プログラムをラインごとに実行したりできます
また必要に応じて
グローバル変数にアクセスする必要があります
新たなラインに達した時は変数steppingの値を確認し
配列breakpointsの中に
現在のラインが含まれているかをチェックします
配列に含まれている場合は
診断した結果を適切に出力します
変数event、変数frame.f_linenoなど
変数の出力はデバッグの際に役立ちます
有个想法,用trace-it函数来构建你自己的交互式调试器。
我们把它叫做我的spyder,因为他是我的简单Python调试器,
想法来自于可以捉住所有bug的蜘蛛。
我的spyder非常之简单,物如其名。
命令行调试,所以它要求你输入各种命令,
比如第70行中的这个break 命令。
运行程序,到第70行时,程序就暂停,等它停下来了,
就可以打印各个变量的值,比如,这个x,假设它的值是42.
如何做到这点呢?需要一些变量来控制调试器的工作。
首先,一个叫stepping的变量,负责控制是
沿着程序逐行执行还是一次运行到底。
我们有一堆的断点,它其实就是个映射,
从独立的行数映射到任意值,但如果映射中有个数字7的条目,
也就是说,当调试器运行到第7行时,程序将暂停。
不过根据当前设置,先做简化,有一些预先定义好的断点。
比如第9行和第14行。
我们逐行调用 trace_it函数,当然,希望调试器能暂停在
是断点的那几行代码上,或我们逐行地执行的时候。
我们想读取这些全局变量,所以需要声明一下。
如果这个事件是运行到了新的一行,我们要检查是逐步运行,
或是当前行能对应到我们的断点字典中,
如果符合的话,我们就输出适当的论断信息,
能帮助到调试员--发生了什么,现在在哪里,
当前执行的函数以及局部变量。