Skip to content

Commit 7b2a9b0

Browse files
committed
py/pystack: Use "pystack exhausted" as error msg for out of pystack mem.
Using the message "maximum recursion depth exceeded" for when the pystack runs out of memory can be misleading because the pystack can run out for reasons other than deep recursion (although in most cases pystack exhaustion is probably indirectly related to deep recursion). And it's important to give the user more precise feedback as to the reason for the error: if they know precisely that the pystack was exhausted then they have a chance to increase the amount of memory available to the pystack (as opposed to not knowing if it was the C stack or pystack that ran out). Also, C stack exhaustion is more serious than pystack exhaustion because it could have been that the C stack overflowed and overwrote/corrupted some data and so the system must be restarted. The pystack can never corrupt data in this way so pystack exhaustion does not require a system restart. Knowing the difference between these two cases is therefore important. The actual exception type for pystack exhaustion remains as RuntimeError so that programatically it behaves the same as a C stack exhaustion.
1 parent 3759aa2 commit 7b2a9b0

2 files changed

Lines changed: 6 additions & 1 deletion

File tree

py/pystack.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ void *mp_pystack_alloc(size_t n_bytes) {
4343
#endif
4444
if (MP_STATE_THREAD(pystack_cur) + n_bytes > MP_STATE_THREAD(pystack_end)) {
4545
// out of memory in the pystack
46-
mp_raise_recursion_depth();
46+
nlr_raise(mp_obj_new_exception_arg1(&mp_type_RuntimeError,
47+
MP_OBJ_NEW_QSTR(MP_QSTR_pystack_space_exhausted)));
4748
}
4849
void *ptr = MP_STATE_THREAD(pystack_cur);
4950
MP_STATE_THREAD(pystack_cur) += n_bytes;

py/qstrdefs.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,7 @@ Q(<genexpr>)
5252
Q(<string>)
5353
Q(<stdin>)
5454
Q(utf-8)
55+
56+
#if MICROPY_ENABLE_PYSTACK
57+
Q(pystack exhausted)
58+
#endif

0 commit comments

Comments
 (0)