Skip to content

Commit bee0d2e

Browse files
authored
Merge pull request adafruit#710 from jepler/assertion-failures-to-exceptions
Assertion failures to exceptions
2 parents 676ed4e + ff06a45 commit bee0d2e

5 files changed

Lines changed: 50 additions & 3 deletions

File tree

py/objint_mpz.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,10 @@ mp_obj_t mp_obj_int_pow3(mp_obj_t base, mp_obj_t exponent, mp_obj_t modulus) {
343343
mpz_t *rhs = mp_mpz_for_int(exponent, &r_temp);
344344
mpz_t *mod = mp_mpz_for_int(modulus, &m_temp);
345345

346+
if (mpz_is_zero(mod)) {
347+
mp_raise_msg(&mp_type_ValueError, "pow() 3rd argument cannot be 0");
348+
}
349+
346350
mpz_pow3_inpl(&(res_p->mpz), lhs, rhs, mod);
347351

348352
if (lhs == &l_temp) { mpz_deinit(lhs); }

py/objtype.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -981,8 +981,14 @@ const mp_obj_type_t mp_type_type = {
981981
};
982982

983983
mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict) {
984-
assert(MP_OBJ_IS_TYPE(bases_tuple, &mp_type_tuple)); // MicroPython restriction, for now
985-
assert(MP_OBJ_IS_TYPE(locals_dict, &mp_type_dict)); // MicroPython restriction, for now
984+
if(!MP_OBJ_IS_TYPE(bases_tuple, &mp_type_tuple)) {
985+
// MicroPython restriction, for now
986+
mp_raise_TypeError("type() argument 2 must be tuple");
987+
}
988+
if(!MP_OBJ_IS_TYPE(locals_dict, &mp_type_dict)) {
989+
// MicroPython restriction, for now
990+
mp_raise_TypeError("type() argument 3 must be dict");
991+
}
986992

987993
// TODO might need to make a copy of locals_dict; at least that's how CPython does it
988994

@@ -991,7 +997,9 @@ mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict)
991997
mp_obj_t *items;
992998
mp_obj_tuple_get(bases_tuple, &len, &items);
993999
for (size_t i = 0; i < len; i++) {
994-
assert(MP_OBJ_IS_TYPE(items[i], &mp_type_type));
1000+
if(!MP_OBJ_IS_TYPE(items[i], &mp_type_type)) {
1001+
mp_raise_TypeError("type is not an acceptable base type");
1002+
}
9951003
mp_obj_type_t *t = MP_OBJ_TO_PTR(items[i]);
9961004
// TODO: Verify with CPy, tested on function type
9971005
if (t->make_new == NULL) {
@@ -1077,6 +1085,9 @@ STATIC mp_obj_t super_make_new(const mp_obj_type_t *type_in, size_t n_args, size
10771085
// 0 arguments are turned into 2 in the compiler
10781086
// 1 argument is not yet implemented
10791087
mp_arg_check_num(n_args, n_kw, 2, 2, false);
1088+
if(!MP_OBJ_IS_TYPE(args[0], &mp_type_type)) {
1089+
mp_raise_TypeError("first argument to super() must be type");
1090+
}
10801091
mp_obj_super_t *o = m_new_obj(mp_obj_super_t);
10811092
*o = (mp_obj_super_t){{type_in}, args[0], args[1]};
10821093
return MP_OBJ_FROM_PTR(o);

tests/basics/builtin_pow3.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,8 @@
2222
print(pow(4, 5, "z"))
2323
except TypeError:
2424
print("TypeError expected")
25+
26+
try:
27+
print(pow(4, 5, 0))
28+
except ValueError:
29+
print("ValueError expected")

tests/basics/class_super.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,10 @@ def foo(self):
3434
print(super().bar) # accessing attribute after super()
3535
return super().foo().count(2) # calling a subsequent method
3636
print(B().foo())
37+
38+
try:
39+
super(1, 1).x
40+
except TypeError:
41+
print(True)
42+
else:
43+
print(False)

tests/basics/types3.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
try:
2+
type('abc', None, None)
3+
except TypeError:
4+
print(True)
5+
else:
6+
print(False)
7+
8+
try:
9+
type('abc', (), None)
10+
except TypeError:
11+
print(True)
12+
else:
13+
print(False)
14+
15+
try:
16+
type('abc', (1,), {})
17+
except TypeError:
18+
print(True)
19+
else:
20+
print(False)

0 commit comments

Comments
 (0)