Skip to content

Actually implement vararg support for exceptions. #139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions py/obj.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ const char *mp_obj_get_type_str(mp_obj_t o_in) {
}

void printf_wrapper(void *env, const char *fmt, ...) {
// Special format spec to emulate vprintf():
// If format spec exactly matches "%V", then next argument is
// interpreted as va_list (whose first arg is fmt, and rest are
// subst values).
if (fmt[0] == '%' && fmt[1] == 'V') {
va_list args;
va_start(args, fmt);
void **subargs = va_arg(args, void**);
vprintf(subargs[0], (va_list)&subargs[1]);
va_end(args);
return;
}
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
Expand Down
20 changes: 5 additions & 15 deletions py/objexcept.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,11 @@ typedef struct mp_obj_exception_t {

void exception_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in) {
mp_obj_exception_t *o = o_in;
switch (o->n_args) {
case 0:
print(env, "%s", qstr_str(o->id));
break;
case 1:
print(env, "%s: %s", qstr_str(o->id), (const char*)o->args[0]);
break;
case 2:
print(env, "%s: ", qstr_str(o->id));
print(env, (const char*)o->args[0], o->args[1]);
break;
default: // here we just assume at least 3 args, but only use first 3
print(env, "%s: ", qstr_str(o->id));
print(env, (const char*)o->args[0], o->args[1], o->args[2]);
break;
if (o->n_args == 0) {
print(env, "%s", qstr_str(o->id));
} else {
print(env, "%s: ", qstr_str(o->id));
print(env, "%V", &o->args);
}
}

Expand Down
12 changes: 12 additions & 0 deletions py/objstr.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,18 @@ mp_obj_t str_strip(int n_args, const mp_obj_t *args) {
}

void vstr_printf_wrapper(void *env, const char *fmt, ...) {
// Special format spec to emulate vprintf():
// If format spec exactly matches "%V", then next argument is
// interpreted as va_list (whose first arg is fmt, and rest are
// subst values).
if (fmt[0] == '%' && fmt[1] == 'V') {
va_list args;
va_start(args, fmt);
void **subargs = va_arg(args, void**);
vstr_vprintf(env, subargs[0], (va_list)&subargs[1]);
va_end(args);
return;
}
va_list args;
va_start(args, fmt);
vstr_vprintf(env, fmt, args);
Expand Down