4
ipython %pdb
のマジックが有効になっているコードを実行し、そのコードが例外をスローすると、その後に実行を続行するようにコードに指示する方法はありますか?python pdb:例外が捕捉された後にコードの実行を再開しますか?
たとえば、例外がValueError: x=0 not allowed
であるとします。 pdbでx=1
を設定し、コードを実行(再開)できるようにすることはできますか?
ipython %pdb
のマジックが有効になっているコードを実行し、そのコードが例外をスローすると、その後に実行を続行するようにコードに指示する方法はありますか?python pdb:例外が捕捉された後にコードの実行を再開しますか?
たとえば、例外がValueError: x=0 not allowed
であるとします。 pdbでx=1
を設定し、コードを実行(再開)できるようにすることはできますか?
私はコードを死後再開することはできません(つまり、例外が実際に発生し、デバッガの起動をトリガした)。あなたはになります。は、エラーが発生したコードにブレークポイントを入れ、値を変更してエラーを回避してプログラムを続けることができます。
# myscript.py
from IPython.core.debugger import Tracer
# a callable to invoke the IPython debugger. debug_here() is like pdb.set_trace()
debug_here = Tracer()
def test():
counter = 0
while True:
counter += 1
if counter % 4 == 0:
# invoke debugger here, so we can prevent the forbidden condition
debug_here()
if counter % 4 == 0:
raise ValueError("forbidden counter: %s" % counter)
print counter
test()
継続的に、それは4で、これまで割り切れるだ場合はエラーを上げ、カウンタをインクリメントしかし、我々は、エラー条件の下でデバッガにドロップし、それを編集したので、我々は可能性があります:スクリプトmyscript.py
考える
自分自身を救うことができます。
実行IPythonからこのスクリプト:
In [5]: run myscript
1
2
3
> /Users/minrk/dev/ip/mine/myscript.py(14)test()
13 debug_here()
---> 14 if counter % 4 == 0:
15 raise ValueError("forbidden counter: %s" % counter)
# increment counter to prevent the error from raising:
ipdb> counter += 1
# continue the program:
ipdb> continue
5
6
7
> /Users/minrk/dev/ip/mine/myscript.py(13)test()
12 # invoke debugger here, so we can prevent the forbidden condition
---> 13 debug_here()
14 if counter % 4 == 0:
# if we just let it continue, the error will raise
ipdb> continue
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
IPython/utils/py3compat.pyc in execfile(fname, *where)
173 else:
174 filename = fname
--> 175 __builtin__.execfile(filename, *where)
myscript.py in <module>()
17 print counter
18
---> 19 test()
myscript.py in test()
11 if counter % 4 == 0:
12 # invoke debugger here, so we can prevent the forbidden condition
13 debug_here()
14 if counter % 4 == 0:
---> 15 raise ValueError("forbidden counter: %s" % counter)
ValueError: forbidden counter: 8
In [6]: