Skip to content

Commit 69b3ba0

Browse files
committed
py: Swap around the double return value of mp_obj_gen_resume.
Just to keep things consistent :)
1 parent 66eaf84 commit 69b3ba0

2 files changed

Lines changed: 33 additions & 29 deletions

File tree

py/objgenerator.c

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ mp_obj_t gen_instance_getiter(mp_obj_t self_in) {
7474
return self_in;
7575
}
7676

77-
mp_obj_t mp_obj_gen_resume(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw_value, mp_vm_return_kind_t *ret_kind) {
77+
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) {
7878
mp_obj_gen_instance_t *self = self_in;
7979
if (self->ip == 0) {
80-
*ret_kind = MP_VM_RETURN_NORMAL;
81-
return MP_OBJ_NULL;
80+
*ret_val = MP_OBJ_NULL;
81+
return MP_VM_RETURN_NORMAL;
8282
}
8383
if (self->sp == self->state - 1) {
8484
if (send_value != mp_const_none) {
@@ -87,38 +87,42 @@ mp_obj_t mp_obj_gen_resume(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw
8787
} else {
8888
*self->sp = send_value;
8989
}
90-
*ret_kind = mp_execute_byte_code_2(self->code_info, &self->ip,
90+
mp_vm_return_kind_t ret_kind = mp_execute_byte_code_2(self->code_info, &self->ip,
9191
&self->state[self->n_state - 1], &self->sp, (mp_exc_stack*)(self->state + self->n_state),
9292
&self->exc_sp, throw_value);
9393

94-
switch (*ret_kind) {
94+
switch (ret_kind) {
9595
case MP_VM_RETURN_NORMAL:
9696
// Explicitly mark generator as completed. If we don't do this,
9797
// subsequent next() may re-execute statements after last yield
9898
// again and again, leading to side effects.
9999
// TODO: check how return with value behaves under such conditions
100100
// in CPython.
101101
self->ip = 0;
102-
return *self->sp;
102+
*ret_val = *self->sp;
103+
break;
103104

104105
case MP_VM_RETURN_YIELD:
105-
return *self->sp;
106+
*ret_val = *self->sp;
107+
break;
106108

107109
case MP_VM_RETURN_EXCEPTION:
108110
self->ip = 0;
109-
return self->state[self->n_state - 1];
111+
*ret_val = self->state[self->n_state - 1];
112+
break;
110113

111114
default:
112115
assert(0);
113-
return mp_const_none;
116+
*ret_val = mp_const_none;
117+
break;
114118
}
119+
120+
return ret_kind;
115121
}
116122

117123
STATIC mp_obj_t gen_resume_and_raise(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw_value) {
118-
mp_vm_return_kind_t ret_kind;
119-
mp_obj_t ret = mp_obj_gen_resume(self_in, send_value, throw_value, &ret_kind);
120-
121-
switch (ret_kind) {
124+
mp_obj_t ret;
125+
switch (mp_obj_gen_resume(self_in, send_value, throw_value, &ret)) {
122126
case MP_VM_RETURN_NORMAL:
123127
// Optimize return w/o value in case generator is used in for loop
124128
if (ret == mp_const_none) {
@@ -166,24 +170,24 @@ STATIC mp_obj_t gen_instance_throw(uint n_args, const mp_obj_t *args) {
166170
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(gen_instance_throw_obj, 2, 4, gen_instance_throw);
167171

168172
STATIC mp_obj_t gen_instance_close(mp_obj_t self_in) {
169-
mp_vm_return_kind_t ret_kind;
170-
mp_obj_t ret = mp_obj_gen_resume(self_in, mp_const_none, (mp_obj_t)&mp_type_GeneratorExit, &ret_kind);
173+
mp_obj_t ret;
174+
switch (mp_obj_gen_resume(self_in, mp_const_none, (mp_obj_t)&mp_type_GeneratorExit, &ret)) {
175+
case MP_VM_RETURN_YIELD:
176+
nlr_jump(mp_obj_new_exception_msg(&mp_type_RuntimeError, "generator ignored GeneratorExit"));
171177

172-
if (ret_kind == MP_VM_RETURN_YIELD) {
173-
nlr_jump(mp_obj_new_exception_msg(&mp_type_RuntimeError, "generator ignored GeneratorExit"));
174-
}
175-
// Swallow StopIteration & GeneratorExit (== successful close), and re-raise any other
176-
if (ret_kind == MP_VM_RETURN_EXCEPTION) {
177-
// ret should always be an instance of an exception class
178-
if (mp_obj_is_subclass_fast(mp_obj_get_type(ret), &mp_type_GeneratorExit) ||
179-
mp_obj_is_subclass_fast(mp_obj_get_type(ret), &mp_type_StopIteration)) {
178+
// Swallow StopIteration & GeneratorExit (== successful close), and re-raise any other
179+
case MP_VM_RETURN_EXCEPTION:
180+
// ret should always be an instance of an exception class
181+
if (mp_obj_is_subclass_fast(mp_obj_get_type(ret), &mp_type_GeneratorExit) ||
182+
mp_obj_is_subclass_fast(mp_obj_get_type(ret), &mp_type_StopIteration)) {
183+
return mp_const_none;
184+
}
185+
nlr_jump(ret);
186+
187+
default:
188+
// The only choice left is MP_VM_RETURN_NORMAL which is successful close
180189
return mp_const_none;
181-
}
182-
nlr_jump(ret);
183190
}
184-
185-
// The only choice left is MP_VM_RETURN_NORMAL which is successful close
186-
return mp_const_none;
187191
}
188192

189193
STATIC MP_DEFINE_CONST_FUN_OBJ_1(gen_instance_close_obj, gen_instance_close);

py/objgenerator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
mp_obj_t mp_obj_gen_resume(mp_obj_t self_in, mp_obj_t send_val, mp_obj_t throw_val, mp_vm_return_kind_t *ret_kind);
1+
mp_vm_return_kind_t mp_obj_gen_resume(mp_obj_t self_in, mp_obj_t send_val, mp_obj_t throw_val, mp_obj_t *ret_val);

0 commit comments

Comments
 (0)