aboutsummaryrefslogtreecommitdiff
path: root/py/objexcept.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2020-02-11 11:48:28 +1100
committerDamien George <damien.p.george@gmail.com>2020-02-13 11:52:40 +1100
commitad7213d3c31bccb26a3f54f7492ccf4b0cc920f3 (patch)
tree2ab64a805c05d790579052f557a8168061afcebe /py/objexcept.c
parent97eca38c4f03e76bd464fb25974544a043af2a9e (diff)
py: Add mp_raise_msg_varg helper and use it where appropriate.
This commit adds mp_raise_msg_varg(type, fmt, ...) as a helper for nlr_raise(mp_obj_new_exception_msg_varg(type, fmt, ...)). It makes the C-level API for raising exceptions more consistent, and reduces code size on most ports: bare-arm: +28 +0.042% minimal x86: +100 +0.067% unix x64: -56 -0.011% unix nanbox: -300 -0.068% stm32: -204 -0.054% PYBV10 cc3200: +0 +0.000% esp8266: -64 -0.010% GENERIC esp32: -104 -0.007% GENERIC nrf: -136 -0.094% pca10040 samd: +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS
Diffstat (limited to 'py/objexcept.c')
-rw-r--r--py/objexcept.c13
1 files changed, 9 insertions, 4 deletions
diff --git a/py/objexcept.c b/py/objexcept.c
index 33ad74ee7..30592fd56 100644
--- a/py/objexcept.c
+++ b/py/objexcept.c
@@ -385,6 +385,14 @@ STATIC void exc_add_strn(void *data, const char *str, size_t len) {
}
mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char *fmt, ...) {
+ va_list args;
+ va_start(args, fmt);
+ mp_obj_t exc = mp_obj_new_exception_msg_varg2(exc_type, fmt, args);
+ va_end(args);
+ return exc;
+}
+
+mp_obj_t mp_obj_new_exception_msg_varg2(const mp_obj_type_t *exc_type, const char *fmt, va_list args) {
assert(fmt != NULL);
// Check that the given type is an exception type
@@ -425,10 +433,7 @@ mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char
// We have some memory to format the string
struct _exc_printer_t exc_pr = {!used_emg_buf, o_str_alloc, 0, o_str_buf};
mp_print_t print = {&exc_pr, exc_add_strn};
- va_list ap;
- va_start(ap, fmt);
- mp_vprintf(&print, fmt, ap);
- va_end(ap);
+ mp_vprintf(&print, fmt, args);
exc_pr.buf[exc_pr.len] = '\0';
o_str->len = exc_pr.len;
o_str->data = exc_pr.buf;