summaryrefslogtreecommitdiff
path: root/py
diff options
context:
space:
mode:
authorScott Shawcroft <scott.shawcroft@gmail.com>2017-02-24 17:47:02 +0100
committerScott Shawcroft <scott.shawcroft@gmail.com>2017-02-24 17:47:02 +0100
commitaef0586ee101aa1ba9f0cda38f23b0fdef3312a6 (patch)
treed2689777e401c5ce00b5216aa4c3852cf8c9bae7 /py
parent48d7fca25ec8e0363bade6b51bd0bb8e2e844089 (diff)
py: Fix varg helpers by adding vlist variant of mp_obj_new_exception_msg
Diffstat (limited to 'py')
-rw-r--r--py/obj.h3
-rw-r--r--py/objexcept.c14
-rw-r--r--py/runtime.c6
3 files changed, 14 insertions, 9 deletions
diff --git a/py/obj.h b/py/obj.h
index 55f235970..a794e0e95 100644
--- a/py/obj.h
+++ b/py/obj.h
@@ -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);
diff --git a/py/objexcept.c b/py/objexcept.c
index c1b992d27..9320b5147 100644
--- a/py/objexcept.c
+++ b/py/objexcept.c
@@ -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);
}
}
diff --git a/py/runtime.c b/py/runtime.c
index 1177c60c0..ad1228d4d 100644
--- a/py/runtime.c
+++ b/py/runtime.c
@@ -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);
}