Skip to content

Commit d8351ca

Browse files
committed
objtype: .print() Exception instances in adhoc way.
This is ugly, just as expected.
1 parent f2021ff commit d8351ca

4 files changed

Lines changed: 26 additions & 11 deletions

File tree

py/obj.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,10 @@ typedef mp_obj_t (*mp_fun_var_t)(uint n, const mp_obj_t *);
169169
typedef mp_obj_t (*mp_fun_kw_t)(uint n, const mp_obj_t *, mp_map_t *);
170170

171171
typedef enum {
172-
PRINT_STR,
173-
PRINT_REPR,
174-
PRINT_EXC, // Special format for printing exception in unhandled exception message
172+
PRINT_STR = 0,
173+
PRINT_REPR = 1,
174+
PRINT_EXC = 2, // Special format for printing exception in unhandled exception message
175+
PRINT_EXC_SUBCLASS = 4, // Internal flag for printing exception subclasses
175176
} mp_print_kind_t;
176177

177178
typedef void (*mp_print_fun_t)(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o, mp_print_kind_t kind);
@@ -424,13 +425,15 @@ mp_float_t mp_obj_int_as_float(mp_obj_t self_in);
424425
machine_int_t mp_obj_int_get_checked(mp_obj_t self_in);
425426

426427
// exception
428+
#define mp_obj_is_native_exception_instance(o) (mp_obj_get_type(o)->make_new == mp_obj_exception_make_new)
427429
bool mp_obj_is_exception_type(mp_obj_t self_in);
428430
bool mp_obj_is_exception_instance(mp_obj_t self_in);
429431
bool mp_obj_exception_match(mp_obj_t exc, const mp_obj_type_t *exc_type);
430432
void mp_obj_exception_clear_traceback(mp_obj_t self_in);
431433
void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, machine_uint_t line, qstr block);
432434
void mp_obj_exception_get_traceback(mp_obj_t self_in, machine_uint_t *n, machine_uint_t **values);
433435
mp_obj_t mp_obj_exception_get_value(mp_obj_t self_in);
436+
mp_obj_t mp_obj_exception_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args);
434437

435438
// str
436439
mp_obj_t mp_obj_str_builder_start(const mp_obj_type_t *type, uint len, byte **data);

py/objexcept.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,17 @@ const mp_obj_exception_t mp_const_GeneratorExit_obj = {{&mp_type_GeneratorExit},
3030

3131
STATIC void mp_obj_exception_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
3232
mp_obj_exception_t *o = o_in;
33-
if (kind == PRINT_REPR) {
33+
mp_print_kind_t k = kind & ~PRINT_EXC_SUBCLASS;
34+
bool is_subclass = kind & PRINT_EXC_SUBCLASS;
35+
if (!is_subclass && (k == PRINT_REPR || k == PRINT_EXC)) {
3436
print(env, "%s", qstr_str(o->base.type->name));
35-
} else if (kind == PRINT_EXC) {
36-
print(env, "%s: ", qstr_str(o->base.type->name));
3737
}
38-
if (kind == PRINT_STR || kind == PRINT_EXC) {
38+
39+
if (k == PRINT_EXC) {
40+
print(env, ": ");
41+
}
42+
43+
if (k == PRINT_STR || k == PRINT_EXC) {
3944
if (o->args == NULL || o->args->len == 0) {
4045
print(env, "");
4146
return;
@@ -47,7 +52,7 @@ STATIC void mp_obj_exception_print(void (*print)(void *env, const char *fmt, ...
4752
tuple_print(print, env, o->args, kind);
4853
}
4954

50-
STATIC mp_obj_t mp_obj_exception_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
55+
mp_obj_t mp_obj_exception_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
5156
mp_obj_type_t *type = type_in;
5257

5358
if (n_kw != 0) {

py/objtype.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,15 @@ STATIC void class_print(void (*print)(void *env, const char *fmt, ...), void *en
148148
}
149149

150150
if (member[0] == MP_OBJ_SENTINEL) {
151-
mp_obj_print_helper(print, env, self->subobj[0], kind);
151+
// Handle Exception subclasses specially
152+
if (mp_obj_is_native_exception_instance(self->subobj[0])) {
153+
if (kind != PRINT_STR) {
154+
print(env, "%s", qstr_str(self->base.type->name));
155+
}
156+
mp_obj_print_helper(print, env, self->subobj[0], kind | PRINT_EXC_SUBCLASS);
157+
} else {
158+
mp_obj_print_helper(print, env, self->subobj[0], kind);
159+
}
152160
return;
153161
}
154162

tests/basics/subclass-native3.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@ class MyExc(Exception):
33

44
e = MyExc(100, "Some error")
55
print(e)
6-
# TODO: Prints native base class name
7-
#print(repr(e))
6+
print(repr(e))
87
print(e.args)

0 commit comments

Comments
 (0)