Skip to content

Commit ff8007c

Browse files
committed
Merge remote-tracking branch 'upstream/master' into builtins
2 parents 9daa789 + c6920d3 commit ff8007c

29 files changed

Lines changed: 3004 additions & 98 deletions

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
The Micro Python project
22
========================
3+
<p align="center">
4+
<img src="https://raw2.github.com/micropython/micropython/master/logo/upython-with-micro.png" alt="MicroPython Logo"/>
5+
</p>
36

47
This is the Micro Python project, which aims to put an implementation
58
of Python 3.x on a microcontroller.

logo/upython-with-micro.png

563 KB
Loading

py/builtin.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ static mp_obj_t mp_builtin_sorted(mp_obj_t args, mp_map_t *kwargs) {
339339
}
340340
mp_obj_t self = list_type.make_new((mp_obj_t)&list_type, 1, args_items);
341341
mp_obj_t new_args = rt_build_tuple(1, &self);
342-
list_sort(new_args, kwargs);
342+
mp_obj_list_sort(new_args, kwargs);
343343

344344
return self;
345345
}

py/lexer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ mp_lexer_t *mp_lexer_new(const char *src_name, void *stream_data, mp_lexer_strea
614614
lex->num_indent_level = 1;
615615
lex->indent_level = m_new(uint16_t, lex->alloc_indent_level);
616616
lex->indent_level[0] = 0;
617-
vstr_init(&lex->vstr);
617+
vstr_init(&lex->vstr, 32);
618618

619619
// preload characters
620620
lex->chr0 = stream_next_char(stream_data);

py/misc.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,19 @@ typedef struct _vstr_t {
5959
bool had_error;
6060
} vstr_t;
6161

62-
void vstr_init(vstr_t *vstr);
62+
void vstr_init(vstr_t *vstr, int alloc);
6363
void vstr_clear(vstr_t *vstr);
6464
vstr_t *vstr_new(void);
65+
vstr_t *vstr_new_size(int alloc);
6566
void vstr_free(vstr_t *vstr);
6667
void vstr_reset(vstr_t *vstr);
6768
bool vstr_had_error(vstr_t *vstr);
6869
char *vstr_str(vstr_t *vstr);
6970
int vstr_len(vstr_t *vstr);
7071
void vstr_hint_size(vstr_t *vstr, int size);
72+
char *vstr_extend(vstr_t *vstr, int size);
73+
bool vstr_set_size(vstr_t *vstr, int size);
74+
bool vstr_shrink(vstr_t *vstr);
7175
char *vstr_add_len(vstr_t *vstr, int len);
7276
void vstr_add_byte(vstr_t *vstr, byte v);
7377
void vstr_add_char(vstr_t *vstr, unichar chr);

py/obj.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ extern const mp_obj_type_t list_type;
292292
mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg);
293293
void mp_obj_list_get(mp_obj_t self_in, uint *len, mp_obj_t **items);
294294
void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value);
295-
mp_obj_t list_sort(mp_obj_t args, struct _mp_map_t *kwargs);
295+
mp_obj_t mp_obj_list_sort(mp_obj_t args, struct _mp_map_t *kwargs);
296296

297297
// enumerate
298298
extern const mp_obj_type_t enumerate_type;

py/objdict.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ static mp_obj_t dict_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
5757
return elem->value;
5858
}
5959
}
60+
case RT_COMPARE_OP_IN:
61+
case RT_COMPARE_OP_NOT_IN:
62+
{
63+
mp_map_elem_t *elem = mp_map_lookup(&o->map, rhs_in, MP_MAP_LOOKUP);
64+
return MP_BOOL((op == RT_COMPARE_OP_IN) ^ (elem == NULL));
65+
}
6066
default:
6167
// op not supported
6268
return NULL;
@@ -362,10 +368,20 @@ static void dict_view_print(void (*print)(void *env, const char *fmt, ...), void
362368
print(env, "])");
363369
}
364370

371+
static mp_obj_t dict_view_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
372+
/* only supported for the 'keys' kind until sets and dicts are refactored */
373+
mp_obj_dict_view_t *o = lhs_in;
374+
if (o->kind != MP_DICT_VIEW_KEYS) return NULL;
375+
if (op != RT_COMPARE_OP_IN && op != RT_COMPARE_OP_NOT_IN) return NULL;
376+
return dict_binary_op(op, o->dict, rhs_in);
377+
}
378+
379+
365380
static const mp_obj_type_t dict_view_type = {
366381
{ &mp_const_type },
367382
"dict_view",
368383
.print = dict_view_print,
384+
.binary_op = dict_view_binary_op,
369385
.getiter = dict_view_getiter,
370386
};
371387

