Skip to content
This repository has been archived by the owner on Feb 13, 2021. It is now read-only.

Commit

Permalink
Merge pull request micropython#7 from micropython/master
Browse files Browse the repository at this point in the history
*: Heading to 2015-09-04
  • Loading branch information
EcmaXp committed Sep 4, 2015
2 parents 8ceaaa2 + 8d8fdcb commit 9ea0bbc
Show file tree
Hide file tree
Showing 32 changed files with 197 additions and 36 deletions.
3 changes: 1 addition & 2 deletions py/argcheck.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ NORETURN void mp_arg_error_terse_mismatch(void) {

#if MICROPY_CPYTHON_COMPAT
NORETURN void mp_arg_error_unimpl_kw(void) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError,
"keyword argument(s) not yet implemented - use normal args instead"));
mp_not_implemented("keyword argument(s) not yet implemented - use normal args instead");
}
#endif
23 changes: 15 additions & 8 deletions py/binary.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ mp_obj_t mp_binary_get_val_array(char typecode, void *p, mp_uint_t index) {
return mp_obj_new_int(((long*)p)[index]);
case 'L':
return mp_obj_new_int_from_uint(((unsigned long*)p)[index]);
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
case 'q':
case 'Q':
// TODO: Explode API more to cover signedness
return mp_obj_new_int_from_ll(((long long*)p)[index]);
#endif
case 'Q':
return mp_obj_new_int_from_ull(((unsigned long long*)p)[index]);
#endif
#if MICROPY_PY_BUILTINS_FLOAT
case 'f':
return mp_obj_new_float(((float*)p)[index]);
Expand Down Expand Up @@ -316,6 +316,13 @@ void mp_binary_set_val_array(char typecode, void *p, mp_uint_t index, mp_obj_t v
((mp_obj_t*)p)[index] = val_in;
break;
default:
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
if ((typecode | 0x20) == 'q' && MP_OBJ_IS_TYPE(val_in, &mp_type_int)) {
mp_obj_int_to_bytes_impl(val_in, MP_ENDIANNESS_BIG,
sizeof(long long), (byte*)&((long long*)p)[index]);
return;
}
#endif
mp_binary_set_val_array_from_int(typecode, p, index, mp_obj_get_int(val_in));
}
}
Expand Down Expand Up @@ -347,13 +354,13 @@ void mp_binary_set_val_array_from_int(char typecode, void *p, mp_uint_t index, m
case 'L':
((unsigned long*)p)[index] = val;
break;
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
case 'q':
case 'Q':
assert(0);
((long long*)p)[index] = val;
case 'Q':
((unsigned long long*)p)[index] = val;
break;
#endif
#endif
#if MICROPY_PY_BUILTINS_FLOAT
case 'f':
((float*)p)[index] = val;
Expand Down
1 change: 1 addition & 0 deletions py/makeqstrdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
codepoint2name[ord('*')] = 'star'
codepoint2name[ord('!')] = 'bang'
codepoint2name[ord('\\')] = 'backslash'
codepoint2name[ord('+')] = 'plus'

# this must match the equivalent function in qstr.c
def compute_hash(qstr, bytes_hash):
Expand Down
4 changes: 3 additions & 1 deletion py/obj.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "py/obj.h"
#include "py/objtype.h"
#include "py/objint.h"
#include "py/objstr.h"
#include "py/runtime0.h"
#include "py/runtime.h"
#include "py/stackctrl.h"
Expand Down Expand Up @@ -422,7 +423,8 @@ mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) {
MP_OBJ_IS_STR(o_in) ||
#endif
MP_OBJ_IS_TYPE(o_in, &mp_type_bytes)) {
return MP_OBJ_NEW_SMALL_INT(mp_obj_str_get_len(o_in));
GET_STR_LEN(o_in, l);
return MP_OBJ_NEW_SMALL_INT(l);
} else {
mp_obj_type_t *type = mp_obj_get_type(o_in);
if (type->unary_op != NULL) {
Expand Down
2 changes: 0 additions & 2 deletions py/obj.h
Original file line number Diff line number Diff line change
Expand Up @@ -597,8 +597,6 @@ void mp_init_emergency_exception_buf(void);

// str
bool mp_obj_str_equal(mp_obj_t s1, mp_obj_t s2);
mp_uint_t mp_obj_str_get_hash(mp_obj_t self_in);
mp_uint_t mp_obj_str_get_len(mp_obj_t self_in);
qstr mp_obj_str_get_qstr(mp_obj_t self_in); // use this if you will anyway convert the string to a qstr
const char *mp_obj_str_get_str(mp_obj_t self_in); // use this only if you need the string to be null terminated
const char *mp_obj_str_get_data(mp_obj_t self_in, mp_uint_t *len);
Expand Down
3 changes: 1 addition & 2 deletions py/objarray.c
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,7 @@ STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value
} else if (MP_OBJ_IS_TYPE(index_in, &mp_type_slice)) {
mp_bound_slice_t slice;
if (!mp_seq_get_fast_slice_indexes(o->len, index_in, &slice)) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError,
"only slices with step=1 (aka None) are supported"));
mp_not_implemented("only slices with step=1 (aka None) are supported");
}
if (value != MP_OBJ_SENTINEL) {
#if MICROPY_PY_ARRAY_SLICE_ASSIGN
Expand Down
23 changes: 8 additions & 15 deletions py/objstr.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,12 @@ STATIC void str_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t
return;
}
#endif
#if !MICROPY_PY_BUILTINS_STR_UNICODE
bool is_bytes = MP_OBJ_IS_TYPE(self_in, &mp_type_bytes);
if (kind == PRINT_STR && !is_bytes) {
#else
bool is_bytes = true;
#endif
if (!MICROPY_PY_BUILTINS_STR_UNICODE && kind == PRINT_STR && !is_bytes) {
mp_printf(print, "%.*s", str_len, str_data);
} else {
if (is_bytes) {
Expand Down Expand Up @@ -387,8 +391,7 @@ STATIC mp_obj_t bytes_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) {
mp_bound_slice_t slice;
if (!mp_seq_get_fast_slice_indexes(self_len, index, &slice)) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError,
"only slices with step=1 (aka None) are supported"));
mp_not_implemented("only slices with step=1 (aka None) are supported");
}
return mp_obj_new_str_of_type(type, self_data + slice.start, slice.stop - slice.start);
}
Expand Down Expand Up @@ -578,7 +581,7 @@ STATIC mp_obj_t str_rsplit(mp_uint_t n_args, const mp_obj_t *args) {
mp_int_t idx = splits;

if (sep == mp_const_none) {
assert(!"TODO: rsplit(None,n) not implemented");
mp_not_implemented("rsplit(None,n)");
} else {
mp_uint_t sep_len;
const char *sep_str = mp_obj_str_get_data(sep, &sep_len);
Expand Down Expand Up @@ -971,7 +974,7 @@ mp_obj_t mp_obj_str_format(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwa
arg = key_elem->value;
}
if (*lookup) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError, "attributes not supported yet"));
mp_not_implemented("attributes not supported yet");
}
vstr_free(field_name);
field_name = NULL;
Expand Down Expand Up @@ -1996,16 +1999,6 @@ STATIC void bad_implicit_conversion(mp_obj_t self_in) {
}
}

