This commit is contained in:
John R. Lenton 2014-01-13 05:12:50 +00:00
parent 07205ec323
commit 5c76839559
7 changed files with 44 additions and 5 deletions

View File

@ -296,3 +296,29 @@ mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) {
}
return value;
}
extern mp_obj_t list_sort(mp_obj_t args, mp_map_t *kwargs);
static mp_obj_t mp_builtin_sorted(mp_obj_t args, mp_map_t *kwargs) {
mp_obj_t *args_items = NULL;
uint args_len = 0;
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,
"must use keyword argument for key function"));
}
mp_obj_t iterable = rt_getiter(args_items[0]);
mp_obj_t self = rt_build_list(0, NULL);
mp_obj_t item;
while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
rt_list_append(self, item);
}
mp_obj_t new_args = rt_build_tuple(1, &self);
list_sort(new_args, kwargs);
return self;
}
MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_sorted_obj, mp_builtin_sorted);

View File

@ -23,3 +23,4 @@ mp_obj_t mp_builtin_pow(int n_args, const mp_obj_t *args);
mp_obj_t mp_builtin_print(int n_args, const mp_obj_t *args);
mp_obj_t mp_builtin_range(int n_args, const mp_obj_t *args);
mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args);
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_sorted_obj);

View File

@ -57,6 +57,7 @@ Q(pow)
Q(print)
Q(range)
Q(set)
Q(sorted)
Q(sum)
Q(tuple)
Q(type)

View File

@ -248,13 +248,14 @@ static void mp_quicksort(mp_obj_t *head, mp_obj_t *tail, mp_obj_t key_fn, bool r
}
}
static mp_obj_t list_sort(mp_obj_t args, mp_map_t *kwargs) {
mp_obj_t list_sort(mp_obj_t args, mp_map_t *kwargs) {
mp_obj_t *args_items = NULL;
uint args_len = 0;
assert(MP_OBJ_IS_TYPE(args, &tuple_type));
mp_obj_tuple_get(args, &args_len, &args_items);
assert(args_len >= 1);
assert(MP_OBJ_IS_TYPE(args_items[0], &list_type));
if (args_len > 1) {
nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError,
"list.sort takes no positional arguments"));

View File

@ -134,6 +134,7 @@ void rt_init(void) {
mp_map_add_qstr(&map_builtins, MP_QSTR_pow, rt_make_function_var(2, mp_builtin_pow));
mp_map_add_qstr(&map_builtins, MP_QSTR_print, rt_make_function_var(0, mp_builtin_print));
mp_map_add_qstr(&map_builtins, MP_QSTR_range, rt_make_function_var(1, mp_builtin_range));
mp_map_add_qstr(&map_builtins, MP_QSTR_sorted, (mp_obj_t)&mp_builtin_sorted_obj);
mp_map_add_qstr(&map_builtins, MP_QSTR_sum, rt_make_function_var(1, mp_builtin_sum));
next_unique_code_id = 1; // 0 indicates "no code"

15
py/vm.c
View File

@ -440,10 +440,17 @@ bool mp_execute_byte_code_2(const byte **ip_in_out, mp_obj_t *fastn, mp_obj_t **
case MP_BC_CALL_FUNCTION:
DECODE_UINT;
assert((unum & 0xff00) == 0); // n_keyword
unum &= 0xff; // n_positional
sp += unum;
*sp = rt_call_function_n(*sp, unum, sp - unum);
if ((unum & 0xff00) == 0) {
// no keywords
unum &= 0xff; // n_positional
sp += unum;
*sp = rt_call_function_n(*sp, unum, sp - unum);
} else {
// keywords
int argsize = (unum & 0xff) + ((unum >> 7) & 0x1fe);
sp += argsize;
*sp = rt_call_function_n_kw(*sp, unum & 0xff, (unum >> 8) & 0xff, sp - argsize);
}
break;
case MP_BC_CALL_METHOD:

View File

@ -0,0 +1,2 @@
print(sorted(set(range(100))))
print(sorted(set(range(100)), key=lambda x: x + 100*(x % 2)))