Skip to content

Commit 9512e9e

Browse files
committed
objexcept: Add "args" exception attribute, as well as StopIteration.value.
1 parent 7f8b313 commit 9512e9e

3 files changed

Lines changed: 24 additions & 0 deletions

File tree

py/objexcept.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,31 @@ STATIC mp_obj_t mp_obj_exception_make_new(mp_obj_t type_in, uint n_args, uint n_
5353
o->base.type = type;
5454
o->traceback = MP_OBJ_NULL;
5555
o->msg = NULL;
56+
o->args.base.type = &tuple_type;
5657
o->args.len = n_args;
5758
memcpy(o->args.items, args, n_args * sizeof(mp_obj_t));
5859
return o;
5960
}
6061

62+
STATIC void exception_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
63+
mp_obj_exception_t *self = self_in;
64+
if (attr == MP_QSTR_args) {
65+
dest[0] = &self->args;
66+
} else if (self->base.type == &mp_type_StopIteration && attr == MP_QSTR_value) {
67+
if (self->args.len == 0) {
68+
dest[0] = mp_const_none;
69+
} else {
70+
dest[0] = self->args.items[0];
71+
}
72+
}
73+
}
74+
6175
const mp_obj_type_t mp_type_BaseException = {
6276
{ &mp_type_type },
6377
.name = MP_QSTR_BaseException,
6478
.print = mp_obj_exception_print,
6579
.make_new = mp_obj_exception_make_new,
80+
.load_attr = exception_load_attr,
6681
};
6782

6883
#define MP_DEFINE_EXCEPTION_BASE(base_name) \
@@ -74,6 +89,7 @@ const mp_obj_type_t mp_type_ ## exc_name = { \
7489
.name = MP_QSTR_ ## exc_name, \
7590
.print = mp_obj_exception_print, \
7691
.make_new = mp_obj_exception_make_new, \
92+
.load_attr = exception_load_attr, \
7793
.bases_tuple = (mp_obj_t)&mp_type_ ## base_name ## _base_tuple, \
7894
};
7995

py/qstrdefs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ Q(NoneType)
7878
Q(abs)
7979
Q(all)
8080
Q(any)
81+
Q(args)
8182
Q(array)
8283
Q(bool)
8384
Q(bytearray)
@@ -123,6 +124,7 @@ Q(str)
123124
Q(sys)
124125
Q(tuple)
125126
Q(type)
127+
Q(value)
126128
Q(zip)
127129

128130
Q(append)

tests/basics/exception1.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,9 @@
77
a = IndexError(1, "test", [100, 200])
88
print(repr(a))
99
print(str(a))
10+
print(a.args)
11+
12+
s = StopIteration()
13+
print(s.value)
14+
s = StopIteration(1, 2, 3)
15+
print(s.value)

0 commit comments

Comments
 (0)