Skip to content

Commit cbf981f

Browse files
jeplerdpgeorge
authored andcommitted
py/objgenerator: Check stack before resuming a generator.
This turns a hard crash in a recursive generator into a 'maximum recursion depth exceeded' exception.
1 parent 6a693db commit cbf981f

3 files changed

Lines changed: 12 additions & 0 deletions

File tree

py/objgenerator.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "py/bc.h"
3333
#include "py/objgenerator.h"
3434
#include "py/objfun.h"
35+
#include "py/stackctrl.h"
3536

3637
/******************************************************************************/
3738
/* generator wrapper */
@@ -92,6 +93,7 @@ STATIC void gen_instance_print(const mp_print_t *print, mp_obj_t self_in, mp_pri
9293
}
9394

9495
mp_vm_return_kind_t mp_obj_gen_resume(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw_value, mp_obj_t *ret_val) {
96+
MP_STACK_CHECK();
9597
mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_gen_instance));
9698
mp_obj_gen_instance_t *self = MP_OBJ_TO_PTR(self_in);
9799
if (self->code_state.ip == 0) {

tests/run-tests

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ def run_tests(pyb, tests, args, base_path="."):
367367
skip_tests.add('micropython/heapalloc_iter.py') # requires generators
368368
skip_tests.add('micropython/schedule.py') # native code doesn't check pending events
369369
skip_tests.add('stress/gc_trace.py') # requires yield
370+
skip_tests.add('stress/recursive_gen.py') # requires yield
370371

371372
for test_file in tests:
372373
test_file = test_file.replace('\\', '/')

tests/stress/recursive_gen.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# test deeply recursive generators
2+
3+
def gen():
4+
yield from gen()
5+
6+
try:
7+
list(gen())
8+
except RuntimeError:
9+
print('RuntimeError')

0 commit comments

Comments
 (0)