递归日志记录使Python 3中的解释器崩溃 [英] Recursive logging crashes Interpreter in Python 3

查看:70
本文介绍了递归日志记录使Python 3中的解释器崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码记录了一个错误,并调用自身导致栈溢出,并最终导致Python 3.6中的核心转储.

Following code logs an error and calls itself leading to stack overflow and eventually core dump in Python 3.6.

>>> import logging
>>> def rec():
...     logging.error("foo")
...     rec()
>>> rec()

[1]    101641 abort (core dumped)  python3

FTR,这不会使Python 2.7崩溃.

FTR, this doesn't crash Python 2.7.

在Python 3.6中附加错误(压缩):

Attaching the error (condensed) in Python 3.6:

ERROR:root:foo
...
--- Logging error ---
Traceback (most recent call last):
...
RecursionError: maximum recursion depth exceeded in comparison
...
Fatal Python error: Cannot recover from stack overflow.
...
[1]    101641 abort (core dumped)  python3

Python 2.7:

Python 2.7:

RuntimeError: maximum recursion depth exceeded

但是Python 2.7中没有核心转储.

But no core dump in Python 2.7.

FTR,如果将日志级别设置为logging.ERROR,则Python 3.6的上述错误将起作用.对于其他日志级别也是如此.

FTR, the error above with Python 3.6 will come into play if the log level is set to logging.ERROR. Similarly for other log levels.

更新:我已经记录了问题,以便在Python社区中进行跟踪.

UPDATE: I have logged issue to follow this up with the Python community.

推荐答案

您的代码最终将导致 RuntimeError (python 2.7)或 RecursionError (python 3.6).这是因为两者的递归深度均限制为1000:

Your code will eventually result in a RuntimeError (python 2.7) or RecursionError (python 3.6). This is because the recursion depth in both is limited to 1000:

>>>import sys
>>>sys.getrecursionlimit()
1000

如果它实际上是由堆栈溢出引起的崩溃,则可能的原因是递归深度限制已被修改.

If it's actually causing a stack-overflow induced crash, one possible reason is that the recursion depth limit has been modified.

这篇关于递归日志记录使Python 3中的解释器崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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