[英]For this recursive code why does Python 2.7 not give a stack overflow error but 3.5 does?
今天早些時候,我在Python
, Java
和JS
讀到了這個奇怪的案例:
try:
return True
finally:
return False
返回False
。
所以,我決定玩弄它:
def caseThree():
try:
caseThree()
except:
print("Error")
caseThree()
finally:
return False
print(caseThree())
在Python 2.7
它返回:
Error
False
但是,在Python 3.5
:
Error
Fatal Python error: Cannot recover from stack overflow.
Current thread 0x000025ec (most recent call first):
File "`<stdin>`", line 3 in caseThree
最后一行重復,直到你最終得到: ...
任何人都可以解釋為什么2.7
的代碼不會導致堆棧溢出,而3.5
呢?
看起來您遇到的錯誤實際上是預期的,因為它在Lib/test/test_sys.py
函數test_recursionlimit_fatalerror
進行了明確的測試。
現在,在沒有批評你的豐富多彩的實驗的情況下,這也是導致段錯誤的錯誤的原因(有時,請參閱問題); 問題28179已經有一個關於Python bug跟蹤器的 報告 。
如果您對導致此問題的原因感到好奇,請密切關注該主題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.