2011-12-07 3 views

답변

5

나는 사후에 코드를 재개 할 수 없다고 생각합니다 (즉, 실제로 예외가 발생하여 디버거가 호출 됨). 당신이 무엇을 수 있습니다 할 오류가 발생했습니다 코드에 중단 점을 넣습니다 및 값을 변경하고 오류를 방지 프로그램을 계속할 수 있습니다.

스크립트 myscript.py 감안할 때 : 지속적는 4에 의해 지금까지 나눌이다 그러나 우리는 오류 상태에서 디버거에 드롭을 편집 한 경우 오류를 제기, 카운터를 증가

# 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() 

를, 그래서 우리는 수도 우리 자신을 구할 수 있어야합니다.

실행 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]: 
관련 문제