py: Msg in exception is no longer interned.

This commit is contained in:
Damien George 2014-01-29 18:57:20 +00:00
parent 4d5b28cd08
commit 1ba1facaaa
1 changed files with 8 additions and 10 deletions

View File

@ -13,20 +13,19 @@
// This is unified class for C-level and Python-level exceptions // This is unified class for C-level and Python-level exceptions
// Python-level exception have empty ->msg and all arguments are in // Python-level exception have empty ->msg and all arguments are in
// args tuple. C-level excepttion likely have ->msg, and may as well // args tuple. C-level excepttion likely have ->msg and args is empty.
// have args tuple (or otherwise have it as NULL).
typedef struct mp_obj_exception_t { typedef struct mp_obj_exception_t {
mp_obj_base_t base; mp_obj_base_t base;
mp_obj_t traceback; // a list object, holding (file,line,block) as numbers (not Python objects); a hack for now mp_obj_t traceback; // a list object, holding (file,line,block) as numbers (not Python objects); a hack for now
qstr id; qstr id;
qstr msg; vstr_t *msg;
mp_obj_tuple_t args; mp_obj_tuple_t args;
} mp_obj_exception_t; } mp_obj_exception_t;
void exception_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) { void exception_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
mp_obj_exception_t *o = o_in; mp_obj_exception_t *o = o_in;
if (o->msg != 0) { if (o->msg != NULL) {
print(env, "%s: %s", qstr_str(o->id), qstr_str(o->msg)); print(env, "%s: %s", qstr_str(o->id), vstr_str(o->msg));
} else { } else {
// Yes, that's how CPython has it // Yes, that's how CPython has it
if (kind == PRINT_REPR) { if (kind == PRINT_REPR) {
@ -55,7 +54,7 @@ static mp_obj_t exception_call(mp_obj_t self_in, uint n_args, uint n_kw, const m
mp_obj_exception_t *o = m_new_obj_var(mp_obj_exception_t, mp_obj_t, n_args); mp_obj_exception_t *o = m_new_obj_var(mp_obj_exception_t, mp_obj_t, n_args);
o->base.type = &exception_type; o->base.type = &exception_type;
o->id = base->id; o->id = base->id;
o->msg = 0; o->msg = NULL;
o->args.len = n_args; o->args.len = n_args;
memcpy(o->args.items, args, n_args * sizeof(mp_obj_t)); memcpy(o->args.items, args, n_args * sizeof(mp_obj_t));
return o; return o;
@ -92,15 +91,14 @@ mp_obj_t mp_obj_new_exception_msg_varg(qstr id, const char *fmt, ...) {
o->id = id; o->id = id;
o->args.len = 0; o->args.len = 0;
if (fmt == NULL) { if (fmt == NULL) {
o->msg = 0; o->msg = NULL;
} else { } else {
// render exception message // render exception message
vstr_t *vstr = vstr_new(); o->msg = vstr_new();
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
vstr_vprintf(vstr, fmt, ap); vstr_vprintf(o->msg, fmt, ap);
va_end(ap); va_end(ap);
o->msg = qstr_from_strn_take(vstr->buf, vstr->alloc, vstr->len);
} }
return o; return o;