CPython中为什么会出现Stackoverflow
Why does Stackoverflow occur in CPython
目前 python 的递归深度为 1000,但我无法理解为什么存在限制,因为所有内容都位于堆上。
Memory management in Python involves a private heap containing all
Python objects and data structures. The management of this private
heap is ensured internally by the Python memory manager
避免 Whosebug 的一种常见方法是声明自己的堆栈并使用循环,但在这种情况下我们已经在使用 heap.Is 它是由 python 内存管理设置的限制? .
CPython
是在C中实现的,而Python的数据是从堆中分配的,实现中的本地C函数调用层必然使用平台C的堆栈。因此,例如,Python 代码中深度 R
的递归调用在运行时也会导致在 C 实现中调用至少 R
深度的 C 函数。
所以它主要不是关于数据,而是关于调用堆栈深度。可以通过不那么直接依赖平台 C 的调用堆栈的方式在 C 中实现 Python 。例如,参见“Stackless Python”实验。不过,这比较棘手,核心 CPython 实现不太可能采用这种方法。
目前 python 的递归深度为 1000,但我无法理解为什么存在限制,因为所有内容都位于堆上。
Memory management in Python involves a private heap containing all Python objects and data structures. The management of this private heap is ensured internally by the Python memory manager
避免 Whosebug 的一种常见方法是声明自己的堆栈并使用循环,但在这种情况下我们已经在使用 heap.Is 它是由 python 内存管理设置的限制? .
CPython
是在C中实现的,而Python的数据是从堆中分配的,实现中的本地C函数调用层必然使用平台C的堆栈。因此,例如,Python 代码中深度 R
的递归调用在运行时也会导致在 C 实现中调用至少 R
深度的 C 函数。
所以它主要不是关于数据,而是关于调用堆栈深度。可以通过不那么直接依赖平台 C 的调用堆栈的方式在 C 中实现 Python 。例如,参见“Stackless Python”实验。不过,这比较棘手,核心 CPython 实现不太可能采用这种方法。