RecursionError that happens only without a debugger

。_饼干妹妹 提交于 2019-12-11 07:00:51

问题


I am working on something that needs a recursive calling. The bigger app crashed. While minimizing a problem, I am experiencing a rather strange phenomena.

consider the following code:

def merge(A, s, m, e):
    left = A[s:m+1]
    right = A[m+1:e+1]

    i = 0
    j = 0
    k = s
    while(i < len(left) and j < len(right)):
        if left[i] < right[j]:
            A[k] = left[i]
            k+=1
            i+=1
        else:
            A[k] = right[j]
            k+=1
            j+=1
    while i < len(left):
        A[k] = left[i]
        k+=1
        i+=1
    while j < len(right):
        A[k] = right[j]
        k+=1
        j+=1
    return A

def mergesort(A, s, e):
    if(s < e):
        m = s + (e-s)/2
        mergesort(A, s, m)
        mergesort(A, m+1, e)
        merge(A, s, m, e)
    return A

A = [5, 3, 8, 0]
print('original')
print(A)
A = mergesort(A, 0, len(A)-1)
print('sorted:')
print(A)

when I run this code, i get RecursionError: maximum recursion depth exceeded in comparison,

but if I go to pdb to see what is wrong, it terminates correctly.

sorted:
[0, 3, 5, 8]
The program finished and will be restarted

So does this mean pdb changes the stack overflow limit when it starts? Why? Won't that give a false interpretations of what is going on?

Also, for this code specifically, the recursion is not that deep, why the limit is being reached in the first place?


回答1:


When a debugger proper is not usable for any reason, you have the oldest, most venerable, reliable and time-proven debugging technique: debug printing.

E.g. make the functions report the recursion depth:

def merge(A, s, m, e, r):
    print("merge: %d"%r)
    <...>

def mergesort(A, s, e, r=0):
    print("mergesort: %d"%r)
    <...>
        mergesort(A, s, m, r+1)
        mergesort(A, m+1, e, r+1)
        merge(A, s, m, e, r+1)
    <...>

And compare the outputs with and without the debugger if you're wondering what changes. Or just the faulty output to diagnose the problem -- whichever you think is a better investment of your time.



来源:https://stackoverflow.com/questions/47008036/recursionerror-that-happens-only-without-a-debugger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!