Skip to content

Commit 73f3b37

Browse files
authored
Merge branch 'micropython:master' into format_float_with_pow
2 parents 0419279 + 6f4d424 commit 73f3b37

39 files changed

+475
-258
lines changed

docs/library/uasyncio.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,14 @@ class ThreadSafeFlag
153153

154154
.. method:: ThreadSafeFlag.set()
155155

156-
Set the flag. If there is a task waiting on the event, it will be scheduled
156+
Set the flag. If there is a task waiting on the flag, it will be scheduled
157157
to run.
158158

159+
.. method:: ThreadSafeFlag.clear()
160+
161+
Clear the flag. This may be used to ensure that a possibly previously-set
162+
flag is clear before waiting for it.
163+
159164
.. method:: ThreadSafeFlag.wait()
160165

161166
Wait for the flag to be set. If the flag is already set then it returns

extmod/modlwip.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
// All socket options should be globally distinct,
6767
// because we ignore option levels for efficiency.
6868
#define IP_ADD_MEMBERSHIP 0x400
69+
#define IP_DROP_MEMBERSHIP 0x401
6970

7071
// For compatibilily with older lwIP versions.
7172
#ifndef ip_set_option
@@ -1376,15 +1377,21 @@ STATIC mp_obj_t lwip_socket_setsockopt(size_t n_args, const mp_obj_t *args) {
13761377
}
13771378

13781379
// level: IPPROTO_IP
1379-
case IP_ADD_MEMBERSHIP: {
1380+
case IP_ADD_MEMBERSHIP:
1381+
case IP_DROP_MEMBERSHIP: {
13801382
mp_buffer_info_t bufinfo;
13811383
mp_get_buffer_raise(args[3], &bufinfo, MP_BUFFER_READ);
13821384
if (bufinfo.len != sizeof(ip_addr_t) * 2) {
13831385
mp_raise_ValueError(NULL);
13841386
}
13851387

13861388
// POSIX setsockopt has order: group addr, if addr, lwIP has it vice-versa
1387-
err_t err = igmp_joingroup((ip_addr_t *)bufinfo.buf + 1, bufinfo.buf);
1389+
err_t err;
1390+
if (opt == IP_ADD_MEMBERSHIP) {
1391+
err = igmp_joingroup((ip_addr_t *)bufinfo.buf + 1, bufinfo.buf);
1392+
} else {
1393+
err = igmp_leavegroup((ip_addr_t *)bufinfo.buf + 1, bufinfo.buf);
1394+
}
13881395
if (err != ERR_OK) {
13891396
mp_raise_OSError(error_lookup_table[-err]);
13901397
}
@@ -1769,6 +1776,7 @@ STATIC const mp_rom_map_elem_t mp_module_lwip_globals_table[] = {
17691776

17701777
{ MP_ROM_QSTR(MP_QSTR_IPPROTO_IP), MP_ROM_INT(0) },
17711778
{ MP_ROM_QSTR(MP_QSTR_IP_ADD_MEMBERSHIP), MP_ROM_INT(IP_ADD_MEMBERSHIP) },
1779+
{ MP_ROM_QSTR(MP_QSTR_IP_DROP_MEMBERSHIP), MP_ROM_INT(IP_DROP_MEMBERSHIP) },
17721780
};
17731781

17741782
STATIC MP_DEFINE_CONST_DICT(mp_module_lwip_globals, mp_module_lwip_globals_table);

extmod/modubinascii.c

Lines changed: 13 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -30,78 +30,21 @@
3030

3131
#include "py/runtime.h"
3232
#include "py/binary.h"
33+
#include "py/objstr.h"
3334

3435
#if MICROPY_PY_UBINASCII
3536

36-
STATIC mp_obj_t mod_binascii_hexlify(size_t n_args, const mp_obj_t *args) {
37-
// First argument is the data to convert.
38-
// Second argument is an optional separator to be used between values.
39-
const char *sep = NULL;
40-
mp_buffer_info_t bufinfo;
41-
mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
42-
43-
// Code below assumes non-zero buffer length when computing size with
44-
// separator, so handle the zero-length case here.
45-
if (bufinfo.len == 0) {
46-
return mp_const_empty_bytes;
47-
}
48-
49-
vstr_t vstr;
50-
size_t out_len = bufinfo.len * 2;
51-
if (n_args > 1) {
52-
// 1-char separator between hex numbers
53-
out_len += bufinfo.len - 1;
54-
sep = mp_obj_str_get_str(args[1]);
55-
}
56-
vstr_init_len(&vstr, out_len);
57-
byte *in = bufinfo.buf, *out = (byte *)vstr.buf;
58-
for (mp_uint_t i = bufinfo.len; i--;) {
59-
byte d = (*in >> 4);
60-
if (d > 9) {
61-
d += 'a' - '9' - 1;
62-
}
63-
*out++ = d + '0';
64-
d = (*in++ & 0xf);
65-
if (d > 9) {
66-
d += 'a' - '9' - 1;
67-
}
68-
*out++ = d + '0';
69-
if (sep != NULL && i != 0) {
70-
*out++ = *sep;
71-
}
72-
}
73-
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
37+
#if MICROPY_PY_BUILTINS_BYTES_HEX
38+
STATIC mp_obj_t bytes_hex_as_bytes(size_t n_args, const mp_obj_t *args) {
39+
return mp_obj_bytes_hex(n_args, args, &mp_type_bytes);
7440
}
75-
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_binascii_hexlify_obj, 1, 2, mod_binascii_hexlify);
41+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bytes_hex_as_bytes_obj, 1, 2, bytes_hex_as_bytes);
7642

77-
STATIC mp_obj_t mod_binascii_unhexlify(mp_obj_t data) {
78-
mp_buffer_info_t bufinfo;
79-
mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ);
80-
81-
if ((bufinfo.len & 1) != 0) {
82-
mp_raise_ValueError(MP_ERROR_TEXT("odd-length string"));
83-
}
84-
vstr_t vstr;
85-
vstr_init_len(&vstr, bufinfo.len / 2);
86-
byte *in = bufinfo.buf, *out = (byte *)vstr.buf;
87-
byte hex_byte = 0;
88-
for (mp_uint_t i = bufinfo.len; i--;) {
89-
byte hex_ch = *in++;
90-
if (unichar_isxdigit(hex_ch)) {
91-
hex_byte += unichar_xdigit_value(hex_ch);
92-
} else {
93-
mp_raise_ValueError(MP_ERROR_TEXT("non-hex digit found"));
94-
}
95-
if (i & 1) {
96-
hex_byte <<= 4;
97-
} else {
98-
*out++ = hex_byte;
99-
hex_byte = 0;
100-
}
101-
}
102-
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
43+
STATIC mp_obj_t bytes_fromhex_bytes(mp_obj_t data) {
44+
return mp_obj_bytes_fromhex(MP_OBJ_FROM_PTR(&mp_type_bytes), data);
10345
}
104-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_binascii_unhexlify_obj, mod_binascii_unhexlify);
46+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bytes_fromhex_obj, bytes_fromhex_bytes);
47+
#endif
10548

