Skip to content

Commit aef0586

Browse files
committed
py: Fix varg helpers by adding vlist variant of mp_obj_new_exception_msg
1 parent 48d7fca commit aef0586

3 files changed

Lines changed: 14 additions & 9 deletions

File tree

py/obj.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#ifndef __MICROPY_INCLUDED_PY_OBJ_H__
2727
#define __MICROPY_INCLUDED_PY_OBJ_H__
2828

29+
#include <stdarg.h>
30+
2931
#include "py/mpconfig.h"
3032
#include "py/misc.h"
3133
#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);
622624
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);
623625
mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, const char *msg);
624626
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!)
627+
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!)
625628
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);
626629
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);
627630
mp_obj_t mp_obj_new_fun_viper(mp_uint_t n_args, void *fun_data, mp_uint_t type_sig);

py/objexcept.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,14 @@ mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, const char *msg
322322
}
323323

324324
mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char *fmt, ...) {
325+
va_list ap;
326+
va_start(ap, fmt);
327+
mp_obj_t exception = mp_obj_new_exception_msg_vlist(exc_type, fmt, ap);
328+
va_end(ap);
329+
return exception;
330+
}
331+
332+
mp_obj_t mp_obj_new_exception_msg_vlist(const mp_obj_type_t *exc_type, const char *fmt, va_list ap) {
325333
// check that the given type is an exception type
326334
assert(exc_type->make_new == mp_obj_exception_make_new);
327335

@@ -354,10 +362,7 @@ mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char
354362
vstr_t vstr;
355363
vstr_init_fixed_buf(&vstr, max_len, (char *)str_data);
356364

357-
va_list ap;
358-
va_start(ap, fmt);
359365
vstr_vprintf(&vstr, fmt, ap);
360-
va_end(ap);
361366

362367
str->base.type = &mp_type_str;
363368
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
392397
o->args->items[0] = mp_obj_new_str(fmt, strlen(fmt), false);
393398
} else {
394399
// render exception message and store as .args[0]
395-
va_list ap;
396400
vstr_t vstr;
397401
vstr_init(&vstr, 16);
398-
va_start(ap, fmt);
399402
vstr_vprintf(&vstr, fmt, ap);
400-
va_end(ap);
401403
o->args->items[0] = mp_obj_new_str_from_vstr(&mp_type_str, &vstr);
402404
}
403405
}

py/runtime.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,7 +1466,7 @@ NORETURN void mp_raise_msg(const mp_obj_type_t *exc_type, const char *msg) {
14661466
NORETURN void mp_raise_msg_varg(const mp_obj_type_t *exc_type, const char *fmt, ...) {
14671467
va_list argptr;
14681468
va_start(argptr,fmt);
1469-
mp_obj_t exception = mp_obj_new_exception_msg_varg(exc_type, fmt, argptr);
1469+
mp_obj_t exception = mp_obj_new_exception_msg_vlist(exc_type, fmt, argptr);
14701470
va_end(argptr);
14711471
nlr_raise(exception);
14721472
}
@@ -1498,7 +1498,7 @@ NORETURN void mp_raise_ValueError(const char *msg) {
14981498
NORETURN void mp_raise_ValueError_varg(const char *fmt, ...) {
14991499
va_list argptr;
15001500
va_start(argptr,fmt);
1501-
mp_obj_t exception = mp_obj_new_exception_msg_varg(&mp_type_ValueError, fmt, argptr);
1501+
mp_obj_t exception = mp_obj_new_exception_msg_vlist(&mp_type_ValueError, fmt, argptr);
15021502
va_end(argptr);
15031503
nlr_raise(exception);
15041504
}
@@ -1510,7 +1510,7 @@ NORETURN void mp_raise_TypeError(const char *msg) {
15101510
NORETURN void mp_raise_TypeError_varg(const char *fmt, ...) {
15111511
va_list argptr;
15121512
va_start(argptr,fmt);
1513-
mp_obj_t exception = mp_obj_new_exception_msg_varg(&mp_type_TypeError, fmt, argptr);
1513+
mp_obj_t exception = mp_obj_new_exception_msg_vlist(&mp_type_TypeError, fmt, argptr);
15141514
va_end(argptr);
15151515
nlr_raise(exception);
15161516
}

0 commit comments

Comments
 (0)