Skip to content

Commit 0aa4379

Browse files
committed
Merge pull request adafruit#399 from pfalcon/gen-defargs
objgenerator: Handle default args to generator functions.
2 parents f7eaf60 + 7fafb28 commit 0aa4379

4 files changed

Lines changed: 59 additions & 10 deletions

File tree

py/obj.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,6 @@ mp_obj_t mp_obj_new_range_iterator(int cur, int stop, int step);
331331
mp_obj_t mp_obj_new_fun_bc(uint scope_flags, qstr *args, uint n_args, mp_obj_t def_args, const byte *code);
332332
mp_obj_t mp_obj_new_fun_asm(uint n_args, void *fun);
333333
mp_obj_t mp_obj_new_gen_wrap(mp_obj_t fun);
334-
mp_obj_t mp_obj_new_gen_instance(const byte *bytecode, int n_args, const mp_obj_t *args);
335334
mp_obj_t mp_obj_new_closure(mp_obj_t fun, mp_obj_t closure_tuple);
336335
mp_obj_t mp_obj_new_tuple(uint n, const mp_obj_t *items);
337336
mp_obj_t mp_obj_new_list(uint n, mp_obj_t *items);
@@ -463,6 +462,8 @@ typedef struct _mp_obj_fun_native_t { // need this so we can define const object
463462
} mp_obj_fun_native_t;
464463

465464
void mp_obj_fun_bc_get(mp_obj_t self_in, int *n_args, const byte **code);
465+
bool mp_obj_fun_prepare_simple_args(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args,
466+
uint *out_args1_len, const mp_obj_t **out_args1, uint *out_args2_len, const mp_obj_t **out_args2);
466467

467468
mp_obj_t mp_identity(mp_obj_t self);
468469
MP_DECLARE_CONST_FUN_OBJ(mp_identity_obj);

py/objfun.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,42 @@ void dump_args(const mp_obj_t *a, int sz) {
161161
#endif
162162
}
163163

164+
// If it's possible to call a function without allocating new argument array,
165+
// this function returns true, together with pointers to 2 subarrays to be used
166+
// as arguments. Otherwise, it returns false. It is expected that this fucntion
167+
// will be accompanied by another, mp_obj_fun_prepare_full_args(), which will
168+
// instead take pointer to full-length out-array, and will fill it in. Rationale
169+
// being that a caller can try this function and if it succeeds, the function call
170+
// can be made without allocating extra memory. Otherwise, caller can allocate memory
171+
// and try "full" function. These functions are expected to be refactoring of
172+
// code in fun_bc_call() and evenrually replace it.
173+
bool mp_obj_fun_prepare_simple_args(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args,
174+
uint *out_args1_len, const mp_obj_t **out_args1, uint *out_args2_len, const mp_obj_t **out_args2) {
175+
mp_obj_fun_bc_t *self = self_in;
176+
177+
assert(n_kw == 0);
178+
assert(self->takes_var_args == 0);
179+
assert(self->takes_kw_args == 0);
180+
181+
mp_obj_t *extra_args = self->extra_args + self->n_def_args;
182+
uint n_extra_args = 0;
183+
184+
if (n_args > self->n_args) {
185+
goto arg_error;
186+
} else {
187+
extra_args -= self->n_args - n_args;
188+
n_extra_args += self->n_args - n_args;
189+
}
190+
*out_args1 = args;
191+
*out_args1_len = n_args;
192+
*out_args2 = extra_args;
193+
*out_args2_len = n_extra_args;
194+
return true;
195+
196+
arg_error:
197+
nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "function takes %d positional arguments but %d were given", self->n_args, n_args));
198+
}
199+
164200
STATIC mp_obj_t fun_bc_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) {
165201
DEBUG_printf("Input: ");
166202
dump_args(args, n_args);

py/objgenerator.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,21 @@ typedef struct _mp_obj_gen_wrap_t {
1818
mp_obj_t *fun;
1919
} mp_obj_gen_wrap_t;
2020

21+
mp_obj_t mp_obj_new_gen_instance(const byte *bytecode, uint n_args, const mp_obj_t *args, uint n_args2, const mp_obj_t *args2);
22+
2123
STATIC mp_obj_t gen_wrap_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) {
2224
mp_obj_gen_wrap_t *self = self_in;
2325
mp_obj_t self_fun = self->fun;
2426
assert(MP_OBJ_IS_TYPE(self_fun, &mp_type_fun_bc));
2527
int bc_n_args;
2628
const byte *bc_code;
2729
mp_obj_fun_bc_get(self_fun, &bc_n_args, &bc_code);
28-
if (n_args != bc_n_args) {
29-
nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "function takes %d positional arguments but %d were given", bc_n_args, n_args));
30-
}
31-
if (n_kw != 0) {
32-
nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError, "function does not take keyword arguments"));
33-
}
3430

35-
return mp_obj_new_gen_instance(bc_code, n_args, args);
31+
const mp_obj_t *args1, *args2;
32+
uint len1, len2;
33+
assert(mp_obj_fun_prepare_simple_args(self_fun, n_args, n_kw, args, &len1, &args1, &len2, &args2));
34+
35+
return mp_obj_new_gen_instance(bc_code, len1, args1, len2, args2);
3636
}
3737

3838
const mp_obj_type_t mp_type_gen_wrap = {
@@ -227,7 +227,7 @@ const mp_obj_type_t mp_type_gen_instance = {
227227
.locals_dict = (mp_obj_t)&gen_instance_locals_dict,
228228
};
229229

230-
mp_obj_t mp_obj_new_gen_instance(const byte *bytecode, int n_args, const mp_obj_t *args) {
230+
mp_obj_t mp_obj_new_gen_instance(const byte *bytecode, uint n_args, const mp_obj_t *args, uint n_args2, const mp_obj_t *args2) {
231231
const byte *code_info = bytecode;
232232
// get code info size, and skip the line number table
233233
machine_uint_t code_info_size = bytecode[0] | (bytecode[1] << 8) | (bytecode[2] << 16) | (bytecode[3] << 24);
@@ -254,9 +254,12 @@ mp_obj_t mp_obj_new_gen_instance(const byte *bytecode, int n_args, const mp_obj_
254254
o->n_state = n_state;
255255

256256
// copy args to end of state array, in reverse (that's how mp_execute_byte_code_2 needs it)
257-
for (int i = 0; i < n_args; i++) {
257+
for (uint i = 0; i < n_args; i++) {
258258
o->state[n_state - 1 - i] = args[i];
259259
}
260+
for (uint i = 0; i < n_args2; i++) {
261+
o->state[n_state - 1 - n_args - i] = args2[i];
262+
}
260263

261264
return o;
262265
}

tests/basics/generator-args.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Handling of "complicated" arg forms to generators
2+
# https://github.com/micropython/micropython/issues/397
3+
def gen(v=5):
4+
for i in range(v):
5+
yield i
6+
7+
print(list(gen()))
8+
# Still not supported, ditto for *args and **kwargs
9+
#print(list(gen(v=10)))

0 commit comments

Comments
 (0)