...
 
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");
}
}
......
This diff is collapsed.
......@@ -35,9 +35,12 @@
void mp_obj_tuple_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
mp_obj_tuple_t *o = MP_OBJ_TO_PTR(o_in);
if (MICROPY_PY_UJSON && kind == PRINT_JSON) {
#if MICROPY_PY_UJSON
if (kind == PRINT_JSON) {
mp_print_str(print, "[");
} else {
} else
#endif
{
mp_print_str(print, "(");
kind = PRINT_REPR;
}
......@@ -47,9 +50,12 @@ void mp_obj_tuple_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t
}
mp_obj_print_helper(print, o->items[i], kind);
}
if (MICROPY_PY_UJSON && kind == PRINT_JSON) {
#if MICROPY_PY_UJSON
if (kind == PRINT_JSON) {
mp_print_str(print, "]");
} else {
} else
#endif
{
if (o->len == 1) {
mp_print_str(print, ",");
}
......
......@@ -342,12 +342,12 @@ mp_obj_t mp_obj_instance_make_new(const mp_obj_type_t *self, size_t n_args, size
m_del(mp_obj_t, args2, 2 + n_args + 2 * n_kw);
}
if (init_ret != mp_const_none) {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_raise_TypeError("__init__() should return None");
} else {
#else
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"__init__() should return None, not '%s'", mp_obj_get_type_str(init_ret)));
}
#endif
}
}
......@@ -839,12 +839,12 @@ mp_obj_t mp_obj_instance_call(mp_obj_t self_in, size_t n_args, size_t n_kw, cons
mp_obj_t member[2] = {MP_OBJ_NULL, MP_OBJ_NULL};
mp_obj_t call = mp_obj_instance_get_call(self_in, member);
if (call == MP_OBJ_NULL) {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_raise_TypeError("object not callable");
} else {
#else
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"'%s' object is not callable", mp_obj_get_type_str(self_in)));
}
#endif
}
mp_obj_instance_t *self = MP_OBJ_TO_PTR(self_in);
if (call == MP_OBJ_SENTINEL) {
......@@ -932,12 +932,12 @@ STATIC mp_obj_t type_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp
mp_obj_type_t *self = MP_OBJ_TO_PTR(self_in);
if (self->make_new == NULL) {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_raise_TypeError("cannot create instance");
} else {
#else
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"cannot create '%q' instances", self->name));
}
#endif
}
// make new instance
......@@ -1020,12 +1020,12 @@ mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict)
mp_obj_type_t *t = MP_OBJ_TO_PTR(bases_items[i]);
// TODO: Verify with CPy, tested on function type
if (t->make_new == NULL) {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_raise_TypeError("type is not an acceptable base type");
} else {
#else
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"type '%q' is not an acceptable base type", t->name));
}
#endif
}
}
......
......@@ -141,16 +141,16 @@ overflow:
goto have_ret_val;
}
value_error:
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
value_error:;
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_obj_t exc = mp_obj_new_exception_msg(&mp_type_ValueError,
"invalid syntax for integer");
raise_exc(exc, lex);
} else if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NORMAL) {
#elif MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NORMAL
mp_obj_t exc = mp_obj_new_exception_msg_varg(&mp_type_ValueError,
"invalid syntax for integer with base %d", base);
raise_exc(exc, lex);
} else {
#else
vstr_t vstr;
mp_print_t print;
vstr_init_print(&vstr, 50, &print);
......@@ -159,7 +159,7 @@ value_error:
mp_obj_t exc = mp_obj_new_exception_arg1(&mp_type_ValueError,
mp_obj_new_str_from_vstr(&mp_type_str, &vstr));
raise_exc(exc, lex);
}
#endif
}
typedef enum {
......
......@@ -261,6 +261,7 @@ const byte *qstr_data(qstr q, size_t *len) {
return Q_GET_DATA(qd);
}
#if MICROPY_PY_MICROPYTHON_MEM_INFO
void qstr_pool_info(size_t *n_pool, size_t *n_qstr, size_t *n_str_data_bytes, size_t *n_total_bytes) {
QSTR_ENTER();
*n_pool = 0;
......@@ -283,7 +284,6 @@ void qstr_pool_info(size_t *n_pool, size_t *n_qstr, size_t *n_str_data_bytes, si
QSTR_EXIT();
}
#if MICROPY_PY_MICROPYTHON_MEM_INFO
void qstr_dump_data(void) {
QSTR_ENTER();
for (qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL && pool != &CONST_POOL; pool = pool->prev) {
......
......@@ -172,12 +172,12 @@ mp_obj_t mp_load_global(qstr qst) {
#endif
elem = mp_map_lookup((mp_map_t*)&mp_module_builtins_globals.map, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
if (elem == NULL) {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_raise_msg(&mp_type_NameError, "name not defined");
} else {
#else
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_NameError,
"name '%q' is not defined", qst));
}
#endif
}
}
return elem->value;
......@@ -270,13 +270,13 @@ mp_obj_t mp_unary_op(mp_unary_op_t op, mp_obj_t arg) {
return result;
}
}
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_raise_TypeError("unsupported type for operator");
} else {
#else
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"unsupported type for %q: '%s'",
mp_unary_op_method_name[op], mp_obj_get_type_str(arg)));
}
#endif
}
}
......@@ -577,13 +577,13 @@ generic_binary_op:
}
unsupported_op:
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_raise_TypeError("unsupported type for operator");
} else {
#else
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"unsupported types for %q: '%s', '%s'",
mp_binary_op_method_name[op], mp_obj_get_type_str(lhs), mp_obj_get_type_str(rhs)));
}
#endif
zero_division:
mp_raise_msg(&mp_type_ZeroDivisionError, "division by zero");
......@@ -619,12 +619,12 @@ mp_obj_t mp_call_function_n_kw(mp_obj_t fun_in, size_t n_args, size_t n_kw, cons
return type->call(fun_in, n_args, n_kw, args);
}
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_raise_TypeError("object not callable");
} else {
#else
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"'%s' object is not callable", mp_obj_get_type_str(fun_in)));
}
#endif
}
// args contains: fun self/NULL arg(0) ... arg(n_args-2) arg(n_args-1) kw_key(0) kw_val(0) ... kw_key(n_kw-1) kw_val(n_kw-1)
......@@ -848,19 +848,19 @@ void mp_unpack_sequence(mp_obj_t seq_in, size_t num, mp_obj_t *items) {
return;
too_short:
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_raise_ValueError("wrong number of values to unpack");
} else {
#else
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
"need more than %d values to unpack", (int)seq_len));
}
#endif
too_long:
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_raise_ValueError("wrong number of values to unpack");
} else {
#else
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
"too many values to unpack (expected %d)", (int)num));
}
#endif
}
// unpacked items are stored in reverse order into the array pointed to by items
......@@ -912,12 +912,12 @@ void mp_unpack_ex(mp_obj_t seq_in, size_t num_in, mp_obj_t *items) {
return;
too_short:
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_raise_ValueError("wrong number of values to unpack");
} else {
#else
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
"need more than %d values to unpack", (int)seq_len));
}
#endif
}
mp_obj_t mp_load_attr(mp_obj_t base, qstr attr) {
......@@ -951,12 +951,12 @@ STATIC mp_obj_t checked_fun_call(mp_obj_t self_in, size_t n_args, size_t n_kw, c
if (n_args > 0) {
const mp_obj_type_t *arg0_type = mp_obj_get_type(args[0]);
if (arg0_type != self->type) {
if (MICROPY_ERROR_REPORTING != MICROPY_ERROR_REPORTING_DETAILED) {
#if MICROPY_ERROR_REPORTING != MICROPY_ERROR_REPORTING_DETAILED
mp_raise_TypeError("argument has wrong type");
} else {
#else
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"argument should be a '%q' not a '%q'", self->type->name, arg0_type->name));
}
#endif
}
}
return mp_call_function_n_kw(self->fun, n_args, n_kw, args);
......@@ -1073,9 +1073,9 @@ void mp_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest) {
if (dest[0] == MP_OBJ_NULL) {
// no attribute/method called attr
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_raise_msg(&mp_type_AttributeError, "no such attribute");
} else {
#else
// following CPython, we give a more detailed error message for type objects
if (MP_OBJ_IS_TYPE(base, &mp_type_type)) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_AttributeError,
......@@ -1086,7 +1086,7 @@ void mp_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest) {
"'%s' object has no attribute '%q'",
mp_obj_get_type_str(base), attr));
}
}
#endif
}
}
......@@ -1101,13 +1101,13 @@ void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
return;
}
}
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_raise_msg(&mp_type_AttributeError, "no such attribute");
} else {
#else
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_AttributeError,
"'%s' object has no attribute '%q'",
mp_obj_get_type_str(base), attr));
}
#endif
}
mp_obj_t mp_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf) {
......@@ -1142,12 +1142,12 @@ mp_obj_t mp_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf) {