10649
// If ch is a character in the base64 alphabet, and is not a pad character, then
10750
// the corresponding integer between 0 and 63, inclusively, is returned.
@@ -242,8 +185,10 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_binascii_crc32_obj, 1, 2, mod_bin
242185

243186
STATIC const mp_rom_map_elem_t mp_module_binascii_globals_table[] = {
244187
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ubinascii) },
245-
{ MP_ROM_QSTR(MP_QSTR_hexlify), MP_ROM_PTR(&mod_binascii_hexlify_obj) },
246-
{ MP_ROM_QSTR(MP_QSTR_unhexlify), MP_ROM_PTR(&mod_binascii_unhexlify_obj) },
188+
#if MICROPY_PY_BUILTINS_BYTES_HEX
189+
{ MP_ROM_QSTR(MP_QSTR_hexlify), MP_ROM_PTR(&bytes_hex_as_bytes_obj) },
190+
{ MP_ROM_QSTR(MP_QSTR_unhexlify), MP_ROM_PTR(&bytes_fromhex_obj) },
191+
#endif
247192
{ MP_ROM_QSTR(MP_QSTR_a2b_base64), MP_ROM_PTR(&mod_binascii_a2b_base64_obj) },
248193
{ MP_ROM_QSTR(MP_QSTR_b2a_base64), MP_ROM_PTR(&mod_binascii_b2a_base64_obj) },
249194
#if MICROPY_PY_UBINASCII_CRC32

extmod/uasyncio/event.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,29 @@ async def wait(self):
3636
# MicroPython-extension: This can be set from outside the asyncio event loop,
3737
# such as other threads, IRQs or scheduler context. Implementation is a stream
3838
# that asyncio will poll until a flag is set.
39-
# Note: Unlike Event, this is self-clearing.
39+
# Note: Unlike Event, this is self-clearing after a wait().
4040
try:
4141
import uio
4242

4343
class ThreadSafeFlag(uio.IOBase):
4444
def __init__(self):
45-
self._flag = 0
45+
self.state = 0
4646

