仅在没有调试器的情况下发生的 RecursionError [英] RecursionError that happens only without a debugger

查看:52
本文介绍了仅在没有调试器的情况下发生的 RecursionError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一些需要递归调用的事情.更大的应用程序崩溃了.在最小化问题的同时,我遇到了一个相当奇怪的现象.

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.

考虑以下代码:

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)

当我运行这段代码时,我得到RecursionError:比较时超出了最大递归深度,

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

但是如果我去 pdb 看看有什么问题,它会正确终止.

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

那么这是否意味着pdb在启动时改变了堆栈溢出限制?为什么?这不会对正在发生的事情做出错误的解释吗?

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?

推荐答案

当调试器因任何原因无法使用时,您可以使用最古老、最古老、最可靠且久经考验的调试技术:调试打印.

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.

这篇关于仅在没有调试器的情况下发生的 RecursionError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