py: Fix varg helpers by adding vlist variant of mp_obj_new_exception_msg

This commit is contained in:
Scott Shawcroft 2017-02-24 17:47:02 +01:00
parent 48d7fca25e
commit aef0586ee1
3 changed files with 14 additions and 9 deletions

View File

@ -26,6 +26,8 @@
#ifndef __MICROPY_INCLUDED_PY_OBJ_H__
#define __MICROPY_INCLUDED_PY_OBJ_H__
#include <stdarg.h>
#include "py/mpconfig.h"
#include "py/misc.h"
#include "py/qstr.h"
@ -622,6 +624,7 @@ mp_obj_t mp_obj_new_exception_arg1(const mp_obj_type_t *exc_type, mp_obj_t arg);
mp_obj_t mp_obj_new_exception_args(const mp_obj_type_t *exc_type, mp_uint_t n_args, const mp_obj_t *args);
mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, const char *msg);
mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char *fmt, ...); // counts args by number of % symbols in fmt, excluding %%; can only handle void* sizes (ie no float/double!)
mp_obj_t mp_obj_new_exception_msg_vlist(const mp_obj_type_t *exc_type, const char *fmt, va_list ap); // counts args by number of % symbols in fmt, excluding %%; can only handle void* sizes (ie no float/double!)
mp_obj_t mp_obj_new_fun_bc(mp_obj_t def_args, mp_obj_t def_kw_args, const byte *code, const mp_uint_t *const_table);
mp_obj_t mp_obj_new_fun_native(mp_obj_t def_args_in, mp_obj_t def_kw_args, const void *fun_data, const mp_uint_t *const_table);
mp_obj_t mp_obj_new_fun_viper(mp_uint_t n_args, void *fun_data, mp_uint_t type_sig);

View File

@ -322,6 +322,14 @@ mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, const char *msg
}
mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
mp_obj_t exception = mp_obj_new_exception_msg_vlist(exc_type, fmt, ap);
va_end(ap);
return exception;
}
mp_obj_t mp_obj_new_exception_msg_vlist(const mp_obj_type_t *exc_type, const char *fmt, va_list ap) {
// check that the given type is an exception type
assert(exc_type->make_new == mp_obj_exception_make_new);
@ -354,10 +362,7 @@ mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char
vstr_t vstr;
vstr_init_fixed_buf(&vstr, max_len, (char *)str_data);
va_list ap;
va_start(ap, fmt);
vstr_vprintf(&vstr, fmt, ap);
va_end(ap);
str->base.type = &mp_type_str;
str->hash = qstr_compute_hash(str_data, str->len);
@ -392,12 +397,9 @@ mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char
o->args->items[0] = mp_obj_new_str(fmt, strlen(fmt), false);
} else {
// render exception message and store as .args[0]
va_list ap;
vstr_t vstr;
vstr_init(&vstr, 16);
va_start(ap, fmt);
vstr_vprintf(&vstr, fmt, ap);
va_end(ap);
o->args->items[0] = mp_obj_new_str_from_vstr(&mp_type_str, &vstr);
}
}

View File

@ -1466,7 +1466,7 @@ NORETURN void mp_raise_msg(const mp_obj_type_t *exc_type, const char *msg) {
NORETURN void mp_raise_msg_varg(const mp_obj_type_t *exc_type, const char *fmt, ...) {
va_list argptr;
va_start(argptr,fmt);
mp_obj_t exception = mp_obj_new_exception_msg_varg(exc_type, fmt, argptr);
mp_obj_t exception = mp_obj_new_exception_msg_vlist(exc_type, fmt, argptr);
va_end(argptr);
nlr_raise(exception);
}
@ -1498,7 +1498,7 @@ NORETURN void mp_raise_ValueError(const char *msg) {
NORETURN void mp_raise_ValueError_varg(const char *fmt, ...) {
va_list argptr;
va_start(argptr,fmt);
mp_obj_t exception = mp_obj_new_exception_msg_varg(&mp_type_ValueError, fmt, argptr);
mp_obj_t exception = mp_obj_new_exception_msg_vlist(&mp_type_ValueError, fmt, argptr);
va_end(argptr);
nlr_raise(exception);
}
@ -1510,7 +1510,7 @@ NORETURN void mp_raise_TypeError(const char *msg) {
NORETURN void mp_raise_TypeError_varg(const char *fmt, ...) {
va_list argptr;
va_start(argptr,fmt);
mp_obj_t exception = mp_obj_new_exception_msg_varg(&mp_type_TypeError, fmt, argptr);
mp_obj_t exception = mp_obj_new_exception_msg_vlist(&mp_type_TypeError, fmt, argptr);
va_end(argptr);
nlr_raise(exception);
}