This implements a better (more python-conformant) list.sort.
It's not really about that, though; it's about me figuring out a sane way forward for keyword-argument functions (and function metadata). But it's useful as is, and shouldn't break any existing code, so here you have it; I'm going to park it in my mind for a bit while sorting out the rest of the dict branch.
This commit is contained in:
parent
880ce2d7fa
commit
c06763a020
30
py/obj.h
30
py/obj.h
|
@ -43,12 +43,16 @@ struct _mp_obj_base_t {
|
|||
|
||||
#define MP_DECLARE_CONST_FUN_OBJ(obj_name) extern const mp_obj_fun_native_t obj_name
|
||||
|
||||
#define MP_DEFINE_CONST_FUN_OBJ_0(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, 0, 0, fun_name}
|
||||
#define MP_DEFINE_CONST_FUN_OBJ_1(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, 1, 1, fun_name}
|
||||
#define MP_DEFINE_CONST_FUN_OBJ_2(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, 2, 2, fun_name}
|
||||
#define MP_DEFINE_CONST_FUN_OBJ_3(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, 3, 3, fun_name}
|
||||
#define MP_DEFINE_CONST_FUN_OBJ_VAR(obj_name, n_args_min, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, n_args_min, (~((machine_uint_t)0)), fun_name}
|
||||
#define MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name, n_args_min, n_args_max, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, n_args_min, n_args_max, fun_name}
|
||||
#define MP_DEFINE_CONST_FUN_OBJ_0(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, false, 0, 0, fun_name}
|
||||
#define MP_DEFINE_CONST_FUN_OBJ_1(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, false, 1, 1, fun_name}
|
||||
#define MP_DEFINE_CONST_FUN_OBJ_2(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, false, 2, 2, fun_name}
|
||||
#define MP_DEFINE_CONST_FUN_OBJ_3(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, false, 3, 3, fun_name}
|
||||
#define MP_DEFINE_CONST_FUN_OBJ_VAR(obj_name, n_args_min, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, false, n_args_min, (~((machine_uint_t)0)), fun_name}
|
||||
#define MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name, n_args_min, n_args_max, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, false, n_args_min, n_args_max, fun_name}
|
||||
#define MP_DEFINE_CONST_FUN_OBJ_KW(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, true, 0, (~((machine_uint_t)0)), fun_name}
|
||||
|
||||
// Need to declare this here so we are not dependent on map.h
|
||||
struct _mp_map_t;
|
||||
|
||||
// Type definitions for methods
|
||||
|
||||
|
@ -58,10 +62,12 @@ typedef mp_obj_t (*mp_fun_2_t)(mp_obj_t, mp_obj_t);
|
|||
typedef mp_obj_t (*mp_fun_3_t)(mp_obj_t, mp_obj_t, mp_obj_t);
|
||||
typedef mp_obj_t (*mp_fun_t)(void);
|
||||
typedef mp_obj_t (*mp_fun_var_t)(int n, const mp_obj_t *);
|
||||
typedef mp_obj_t (*mp_fun_kw_t)(mp_obj_t*, struct _mp_map_t*);
|
||||
|
||||
typedef void (*mp_print_fun_t)(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o);
|
||||
typedef mp_obj_t (*mp_make_new_fun_t)(mp_obj_t type_in, int n_args, const mp_obj_t *args); // args are in reverse order in the array
|
||||
typedef mp_obj_t (*mp_call_n_fun_t)(mp_obj_t fun, int n_args, const mp_obj_t *args); // args are in reverse order in the array
|
||||
typedef mp_obj_t (*mp_call_n_kw_fun_t)(mp_obj_t fun, int n_args, int n_kw, const mp_obj_t *args); // args are in reverse order in the array
|
||||
typedef mp_obj_t (*mp_unary_op_fun_t)(int op, mp_obj_t);
|
||||
typedef mp_obj_t (*mp_binary_op_fun_t)(int op, mp_obj_t, mp_obj_t);
|
||||
|
||||
|
@ -77,6 +83,7 @@ struct _mp_obj_type_t {
|
|||
mp_make_new_fun_t make_new; // to make an instance of the type
|
||||
|
||||
mp_call_n_fun_t call_n;
|
||||
mp_call_n_kw_fun_t call_n_kw;
|
||||
mp_unary_op_fun_t unary_op; // can return NULL if op not supported
|
||||
mp_binary_op_fun_t binary_op; // can return NULL if op not supported
|
||||
|
||||
|
@ -118,10 +125,6 @@ extern const mp_obj_t mp_const_empty_tuple;
|
|||
extern const mp_obj_t mp_const_ellipsis;
|
||||
extern const mp_obj_t mp_const_stop_iteration; // special object indicating end of iteration (not StopIteration exception!)
|
||||
|
||||
// Need to declare this here so we are not dependent on map.h
|
||||
|
||||
struct _mp_map_t;
|
||||
|
||||
// General API for objects
|
||||
|
||||
mp_obj_t mp_obj_new_none(void);
|
||||
|
@ -144,8 +147,8 @@ mp_obj_t mp_obj_new_fun_asm(uint n_args, void *fun);
|
|||
mp_obj_t mp_obj_new_gen_wrap(uint n_locals, uint n_stack, mp_obj_t fun);
|
||||
mp_obj_t mp_obj_new_gen_instance(const byte *bytecode, uint n_state, int n_args, const mp_obj_t *args);
|
||||
mp_obj_t mp_obj_new_closure(mp_obj_t fun, mp_obj_t closure_tuple);
|
||||
mp_obj_t mp_obj_new_tuple(uint n, mp_obj_t *items);
|
||||
mp_obj_t mp_obj_new_tuple_reverse(uint n, mp_obj_t *items);
|
||||
mp_obj_t mp_obj_new_tuple(uint n, const mp_obj_t *items);
|
||||
mp_obj_t mp_obj_new_tuple_reverse(uint n, const mp_obj_t *items);
|
||||
mp_obj_t mp_obj_new_list(uint n, mp_obj_t *items);
|
||||
mp_obj_t mp_obj_new_list_reverse(uint n, mp_obj_t *items);
|
||||
mp_obj_t mp_obj_new_dict(int n_args);
|
||||
|
@ -234,7 +237,8 @@ void mp_obj_slice_get(mp_obj_t self_in, machine_int_t *start, machine_int_t *sto
|
|||
// functions
|
||||
typedef struct _mp_obj_fun_native_t { // need this so we can define const objects (to go in ROM)
|
||||
mp_obj_base_t base;
|
||||
machine_uint_t n_args_min; // inclusive
|
||||
bool is_kw : 1;
|
||||
machine_uint_t n_args_min : (sizeof(machine_uint_t) - 1); // inclusive
|
||||
machine_uint_t n_args_max; // inclusive
|
||||
void *fun;
|
||||
// TODO add mp_map_t *globals
|
||||
|
|
13
py/objbool.c
13
py/objbool.c
|
@ -32,15 +32,10 @@ static mp_obj_t bool_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args
|
|||
}
|
||||
|
||||
const mp_obj_type_t bool_type = {
|
||||
{ &mp_const_type },
|
||||
"bool",
|
||||
bool_print, // print
|
||||
bool_make_new, // make_new
|
||||
NULL, // call_n
|
||||
NULL, // unary_op
|
||||
NULL, // binary_op
|
||||
NULL, // getiter
|
||||
NULL, // iternext
|
||||
.base = { &mp_const_type },
|
||||
.name = "bool",
|
||||
.print = bool_print,
|
||||
.make_new = bool_make_new,
|
||||
.methods = {{NULL, NULL},},
|
||||
};
|
||||
|
||||
|
|
|
@ -34,15 +34,9 @@ mp_obj_t bound_meth_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) {
|
|||
}
|
||||
|
||||
const mp_obj_type_t bound_meth_type = {
|
||||
{ &mp_const_type },
|
||||
"bound_method",
|
||||
NULL, // print
|
||||
NULL, // make_new
|
||||
bound_meth_call_n, // call_n
|
||||
NULL, // unary_op
|
||||
NULL, // binary_op
|
||||
NULL, // getiter
|
||||
NULL, // iternext
|
||||
.base = { &mp_const_type },
|
||||
.name = "bound_method",
|
||||
.call_n = bound_meth_call_n,
|
||||
.methods = {{NULL, NULL},},
|
||||
};
|
||||
|
||||
|
|
11
py/objcell.c
11
py/objcell.c
|
@ -24,15 +24,8 @@ void mp_obj_cell_set(mp_obj_t self_in, mp_obj_t obj) {
|
|||
}
|
||||
|
||||
const mp_obj_type_t cell_type = {
|
||||
{ &mp_const_type },
|
||||
"cell",
|
||||
NULL, // print
|
||||
NULL, // make_new
|
||||
NULL, // call_n
|
||||
NULL, // unary_op
|
||||
NULL, // binary_op
|
||||
NULL, // getiter
|
||||
NULL, // iternext
|
||||
.base = { &mp_const_type },
|
||||
.name = "cell",
|
||||
.methods = {{NULL, NULL},},
|
||||
};
|
||||
|
||||
|
|
|
@ -61,15 +61,9 @@ mp_map_t *mp_obj_class_get_locals(mp_obj_t self_in) {
|
|||
}
|
||||
|
||||
const mp_obj_type_t class_type = {
|
||||
{ &mp_const_type },
|
||||
"class",
|
||||
NULL, // print
|
||||
NULL, // make_new
|
||||
class_call_n, // call_n
|
||||
NULL, // unary_op
|
||||
NULL, // binary_op
|
||||
NULL, // getiter
|
||||
NULL, // iternext
|
||||
.base = { &mp_const_type },
|
||||
.name = "class",
|
||||
.call_n = class_call_n,
|
||||
.methods = {{NULL, NULL},},
|
||||
};
|
||||
|
||||
|
|
|
@ -33,15 +33,9 @@ mp_obj_t closure_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) {
|
|||
}
|
||||
|
||||
const mp_obj_type_t closure_type = {
|
||||
{ &mp_const_type },
|
||||
"closure",
|
||||
NULL, // print
|
||||
NULL, // make_new
|
||||
closure_call_n, // call_n
|
||||
NULL, // unary_op
|
||||
NULL, // binary_op
|
||||
NULL, // getiter
|
||||
NULL, // iternext
|
||||
.base = { &mp_const_type },
|
||||
.name = "closure",
|
||||
.call_n = closure_call_n,
|
||||
.methods = {{NULL, NULL},},
|
||||
};
|
||||
|
||||
|
|
|
@ -85,15 +85,12 @@ static mp_obj_t complex_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
|
|||
}
|
||||
|
||||
const mp_obj_type_t complex_type = {
|
||||
{ &mp_const_type },
|
||||
"complex",
|
||||
complex_print, // print
|
||||
complex_make_new, // make_new
|
||||
NULL, // call_n
|
||||
complex_unary_op, // unary_op
|
||||
complex_binary_op, // binary_op
|
||||
NULL, // getiter
|
||||
NULL, // iternext
|
||||
.base = { &mp_const_type },
|
||||
.name = "complex",
|
||||
.print = complex_print,
|
||||
.make_new = complex_make_new,
|
||||
.unary_op = complex_unary_op,
|
||||
.binary_op = complex_binary_op,
|
||||
.methods = { { NULL, NULL }, },
|
||||
};
|
||||
|
||||
|
|
|
@ -61,12 +61,11 @@ static mp_obj_t dict_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
|
|||
}
|
||||
|
||||
const mp_obj_type_t dict_type = {
|
||||
{ &mp_const_type },
|
||||
"dict",
|
||||
.base = { &mp_const_type },
|
||||
.name = "dict",
|
||||
.print = dict_print,
|
||||
.make_new = dict_make_new,
|
||||
.binary_op = dict_binary_op,
|
||||
.getiter = NULL,
|
||||
.methods = {{NULL, NULL},},
|
||||
};
|
||||
|
||||
|
|
|
@ -36,15 +36,9 @@ void exception_print(void (*print)(void *env, const char *fmt, ...), void *env,
|
|||
}
|
||||
|
||||
const mp_obj_type_t exception_type = {
|
||||
{ &mp_const_type },
|
||||
"exception",
|
||||
exception_print, // print
|
||||
NULL, // make_new
|
||||
NULL, // call_n
|
||||
NULL, // unary_op
|
||||
NULL, // binary_op
|
||||
NULL, // getiter
|
||||
NULL, // iternext
|
||||
.base = { &mp_const_type },
|
||||
.name = "exception",
|
||||
.print = exception_print,
|
||||
.methods = {{NULL, NULL},},
|
||||
};
|
||||
|
||||
|
|
|
@ -62,8 +62,8 @@ static mp_obj_t float_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
|
|||
}
|
||||
|
||||
const mp_obj_type_t float_type = {
|
||||
{ &mp_const_type },
|
||||
"float",
|
||||
.base = { &mp_const_type },
|
||||
.name = "float",
|
||||
.print = float_print,
|
||||
.make_new = float_make_new,
|
||||
.unary_op = float_unary_op,
|
||||
|
|
59
py/objfun.c
59
py/objfun.c
|
@ -17,9 +17,13 @@
|
|||
|
||||
// mp_obj_fun_native_t defined in obj.h
|
||||
|
||||
mp_obj_t fun_native_call_n_kw(mp_obj_t self_in, int n_args, int n_kw, const mp_obj_t *args);
|
||||
// args are in reverse order in the array
|
||||
mp_obj_t fun_native_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) {
|
||||
mp_obj_fun_native_t *self = self_in;
|
||||
if (self->is_kw) {
|
||||
return fun_native_call_n_kw(self_in, n_args, 0, args);
|
||||
}
|
||||
if (self->n_args_min == self->n_args_max) {
|
||||
// function requires a fixed number of arguments
|
||||
|
||||
|
@ -69,16 +73,29 @@ mp_obj_t fun_native_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) {
|
|||
}
|
||||
}
|
||||
|
||||
mp_obj_t fun_native_call_n_kw(mp_obj_t self_in, int n_args, int n_kw, const mp_obj_t *args) {
|
||||
mp_obj_fun_native_t *self = self_in;
|
||||
|
||||
if (!self->is_kw) {
|
||||
nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "function does not take keyword arguments"));
|
||||
}
|
||||
|
||||
mp_obj_t *vargs = mp_obj_new_tuple_reverse(n_args, args + 2*n_kw);
|
||||
mp_map_t *kw_args = mp_map_new(MP_MAP_QSTR, n_kw);
|
||||
for (int i = 0; i < 2*n_kw; i+=2) {
|
||||
qstr name = mp_obj_str_get(args[i+1]);
|
||||
mp_qstr_map_lookup(kw_args, name, true)->value = args[i];
|
||||
}
|
||||
mp_obj_t res = ((mp_fun_kw_t)self->fun)(vargs, kw_args);
|
||||
/* TODO clean up vargs and kw_args */
|
||||
return res;
|
||||
}
|
||||
|
||||
const mp_obj_type_t fun_native_type = {
|
||||
{ &mp_const_type },
|
||||
"function",
|
||||
NULL, // print
|
||||
NULL, // make_new
|
||||
fun_native_call_n, // call_n
|
||||
NULL, // unary_op
|
||||
NULL, // binary_op
|
||||
NULL, // getiter
|
||||
NULL, // iternext
|
||||
.base = { &mp_const_type },
|
||||
.name = "function",
|
||||
.call_n = fun_native_call_n,
|
||||
.call_n_kw = fun_native_call_n_kw,
|
||||
.methods = {
|
||||
{NULL, NULL}, // end-of-list sentinel
|
||||
},
|
||||
|
@ -172,15 +189,9 @@ mp_obj_t fun_bc_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) {
|
|||
}
|
||||
|
||||
const mp_obj_type_t fun_bc_type = {
|
||||
{ &mp_const_type },
|
||||
"function",
|
||||
NULL, // print
|
||||
NULL, // make_new
|
||||
fun_bc_call_n, // call_n
|
||||
NULL, // unary_op
|
||||
NULL, // binary_op
|
||||
NULL, // getiter
|
||||
NULL, // iternext
|
||||
.base = { &mp_const_type },
|
||||
.name = "function",
|
||||
.call_n = fun_bc_call_n,
|
||||
.methods = {
|
||||
{NULL, NULL}, // end-of-list sentinel
|
||||
},
|
||||
|
@ -286,15 +297,9 @@ mp_obj_t fun_asm_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) {
|
|||
}
|
||||
|
||||
static const mp_obj_type_t fun_asm_type = {
|
||||
{ &mp_const_type },
|
||||
"function",
|
||||
NULL, // print
|
||||
NULL, // make_new
|
||||
fun_asm_call_n, // call_n
|
||||
NULL, // unary_op
|
||||
NULL, // binary_op
|
||||
NULL, // getiter
|
||||
NULL, // iternext
|
||||
.base = { &mp_const_type },
|
||||
.name = "function",
|
||||
.call_n = fun_asm_call_n,
|
||||
.methods = {
|
||||
{NULL, NULL}, // end-of-list sentinel
|
||||
},
|
||||
|
|
|
@ -37,15 +37,11 @@ mp_obj_t gen_wrap_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) {
|
|||
}
|
||||
|
||||
const mp_obj_type_t gen_wrap_type = {
|
||||
{ &mp_const_type },
|
||||
"generator",
|
||||
NULL, // print
|
||||
NULL, // make_new
|
||||
gen_wrap_call_n, // call_n
|
||||
NULL, // unary_op
|
||||
NULL, // binary_op
|
||||
NULL, // getiter
|
||||
NULL, // iternext
|
||||
.base = { &mp_const_type },
|
||||
.name = "generator",
|
||||
.call_n = gen_wrap_call_n,
|
||||
.unary_op = NULL,
|
||||
.binary_op = NULL,
|
||||
.methods = {{NULL, NULL},},
|
||||
};
|
||||
|
||||
|
@ -92,15 +88,11 @@ mp_obj_t gen_instance_iternext(mp_obj_t self_in) {
|
|||
}
|
||||
|
||||
const mp_obj_type_t gen_instance_type = {
|
||||
{ &mp_const_type },
|
||||
"generator",
|
||||
gen_instance_print, // print
|
||||
NULL, // make_new
|
||||
NULL, // call_n
|
||||
NULL, // unary_op
|
||||
NULL, // binary_op
|
||||
gen_instance_getiter, // getiter
|
||||
gen_instance_iternext, // iternext
|
||||
.base = { &mp_const_type },
|
||||
.name = "generator",
|
||||
.print = gen_instance_print,
|
||||
.getiter = gen_instance_getiter,
|
||||
.iternext = gen_instance_iternext,
|
||||
.methods = {{NULL, NULL},},
|
||||
};
|
||||
|
||||
|
|
|
@ -90,15 +90,8 @@ void mp_obj_instance_store_attr(mp_obj_t self_in, qstr attr, mp_obj_t value) {
|
|||
}
|
||||
|
||||
const mp_obj_type_t instance_type = {
|
||||
{ &mp_const_type },
|
||||
"instance",
|
||||
NULL, // print
|
||||
NULL, // make_new
|
||||
NULL, // call_n
|
||||
NULL, // unary_op
|
||||
NULL, // binary_op
|
||||
NULL, // getiter
|
||||
NULL, // iternext
|
||||
.base = { &mp_const_type },
|
||||
.name = "instance",
|
||||
.methods = {{NULL, NULL},},
|
||||
};
|
||||
|
||||
|
|
|
@ -31,8 +31,8 @@ static mp_obj_t int_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args)
|
|||
}
|
||||
|
||||
const mp_obj_type_t int_type = {
|
||||
{ &mp_const_type },
|
||||
"int",
|
||||
.base = { &mp_const_type },
|
||||
.name = "int",
|
||||
.make_new = int_make_new,
|
||||
.methods = { { NULL, NULL }, },
|
||||
};
|
||||
|
|
45
py/objlist.c
45
py/objlist.c
|
@ -8,6 +8,7 @@
|
|||
#include "mpconfig.h"
|
||||
#include "mpqstr.h"
|
||||
#include "obj.h"
|
||||
#include "map.h"
|
||||
#include "runtime0.h"
|
||||
#include "runtime.h"
|
||||
|
||||
|
@ -119,14 +120,15 @@ static mp_obj_t list_pop(int n_args, const mp_obj_t *args) {
|
|||
}
|
||||
|
||||
// TODO make this conform to CPython's definition of sort
|
||||
static void mp_quicksort(mp_obj_t *head, mp_obj_t *tail, mp_obj_t key_fn) {
|
||||
static void mp_quicksort(mp_obj_t *head, mp_obj_t *tail, mp_obj_t key_fn, bool reversed) {
|
||||
int op = reversed ? RT_COMPARE_OP_MORE : RT_COMPARE_OP_LESS;
|
||||
while (head < tail) {
|
||||
mp_obj_t *h = head - 1;
|
||||
mp_obj_t *t = tail;
|
||||
mp_obj_t v = rt_call_function_1(key_fn, tail[0]); // get pivot using key_fn
|
||||
mp_obj_t v = key_fn == NULL ? tail[0] : rt_call_function_1(key_fn, tail[0]); // get pivot using key_fn
|
||||
for (;;) {
|
||||
do ++h; while (rt_compare_op(RT_COMPARE_OP_LESS, rt_call_function_1(key_fn, h[0]), v) == mp_const_true);
|
||||
do --t; while (h < t && rt_compare_op(RT_COMPARE_OP_LESS, v, rt_call_function_1(key_fn, t[0])) == mp_const_true);
|
||||
do ++h; while (rt_compare_op(op, key_fn == NULL ? h[0] : rt_call_function_1(key_fn, h[0]), v) == mp_const_true);
|
||||
do --t; while (h < t && rt_compare_op(op, v, key_fn == NULL ? t[0] : rt_call_function_1(key_fn, t[0])) == mp_const_true);
|
||||
if (h >= t) break;
|
||||
mp_obj_t x = h[0];
|
||||
h[0] = t[0];
|
||||
|
@ -135,16 +137,31 @@ static void mp_quicksort(mp_obj_t *head, mp_obj_t *tail, mp_obj_t key_fn) {
|
|||
mp_obj_t x = h[0];
|
||||
h[0] = tail[0];
|
||||
tail[0] = x;
|
||||
mp_quicksort(head, t, key_fn);
|
||||
mp_quicksort(head, t, key_fn, reversed);
|
||||
head = h + 1;
|
||||
}
|
||||
}
|
||||
|
||||
static mp_obj_t list_sort(mp_obj_t self_in, mp_obj_t key_fn) {
|
||||
assert(MP_OBJ_IS_TYPE(self_in, &list_type));
|
||||
mp_obj_list_t *self = self_in;
|
||||
static mp_obj_t list_sort(mp_obj_t *args, mp_map_t *kwargs) {
|
||||
mp_obj_t *args_items = NULL;
|
||||
machine_uint_t args_len = 0;
|
||||
qstr key_idx = qstr_from_str_static("key");
|
||||
qstr reverse_idx = qstr_from_str_static("reverse");
|
||||
|
||||
assert(MP_OBJ_IS_TYPE(args, &tuple_type));
|
||||
mp_obj_tuple_get(args, &args_len, &args_items);
|
||||
assert(args_len >= 1);
|
||||
if (args_len > 1) {
|
||||
nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError,
|
||||
"list.sort takes no positional arguments"));
|
||||
}
|
||||
mp_obj_list_t *self = args_items[0];
|
||||
if (self->len > 1) {
|
||||
mp_quicksort(self->items, self->items + self->len - 1, key_fn);
|
||||
mp_map_elem_t *keyfun = mp_qstr_map_lookup(kwargs, key_idx, false);
|
||||
mp_map_elem_t *reverse = mp_qstr_map_lookup(kwargs, reverse_idx, false);
|
||||
mp_quicksort(self->items, self->items + self->len - 1,
|
||||
keyfun ? keyfun->value : NULL,
|
||||
reverse && reverse->value ? rt_is_true(reverse->value) : false);
|
||||
}
|
||||
return mp_const_none; // return None, as per CPython
|
||||
}
|
||||
|
@ -258,11 +275,11 @@ static MP_DEFINE_CONST_FUN_OBJ_3(list_insert_obj, list_insert);
|
|||
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_pop_obj, 1, 2, list_pop);
|
||||
static MP_DEFINE_CONST_FUN_OBJ_2(list_remove_obj, list_remove);
|
||||
static MP_DEFINE_CONST_FUN_OBJ_1(list_reverse_obj, list_reverse);
|
||||
static MP_DEFINE_CONST_FUN_OBJ_2(list_sort_obj, list_sort);
|
||||
static MP_DEFINE_CONST_FUN_OBJ_KW(list_sort_obj, list_sort);
|
||||
|
||||
const mp_obj_type_t list_type = {
|
||||
{ &mp_const_type },
|
||||
"list",
|
||||
.base = { &mp_const_type },
|
||||
.name = "list",
|
||||
.print = list_print,
|
||||
.make_new = list_make_new,
|
||||
.unary_op = NULL,
|
||||
|
@ -341,8 +358,8 @@ mp_obj_t list_it_iternext(mp_obj_t self_in) {
|
|||
}
|
||||
|
||||
static const mp_obj_type_t list_it_type = {
|
||||
{ &mp_const_type },
|
||||
"list_iterator",
|
||||
.base = { &mp_const_type },
|
||||
.name = "list_iterator",
|
||||
.iternext = list_it_iternext,
|
||||
.methods = { { NULL, NULL }, },
|
||||
};
|
||||
|
|
|
@ -22,15 +22,9 @@ void module_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_
|
|||
}
|
||||
|
||||
const mp_obj_type_t module_type = {
|
||||
{ &mp_const_type },
|
||||
"module",
|
||||
module_print, // print
|
||||
NULL, // make_new
|
||||
NULL, // call_n
|
||||
NULL, // unary_op
|
||||
NULL, // binary_op
|
||||
NULL, // getiter
|
||||
NULL, // iternext
|
||||
.base = { &mp_const_type },
|
||||
.name = "module",
|
||||
.print = module_print,
|
||||
.methods = {{NULL, NULL},},
|
||||
};
|
||||
|
||||
|
|
|
@ -15,8 +15,8 @@ void none_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_ob
|
|||
}
|
||||
|
||||
const mp_obj_type_t none_type = {
|
||||
{ &mp_const_type },
|
||||
"NoneType",
|
||||
.base = { &mp_const_type },
|
||||
.name = "NoneType",
|
||||
.print = none_print,
|
||||
.methods = {{NULL, NULL},},
|
||||
};
|
||||
|
|
|
@ -23,15 +23,9 @@ mp_obj_t range_getiter(mp_obj_t o_in) {
|
|||
}
|
||||
|
||||
static const mp_obj_type_t range_type = {
|
||||
{ &mp_const_type} ,
|
||||
"range",
|
||||
NULL, // print
|
||||
NULL, // make_new
|
||||
NULL, // call_n
|
||||
NULL, // unary_op
|
||||
NULL, // binary_op
|
||||
range_getiter,
|
||||
NULL, // iternext
|
||||
.base = { &mp_const_type} ,
|
||||
.name = "range",
|
||||
.getiter = range_getiter,
|
||||
.methods = {{NULL, NULL},},
|
||||
};
|
||||
|
||||
|
@ -68,15 +62,9 @@ mp_obj_t range_it_iternext(mp_obj_t o_in) {
|
|||
}
|
||||
|
||||
static const mp_obj_type_t range_it_type = {
|
||||
{ &mp_const_type },
|
||||
"range_iterator",
|
||||
NULL, // print
|
||||
NULL, // make_new
|
||||
NULL, // call_n
|
||||
NULL, // unary_op
|
||||
NULL, // binary_op
|
||||
NULL, // getiter
|
||||
range_it_iternext,
|
||||
.base = { &mp_const_type },
|
||||
.name = "range_iterator",
|
||||
.iternext = range_it_iternext,
|
||||
.methods = {{NULL, NULL},},
|
||||
};
|
||||
|
||||
|
|
13
py/objset.c
13
py/objset.c
|
@ -55,15 +55,10 @@ static mp_obj_t set_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args)
|
|||
}
|
||||
|
||||
const mp_obj_type_t set_type = {
|
||||
{ &mp_const_type },
|
||||
"set",
|
||||
set_print, // print
|
||||
set_make_new, // make_new
|
||||
NULL, // call_n
|
||||
NULL, // unary_op
|
||||
NULL, // binary_op
|
||||
NULL, // getiter
|
||||
NULL, // iternext
|
||||
.base = { &mp_const_type },
|
||||
.name = "set",
|
||||
.print = set_print,
|
||||
.make_new = set_make_new,
|
||||
.methods = { { NULL, NULL }, },
|
||||
};
|
||||
|
||||
|
|
|
@ -21,15 +21,9 @@ void ellipsis_print(void (*print)(void *env, const char *fmt, ...), void *env, m
|
|||
}
|
||||
|
||||
const mp_obj_type_t ellipsis_type = {
|
||||
{ &mp_const_type },
|
||||
"ellipsis",
|
||||
ellipsis_print, // print
|
||||
NULL, // make_new
|
||||
NULL, // call_n
|
||||
NULL, // unary_op
|
||||
NULL, // binary_op
|
||||
NULL, // getiter
|
||||
NULL, // iternext
|
||||
.base = { &mp_const_type },
|
||||
.name = "ellipsis",
|
||||
.print = ellipsis_print,
|
||||
.methods = {{NULL, NULL},},
|
||||
};
|
||||
|
||||
|
@ -55,8 +49,8 @@ void slice_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_o
|
|||
}
|
||||
|
||||
const mp_obj_type_t slice_type = {
|
||||
{ &mp_const_type },
|
||||
"slice",
|
||||
.base = { &mp_const_type },
|
||||
.name = "slice",
|
||||
.print = slice_print,
|
||||
.methods = { { NULL, NULL }, },
|
||||
};
|
||||
|
|
|
@ -185,8 +185,8 @@ static MP_DEFINE_CONST_FUN_OBJ_2(str_join_obj, str_join);
|
|||
static MP_DEFINE_CONST_FUN_OBJ_VAR(str_format_obj, 1, str_format);
|
||||
|
||||
const mp_obj_type_t str_type = {
|
||||
{ &mp_const_type },
|
||||
"str",
|
||||
.base = { &mp_const_type },
|
||||
.name = "str",
|
||||
.print = str_print,
|
||||
.binary_op = str_binary_op,
|
||||
.getiter = str_getiter,
|
||||
|
@ -232,8 +232,8 @@ mp_obj_t str_it_iternext(mp_obj_t self_in) {
|
|||
}
|
||||
|
||||
static const mp_obj_type_t str_it_type = {
|
||||
{ &mp_const_type },
|
||||
"str_iterator",
|
||||
.base = { &mp_const_type },
|
||||
.name = "str_iterator",
|
||||
.iternext = str_it_iternext,
|
||||
.methods = { { NULL, NULL }, },
|
||||
};
|
||||
|
|
|
@ -97,8 +97,8 @@ static mp_obj_t tuple_getiter(mp_obj_t o_in) {
|
|||
}
|
||||
|
||||
const mp_obj_type_t tuple_type = {
|
||||
{ &mp_const_type },
|
||||
"tuple",
|
||||
.base = { &mp_const_type },
|
||||
.name = "tuple",
|
||||
.print = tuple_print,
|
||||
.make_new = tuple_make_new,
|
||||
.binary_op = tuple_binary_op,
|
||||
|
@ -110,7 +110,7 @@ const mp_obj_type_t tuple_type = {
|
|||
static const mp_obj_tuple_t empty_tuple_obj = {{&tuple_type}, 0};
|
||||
const mp_obj_t mp_const_empty_tuple = (mp_obj_t)&empty_tuple_obj;
|
||||
|
||||
mp_obj_t mp_obj_new_tuple(uint n, mp_obj_t *items) {
|
||||
mp_obj_t mp_obj_new_tuple(uint n, const mp_obj_t *items) {
|
||||
if (n == 0) {
|
||||
return mp_const_empty_tuple;
|
||||
}
|
||||
|
@ -123,7 +123,7 @@ mp_obj_t mp_obj_new_tuple(uint n, mp_obj_t *items) {
|
|||
return o;
|
||||
}
|
||||
|
||||
mp_obj_t mp_obj_new_tuple_reverse(uint n, mp_obj_t *items) {
|
||||
mp_obj_t mp_obj_new_tuple_reverse(uint n, const mp_obj_t *items) {
|
||||
if (n == 0) {
|
||||
return mp_const_empty_tuple;
|
||||
}
|
||||
|
@ -163,8 +163,8 @@ static mp_obj_t tuple_it_iternext(mp_obj_t self_in) {
|
|||
}
|
||||
|
||||
static const mp_obj_type_t tuple_it_type = {
|
||||
{ &mp_const_type },
|
||||
"tuple_iterator",
|
||||
.base = { &mp_const_type },
|
||||
.name = "tuple_iterator",
|
||||
.iternext = tuple_it_iternext,
|
||||
.methods = {{NULL, NULL},},
|
||||
};
|
||||
|
|
|
@ -23,8 +23,8 @@ static mp_obj_t type_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args)
|
|||
}
|
||||
|
||||
const mp_obj_type_t mp_const_type = {
|
||||
{ &mp_const_type },
|
||||
"type",
|
||||
.base = { &mp_const_type },
|
||||
.name = "type",
|
||||
.print = type_print,
|
||||
.call_n = type_call_n,
|
||||
.methods = {{NULL, NULL},},
|
||||
|
|
18
py/runtime.c
18
py/runtime.c
|
@ -683,10 +683,20 @@ mp_obj_t rt_call_function_n(mp_obj_t fun_in, int n_args, const mp_obj_t *args) {
|
|||
|
||||
// args are in reverse order in the array; keyword arguments come first, value then key
|
||||
// eg: (value1, key1, value0, key0, arg1, arg0)
|
||||
mp_obj_t rt_call_function_n_kw(mp_obj_t fun, uint n_args, uint n_kw, const mp_obj_t *args) {
|
||||
// TODO
|
||||
assert(0);
|
||||
return mp_const_none;
|
||||
mp_obj_t rt_call_function_n_kw(mp_obj_t fun_in, uint n_args, uint n_kw, const mp_obj_t *args) {
|
||||
// TODO merge this and _n into a single, smarter thing
|
||||
DEBUG_OP_printf("calling function %p(n_args=%d, n_kw=%d, args=%p)\n", fun_in, n_args, n_kw, args);
|
||||
|
||||
if (MP_OBJ_IS_SMALL_INT(fun_in)) {
|
||||
nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "'int' object is not callable"));
|
||||
} else {
|
||||
mp_obj_base_t *fun = fun_in;
|
||||
if (fun->type->call_n_kw != NULL) {
|
||||
return fun->type->call_n_kw(fun_in, n_args, n_kw, args);
|
||||
} else {
|
||||
nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "'%s' object is not callable", fun->type->name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// args contains: arg(n_args-1) arg(n_args-2) ... arg(0) self/NULL fun
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
l = [1, 3, 2, 5]
|
||||
print(l)
|
||||
l.sort()
|
||||
print(l)
|
||||
l.sort(key=lambda x: -x)
|
||||
print(l)
|
||||
l.sort(key=lambda x: -x, reverse=True)
|
||||
print(l)
|
||||
l.sort(reverse=True)
|
||||
print(l)
|
||||
l.sort(reverse=False)
|
||||
print(l)
|
||||
|
Loading…
Reference in New Issue