Skip to content

Commit 8c2b333

Browse files
committed
Merge branch 'master' of github.com:micropython/micropython
2 parents 7d0bfbe + 2e24ee8 commit 8c2b333

21 files changed

Lines changed: 300 additions & 91 deletions

examples/unix/sock-client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
try:
2-
import rawsocket as _socket
2+
import microsocket as _socket
33
except:
44
import _socket
55

examples/unix/sock-server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
try:
2-
import rawsocket as socket
2+
import microsocket as socket
33
except:
44
import socket
55

py/obj.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,3 +253,4 @@ mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) {
253253
mp_obj_t mp_identity(mp_obj_t self) {
254254
return self;
255255
}
256+
MP_DEFINE_CONST_FUN_OBJ_1(mp_identity_obj, mp_identity);

py/obj.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ extern const mp_obj_type_t fun_bc_type;
370370
void mp_obj_fun_bc_get(mp_obj_t self_in, int *n_args, uint *n_state, const byte **code);
371371

372372
mp_obj_t mp_identity(mp_obj_t self);
373+
MP_DECLARE_CONST_FUN_OBJ(mp_identity_obj);
373374

374375
// super
375376
extern const mp_obj_type_t super_type;
@@ -397,5 +398,9 @@ typedef struct _mp_obj_static_class_method_t {
397398
// sequence helpers
398399
void mp_seq_multiply(const void *items, uint item_sz, uint len, uint times, void *dest);
399400
bool m_seq_get_fast_slice_indexes(machine_uint_t len, mp_obj_t slice, machine_uint_t *begin, machine_uint_t *end);
400-
#define m_seq_copy(dest, src, len, item_sz) memcpy(dest, src, len * sizeof(item_sz))
401+
#define m_seq_copy(dest, src, len, item_t) memcpy(dest, src, len * sizeof(item_t))
402+
#define m_seq_cat(dest, src1, len1, src2, len2, item_t) { memcpy(dest, src1, len1 * sizeof(item_t)); memcpy(dest + len1, src2, len2 * sizeof(item_t)); }
401403
bool mp_seq_cmp_bytes(int op, const byte *data1, uint len1, const byte *data2, uint len2);
404+
bool mp_seq_cmp_objs(int op, const mp_obj_t *items1, uint len1, const mp_obj_t *items2, uint len2);
405+
mp_obj_t mp_seq_index_obj(const mp_obj_t *items, uint len, uint n_args, const mp_obj_t *args);
406+
mp_obj_t mp_seq_count_obj(const mp_obj_t *items, uint len, mp_obj_t value);

py/objlist.c

Lines changed: 4 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -75,51 +75,8 @@ static bool list_cmp_helper(int op, mp_obj_t self_in, mp_obj_t another_in) {
7575
}
7676
mp_obj_list_t *self = self_in;
7777
mp_obj_list_t *another = another_in;
78-
if (op == RT_BINARY_OP_EQUAL && self->len != another->len) {
79-
return false;
80-
}
81-
82-
// Let's deal only with > & >=
83-
if (op == RT_BINARY_OP_LESS || op == RT_BINARY_OP_LESS_EQUAL) {
84-
mp_obj_t t = self;
85-
self = another;
86-
another = t;
87-
if (op == RT_BINARY_OP_LESS) {
88-
op = RT_BINARY_OP_MORE;
89-
} else {
90-
op = RT_BINARY_OP_MORE_EQUAL;
91-
}
92-
}
93-
94-
int len = self->len < another->len ? self->len : another->len;
95-
bool eq_status = true; // empty lists are equal
96-
bool rel_status;
97-
for (int i = 0; i < len; i++) {
98-
eq_status = mp_obj_equal(self->items[i], another->items[i]);
99-
if (op == RT_BINARY_OP_EQUAL && !eq_status) {
100-
return false;
101-
}
102-
rel_status = (rt_binary_op(op, self->items[i], another->items[i]) == mp_const_true);
103-
if (!eq_status && !rel_status) {
104-
return false;
105-
}
106-
}
107-
108-
// If we had tie in the last element...
109-
if (eq_status) {
110-
// ... and we have lists of different lengths...
111-
if (self->len != another->len) {
112-
if (self->len < another->len) {
113-
// ... then longer list length wins (we deal only with >)
114-
return false;
115-
}
116-
} else if (op == RT_BINARY_OP_MORE) {
117-
// Otherwise, if we have strict relation, equality means failure
118-
return false;
119-
}
120-
}
12178

122-
return true;
79+
return mp_seq_cmp_objs(op, self->items, self->len, another->items, another->len);
12380
}
12481

12582
static mp_obj_t list_unary_op(int op, mp_obj_t self_in) {
@@ -157,8 +114,7 @@ static mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
157114
}
158115
mp_obj_list_t *p = rhs;
159116
mp_obj_list_t *s = list_new(o->len + p->len);
160-
memcpy(s->items, o->items, sizeof(mp_obj_t) * o->len);
161-
memcpy(s->items + o->len, p->items, sizeof(mp_obj_t) * p->len);
117+
m_seq_cat(s->items, o->items, o->len, p->items, p->len, mp_obj_t);
162118
return s;
163119
}
164120
case RT_BINARY_OP_INPLACE_ADD:
@@ -304,38 +260,14 @@ static mp_obj_t list_copy(mp_obj_t self_in) {
304260
static mp_obj_t list_count(mp_obj_t self_in, mp_obj_t value) {
305261
assert(MP_OBJ_IS_TYPE(self_in, &list_type));
306262
mp_obj_list_t *self = self_in;
307-
int count = 0;
308-
for (int i = 0; i < self->len; i++) {
309-
if (mp_obj_equal(self->items[i], value)) {
310-
count++;
311-
}
312-
}
313-
314-
return mp_obj_new_int(count);
263+
return mp_seq_count_obj(self->items, self->len, value);
315264
}
316265

317266
static mp_obj_t list_index(uint n_args, const mp_obj_t *args) {
318267
assert(2 <= n_args && n_args <= 4);
319268
assert(MP_OBJ_IS_TYPE(args[0], &list_type));
320269
mp_obj_list_t *self = args[0];
321-
mp_obj_t *value = args[1];
322-
uint start = 0;
323-
uint stop = self->len;
324-
325-
if (n_args >= 3) {
326-
start = mp_get_index(self->base.type, self->len, args[2]);
327-
if (n_args >= 4) {
328-
stop = mp_get_index(self->base.type, self->len, args[3]);
329-
}
330-
}
331-
332-
for (uint i = start; i < stop; i++) {
333-
if (mp_obj_equal(self->items[i], value)) {
334-
return mp_obj_new_int(i);
335-
}
336-
}
337-
338-
nlr_jump(mp_obj_new_exception_msg(MP_QSTR_ValueError, "object not in list"));
270+
return mp_seq_index_obj(self->items, self->len, n_args, args);
339271
}
340272

341273
static mp_obj_t list_insert(mp_obj_t self_in, mp_obj_t idx, mp_obj_t obj) {

py/objtuple.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@ static mp_obj_t tuple_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const m
7474
}
7575
}
7676