py/objexcept.c

Lines changed: 46 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -8,105 +8,86 @@
88
#include "misc.h"
99
#include "mpconfig.h"
1010
#include "obj.h"
11+
#include "objtuple.h"
1112

13+
// This is unified class for C-level and Python-level exceptions
14+
// Python-level exception have empty ->msg and all arguments are in
15+
// args tuple. C-level excepttion likely have ->msg, and may as well
16+
// have args tuple (or otherwise have it as NULL).
1217
typedef struct mp_obj_exception_t {
1318
mp_obj_base_t base;
1419
qstr id;
15-
int n_args;
16-
const void *args[];
20+
qstr msg;
21+
mp_obj_tuple_t args;
1722
} mp_obj_exception_t;
1823

1924
void exception_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in) {
2025
mp_obj_exception_t *o = o_in;
21-
switch (o->n_args) {
22-
case 0:
23-
print(env, "%s", qstr_str(o->id));
24-
break;
25-
case 1:
26-
print(env, "%s: %s", qstr_str(o->id), (const char*)o->args[0]);
27-
break;
28-
case 2:
29-
print(env, "%s: ", qstr_str(o->id));
30-
print(env, (const char*)o->args[0], o->args[1]);
31-
break;
32-
default: // here we just assume at least 3 args, but only use first 3
33-
print(env, "%s: ", qstr_str(o->id));
34-
print(env, (const char*)o->args[0], o->args[1], o->args[2]);
35-
break;
26+
if (o->msg != 0) {
27+
print(env, "%s: %s", qstr_str(o->id), qstr_str(o->msg));
28+
} else {
29+
print(env, "%s", qstr_str(o->id));
30+
tuple_print(print, env, &o->args);
3631
}
3732
}
3833

34+
// args in reversed order
35+
static mp_obj_t exception_call(mp_obj_t self_in, int n_args, const mp_obj_t *args) {
36+
mp_obj_exception_t *base = self_in;
37+
mp_obj_exception_t *o = m_new_obj_var(mp_obj_exception_t, mp_obj_t*, n_args);
38+
o->base.type = &exception_type;
39+
o->id = base->id;
40+
o->msg = 0;
41+
o->args.len = n_args;
42+
43+
// TODO: factor out as reusable copy_reversed()
44+
int j = 0;
45+
for (int i = n_args - 1; i >= 0; i--) {
46+
o->args.items[i] = args[j++];
47+
}
48+
return o;
49+
}
50+
3951
const mp_obj_type_t exception_type = {
4052
{ &mp_const_type },
4153
"exception",
4254
.print = exception_print,
55+
.call_n = exception_call,
4356
};
4457

4558
mp_obj_t mp_obj_new_exception(qstr id) {
46-
mp_obj_exception_t *o = m_new_obj(mp_obj_exception_t);
47-
o->base.type = &exception_type;
48-
o->id = id;
49-
o->n_args = 0;
50-
return o;
59+
return mp_obj_new_exception_msg_varg(id, NULL);
5160
}
5261

5362
mp_obj_t mp_obj_new_exception_msg(qstr id, const char *msg) {
54-
mp_obj_exception_t *o = m_new_obj_var(mp_obj_exception_t, void*, 1);
55-
o->base.type = &exception_type;
56-
o->id = id;
57-
o->n_args = 1;
58-
o->args[0] = msg;
59-
return o;
63+
return mp_obj_new_exception_msg_varg(id, msg);
6064
}
6165

6266
mp_obj_t mp_obj_new_exception_msg_1_arg(qstr id, const char *fmt, const char *a1) {
63-
mp_obj_exception_t *o = m_new_obj_var(mp_obj_exception_t, void*, 2);
64-
o->base.type = &exception_type;
65-
o->id = id;
66-
o->n_args = 2;
67-
o->args[0] = fmt;
68-
o->args[1] = a1;
69-
return o;
67+
return mp_obj_new_exception_msg_varg(id, fmt, a1);
7068
}
7169

7270
mp_obj_t mp_obj_new_exception_msg_2_args(qstr id, const char *fmt, const char *a1, const char *a2) {
73-
mp_obj_exception_t *o = m_new_obj_var(mp_obj_exception_t, void*, 3);
74-
o->base.type = &exception_type;
75-
o->id = id;
76-
o->n_args = 3;
77-
o->args[0] = fmt;
78-
o->args[1] = a1;
79-
o->args[2] = a2;
80-
return o;
71+
return mp_obj_new_exception_msg_varg(id, fmt, a1, a2);
8172
}
8273

8374
mp_obj_t mp_obj_new_exception_msg_varg(qstr id, const char *fmt, ...) {
84-
// count number of arguments by number of % signs, excluding %%
85-
int n_args = 1; // count fmt
86-
for (const char *s = fmt; *s; s++) {
87-
if (*s == '%') {
88-
if (s[1] == '%') {
89-
s += 1;
90-
} else {
91-
n_args += 1;
92-
}
93-
}
94-
}
95-
9675
// make exception object
97-
mp_obj_exception_t *o = m_new_obj_var(mp_obj_exception_t, void*, n_args);
76+
mp_obj_exception_t *o = m_new_obj_var(mp_obj_exception_t, mp_obj_t*, 0);
9877
o->base.type = &exception_type;
9978
o->id = id;
100-
o->n_args = n_args;
101-
o->args[0] = fmt;
102-
103-
// extract args and store them
104-
va_list ap;
105-
va_start(ap, fmt);
106-
for (int i = 1; i < n_args; i++) {
107-
o->args[i] = va_arg(ap, void*);
79+
o->args.len = 0;
80+
if (fmt == NULL) {
81+
o->msg = 0;
82+
} else {
83+
// render exception message
84+
vstr_t *vstr = vstr_new();
85+
va_list ap;
86+
va_start(ap, fmt);
87+
vstr_vprintf(vstr, fmt, ap);
88+
va_end(ap);
89+
o->msg = qstr_from_str_take(vstr->buf, vstr->alloc);
10890
}
109-
va_end(ap);
11091

11192
return o;
11293
}

py/objlist.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ static void mp_quicksort(mp_obj_t *head, mp_obj_t *tail, mp_obj_t key_fn, bool r
248248
}
249249
}
250250

251-
mp_obj_t list_sort(mp_obj_t args, mp_map_t *kwargs) {
251+
mp_obj_t mp_obj_list_sort(mp_obj_t args, mp_map_t *kwargs) {
252252
mp_obj_t *args_items = NULL;
253253
uint args_len = 0;
254254

@@ -381,7 +381,7 @@ static MP_DEFINE_CONST_FUN_OBJ_3(list_insert_obj, list_insert);
381381
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_pop_obj, 1, 2, list_pop);
382382
static MP_DEFINE_CONST_FUN_OBJ_2(list_remove_obj, list_remove);
383383
static MP_DEFINE_CONST_FUN_OBJ_1(list_reverse_obj, list_reverse);
384-
static MP_DEFINE_CONST_FUN_OBJ_KW(list_sort_obj, 0, list_sort);
384+
static MP_DEFINE_CONST_FUN_OBJ_KW(list_sort_obj, 0, mp_obj_list_sort);
385385

386386
static const mp_method_t list_type_methods[] = {
387387
{ "append", &list_append_obj },

py/objset.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ void set_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj
4545
print(env, "}");
4646
}
4747

48+
4849
static mp_obj_t set_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) {
4950
switch (n_args) {
5051
case 0:
@@ -405,6 +406,13 @@ static mp_obj_t set_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
405406
return set_issuperset(lhs, rhs);
406407
case RT_COMPARE_OP_NOT_EQUAL:
407408
return MP_BOOL(set_equal(lhs, rhs) == mp_const_false);
409+
case RT_COMPARE_OP_IN:
410+
case RT_COMPARE_OP_NOT_IN:
411+
{
412+
mp_obj_set_t *o = lhs;
413+
mp_obj_t elem = mp_set_lookup(&o->set, rhs, MP_MAP_LOOKUP);
414+
return MP_BOOL((op == RT_COMPARE_OP_IN) ^ (elem == NULL));
415+
}
408416
default:
409417
// op not supported
410418
return NULL;

0 commit comments

Comments
 (0)