...
 
Commits (8)
try:
enumerate
except:
import sys
print("SKIP")
sys.exit()
raise SystemExit
print(list(enumerate([])))
print(list(enumerate([1, 2, 3])))
......
try:
filter
except:
import sys
print("SKIP")
sys.exit()
raise SystemExit
print(list(filter(lambda x: x & 1, range(-3, 4))))
print(list(filter(None, range(-3, 4))))
# test builtin hash function, on generators
def gen():
yield
print(type(hash(gen)))
print(type(hash(gen())))
True
False
True
True
True
True
True
True
True
True
True
False
TypeError
# test binary operations on range objects; (in)equality only
# this "feature test" actually tests the implementation but is the best we can do
if range(1) != range(1):
print("SKIP")
raise SystemExit
# basic (in)equality
print(range(1) == range(1))
print(range(1) != range(1))
print(range(1) != range(2))
# empty range
print(range(0) == range(0))
print(range(1, 0) == range(0))
print(range(1, 4, -1) == range(6, 3))
# 1 element range
print(range(1, 4, 10) == range(1, 4, 10))
print(range(1, 4, 10) == range(1, 4, 20))
print(range(1, 4, 10) == range(1, 8, 20))
# more than 1 element
print(range(0, 3, 2) == range(0, 3, 2))
print(range(0, 3, 2) == range(0, 4, 2))
print(range(0, 3, 2) == range(0, 5, 2))
# unsupported binary op
try:
range(1) + 10
except TypeError:
print('TypeError')
123
56
TypeError
AttributeError/TypeError
......@@ -16,3 +16,10 @@ try:
setattr(a, b'var3', 1)
except TypeError:
print('TypeError')
# try setattr on a built-in function
try:
setattr(int, 'to_bytes', 1)
except (AttributeError, TypeError):
# uPy raises AttributeError, CPython raises TypeError
print('AttributeError/TypeError')
......@@ -3,8 +3,7 @@ try:
set
except NameError:
print("SKIP")
import sys
sys.exit()
raise SystemExit
print(list(zip()))
print(list(zip([1], set([2, 3]))))
A(8)
A(5)
L([1, 2, 3, 4])
L([1, 2, 3, 4])
# Case 1: Immutable object (e.g. number-like)
# __iadd__ should not be defined, will be emulated using __add__
class A:
def __init__(self, v):
self.v = v
def __add__(self, o):
return A(self.v + o.v)
def __repr__(self):
return "A(%s)" % self.v
a = A(5)
b = a
a += A(3)
print(a)
# Should be original a's value, i.e. A(5)
print(b)
# Case 2: Mutable object (e.g. list-like)
# __iadd__ should be defined
class L:
def __init__(self, v):
self.v = v
def __add__(self, o):
# Should not be caled in this test
print("L.__add__")
return L(self.v + o.v)
def __iadd__(self, o):
self.v += o.v
return self
def __repr__(self):
return "L(%s)" % self.v
c = L([1, 2])
d = c
c += L([3, 4])
print(c)
# Should be updated c's value, i.e. L([1, 2, 3, 4])
print(d)
# test using the name "super" as a local variable
class A:
def foo(self):
super = [1, 2]
super.pop()
print(super)
A().foo()
# test super with multiple inheritance
class A:
def foo(self):
print('A.foo')
class B:
def foo(self):
print('B.foo')
class C(A, B):
def foo(self):
print('C.foo')
super().foo()
C().foo()
0
1
else
0
0
1
2
3
0
1
else
0
1
else
0
# test for-else statement
# test optimised range with simple else
for i in range(2):
print(i)
else:
print('else')
# test optimised range with break over else
for i in range(2):
print(i)
break
else:
print('else')
# test nested optimised range with continue in the else
for i in range(4):
print(i)
for j in range(4):
pass
else:
continue
break
# test optimised range with non-constant end value
N = 2
for i in range(N):
print(i)
else:
print('else')
# test generic iterator with simple else
for i in [0, 1]:
print(i)
else:
print('else')
# test generic iterator with break over else
for i in [0, 1]:
print(i)
break
else:
print('else')
# test errors from bad function calls
try:
enumerate
except:
print("SKIP")
raise SystemExit
# function with keyword args not given a specific keyword arg
try:
enumerate()
except TypeError:
print('TypeError')
0
1
raised ValueError()
ret was: None
TypeError
def gen():
i = 0
while 1:
yield i
i += 1
g = gen()
try:
g.pend_throw
except AttributeError:
print("SKIP")
raise SystemExit
print(next(g))
print(next(g))
g.pend_throw(ValueError())
v = None
try:
v = next(g)
except Exception as e:
print("raised", repr(e))
print("ret was:", v)
try:
gen().pend_throw(ValueError())
except TypeError:
print("TypeError")
[2130706432, -2147483648]
[2130706432, 2147483648, 2164260864, 4294967295]
# test memoryview accessing maximum values for signed/unsigned elements
try:
from array import array
memoryview
except:
print("SKIP")
raise SystemExit
print(list(memoryview(array('i', [0x7f000000, -0x80000000]))))
print(list(memoryview(array('I', [0x7f000000, 0x80000000, 0x81000000, 0xffffffff]))))
# test errors from bad operations (unary, binary, etc)
def test_exc(code, exc):
try:
exec(code)
print("no exception")
except exc:
print("right exception")
except:
print("wrong exception")
# object with buffer protocol needed on rhs
try:
(1 << 70) in 1
except TypeError:
print('TypeError')
# test errors from bad operations (unary, binary, etc)
try:
memoryview
except:
print("SKIP")
raise SystemExit
# unsupported binary operators
try:
m = memoryview(bytearray())
m += bytearray()
except TypeError:
print('TypeError')
# see https://docs.python.org/3/reference/expressions.html#operator-precedence
# '|' is the least binding numeric operator
# '^'
# OK: 1 | (2 ^ 3) = 1 | 1 = 1
# BAD: (1 | 2) ^ 3 = 3 ^ 3 = 0
print(1 | 2 ^ 3)
# '&'
# OK: 3 ^ (2 & 1) = 3 ^ 0 = 3
# BAD: (3 ^ 2) & 1 = 1 & 1 = 1
print(3 ^ 2 & 1)
# '<<', '>>'
# OK: 2 & (3 << 1) = 2 & 6 = 2
# BAD: (2 & 3) << 1 = 2 << 1 = 4
print(2 & 3 << 1)
# OK: 6 & (4 >> 1) = 6 & 2 = 2
# BAD: (6 & 4) >> 1 = 2 >> 1 = 1
print(6 & 4 >> 1)
# '+', '-'
# OK: 1 << (1 + 1) = 1 << 2 = 4
# BAD: (1 << 1) + 1 = 2 + 1 = 3
print(1 << 1 + 1)
# '*', '/', '//', '%'
# OK: 2 + (2 * 2) = 2 + 4 = 6
# BAD: (2 + 2) * 2 = 4 * 2 = 8
print(2 + 2 * 2)
# '+x', '-x', '~x'
# '**'
# OK: -(2**2) = -4
# BAD: (-2)**2 = 4
print(-2**2)
# OK: 2**(-1) = 0.5
print(2**-1)
# (expr...)
print((2 + 2) * 2)
# MicroPython doesn't support tuple argument
try:
"foobar".endswith(("bar", "sth"))
except TypeError:
print("TypeError")
# MicroPython doesn't support tuple argument
try:
"foobar".startswith(("foo", "sth"))
except TypeError:
print("TypeError")
# test containment operator on subclass of a native type
class mylist(list):
pass
class mydict(dict):
pass
class mybytes(bytes):
pass
l = mylist([1, 2, 3])
print(0 in l)
print(1 in l)
d = mydict({1:1, 2:2})
print(0 in l)
print(1 in l)
b = mybytes(b'1234')
print(0 in b)
print(b'1' in b)
[2, 3]
A.__init__
B.__init__
D.__init__
D.__init__
D.foo
[0, 1, 2, 3, 4]
[0, 1]
# test subclassing a native type and overriding __init__
# overriding list.__init__()
class L(list):
def __init__(self, a, b):
super().__init__([a, b])
print(L(2, 3))
# inherits implicitly from object
class A:
def __init__(self):
print("A.__init__")
super().__init__()
A()
# inherits explicitly from object
class B(object):
def __init__(self):
print("B.__init__")
super().__init__()
B()
# multiple inheritance with object explicitly mentioned
class C:
pass
class D(C, object):
def __init__(self):
print('D.__init__')
super().__init__()
def reinit(self):
print('D.foo')
super().__init__()
a = D()
a.__init__()
a.reinit()
# call __init__() after object is already init'd
class L(list):
def reinit(self):
super().__init__(range(2))
a = L(range(5))
print(a)
a.reinit()
print(a)
......@@ -4,11 +4,21 @@ import sys
print(sys.__name__)
print(sys.byteorder in ('little', 'big'))
print(sys.maxsize > 100)
print(sys.implementation.name in ('cpython', 'micropython'))
try:
sys.exit()
print(sys.maxsize > 100)
except AttributeError:
# Effectively skip subtests
print(True)
try:
print(sys.implementation.name in ('cpython', 'micropython'))
except AttributeError:
# Effectively skip subtests
print(True)
try:
raise SystemExit
except SystemExit as e:
print("SystemExit", e.args)
......
......@@ -33,40 +33,40 @@ void mp_arg_check_num(size_t n_args, size_t n_kw, size_t n_args_min, size_t n_ar
// TODO maybe take the function name as an argument so we can print nicer error messages
if (n_kw && !takes_kw) {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_arg_error_terse_mismatch();
} else {
#else
mp_raise_TypeError("function does not take keyword arguments");
}
#endif
}
if (n_args_min == n_args_max) {
if (n_args != n_args_min) {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_arg_error_terse_mismatch();
} else {
#else
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"function takes %d positional arguments but %d were given",
n_args_min, n_args));
}
#endif
}
} else {
if (n_args < n_args_min) {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_arg_error_terse_mismatch();
} else {
#else
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"function missing %d required positional arguments",
n_args_min - n_args));
}
#endif
} else if (n_args > n_args_max) {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_arg_error_terse_mismatch();
} else {
#else
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"function expected at most %d arguments, got %d",
n_args_max, n_args));
}
#endif
}
}
}
......@@ -85,12 +85,12 @@ void mp_arg_parse_all(size_t n_pos, const mp_obj_t *pos, mp_map_t *kws, size_t n
mp_map_elem_t *kw = mp_map_lookup(kws, MP_OBJ_NEW_QSTR(allowed[i].qst), MP_MAP_LOOKUP);
if (kw == NULL) {
if (allowed[i].flags & MP_ARG_REQUIRED) {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_arg_error_terse_mismatch();
} else {
#else
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"'%q' argument required", allowed[i].qst));
}
#endif
}
out_vals[i] = allowed[i].defval;
continue;
......@@ -110,20 +110,20 @@ void mp_arg_parse_all(size_t n_pos, const mp_obj_t *pos, mp_map_t *kws, size_t n
}
if (pos_found < n_pos) {
extra_positional:
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_arg_error_terse_mismatch();
} else {
#else
// TODO better error message
mp_raise_TypeError("extra positional arguments given");
}
#endif
}
if (kws_found < kws->used) {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_arg_error_terse_mismatch();
} else {
#else
// TODO better error message
mp_raise_TypeError("extra keyword arguments given");
}
#endif
}
}
......
......@@ -202,12 +202,12 @@ uint8_t mp_setup_code_state(mp_code_state_t *code_state, size_t n_args, size_t n
}
// Didn't find name match with positional args
if ((scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) == 0) {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_raise_TypeError("unexpected keyword argument");
} else {
#else
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"unexpected keyword argument '%q'", MP_OBJ_QSTR_VALUE(wanted_arg_name)));
}
#endif
}
mp_obj_dict_store(dict, kwargs[2 * i], kwargs[2 * i + 1]);
continue2:;
......
......@@ -231,18 +231,26 @@ mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) {
}
void mp_binary_set_int(mp_uint_t val_sz, bool big_endian, byte *dest, mp_uint_t val) {
if (MP_ENDIANNESS_LITTLE && !big_endian) {
#if MP_ENDIANNESS_LITTLE
if (!big_endian) {
memcpy(dest, &val, val_sz);
} else if (MP_ENDIANNESS_BIG && big_endian) {
return;
}
#endif
#if MP_ENDIANNESS_BIG
if (big_endian) {
// only copy the least-significant val_sz bytes
memcpy(dest, (byte*)&val + sizeof(mp_uint_t) - val_sz, val_sz);
} else {
return;
}
#endif
{
const byte *src;
if (MP_ENDIANNESS_LITTLE) {
#if MP_ENDIANNESS_LITTLE
src = (const byte*)&val + val_sz;
} else {
#else
src = (const byte*)&val + sizeof(mp_uint_t);
}
#endif
while (val_sz--) {
*dest++ = *--src;
}
......@@ -257,11 +265,11 @@ void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte **
if (struct_type == '@') {
// Make pointer aligned
p = (byte*)MP_ALIGN(p, (size_t)align);
if (MP_ENDIANNESS_LITTLE) {
#if MP_ENDIANNESS_LITTLE
struct_type = '<';
} else {
#else
struct_type = '>';
}
#endif
}
*ptr = p + size;
......
......@@ -398,12 +398,12 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) {
{
#endif
// couldn't find the file, so fail
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_raise_msg(&mp_type_ImportError, "module not found");
} else {
#else
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ImportError,
"no module named '%q'", mod_name));
}
#endif
}
} else {
// found the file, so get the module
......
......@@ -2481,21 +2481,21 @@ STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
compile_node(comp, pn_i);
if (is_dict) {
if (!is_key_value) {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
compile_syntax_error(comp, (mp_parse_node_t)pns, "invalid syntax");
} else {
#else
compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dict");
}
#endif
return;
}
EMIT(store_map);
} else {
if (is_key_value) {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
compile_syntax_error(comp, (mp_parse_node_t)pns, "invalid syntax");
} else {
#else
compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
}
#endif
return;
}
}
......
......@@ -785,6 +785,8 @@ void *gc_realloc(void *ptr_in, size_t n_bytes, bool allow_move) {
}
#endif // Alternative gc_realloc impl
#if EXTENSIVE_HEAP_PROFILING || MICROPY_PY_MICROPYTHON_MEM_INFO
void gc_dump_info(void) {
gc_info_t info;
gc_info(&info);
......@@ -904,6 +906,8 @@ void gc_dump_alloc_table(void) {
GC_EXIT();
}
#endif // EXTENSIVE_HEAP_PROFILING || MICROPY_PY_MICROPYTHON_MEM_INFO
#if DEBUG_PRINT
void gc_test(void) {
mp_uint_t len = 500;
......
......@@ -337,12 +337,12 @@ STATIC mp_obj_t mp_builtin_ord(mp_obj_t o_in) {
}
}
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_raise_TypeError("ord expects a character");
} else {
#else
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"ord() expected a character, but string of length %d found", (int)len));
}
#endif
}
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_ord_obj, mp_builtin_ord);
......
......@@ -235,12 +235,12 @@ mp_int_t mp_obj_get_int(mp_const_obj_t arg) {
} else if (MP_OBJ_IS_TYPE(arg, &mp_type_int)) {
return mp_obj_int_get_checked(arg);
} else {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_raise_TypeError("can't convert to int");
} else {
#else
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"can't convert %s to int", mp_obj_get_type_str(arg)));
}
#endif
}
}
......@@ -298,12 +298,12 @@ mp_float_t mp_obj_get_float(mp_obj_t arg) {
mp_float_t val;
if (!mp_obj_get_float_maybe(arg, &val)) {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_raise_TypeError("can't convert to float");
} else {
#else
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"can't convert %s to float", mp_obj_get_type_str(arg)));
}
#endif
}
return val;
......@@ -331,12 +331,12 @@ void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
} else if (MP_OBJ_IS_TYPE(arg, &mp_type_complex)) {
mp_obj_complex_get(arg, real, imag);
} else {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_raise_TypeError("can't convert to complex");
} else {
#else
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"can't convert %s to complex", mp_obj_get_type_str(arg)));
}
#endif
}
}
#endif
......@@ -349,12 +349,12 @@ void mp_obj_get_array(mp_obj_t o, size_t *len, mp_obj_t **items) {
} else if (MP_OBJ_IS_TYPE(o, &mp_type_list)) {
mp_obj_list_get(o, len, items);
} else {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_raise_TypeError("expected tuple/list");
} else {
#else
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"object '%s' is not a tuple or list", mp_obj_get_type_str(o)));
}
#endif
}
}
......@@ -363,12 +363,12 @@ void mp_obj_get_array_fixed_n(mp_obj_t o, size_t len, mp_obj_t **items) {
size_t seq_len;
mp_obj_get_array(o, &seq_len, items);
if (seq_len != len) {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_raise_ValueError("tuple/list has wrong length");
} else {
#else
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
"requested length %d but object has length %d", (int)len, (int)seq_len));
}
#endif
}
}
......@@ -378,13 +378,13 @@ size_t mp_get_index(const mp_obj_type_t *type, size_t len, mp_obj_t index, bool
if (MP_OBJ_IS_SMALL_INT(index)) {
i = MP_OBJ_SMALL_INT_VALUE(index);
} else if (!mp_obj_get_int_maybe(index, &i)) {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_raise_TypeError("indices must be integers");
} else {
#else
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"%q indices must be integers, not %s",
type->name, mp_obj_get_type_str(index)));
}
#endif
}
if (i < 0) {
......@@ -398,12 +398,12 @@ size_t mp_get_index(const mp_obj_type_t *type, size_t len, mp_obj_t index, bool
}
} else {
if (i < 0 || (mp_uint_t)i >= len) {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_raise_msg(&mp_type_IndexError, "index out of range");
} else {
#else
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_IndexError,
"%q index out of range", type->name));
}
#endif
}
}
......@@ -433,12 +433,12 @@ mp_obj_t mp_obj_id(mp_obj_t o_in) {
mp_obj_t mp_obj_len(mp_obj_t o_in) {
mp_obj_t len = mp_obj_len_maybe(o_in);
if (len == MP_OBJ_NULL) {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_raise_TypeError("object has no len");
} else {
#else
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"object of type '%s' has no len()", mp_obj_get_type_str(o_in)));
}
#endif
} else {
return len;
}
......@@ -474,26 +474,26 @@ mp_obj_t mp_obj_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) {
// TODO: call base classes here?
}
if (value == MP_OBJ_NULL) {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_raise_TypeError("object does not support item deletion");
} else {
#else
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"'%s' object does not support item deletion", mp_obj_get_type_str(base)));
}
#endif
} else if (value == MP_OBJ_SENTINEL) {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_raise_TypeError("object is not subscriptable");
} else {
#else
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"'%s' object is not subscriptable", mp_obj_get_type_str(base)));
}
#endif
} else {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_raise_TypeError("object does not support item assignment");
} else {
#else
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"'%s' object does not support item assignment", mp_obj_get_type_str(base)));
}
#endif
}
}
......
......@@ -35,13 +35,19 @@ typedef struct _mp_obj_bool_t {
STATIC void bool_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
mp_obj_bool_t *self = MP_OBJ_TO_PTR(self_in);
if (MICROPY_PY_UJSON && kind == PRINT_JSON) {
#if MICROPY_PY_UJSON
if (kind == PRINT_JSON) {
if (self->value) {
mp_print_str(print, "true");
} else {
mp_print_str(print, "false");
}
} else {
return;
}
#else
(void)kind;
#endif
{
if (self->value) {
mp_print_str(print, "True");
} else {
......
......@@ -55,12 +55,17 @@ STATIC mp_map_elem_t *dict_iter_next(mp_obj_dict_t *dict, size_t *cur) {
STATIC void dict_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
bool first = true;
if (!(MICROPY_PY_UJSON && kind == PRINT_JSON)) {
#if MICROPY_PY_UJSON
if (kind != PRINT_JSON)
#endif
{
kind = PRINT_REPR;
}
if (MICROPY_PY_COLLECTIONS_ORDEREDDICT && self->base.type != &mp_type_dict) {
#if MICROPY_PY_COLLECTIONS_ORDEREDDICT
if (self->base.type != &mp_type_dict) {
mp_printf(print, "%q(", self->base.type->name);
}
#endif
mp_print_str(print, "{");
size_t cur = 0;
mp_map_elem_t *next = NULL;
......@@ -74,9 +79,11 @@ STATIC void dict_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_
mp_obj_print_helper(print, next->value, kind);
}
mp_print_str(print, "}");
if (MICROPY_PY_COLLECTIONS_ORDEREDDICT && self->base.type != &mp_type_dict) {
#if MICROPY_PY_COLLECTIONS_ORDEREDDICT
if (self->base.type != &mp_type_dict) {
mp_print_str(print, ")");
}
#endif
}
STATIC mp_obj_t dict_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
......
......@@ -44,7 +44,10 @@ STATIC mp_obj_t list_pop(size_t n_args, const mp_obj_t *args);
STATIC void list_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
mp_obj_list_t *o = MP_OBJ_TO_PTR(o_in);
if (!(MICROPY_PY_UJSON && kind == PRINT_JSON)) {
#if MICROPY_PY_UJSON
if (kind != PRINT_JSON)
#endif
{
kind = PRINT_REPR;
}
mp_print_str(print, "[");
......
......@@ -95,17 +95,17 @@ STATIC mp_obj_t namedtuple_make_new(const mp_obj_type_t *type_in, size_t n_args,
const mp_obj_namedtuple_type_t *type = (const mp_obj_namedtuple_type_t*)type_in;
size_t num_fields = type->n_fields;
if (n_args + n_kw != num_fields) {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_arg_error_terse_mismatch();
} else if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NORMAL) {
#elif MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NORMAL
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"function takes %d positional arguments but %d were given",
num_fields, n_args + n_kw));
} else if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED) {
#elif MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"%q() takes %d positional arguments but %d were given",
type->base.name, num_fields, n_args + n_kw));
}
#endif
}
// Create a tuple and set the type to this namedtuple
......@@ -121,20 +121,20 @@ STATIC mp_obj_t namedtuple_make_new(const mp_obj_type_t *type_in, size_t n_args,
qstr kw = mp_obj_str_get_qstr(args[i]);
size_t id = mp_obj_namedtuple_find_field(type, kw);
if (id == (size_t)-1) {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_arg_error_terse_mismatch();
} else {
#else
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"unexpected keyword argument '%q'", kw));
}
#endif
}
if (tuple->items[id] != MP_OBJ_NULL) {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_arg_error_terse_mismatch();
} else {
#else
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"function got multiple values for argument '%q'", kw));
}
#endif
}
tuple->items[id] = args[i + 1];
}
......
......@@ -34,9 +34,14 @@ typedef struct _mp_obj_none_t {
STATIC void none_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
(void)self_in;
if (MICROPY_PY_UJSON && kind == PRINT_JSON) {
#if MICROPY_PY_UJSON
if (kind == PRINT_JSON) {
mp_print_str(print, "null");
} else {
} else
#else
(void)kind;
#endif
{
mp_print_str(print, "None");
}
}
......
......@@ -429,11 +429,15 @@ STATIC mp_obj_t bytes_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
#endif
size_t index_val = mp_get_index(type, self_len, index, false);
// If we have unicode enabled the type will always be bytes, so take the short cut.
if (MICROPY_PY_BUILTINS_STR_UNICODE || type == &mp_type_bytes) {
#if MICROPY_PY_BUILTINS_STR_UNICODE
return MP_OBJ_NEW_SMALL_INT(self_data[index_val]);
#else
if (type == &mp_type_bytes) {
return MP_OBJ_NEW_SMALL_INT(self_data[index_val]);
} else {
return mp_obj_new_str_via_qstr((char*)&self_data[index_val], 1);
}
#endif
} else {
return MP_OBJ_NULL; // op not supported
}
......@@ -663,9 +667,7 @@ STATIC mp_obj_t str_rsplit(size_t n_args, const mp_obj_t *args) {
}
res->items[idx--] = mp_obj_new_str_of_type(self_type, s + sep_len, last - s - sep_len);
last = s;
if (splits > 0) {
splits--;
}
splits--;
}
if (idx != 0) {
// We split less parts than split limit, now go cleanup surplus
......@@ -942,11 +944,11 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar
vstr_add_byte(&vstr, '}');
continue;
}
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
terse_str_format_value_error();
} else {
#else
mp_raise_ValueError("single '}' encountered in format string");
}
#endif
}
if (*str != '{') {
vstr_add_byte(&vstr, *str);
......@@ -981,11 +983,11 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar
if (str < top && (*str == 'r' || *str == 's')) {
conversion = *str++;
} else {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
terse_str_format_value_error();
} else if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NORMAL) {
#elif MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NORMAL
mp_raise_ValueError("bad conversion specifier");
} else {
#else
if (str >= top) {
mp_raise_ValueError(
"end of format while looking for conversion specifier");
......@@ -993,7 +995,7 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
"unknown conversion specifier %c", *str));
}
}
#endif
}
}
......@@ -1019,18 +1021,18 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar
}
}
if (str >= top) {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
terse_str_format_value_error();
} else {
#else
mp_raise_ValueError("unmatched '{' in format");
}
#endif
}
if (*str != '}') {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
terse_str_format_value_error();
} else {
#else
mp_raise_ValueError("expected ':' after format specifier");
}
#endif
}
mp_obj_t arg = mp_const_none;
......@@ -1039,12 +1041,12 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar
int index = 0;
if (MP_LIKELY(unichar_isdigit(*field_name))) {
if (*arg_i > 0) {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
terse_str_format_value_error();
} else {
#else
mp_raise_ValueError(
"can't switch from automatic field numbering to manual field specification");
}
#endif
}
field_name = str_to_int(field_name, field_name_top, &index);
if ((uint)index >= n_args - 1) {
......@@ -1068,12 +1070,12 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar
}
} else {
if (*arg_i < 0) {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
terse_str_format_value_error();
} else {
#else
mp_raise_ValueError(
"can't switch from manual field specification to automatic field numbering");
}
#endif
}
if ((uint)*arg_i >= n_args - 1) {
mp_raise_msg(&mp_type_IndexError, "tuple index out of range");
......@@ -1161,11 +1163,11 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar
type = *s++;
}
if (*s) {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
terse_str_format_value_error();
} else {
#else
mp_raise_ValueError("invalid format specifier");
}
#endif
}
vstr_clear(&format_spec_vstr);
}
......@@ -1182,19 +1184,19 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar
if (flags & (PF_FLAG_SHOW_SIGN | PF_FLAG_SPACE_SIGN)) {
if (type == 's') {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
terse_str_format_value_error();
} else {
#else
mp_raise_ValueError("sign not allowed in string format specifier");
}
#endif
}
if (type == 'c') {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
terse_str_format_value_error();
} else {
#else
mp_raise_ValueError(
"sign not allowed with integer format specifier 'c'");
}
#endif
}
}
......@@ -1248,13 +1250,13 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar
break;
default:
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
terse_str_format_value_error();
} else {
#else
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
"unknown format code '%c' for object of type '%s'",
type, mp_obj_get_type_str(arg)));
}
#endif
}
}
......@@ -1320,24 +1322,24 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar
#endif
default:
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
terse_str_format_value_error();
} else {
#else
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
"unknown format code '%c' for object of type 'float'",
type, mp_obj_get_type_str(arg)));
}
#endif
}
} else {
// arg doesn't look like a number
if (align == '=') {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
terse_str_format_value_error();
} else {
#else
mp_raise_ValueError(
"'=' alignment not allowed in string format specifier");
}
#endif
}
switch (type) {
......@@ -1356,13 +1358,13 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar
}
default:
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
terse_str_format_value_error();
} else {
#else
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
"unknown format code '%c' for object of type 'str'",
type, mp_obj_get_type_str(arg)));
}
#endif
}
}
}
......@@ -1414,11 +1416,11 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, size_t n_args, const mp_obj_
const byte *key = ++str;
while (*str != ')') {
if (str >= top) {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
terse_str_format_value_error();
} else {
#else
mp_raise_ValueError("incomplete format key");
}
#endif
}
++str;
}
......@@ -1472,11 +1474,11 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, size_t n_args, const mp_obj_
if (str >= top) {
incomplete_format:
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
terse_str_format_value_error();
} else {
#else
mp_raise_ValueError("incomplete format");
}
#endif
}
// Tuple value lookup
......@@ -1559,13 +1561,13 @@ not_enough_args:
break;
default:
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
terse_str_format_value_error();
} else {
#else
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
"unsupported format character '%c' (0x%x) at index %d",
*str, *str, str - start_str));
}
#endif
}
}
......@@ -2088,14 +2090,14 @@ bool mp_obj_str_equal(mp_obj_t s1, mp_obj_t s2) {
}
STATIC NORETURN void bad_implicit_conversion(mp_obj_t self_in) {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_raise_TypeError("can't convert to str implicitly");
} else {
#else
const qstr src_name = mp_obj_get_type(self_in)->name;
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"can't convert '%q' object to %q implicitly",
src_name, src_name == MP_QSTR_str ? MP_QSTR_bytes : MP_QSTR_str));
}
#endif
}
// use this if you will anyway convert the string to a qstr
......