77+
// Don't pass RT_BINARY_OP_NOT_EQUAL here
78+
static bool tuple_cmp_helper(int op, mp_obj_t self_in, mp_obj_t another_in) {
79+
assert(MP_OBJ_IS_TYPE(self_in, &tuple_type));
80+
if (!MP_OBJ_IS_TYPE(another_in, &tuple_type)) {
81+
return false;
82+
}
83+
mp_obj_tuple_t *self = self_in;
84+
mp_obj_tuple_t *another = another_in;
85+
86+
return mp_seq_cmp_objs(op, self->items, self->len, another->items, another->len);
87+
}
88+
7789
static mp_obj_t tuple_unary_op(int op, mp_obj_t self_in) {
7890
mp_obj_tuple_t *self = self_in;
7991
switch (op) {
@@ -102,6 +114,35 @@ static mp_obj_t tuple_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
102114
uint index = mp_get_index(o->base.type, o->len, rhs);
103115
return o->items[index];
104116
}
117+
case RT_BINARY_OP_ADD:
118+
{
119+
if (!MP_OBJ_IS_TYPE(rhs, &tuple_type)) {
120+
return NULL;
121+
}
122+
mp_obj_tuple_t *p = rhs;
123+
mp_obj_tuple_t *s = mp_obj_new_tuple(o->len + p->len, NULL);
124+
m_seq_cat(s->items, o->items, o->len, p->items, p->len, mp_obj_t);
125+
return s;
126+
}
127+
case RT_BINARY_OP_MULTIPLY:
128+
{
129+
if (!MP_OBJ_IS_SMALL_INT(rhs)) {
130+
return NULL;
131+
}
132+
int n = MP_OBJ_SMALL_INT_VALUE(rhs);
133+
mp_obj_tuple_t *s = mp_obj_new_tuple(o->len * n, NULL);
134+
mp_seq_multiply(o->items, sizeof(*o->items), o->len, n, s->items);
135+
return s;
136+
}
137+
case RT_BINARY_OP_EQUAL:
138+
case RT_BINARY_OP_LESS:
139+
case RT_BINARY_OP_LESS_EQUAL:
140+
case RT_BINARY_OP_MORE:
141+
case RT_BINARY_OP_MORE_EQUAL:
142+
return MP_BOOL(tuple_cmp_helper(op, lhs, rhs));
143+
case RT_BINARY_OP_NOT_EQUAL:
144+
return MP_BOOL(!tuple_cmp_helper(RT_BINARY_OP_EQUAL, lhs, rhs));
145+
105146
default:
106147
// op not supported
107148
return NULL;
@@ -112,6 +153,26 @@ static mp_obj_t tuple_getiter(mp_obj_t o_in) {
112153
return mp_obj_new_tuple_iterator(o_in, 0);
113154
}
114155

156+
static mp_obj_t tuple_count(mp_obj_t self_in, mp_obj_t value) {
157+
assert(MP_OBJ_IS_TYPE(self_in, &tuple_type));
158+
mp_obj_tuple_t *self = self_in;
159+
return mp_seq_count_obj(self->items, self->len, value);
160+
}
161+
static MP_DEFINE_CONST_FUN_OBJ_2(tuple_count_obj, tuple_count);
162+
163+
static mp_obj_t tuple_index(uint n_args, const mp_obj_t *args) {
164+
assert(MP_OBJ_IS_TYPE(args[0], &tuple_type));
165+
mp_obj_tuple_t *self = args[0];
166+
return mp_seq_index_obj(self->items, self->len, n_args, args);
167+
}
168+
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tuple_index_obj, 2, 4, tuple_index);
169+
170+
static const mp_method_t tuple_type_methods[] = {
171+
{ "count", &tuple_count_obj },
172+
{ "index", &tuple_index_obj },
173+
{ NULL, NULL }, // end-of-list sentinel
174+
};
175+
115176
const mp_obj_type_t tuple_type = {
116177
{ &mp_const_type },
117178
"tuple",
@@ -120,6 +181,7 @@ const mp_obj_type_t tuple_type = {
120181
.unary_op = tuple_unary_op,
121182
.binary_op = tuple_binary_op,
122183
.getiter = tuple_getiter,
184+
.methods = tuple_type_methods,
123185
};
124186

125187
// the zero-length tuple

py/qstrdefs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Q(IndentationError)
3737
Q(IndexError)
3838
Q(KeyError)
3939
Q(NameError)
40+
Q(NotImplementedError)
4041
Q(OSError)
4142
Q(SyntaxError)
4243
Q(TypeError)

py/runtime.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ void rt_init(void) {
177177
mp_map_add_qstr(&map_builtins, MP_QSTR_OverflowError, mp_obj_new_exception(MP_QSTR_OverflowError));
178178
mp_map_add_qstr(&map_builtins, MP_QSTR_OSError, mp_obj_new_exception(MP_QSTR_OSError));
179179
mp_map_add_qstr(&map_builtins, MP_QSTR_AssertionError, mp_obj_new_exception(MP_QSTR_AssertionError));
180+
mp_map_add_qstr(&map_builtins, MP_QSTR_NotImplementedError, mp_obj_new_exception(MP_QSTR_NotImplementedError));
180181
mp_map_add_qstr(&map_builtins, MP_QSTR_StopIteration, mp_obj_new_exception(MP_QSTR_StopIteration));
181182

182183
// built-in objects

py/sequence.c

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,88 @@ bool mp_seq_cmp_bytes(int op, const byte *data1, uint len1, const byte *data2, u
9191
}
9292
return true;
9393
}
94+
95+
// Special-case comparison function for sequences of mp_obj_t
96+
// Don't pass RT_BINARY_OP_NOT_EQUAL here
97+
bool mp_seq_cmp_objs(int op, const mp_obj_t *items1, uint len1, const mp_obj_t *items2, uint len2) {
98+
if (op == RT_BINARY_OP_EQUAL && len1 != len2) {
99+
return false;
100+
}
101+
102+
// Let's deal only with > & >=
103+
if (op == RT_BINARY_OP_LESS || op == RT_BINARY_OP_LESS_EQUAL) {
104+
SWAP(const mp_obj_t *, items1, items2);
105+
SWAP(uint, len1, len2);
106+
if (op == RT_BINARY_OP_LESS) {
107+
op = RT_BINARY_OP_MORE;
108+
} else {
109+
op = RT_BINARY_OP_MORE_EQUAL;
110+
}
111+
}
112+
113+
int len = len1 < len2 ? len1 : len2;
114+
bool eq_status = true; // empty lists are equal
115+
bool rel_status;
116+
for (int i = 0; i < len; i++) {
117+
eq_status = mp_obj_equal(items1[i], items2[i]);
118+
if (op == RT_BINARY_OP_EQUAL && !eq_status) {
119+
return false;
120+
}
121+
rel_status = (rt_binary_op(op, items1[i], items2[i]) == mp_const_true);
122+
if (!eq_status && !rel_status) {
123+
return false;
124+
}
125+
}
126+
127+
// If we had tie in the last element...
128+
if (eq_status) {
129+
// ... and we have lists of different lengths...
130+
if (len1 != len2) {
131+
if (len1 < len2) {
132+
// ... then longer list length wins (we deal only with >)
133+
return false;
134+
}
135+
} else if (op == RT_BINARY_OP_MORE) {
136+
// Otherwise, if we have strict relation, equality means failure
137+
return false;
138+
}
139+
}
140+
141+
return true;
142+
}
143+
144+
// Special-case of index() which searches for mp_obj_t
145+
mp_obj_t mp_seq_index_obj(const mp_obj_t *items, uint len, uint n_args, const mp_obj_t *args) {
146+
mp_obj_type_t *type = mp_obj_get_type(args[0]);
147+
mp_obj_t *value = args[1];
148+
uint start = 0;
149+
uint stop = len;
150+
151+
if (n_args >= 3) {
152+
start = mp_get_index(type, len, args[2]);
153+
if (n_args >= 4) {
154+
stop = mp_get_index(type, len, args[3]);
155+
}
156+
}
157+
158+
for (uint i = start; i < stop; i++) {
159+
if (mp_obj_equal(items[i], value)) {
160+
// Common sense says this cannot overflow small int
161+
return MP_OBJ_NEW_SMALL_INT(i);
162+
}
163+
}
164+
165+
nlr_jump(mp_obj_new_exception_msg(MP_QSTR_ValueError, "object not in sequence"));
166+
}
167+
168+
mp_obj_t mp_seq_count_obj(const mp_obj_t *items, uint len, mp_obj_t value) {
169+
uint count = 0;
170+
for (uint i = 0; i < len; i++) {
171+
if (mp_obj_equal(items[i], value)) {
172+
count++;
173+
}
174+
}
175+
176+
// Common sense says this cannot overflow small int
177+
return MP_OBJ_NEW_SMALL_INT(count);
178+
}

tests/basics/tuple1.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@
1414
print(x[1:])
1515
print(x[:-1])
1616
print(x[2:3])
17+
18+
print(x + (10, 100, 10000))

0 commit comments

Comments
 (0)