diff --git a/py/pystack.c b/py/pystack.c
index 767c307e9a9d92345dfb1794194a46d43ef8a84a..552e59d537445c4741c3273aad84a907fe5f3b02 100644
--- a/py/pystack.c
+++ b/py/pystack.c
@@ -43,7 +43,8 @@ void *mp_pystack_alloc(size_t n_bytes) {
     #endif
     if (MP_STATE_THREAD(pystack_cur) + n_bytes > MP_STATE_THREAD(pystack_end)) {
         // out of memory in the pystack
-        mp_raise_recursion_depth();
+        nlr_raise(mp_obj_new_exception_arg1(&mp_type_RuntimeError,
+            MP_OBJ_NEW_QSTR(MP_QSTR_pystack_space_exhausted)));
     }
     void *ptr = MP_STATE_THREAD(pystack_cur);
     MP_STATE_THREAD(pystack_cur) += n_bytes;
diff --git a/py/qstrdefs.h b/py/qstrdefs.h
index 1b480c9c75a96d2d925462a7c81145e225e13485..a609058120d7a2e4e66504281be88650702ccd29 100644
--- a/py/qstrdefs.h
+++ b/py/qstrdefs.h
@@ -52,3 +52,7 @@ Q(<genexpr>)
 Q(<string>)
 Q(<stdin>)
 Q(utf-8)
+
+#if MICROPY_ENABLE_PYSTACK
+Q(pystack exhausted)
+#endif