From a55988a547011c04978a1661325062397ef74110 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Sun, 25 Mar 2018 16:01:06 -0500 Subject: Fix assertion failure in mpz_divmod_inpl .. turning this from an assertion failure into an exception: pow(1,1,0) --- tests/basics/builtin_pow3.py | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'tests') diff --git a/tests/basics/builtin_pow3.py b/tests/basics/builtin_pow3.py index 69b57e548..293a5acc9 100644 --- a/tests/basics/builtin_pow3.py +++ b/tests/basics/builtin_pow3.py @@ -22,3 +22,8 @@ try: print(pow(4, 5, "z")) except TypeError: print("TypeError expected") + +try: + print(pow(4, 5, 0)) +except ValueError: + print("ValueError expected") -- cgit v1.2.3 From 6da8d7c46583387aa32d5c153b3c5b7d9e1afccd Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Sun, 25 Mar 2018 16:05:32 -0500 Subject: Fix assertion failures in mp_obj_new_type Fixes the following assertion failures when the arguments to type() were not of valid types: micropython: ../../py/objtype.c:984: mp_obj_new_type: Assertion `MP_OBJ_IS_TYPE(bases_tuple, &mp_type_tuple)' failed. micropython: ../../py/objtype.c:994: mp_obj_new_type: Assertion `MP_OBJ_IS_TYPE(items[i], &mp_type_type)' failed. e.g., when making calls like type("", (), 3) type("", 3, {}) --- py/objtype.c | 14 +++++++++++--- tests/basics/types3.py | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 tests/basics/types3.py (limited to 'tests') diff --git a/py/objtype.c b/py/objtype.c index 7262892bd..3fa25eb77 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -981,8 +981,14 @@ const mp_obj_type_t mp_type_type = { }; mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict) { - assert(MP_OBJ_IS_TYPE(bases_tuple, &mp_type_tuple)); // MicroPython restriction, for now - assert(MP_OBJ_IS_TYPE(locals_dict, &mp_type_dict)); // MicroPython restriction, for now + if(!MP_OBJ_IS_TYPE(bases_tuple, &mp_type_tuple)) { + // MicroPython restriction, for now + mp_raise_TypeError("type() argument 2 must be tuple"); + } + if(!MP_OBJ_IS_TYPE(locals_dict, &mp_type_dict)) { + // MicroPython restriction, for now + mp_raise_TypeError("type() argument 3 must be dict"); + } // TODO might need to make a copy of locals_dict; at least that's how CPython does it @@ -991,7 +997,9 @@ mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict) mp_obj_t *items; mp_obj_tuple_get(bases_tuple, &len, &items); for (size_t i = 0; i < len; i++) { - assert(MP_OBJ_IS_TYPE(items[i], &mp_type_type)); + if(!MP_OBJ_IS_TYPE(items[i], &mp_type_type)) { + mp_raise_TypeError("type is not an acceptable base type"); + } mp_obj_type_t *t = MP_OBJ_TO_PTR(items[i]); // TODO: Verify with CPy, tested on function type if (t->make_new == NULL) { diff --git a/tests/basics/types3.py b/tests/basics/types3.py new file mode 100644 index 000000000..71f790692 --- /dev/null +++ b/tests/basics/types3.py @@ -0,0 +1,20 @@ +try: + type('abc', None, None) +except TypeError: + print(True) +else: + print(False) + +try: + type('abc', (), None) +except TypeError: + print(True) +else: + print(False) + +try: + type('abc', (1,), {}) +except TypeError: + print(True) +else: + print(False) -- cgit v1.2.3 From ff06a455995322978dbd9ad9291154ebbdf0abb5 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Sun, 25 Mar 2018 16:13:49 -0500 Subject: Fix assertion failures in super_attr micropython: ../../py/objtype.c:1100: super_attr: Assertion `MP_OBJ_IS_TYPE(self->type, &mp_type_type)' failed. e.g., when making calls like super(1, 1).x --- py/objtype.c | 3 +++ tests/basics/class_super.py | 7 +++++++ 2 files changed, 10 insertions(+) (limited to 'tests') diff --git a/py/objtype.c b/py/objtype.c index 3fa25eb77..5b70e8e48 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -1085,6 +1085,9 @@ STATIC mp_obj_t super_make_new(const mp_obj_type_t *type_in, size_t n_args, size // 0 arguments are turned into 2 in the compiler // 1 argument is not yet implemented mp_arg_check_num(n_args, n_kw, 2, 2, false); + if(!MP_OBJ_IS_TYPE(args[0], &mp_type_type)) { + mp_raise_TypeError("first argument to super() must be type"); + } mp_obj_super_t *o = m_new_obj(mp_obj_super_t); *o = (mp_obj_super_t){{type_in}, args[0], args[1]}; return MP_OBJ_FROM_PTR(o); diff --git a/tests/basics/class_super.py b/tests/basics/class_super.py index 1338ef452..5a18017ac 100644 --- a/tests/basics/class_super.py +++ b/tests/basics/class_super.py @@ -34,3 +34,10 @@ class B(A): print(super().bar) # accessing attribute after super() return super().foo().count(2) # calling a subsequent method print(B().foo()) + +try: + super(1, 1).x +except TypeError: + print(True) +else: + print(False) -- cgit v1.2.3