4747
def ioctl(self, req, flags):
4848
if req == 3: # MP_STREAM_POLL
49-
return self._flag * flags
49+
return self.state * flags
5050
return None
5151

5252
def set(self):
53-
self._flag = 1
53+
self.state = 1
54+
55+
def clear(self):
56+
self.state = 0
5457

5558
async def wait(self):
56-
if not self._flag:
59+
if not self.state:
5760
yield core._io_queue.queue_read(self)
58-
self._flag = 0
61+
self.state = 0
5962

6063
except ImportError:
6164
pass

mpy-cross/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

ports/cc3200/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
#define MICROPY_VFS_FAT (1)
7979
#define MICROPY_PY_ASYNC_AWAIT (0)
8080
#define MICROPY_PY_ALL_SPECIAL_METHODS (1)
81+
#define MICROPY_PY_BUILTINS_BYTES_HEX (1)
8182
#define MICROPY_PY_BUILTINS_INPUT (1)
8283
#define MICROPY_PY_BUILTINS_HELP (1)
8384
#define MICROPY_PY_BUILTINS_HELP_TEXT cc3200_help_text

ports/javascript/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
#define MICROPY_PY_UTIME_MP_HAL (1)
9090
#define MICROPY_REPL_AUTO_INDENT (1)
9191
#define MICROPY_PY_FUNCTION_ATTRS (1)
92+
#define MICROPY_PY_BUILTINS_BYTES_HEX (1)
9293
#define MICROPY_PY_BUILTINS_STR_UNICODE (1)
9394
#define MICROPY_PY_BUILTINS_STR_CENTER (1)
9495
#define MICROPY_PY_BUILTINS_STR_PARTITION (1)

ports/mimxrt/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ uint32_t trng_random_u32(void);
7474
#define MICROPY_PY_DESCRIPTORS (1)
7575
#define MICROPY_PY_DELATTR_SETATTR (1)
7676
#define MICROPY_PY_FSTRINGS (1)
77+
#define MICROPY_PY_BUILTINS_BYTES_HEX (1)
7778
#define MICROPY_PY_BUILTINS_STR_UNICODE (1)
7879
#define MICROPY_PY_BUILTINS_STR_CENTER (1)
7980
#define MICROPY_PY_BUILTINS_STR_PARTITION (1)

ports/nrf/modules/ubluepy/ubluepy_peripheral.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,8 @@ STATIC mp_obj_t peripheral_advertise(mp_uint_t n_args, const mp_obj_t *pos_args,
193193
memset(&adv_data, 0, sizeof(ubluepy_advertise_data_t));
194194

195195
if (device_name_obj != mp_const_none && mp_obj_is_str(device_name_obj)) {
196-
GET_STR_DATA_LEN(device_name_obj, str_data, str_len);
197-
198-
adv_data.p_device_name = (uint8_t *)str_data;
196+
size_t str_len;
197+
adv_data.p_device_name = (uint8_t *)mp_obj_str_get_data(device_name_obj, &str_len);
199198
adv_data.device_name_len = str_len;
200199
}
201200

@@ -357,7 +356,8 @@ STATIC mp_obj_t peripheral_connect(mp_uint_t n_args, const mp_obj_t *pos_args, m
357356
ble_drv_gap_event_handler_set(MP_OBJ_FROM_PTR(self), gap_event_handler);
358357

359358
if (mp_obj_is_str(dev_addr)) {
360-
GET_STR_DATA_LEN(dev_addr, str_data, str_len);
359+
size_t str_len;
360+
const byte *str_data = (const byte *)mp_obj_str_get_data(dev_addr, &str_len);
361361
if (str_len == 17) { // Example "11:22:33:aa:bb:cc"
362362

363363
uint8_t * p_addr = m_new(uint8_t, 6);

ports/nrf/modules/ubluepy/ubluepy_uuid.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ STATIC mp_obj_t ubluepy_uuid_make_new(const mp_obj_type_t *type, size_t n_args,
7070
s->value[1] = (((uint16_t)mp_obj_get_int(uuid_obj)) >> 8) & 0xFF;
7171
s->value[0] = ((uint8_t)mp_obj_get_int(uuid_obj)) & 0xFF;
7272
} else if (mp_obj_is_str(uuid_obj)) {
73-
GET_STR_DATA_LEN(uuid_obj, str_data, str_len);
73+
size_t str_len;
74+
const byte *str_data = (const byte *)mp_obj_str_get_data(uuid_obj, &str_len);
7475
if (str_len == 6) { // Assume hex digit prefixed with 0x
7576
s->type = UBLUEPY_UUID_16_BIT;
7677
s->value[0] = unichar_xdigit_value(str_data[5]);

0 commit comments

Comments
 (0)