mp_uint_t mp_obj_str_get_len(mp_obj_t self_in) {
// TODO This has a double check for the type, one in obj.c and one here
if (MP_OBJ_IS_STR_OR_BYTES(self_in)) {
GET_STR_LEN(self_in, l);
return l;
} else {
bad_implicit_conversion(self_in);
}
}

// use this if you will anyway convert the string to a qstr
// will be more efficient for the case where it's already a qstr
qstr mp_obj_str_get_qstr(mp_obj_t self_in) {
Expand Down
3 changes: 1 addition & 2 deletions py/objstrunicode.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,7 @@ STATIC mp_obj_t str_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
mp_obj_t ostart, ostop, ostep;
mp_obj_slice_get(index, &ostart, &ostop, &ostep);
if (ostep != mp_const_none && ostep != MP_OBJ_NEW_SMALL_INT(1)) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError,
"only slices with step=1 (aka None) are supported"));
mp_not_implemented("only slices with step=1 (aka None) are supported");
}

const byte *pstart, *pstop;
Expand Down
3 changes: 1 addition & 2 deletions py/objtuple.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,7 @@ mp_obj_t mp_obj_tuple_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) {
mp_bound_slice_t slice;
if (!mp_seq_get_fast_slice_indexes(self->len, index, &slice)) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError,
"only slices with step=1 (aka None) are supported"));
mp_not_implemented("only slices with step=1 (aka None) are supported");
}
mp_obj_tuple_t *res = mp_obj_new_tuple(slice.stop - slice.start, NULL);
mp_seq_copy(res->items, self->items + slice.start, res->len, mp_obj_t);
Expand Down
2 changes: 2 additions & 0 deletions py/qstrdefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,9 @@ Q(imag)
Q(dict)
Q(dir)
Q(divmod)
#if MICROPY_PY_BUILTINS_ENUMERATE
Q(enumerate)
#endif
Q(eval)
Q(exec)
#if MICROPY_PY_BUILTINS_EXECFILE
Expand Down
6 changes: 5 additions & 1 deletion py/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -1147,7 +1147,11 @@ unwind_jump:;
MARK_EXC_IP_SELECTIVE();
mp_uint_t unum = *ip++;
mp_obj_t obj;
assert(unum <= 1);
if (unum == 2) {
mp_warning("exception chaining not supported");
// ignore (pop) "from" argument
sp--;
}
if (unum == 0) {
// search for the inner-most previous exception, to reraise it
obj = MP_OBJ_NULL;
Expand Down
13 changes: 13 additions & 0 deletions stmhal/qstrdefsport.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,19 @@ Q(hid)
Q(hid_mouse)
Q(hid_keyboard)

// for usb modes
Q(host)
Q(VCP)
Q(MSC)
Q(HID)
Q(MSC+HID)
Q(VCP+MSC)
Q(VCP+HID)
// CDC is a synonym for VCP for backwards compatibility
Q(CDC)
Q(CDC+MSC)
Q(CDC+HID)

// for USB VCP class
Q(USB_VCP)
Q(setinterrupt)
Expand Down
28 changes: 27 additions & 1 deletion stmhal/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ void usb_vcp_send_strn_cooked(const char *str, int len) {
We have:
pyb.usb_mode() # return the current usb mode
pyb.usb_mode(None) # disable USB
pyb.usb_mode('VCP') # enable with VCP interface
pyb.usb_mode('VCP+MSC') # enable with VCP and MSC interfaces
Expand All @@ -205,6 +206,31 @@ STATIC mp_obj_t pyb_usb_mode(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_
{ MP_QSTR_hid, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = (mp_obj_t)&pyb_usb_hid_mouse_obj} },
};

// fetch the current usb mode -> pyb.usb_mode()
if (n_args == 0) {
#if defined(USE_HOST_MODE)
return MP_OBJ_NEW_QSTR(MP_QSTR_host);
#elif defined(USE_DEVICE_MODE)
uint8_t mode = USBD_GetMode();
switch (mode) {
case USBD_MODE_CDC:
return MP_OBJ_NEW_QSTR(MP_QSTR_VCP);
case USBD_MODE_MSC:
return MP_OBJ_NEW_QSTR(MP_QSTR_MSC);
case USBD_MODE_HID:
return MP_OBJ_NEW_QSTR(MP_QSTR_HID);
case USBD_MODE_CDC_MSC:
return MP_OBJ_NEW_QSTR(MP_QSTR_VCP_plus_MSC);
case USBD_MODE_CDC_HID:
return MP_OBJ_NEW_QSTR(MP_QSTR_VCP_plus_HID);
case USBD_MODE_MSC_HID:
return MP_OBJ_NEW_QSTR(MP_QSTR_MSC_plus_HID);
default:
return mp_const_none;
}
#endif
}

// parse args
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
Expand Down Expand Up @@ -296,7 +322,7 @@ STATIC mp_obj_t pyb_usb_mode(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_
bad_mode:
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "bad USB mode"));
}
MP_DEFINE_CONST_FUN_OBJ_KW(pyb_usb_mode_obj, 1, pyb_usb_mode);
MP_DEFINE_CONST_FUN_OBJ_KW(pyb_usb_mode_obj, 0, pyb_usb_mode);

/******************************************************************************/
// Micro Python bindings for USB VCP
Expand Down
2 changes: 2 additions & 0 deletions stmhal/usbdev/class/inc/usbd_cdc_msc_hid.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ extern USBD_ClassTypeDef USBD_CDC_MSC_HID;

// returns 0 on success, -1 on failure
int USBD_SelectMode(uint32_t mode, USBD_HID_ModeInfoTypeDef *hid_info);
// returns the current usb mode
uint8_t USBD_GetMode();

uint8_t USBD_CDC_RegisterInterface (USBD_HandleTypeDef *pdev, USBD_CDC_ItfTypeDef *fops);
uint8_t USBD_CDC_SetTxBuffer (USBD_HandleTypeDef *pdev, uint8_t *pbuff, uint16_t length);
Expand Down
5 changes: 5 additions & 0 deletions stmhal/usbdev/class/src/usbd_cdc_msc_hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,11 @@ __ALIGN_BEGIN const uint8_t USBD_HID_KEYBOARD_ReportDesc[USBD_HID_KEYBOARD_REPOR
0xC0 // End Collection
};

// return the saved usb mode
uint8_t USBD_GetMode() {
return usbd_mode;
}

int USBD_SelectMode(uint32_t mode, USBD_HID_ModeInfoTypeDef *hid_info) {
// save mode
usbd_mode = mode;
Expand Down
15 changes: 15 additions & 0 deletions tests/basics/array_q.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# test array('q') and array('Q')

from array import array

print(array('q'))
print(array('Q'))

print(array('q', [0]))
print(array('Q', [0]))

print(array('q', [-2**63, -1, 0, 1, 2, 2**63-1]))
print(array('Q', [0, 1, 2, 2**64-1]))

print(bytes(array('q', [-1])))
print(bytes(array('Q', [2**64-1])))
6 changes: 6 additions & 0 deletions tests/basics/builtin_ord.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@
ord(b'')
except TypeError:
print("TypeError")

# argument must be a string
try:
ord(1)
except TypeError:
print('TypeError')
12 changes: 12 additions & 0 deletions tests/basics/bytes_construct.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@
# long ints
print(ord(bytes([14953042807679334000 & 0xff])))

# constructor value out of range
try:
bytes([-1])
except ValueError:
print('ValueError')

# constructor value out of range
try:
bytes([256])
except ValueError:
print('ValueError')

# error in construction
try:
a = bytes([1, 2, 3], 1)
Expand Down
6 changes: 6 additions & 0 deletions tests/basics/exception_chain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Exception chaining is not supported, but check that basic
# exception works as expected.
try:
raise Exception from None
except Exception:
print("Caught Exception")
2 changes: 2 additions & 0 deletions tests/basics/exception_chain.py.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Caught Exception
Warning: exception chaining not supported
5 changes: 5 additions & 0 deletions tests/basics/setattr1.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ def __init__(self):
setattr(a, "var2", 56)
print(a.var)
print(a.var2)

try:
setattr(a, b'var3', 1)
except TypeError:
print('TypeError')
4 changes: 4 additions & 0 deletions tests/basics/string1.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
'123' * '1'
except TypeError:
print('TypeError')
try:
'123' + 1
except TypeError:
print('TypeError')

# subscription
print('abc'[1])
Expand Down
5 changes: 5 additions & 0 deletions tests/basics/string_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,8 @@
print("1" <= "1/")
print("10" <= "1")
print("1/" <= "1")

# this tests an internal string that doesn't have a hash with a string
# that does have a hash, but the lengths of the two strings are different
import sys
print(sys.version == 'a long string that has a hash')
6 changes: 6 additions & 0 deletions tests/basics/string_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,9 @@ def test_fmt(conv, fill, alignment, sign, prefix, width, precision, type, arg):
'{:*"1"}'.format('zz')
except ValueError:
print('ValueError')

# unknown format code for str arg
try:
'{:X}'.format('zz')
except ValueError:
print('ValueError')
6 changes: 6 additions & 0 deletions tests/basics/struct1.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,9 @@

# network byte order
print(struct.pack('!i', 123))

# first arg must be a string
try:
struct.pack(1, 2)
except TypeError:
print('TypeError')
Loading

0 comments on commit 9ea0bbc

Please sign in to comment.