From 12fa5b3a66d6f0034113a56c7d647c7bac5be74c Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Fri, 24 Feb 2017 15:13:07 +0100 Subject: [PATCH] Switch exception throwing to mp_raise helpers. It saves a little code space each time to share the call. --- atmel-samd/common-hal/nativeio/AnalogIn.c | 2 +- atmel-samd/common-hal/nativeio/AnalogOut.c | 7 +- atmel-samd/common-hal/nativeio/DigitalInOut.c | 3 +- atmel-samd/common-hal/nativeio/I2C.c | 7 +- atmel-samd/common-hal/nativeio/PWMOut.c | 11 +- atmel-samd/common-hal/nativeio/SPI.c | 5 +- atmel-samd/common-hal/nativeio/TouchIn.c | 16 +-- atmel-samd/common-hal/nativeio/TouchInStub.c | 3 +- atmel-samd/common-hal/nativeio/UART.c | 8 +- atmel-samd/moduos.c | 3 +- cc3200/mods/moduos.c | 2 +- extmod/modlwip.c | 2 +- extmod/modure.c | 2 +- extmod/modussl_axtls.c | 2 +- extmod/modussl_mbedtls.c | 2 +- extmod/modutimeq.c | 2 +- py/argcheck.c | 26 ++-- py/bc.c | 25 ++-- py/builtinevex.c | 4 +- py/builtinimport.c | 18 ++- py/lexer.c | 2 +- py/modbuiltins.c | 17 ++- py/modmath.c | 3 +- py/modthread.c | 2 +- py/obj.c | 69 +++++---- py/objarray.c | 10 +- py/objcomplex.c | 2 +- py/objdict.c | 2 +- py/objgenerator.c | 6 +- py/objint.c | 10 +- py/objint_longlong.c | 8 +- py/objint_mpz.c | 6 +- py/objlist.c | 6 +- py/objnamedtuple.c | 18 +-- py/objobject.c | 2 +- py/objset.c | 2 +- py/objstr.c | 35 +++-- py/objstringio.c | 2 +- py/objstrunicode.c | 8 +- py/objtuple.c | 2 +- py/objtype.c | 33 +++-- py/parsenum.c | 2 +- py/runtime.c | 134 ++++++++++++------ py/runtime.h | 10 +- py/sequence.c | 2 +- py/stackctrl.c | 4 +- py/stream.c | 4 +- shared-bindings/bitbangio/I2C.c | 2 +- shared-bindings/bitbangio/SPI.c | 13 +- shared-bindings/nativeio/AnalogOut.c | 3 +- shared-bindings/nativeio/DigitalInOut.c | 15 +- shared-bindings/nativeio/I2C.c | 2 +- shared-bindings/nativeio/PWMOut.c | 8 +- shared-bindings/nativeio/SPI.c | 15 +- shared-bindings/nativeio/UART.c | 6 +- shared-bindings/time/__init__.c | 6 +- unix/modjni.c | 2 +- 57 files changed, 327 insertions(+), 296 deletions(-) diff --git a/atmel-samd/common-hal/nativeio/AnalogIn.c b/atmel-samd/common-hal/nativeio/AnalogIn.c index b07bbe12b9..2203fc2ca3 100644 --- a/atmel-samd/common-hal/nativeio/AnalogIn.c +++ b/atmel-samd/common-hal/nativeio/AnalogIn.c @@ -46,7 +46,7 @@ void common_hal_nativeio_analogin_construct(nativeio_analogin_obj_t* self, const mcu_pin_obj_t *pin) { if (!pin->has_adc) { // No ADC function on that pin - nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Pin does not have ADC capabilities")); + mp_raise_ValueError("Pin does not have ADC capabilities"); } self->pin = pin; diff --git a/atmel-samd/common-hal/nativeio/AnalogOut.c b/atmel-samd/common-hal/nativeio/AnalogOut.c index 0046c2f550..f36da19749 100644 --- a/atmel-samd/common-hal/nativeio/AnalogOut.c +++ b/atmel-samd/common-hal/nativeio/AnalogOut.c @@ -27,6 +27,7 @@ #include #include +#include "py/mperrno.h" #include "py/runtime.h" #include "shared-bindings/nativeio/AnalogOut.h" @@ -37,8 +38,7 @@ void common_hal_nativeio_analogout_construct(nativeio_analogout_obj_t* self, const mcu_pin_obj_t *pin) { if (pin->pin != PIN_PA02) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, - "AnalogOut not supported on given pin.")); + mp_raise_ValueError("AnalogOut not supported on given pin"); return; } struct dac_config config_dac; @@ -46,8 +46,7 @@ void common_hal_nativeio_analogout_construct(nativeio_analogout_obj_t* self, config_dac.reference = DAC_REFERENCE_AVCC; enum status_code status = dac_init(&self->dac_instance, DAC, &config_dac); if (status != STATUS_OK) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, - "DAC init failed.")); + mp_raise_OSError(MP_EIO); return; } diff --git a/atmel-samd/common-hal/nativeio/DigitalInOut.c b/atmel-samd/common-hal/nativeio/DigitalInOut.c index a002050e4b..22e8fb179f 100644 --- a/atmel-samd/common-hal/nativeio/DigitalInOut.c +++ b/atmel-samd/common-hal/nativeio/DigitalInOut.c @@ -171,8 +171,7 @@ enum digitalinout_pull_t common_hal_nativeio_digitalinout_get_pull( PortGroup *const port_base = port_get_group_from_gpio_pin(pin); uint32_t pin_mask = (1UL << (pin % 32)); if (self->output) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, - "Cannot get pull while in output mode.")); + mp_raise_AttributeError("Cannot get pull while in output mode"); return PULL_NONE; } else { if (port_base->PINCFG[pin % 32].bit.PULLEN == 0) { diff --git a/atmel-samd/common-hal/nativeio/I2C.c b/atmel-samd/common-hal/nativeio/I2C.c index 00b2a43e57..fb5e765057 100644 --- a/atmel-samd/common-hal/nativeio/I2C.c +++ b/atmel-samd/common-hal/nativeio/I2C.c @@ -30,6 +30,7 @@ #include "shared-bindings/nativeio/I2C.h" #include "py/mperrno.h" #include "py/nlr.h" +#include "py/runtime.h" #include "asf/sam0/drivers/sercom/i2c/i2c_master.h" #include "samd21_pins.h" @@ -70,7 +71,7 @@ void common_hal_nativeio_i2c_construct(nativeio_i2c_obj_t *self, } } if (sercom == NULL) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid pins.")); + mp_raise_ValueError("Invalid pins"); } config_i2c_master.pinmux_pad0 = sda_pinmux; // SDA @@ -86,9 +87,9 @@ void common_hal_nativeio_i2c_construct(nativeio_i2c_obj_t *self, if (status != STATUS_OK) { common_hal_nativeio_i2c_deinit(self); if (status == STATUS_ERR_BAUDRATE_UNAVAILABLE) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Unsupported baudrate")); + mp_raise_ValueError("Unsupported baudrate"); } else { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "I2C bus init error")); + mp_raise_OSError(MP_EIO); } } diff --git a/atmel-samd/common-hal/nativeio/PWMOut.c b/atmel-samd/common-hal/nativeio/PWMOut.c index 6baf1e9467..73d83163a8 100644 --- a/atmel-samd/common-hal/nativeio/PWMOut.c +++ b/atmel-samd/common-hal/nativeio/PWMOut.c @@ -79,13 +79,11 @@ void common_hal_nativeio_pwmout_construct(nativeio_pwmout_obj_t* self, self->variable_frequency = variable_frequency; if (pin->primary_timer.tc == 0 && pin->secondary_timer.tc == 0) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, - "Invalid pin")); + mp_raise_ValueError("Invalid pin"); } if (frequency == 0 || frequency > 6000000) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, - "Invalid PWM frequency")); + mp_raise_ValueError("Invalid PWM frequency"); } uint16_t primary_timer_index = 0xff; @@ -136,7 +134,7 @@ void common_hal_nativeio_pwmout_construct(nativeio_pwmout_obj_t* self, index = primary_timer_index; } if (t == NULL) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "All timers in use")); + mp_raise_RuntimeError("All timers in use"); return; } uint8_t resolution = 0; @@ -265,8 +263,7 @@ uint16_t common_hal_nativeio_pwmout_get_duty_cycle(nativeio_pwmout_obj_t* self) void common_hal_nativeio_pwmout_set_frequency(nativeio_pwmout_obj_t* self, uint32_t frequency) { if (frequency == 0 || frequency > 6000000) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, - "Invalid PWM frequency")); + mp_raise_ValueError("Invalid PWM frequency"); } const pin_timer_t* t = self->timer; uint8_t resolution; diff --git a/atmel-samd/common-hal/nativeio/SPI.c b/atmel-samd/common-hal/nativeio/SPI.c index 0267a546b5..3dec31a10d 100644 --- a/atmel-samd/common-hal/nativeio/SPI.c +++ b/atmel-samd/common-hal/nativeio/SPI.c @@ -29,6 +29,7 @@ #include "shared-bindings/nativeio/SPI.h" #include "py/nlr.h" +#include "py/runtime.h" #include "samd21_pins.h" // We use ENABLE registers below we don't want to treat as a macro. @@ -92,7 +93,7 @@ void common_hal_nativeio_spi_construct(nativeio_spi_obj_t *self, } } if (sercom == NULL) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid pins")); + mp_raise_ValueError("Invalid pins"); } // Depends on where MOSI and CLK are. @@ -111,7 +112,7 @@ void common_hal_nativeio_spi_construct(nativeio_spi_obj_t *self, } } if (dopo == 8) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "SPI MOSI and clock pins incompatible")); + mp_raise_ValueError("MOSI and clock pins incompatible"); } config_spi_master.mux_setting = (dopo << SERCOM_SPI_CTRLA_DOPO_Pos) | diff --git a/atmel-samd/common-hal/nativeio/TouchIn.c b/atmel-samd/common-hal/nativeio/TouchIn.c index 901fe9e707..a98d235ef3 100644 --- a/atmel-samd/common-hal/nativeio/TouchIn.c +++ b/atmel-samd/common-hal/nativeio/TouchIn.c @@ -27,6 +27,7 @@ #include #include "py/nlr.h" +#include "py/mperrno.h" #include "py/runtime.h" #include "py/binary.h" #include "py/mphal.h" @@ -186,8 +187,7 @@ nativeio_touchin_obj_t *active_touchin_obj[DEF_SELFCAP_NUM_CHANNELS]; void common_hal_nativeio_touchin_construct(nativeio_touchin_obj_t* self, const mcu_pin_obj_t *pin) { if (!pin->has_touch) { - // No ADC function on that pin - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "pin %q does not have touch capabilities", pin->name)); + mp_raise_ValueError("Invalid pin"); } if (selfcap_config.num_channels > 0) { @@ -230,18 +230,18 @@ void common_hal_nativeio_touchin_construct(nativeio_touchin_obj_t* self, PRIV_SELFCAP_RS_TABLE_INIT, PRIV_NM_TABLE_INIT, PRIV_FREQ_AUTO_TUNE_CHK, PRIV_MOIS_TOLERANCE_CHK); if (status != TOUCH_SUCCESS) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Touch init failed (%d)", status)); + mp_raise_OSError(MP_EIO); } for (int i = 0; i < selfcap_config.num_channels; i++) { status = touch_selfcap_sensor_config(SENSOR_TYPE_KEY, i, i, NO_AKS_GROUP, 10u, HYST_25, RES_8_BIT, &active_touchin_obj[i]->sensor_id); if (status != TOUCH_SUCCESS) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Touch pad config failed (%d)", status)); + mp_raise_OSError(MP_EIO); } } status = touch_selfcap_sensors_calibrate(AUTO_TUNE_RSEL); if (status != TOUCH_SUCCESS) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Touch pad calibration failed (%d)", status)); + mp_raise_OSError(MP_EIO); } // Run a measurement to get calibrated. @@ -275,7 +275,7 @@ void touch_selfcap_measure_complete_callback(void) bool common_hal_nativeio_touchin_get_value(nativeio_touchin_obj_t *self) { if (p_selfcap_measure_data->acq_status & TOUCH_CC_CALIB_ERROR) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Touch calibration error")); + mp_raise_RuntimeError("Touch calibration error"); } touch_acq_status = TOUCH_BURST_AGAIN; uint64_t start_ticks = ticks_ms; @@ -287,7 +287,7 @@ bool common_hal_nativeio_touchin_get_value(nativeio_touchin_obj_t *self) { touch_selfcap_measure_complete_callback); if (touch_ret != TOUCH_SUCCESS) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Touch measure failed")); + mp_raise_OSError(MP_EIO); } while(!touch_read_ready && ticks_ms - start_ticks < 1000) { @@ -299,7 +299,7 @@ bool common_hal_nativeio_touchin_get_value(nativeio_touchin_obj_t *self) { } if (touch_acq_status & TOUCH_BURST_AGAIN) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Touch read failed")); + mp_raise_OSError(MP_EIO); } return (p_selfcap_measure_data->p_sensor_states[self->sensor_id / 8] & (1 << (self->sensor_id % 8))) != 0; diff --git a/atmel-samd/common-hal/nativeio/TouchInStub.c b/atmel-samd/common-hal/nativeio/TouchInStub.c index 491e936773..5165c806f6 100644 --- a/atmel-samd/common-hal/nativeio/TouchInStub.c +++ b/atmel-samd/common-hal/nativeio/TouchInStub.c @@ -34,8 +34,7 @@ void common_hal_nativeio_touchin_construct(nativeio_touchin_obj_t* self, const mcu_pin_obj_t *pin) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, - "No room in flash for capacitive touch hardware support.")); + mp_raise_NotImplementError("No capacitive touch support"); } void common_hal_nativeio_touchin_deinit(nativeio_touchin_obj_t* self) { diff --git a/atmel-samd/common-hal/nativeio/UART.c b/atmel-samd/common-hal/nativeio/UART.c index 29afaf0d0a..cc42163484 100644 --- a/atmel-samd/common-hal/nativeio/UART.c +++ b/atmel-samd/common-hal/nativeio/UART.c @@ -31,6 +31,7 @@ #include "py/gc.h" #include "py/mperrno.h" #include "py/nlr.h" +#include "py/runtime.h" #include "py/stream.h" #include "samd21_pins.h" #include "tick.h" @@ -162,7 +163,7 @@ void common_hal_nativeio_uart_construct(nativeio_uart_obj_t *self, } } if (sercom == NULL) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid pins")); + mp_raise_ValueError("Invalid pins"); } if (tx == NULL) { tx_pad = 0; @@ -216,12 +217,11 @@ void common_hal_nativeio_uart_construct(nativeio_uart_obj_t *self, self->buffer_length *= (bits + 7) / 8; self->buffer = (uint8_t *) gc_alloc(self->buffer_length * sizeof(uint8_t), false); if (self->buffer == NULL) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, - "Unable to allocate RX buffer.")); + mp_raise_msg(&mp_type_MemoryError, "Failed to allocate RX buffer"); } if (usart_init(&self->uart_instance, sercom, &config_usart) != STATUS_OK) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Unable to init UART")); + mp_raise_OSError(MP_EIO); } // We use our own interrupt handler because we want a circular buffer diff --git a/atmel-samd/moduos.c b/atmel-samd/moduos.c index 33bf7acaf9..f30d85ecd4 100644 --- a/atmel-samd/moduos.c +++ b/atmel-samd/moduos.c @@ -30,6 +30,7 @@ #include "py/mpstate.h" #include "py/objtuple.h" #include "py/objstr.h" +#include "py/runtime.h" #include "genhdr/mpversion.h" #include "lib/fatfs/ff.h" #include "lib/fatfs/diskio.h" @@ -103,7 +104,7 @@ STATIC mp_obj_t os_getcwd(void) { FRESULT res = f_getcwd(buf, sizeof buf); if (res != FR_OK) { - nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(fresult_to_errno_table[res]))); + mp_raise_OSError(fresult_to_errno_table[res]); } return mp_obj_new_str(buf, strlen(buf), false); diff --git a/cc3200/mods/moduos.c b/cc3200/mods/moduos.c index d5b29336af..ffe2b551ad 100644 --- a/cc3200/mods/moduos.c +++ b/cc3200/mods/moduos.c @@ -498,7 +498,7 @@ STATIC mp_obj_t os_unmount(mp_obj_t path_o) { if ((mount_obj = osmount_find_by_path(path))) { unmount (mount_obj); } else { - mp_raise_msg(&mp_type_ValueError, mpexception_value_invalid_arguments); + mp_raise_ValueError(mpexception_value_invalid_arguments); } return mp_const_none; diff --git a/extmod/modlwip.c b/extmod/modlwip.c index 62699bd417..57fb2845fb 100644 --- a/extmod/modlwip.c +++ b/extmod/modlwip.c @@ -1025,7 +1025,7 @@ STATIC mp_obj_t lwip_socket_sendall(mp_obj_t self_in, mp_obj_t buf_in) { break; } case MOD_NETWORK_SOCK_DGRAM: - mp_not_implemented(""); + mp_raise_NotImplementError(""); break; } diff --git a/extmod/modure.c b/extmod/modure.c index b8c242429b..ba8a2e07ad 100644 --- a/extmod/modure.c +++ b/extmod/modure.c @@ -153,7 +153,7 @@ STATIC mp_obj_t re_split(size_t n_args, const mp_obj_t *args) { mp_obj_t s = mp_obj_new_str(subj.begin, caps[0] - subj.begin, false); mp_obj_list_append(retval, s); if (self->re.sub > 0) { - mp_not_implemented("Splitting with sub-captures"); + mp_raise_NotImplementError("Splitting with sub-captures"); } subj.begin = caps[1]; if (maxsplit > 0 && --maxsplit == 0) { diff --git a/extmod/modussl_axtls.c b/extmod/modussl_axtls.c index 4169158216..f238fb7c1a 100644 --- a/extmod/modussl_axtls.c +++ b/extmod/modussl_axtls.c @@ -126,7 +126,7 @@ STATIC mp_obj_t socket_setblocking(mp_obj_t self_in, mp_obj_t flag_in) { // Currently supports only blocking mode (void)self_in; if (!mp_obj_is_true(flag_in)) { - mp_not_implemented(""); + mp_raise_NotImplementError(""); } return mp_const_none; } diff --git a/extmod/modussl_mbedtls.c b/extmod/modussl_mbedtls.c index a7b8a14405..82ad262c4f 100644 --- a/extmod/modussl_mbedtls.c +++ b/extmod/modussl_mbedtls.c @@ -219,7 +219,7 @@ STATIC mp_obj_t socket_setblocking(mp_obj_t self_in, mp_obj_t flag_in) { // Currently supports only blocking mode (void)self_in; if (!mp_obj_is_true(flag_in)) { - mp_not_implemented(""); + mp_raise_NotImplementError(""); } return mp_const_none; } diff --git a/extmod/modutimeq.c b/extmod/modutimeq.c index f73b39103d..26ccb51821 100644 --- a/extmod/modutimeq.c +++ b/extmod/modutimeq.c @@ -121,7 +121,7 @@ STATIC mp_obj_t mod_utimeq_heappush(size_t n_args, const mp_obj_t *args) { mp_obj_t heap_in = args[0]; mp_obj_utimeq_t *heap = get_heap(heap_in); if (heap->len == heap->alloc) { - mp_raise_msg(&mp_type_IndexError, "queue overflow"); + mp_raise_IndexError("queue overflow"); } mp_uint_t l = heap->len; heap->items[l].time = MP_OBJ_SMALL_INT_VALUE(args[1]); diff --git a/py/argcheck.c b/py/argcheck.c index e12800a36b..f5edd2eda9 100644 --- a/py/argcheck.c +++ b/py/argcheck.c @@ -37,7 +37,7 @@ void mp_arg_check_num(size_t n_args, size_t n_kw, size_t n_args_min, size_t n_ar if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { mp_arg_error_terse_mismatch(); } else { - mp_raise_msg(&mp_type_TypeError, "function does not take keyword arguments"); + mp_raise_TypeError("function does not take keyword arguments"); } } @@ -46,9 +46,9 @@ void mp_arg_check_num(size_t n_args, size_t n_kw, size_t n_args_min, size_t n_ar if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { mp_arg_error_terse_mismatch(); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, + mp_raise_TypeError_varg( "function takes %d positional arguments but %d were given", - n_args_min, n_args)); + n_args_min, n_args); } } } else { @@ -56,17 +56,17 @@ void mp_arg_check_num(size_t n_args, size_t n_kw, size_t n_args_min, size_t n_ar if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { mp_arg_error_terse_mismatch(); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, + mp_raise_TypeError_varg( "function missing %d required positional arguments", - n_args_min - n_args)); + n_args_min - n_args); } } else if (n_args > n_args_max) { if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { mp_arg_error_terse_mismatch(); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, + mp_raise_TypeError_varg( "function expected at most %d arguments, got %d", - n_args_max, n_args)); + n_args_max, n_args); } } } @@ -89,8 +89,8 @@ void mp_arg_parse_all(size_t n_pos, const mp_obj_t *pos, mp_map_t *kws, size_t n if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { mp_arg_error_terse_mismatch(); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, - "'%q' argument required", allowed[i].qst)); + mp_raise_TypeError_varg( + "'%q' argument required", allowed[i].qst); } } out_vals[i] = allowed[i].defval; @@ -115,7 +115,7 @@ void mp_arg_parse_all(size_t n_pos, const mp_obj_t *pos, mp_map_t *kws, size_t n mp_arg_error_terse_mismatch(); } else { // TODO better error message - mp_raise_msg(&mp_type_TypeError, "extra positional arguments given"); + mp_raise_TypeError("extra positional arguments given"); } } if (kws_found < kws->used) { @@ -123,7 +123,7 @@ void mp_arg_parse_all(size_t n_pos, const mp_obj_t *pos, mp_map_t *kws, size_t n mp_arg_error_terse_mismatch(); } else { // TODO better error message - mp_raise_msg(&mp_type_TypeError, "extra keyword arguments given"); + mp_raise_TypeError("extra keyword arguments given"); } } } @@ -136,12 +136,12 @@ void mp_arg_parse_all_kw_array(size_t n_pos, size_t n_kw, const mp_obj_t *args, #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE || defined(_MSC_VER) NORETURN void mp_arg_error_terse_mismatch(void) { - mp_raise_msg(&mp_type_TypeError, "argument num/types mismatch"); + mp_raise_TypeError("argument num/types mismatch"); } #endif #if MICROPY_CPYTHON_COMPAT NORETURN void mp_arg_error_unimpl_kw(void) { - mp_not_implemented("keyword argument(s) not yet implemented - use normal args instead"); + mp_raise_NotImplementError("keyword argument(s) not yet implemented - use normal args instead"); } #endif diff --git a/py/bc.c b/py/bc.c index 07de08fc36..3f9cf6de5f 100644 --- a/py/bc.c +++ b/py/bc.c @@ -63,12 +63,12 @@ STATIC NORETURN void fun_pos_args_mismatch(mp_obj_fun_bc_t *f, size_t expected, mp_arg_error_terse_mismatch(); #elif MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NORMAL (void)f; - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, - "function takes %d positional arguments but %d were given", expected, given)); + mp_raise_TypeError_varg( + "function takes %d positional arguments but %d were given", expected, given); #elif MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, + mp_raise_TypeError_varg( "%q() takes %d positional arguments but %d were given", - mp_obj_fun_get_name(MP_OBJ_FROM_PTR(f)), expected, given)); + mp_obj_fun_get_name(MP_OBJ_FROM_PTR(f)), expected, given); #endif } @@ -176,8 +176,8 @@ void mp_setup_code_state(mp_code_state_t *code_state, mp_obj_fun_bc_t *self, siz for (size_t j = 0; j < n_pos_args + n_kwonly_args; j++) { if (wanted_arg_name == arg_names[j]) { if (code_state->state[n_state - 1 - j] != MP_OBJ_NULL) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, - "function got multiple values for argument '%q'", MP_OBJ_QSTR_VALUE(wanted_arg_name))); + mp_raise_TypeError_varg( + "function got multiple values for argument '%q'", MP_OBJ_QSTR_VALUE(wanted_arg_name)); } code_state->state[n_state - 1 - j] = kwargs[2 * i + 1]; goto continue2; @@ -185,7 +185,7 @@ void mp_setup_code_state(mp_code_state_t *code_state, mp_obj_fun_bc_t *self, siz } // Didn't find name match with positional args if ((scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) == 0) { - mp_raise_msg(&mp_type_TypeError, "function does not take keyword arguments"); + mp_raise_TypeError("function does not take keyword arguments"); } mp_obj_dict_store(dict, kwargs[2 * i], kwargs[2 * i + 1]); continue2:; @@ -209,8 +209,8 @@ continue2:; // Check that all mandatory positional args are specified while (d < &code_state->state[n_state]) { if (*d++ == MP_OBJ_NULL) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, - "function missing required positional argument #%d", &code_state->state[n_state] - d)); + mp_raise_TypeError_varg( + "function missing required positional argument #%d", &code_state->state[n_state] - d); } } @@ -225,8 +225,9 @@ continue2:; if (elem != NULL) { code_state->state[n_state - 1 - n_pos_args - i] = elem->value; } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, - "function missing required keyword argument '%q'", MP_OBJ_QSTR_VALUE(arg_names[n_pos_args + i]))); + mp_raise_TypeError_varg( + "function missing required keyword argument '%q'", + MP_OBJ_QSTR_VALUE(arg_names[n_pos_args + i])); } } } @@ -234,7 +235,7 @@ continue2:; } else { // no keyword arguments given if (n_kwonly_args != 0) { - mp_raise_msg(&mp_type_TypeError, "function missing keyword-only argument"); + mp_raise_TypeError("function missing keyword-only argument"); } if ((scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) != 0) { *var_pos_kw_args = mp_obj_new_dict(0); diff --git a/py/builtinevex.c b/py/builtinevex.c index 636f869300..df448bc585 100644 --- a/py/builtinevex.c +++ b/py/builtinevex.c @@ -95,7 +95,7 @@ STATIC mp_obj_t mp_builtin_compile(size_t n_args, const mp_obj_t *args) { case MP_QSTR_exec: parse_input_kind = MP_PARSE_FILE_INPUT; break; case MP_QSTR_eval: parse_input_kind = MP_PARSE_EVAL_INPUT; break; default: - mp_raise_msg(&mp_type_ValueError, "bad compile mode"); + mp_raise_ValueError("bad compile mode"); } mp_obj_code_t *code = m_new_obj(mp_obj_code_t); @@ -137,7 +137,7 @@ STATIC mp_obj_t eval_exec_helper(size_t n_args, const mp_obj_t *args, mp_parse_i if (MICROPY_PY_BUILTINS_EXECFILE && parse_input_kind == MP_PARSE_SINGLE_INPUT) { lex = mp_lexer_new_from_file(str); if (lex == NULL) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "could not open file '%s'", str)); + mp_raise_msg_varg(&mp_type_OSError, "could not open file '%s'", str); } parse_input_kind = MP_PARSE_FILE_INPUT; } else { diff --git a/py/builtinimport.c b/py/builtinimport.c index 4024c5d59a..b7e1c22f19 100644 --- a/py/builtinimport.c +++ b/py/builtinimport.c @@ -144,10 +144,9 @@ STATIC void do_load_from_lexer(mp_obj_t module_obj, mp_lexer_t *lex, const char if (lex == NULL) { // we verified the file exists using stat, but lexer could still fail if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { - mp_raise_msg(&mp_type_ImportError, "module not found"); + mp_raise_ImportError("module not found"); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ImportError, - "no module named '%s'", fname)); + mp_raise_msg_varg(&mp_type_ImportError, "no module named '%s'", fname); } } @@ -249,8 +248,7 @@ STATIC void do_load(mp_obj_t module_obj, vstr_t *file) { #endif // If we get here then the file was not frozen and we can't compile scripts. - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ImportError, - "script compilation not supported")); + mp_raise_ImportError("script compilation not supported"); } STATIC void chop_component(const char *start, const char **end) { @@ -346,7 +344,7 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) { DEBUG_printf("Warning: no dots in current module name and level>0\n"); p = this_name + this_name_l; } else if (level != -1) { - mp_raise_msg(&mp_type_ImportError, "invalid relative import"); + mp_raise_ImportError("invalid relative import"); } uint new_mod_l = (mod_len == 0 ? (size_t)(p - this_name) : (size_t)(p - this_name) + 1 + mod_len); @@ -361,7 +359,7 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) { DEBUG_printf("Resolved base name for relative import: '%s'\n", qstr_str(new_mod_q)); if (new_mod_q == MP_QSTR_) { // CPython raises SystemError - mp_raise_msg(&mp_type_ImportError, "cannot perform relative import"); + mp_raise_ImportError("cannot perform relative import"); } module_name = MP_OBJ_NEW_QSTR(new_mod_q); mod_str = new_mod; @@ -431,10 +429,10 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) { #endif // couldn't find the file, so fail if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { - mp_raise_msg(&mp_type_ImportError, "module not found"); + mp_raise_ImportError("module not found"); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ImportError, - "no module named '%q'", mod_name)); + mp_raise_msg_varg(&mp_type_ImportError, + "no module named '%q'", mod_name); } } } else { diff --git a/py/lexer.c b/py/lexer.c index 458fba0900..8a3fc99853 100644 --- a/py/lexer.c +++ b/py/lexer.c @@ -463,7 +463,7 @@ STATIC void mp_lexer_next_token_into(mp_lexer_t *lex, bool first_token) { // 3MB of text; even gzip-compressed and with minimal structure, it'll take // roughly half a meg of storage. This form of Unicode escape may be added // later on, but it's definitely not a priority right now. -- CJA 20140607 - mp_not_implemented("unicode name escapes"); + mp_raise_NotImplementError("unicode name escapes"); break; default: if (c >= '0' && c <= '7') { diff --git a/py/modbuiltins.c b/py/modbuiltins.c index 2cee546d44..0ab4a0765f 100644 --- a/py/modbuiltins.c +++ b/py/modbuiltins.c @@ -178,7 +178,7 @@ STATIC mp_obj_t mp_builtin_chr(mp_obj_t o_in) { str[3] = (c & 0x3F) | 0x80; len = 4; } else { - mp_raise_msg(&mp_type_ValueError, "chr() arg not in range(0x110000)"); + mp_raise_ValueError("chr() arg not in range(0x110000)"); } return mp_obj_new_str(str, len, true); #else @@ -187,7 +187,7 @@ STATIC mp_obj_t mp_builtin_chr(mp_obj_t o_in) { char str[1] = {ord}; return mp_obj_new_str(str, 1, true); } else { - mp_raise_msg(&mp_type_ValueError, "chr() arg not in range(256)"); + mp_raise_ValueError("chr() arg not in range(256)"); } #endif } @@ -286,7 +286,7 @@ STATIC mp_obj_t mp_builtin_min_max(size_t n_args, const mp_obj_t *args, mp_map_t if (default_elem != NULL) { best_obj = default_elem->value; } else { - mp_raise_msg(&mp_type_ValueError, "arg is an empty sequence"); + mp_raise_ValueError("arg is an empty sequence"); } } return best_obj; @@ -320,7 +320,7 @@ MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_min_obj, 1, mp_builtin_min); STATIC mp_obj_t mp_builtin_next(mp_obj_t o) { mp_obj_t ret = mp_iternext_allow_raise(o); if (ret == MP_OBJ_STOP_ITERATION) { - nlr_raise(mp_obj_new_exception(&mp_type_StopIteration)); + mp_raise_msg(&mp_type_StopIteration, ""); } else { return ret; } @@ -366,11 +366,10 @@ STATIC mp_obj_t mp_builtin_ord(mp_obj_t o_in) { #endif if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, - "ord expects a character")); + mp_raise_TypeError("ord expects a character"); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, - "ord() expected a character, but string of length %d found", (int)len)); + mp_raise_TypeError_varg( + "ord() expected a character, but string of length %d found", (int)len); } } MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_ord_obj, mp_builtin_ord); @@ -499,7 +498,7 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_sum_obj, 1, 2, mp_builtin_sum); STATIC mp_obj_t mp_builtin_sorted(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) { if (n_args > 1) { - mp_raise_msg(&mp_type_TypeError, "must use keyword argument for key function"); + mp_raise_TypeError("must use keyword argument for key function"); } mp_obj_t self = mp_type_list.make_new(&mp_type_list, 1, 0, args); mp_obj_list_sort(1, &self, kwargs); diff --git a/py/modmath.c b/py/modmath.c index 7b67751bcc..54217fe879 100644 --- a/py/modmath.c +++ b/py/modmath.c @@ -26,6 +26,7 @@ #include "py/builtin.h" #include "py/nlr.h" +#include "py/runtime.h" #if MICROPY_PY_BUILTINS_FLOAT && MICROPY_PY_MATH @@ -41,7 +42,7 @@ /// working with floating-point numbers. STATIC NORETURN void math_error(void) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "math domain error")); + mp_raise_ValueError("math domain error"); } #define MATH_FUN_1(py_name, c_name) \ diff --git a/py/modthread.c b/py/modthread.c index 6f55281adc..2ce7e7e4cb 100644 --- a/py/modthread.c +++ b/py/modthread.c @@ -239,7 +239,7 @@ STATIC mp_obj_t mod_thread_start_new_thread(size_t n_args, const mp_obj_t *args) } else { // positional and keyword arguments if (mp_obj_get_type(args[2]) != &mp_type_dict) { - mp_raise_msg(&mp_type_TypeError, "expecting a dict for keyword args"); + mp_raise_TypeError("expecting a dict for keyword args"); } mp_map_t *map = &((mp_obj_dict_t*)MP_OBJ_TO_PTR(args[2]))->map; th_args = m_new_obj_var(thread_entry_args_t, mp_obj_t, pos_args_len + 2 * map->used); diff --git a/py/obj.c b/py/obj.c index 1d0c80ab90..a4a6fdb358 100644 --- a/py/obj.c +++ b/py/obj.c @@ -229,10 +229,10 @@ mp_int_t mp_obj_get_int(mp_const_obj_t arg) { return mp_obj_int_get_checked(arg); } else { if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { - mp_raise_msg(&mp_type_TypeError, "can't convert to int"); + mp_raise_TypeError("can't convert to int"); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, - "can't convert %s to int", mp_obj_get_type_str(arg))); + mp_raise_TypeError_varg( + "can't convert %s to int", mp_obj_get_type_str(arg)); } } } @@ -279,10 +279,10 @@ mp_float_t mp_obj_get_float(mp_obj_t arg) { return mp_obj_float_get(arg); } else { if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { - mp_raise_msg(&mp_type_TypeError, "can't convert to float"); + mp_raise_TypeError("can't convert to float"); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, - "can't convert %s to float", mp_obj_get_type_str(arg))); + mp_raise_TypeError_varg( + "can't convert %s to float", mp_obj_get_type_str(arg)); } } } @@ -310,10 +310,10 @@ void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) { mp_obj_complex_get(arg, real, imag); } else { if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { - mp_raise_msg(&mp_type_TypeError, "can't convert to complex"); + mp_raise_TypeError("can't convert to complex"); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, - "can't convert %s to complex", mp_obj_get_type_str(arg))); + mp_raise_TypeError_varg( + "can't convert %s to complex", mp_obj_get_type_str(arg)); } } } @@ -328,10 +328,10 @@ void mp_obj_get_array(mp_obj_t o, mp_uint_t *len, mp_obj_t **items) { mp_obj_list_get(o, len, items); } else { if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { - mp_raise_msg(&mp_type_TypeError, "expected tuple/list"); + mp_raise_TypeError("expected tuple/list"); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, - "object '%s' is not a tuple or list", mp_obj_get_type_str(o))); + mp_raise_TypeError_varg( + "object '%s' is not a tuple or list", mp_obj_get_type_str(o)); } } } @@ -342,10 +342,10 @@ void mp_obj_get_array_fixed_n(mp_obj_t o, mp_uint_t len, mp_obj_t **items) { mp_obj_get_array(o, &seq_len, items); if (seq_len != len) { if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { - mp_raise_msg(&mp_type_ValueError, "tuple/list has wrong length"); + mp_raise_ValueError("tuple/list has wrong length"); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, - "requested length %d but object has length %d", (int)len, (int)seq_len)); + mp_raise_ValueError_varg("requested length %d but object has length %d", + (int)len, (int)seq_len); } } } @@ -357,11 +357,11 @@ mp_uint_t mp_get_index(const mp_obj_type_t *type, mp_uint_t len, mp_obj_t index, i = MP_OBJ_SMALL_INT_VALUE(index); } else if (!mp_obj_get_int_maybe(index, &i)) { if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { - mp_raise_msg(&mp_type_TypeError, "indices must be integers"); + mp_raise_TypeError("indices must be integers"); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, + mp_raise_TypeError_varg( "%q indices must be integers, not %s", - type->name, mp_obj_get_type_str(index))); + type->name, mp_obj_get_type_str(index)); } } @@ -377,10 +377,10 @@ mp_uint_t mp_get_index(const mp_obj_type_t *type, mp_uint_t len, mp_obj_t index, } else { if (i < 0 || (mp_uint_t)i >= len) { if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { - mp_raise_msg(&mp_type_IndexError, "index out of range"); + mp_raise_IndexError("index out of range"); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_IndexError, - "%q index out of range", type->name)); + mp_raise_msg_varg(&mp_type_IndexError, + "%q index out of range", type->name); } } } @@ -410,10 +410,10 @@ mp_obj_t mp_obj_len(mp_obj_t o_in) { mp_obj_t len = mp_obj_len_maybe(o_in); if (len == MP_OBJ_NULL) { if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { - mp_raise_msg(&mp_type_TypeError, "object has no len"); + mp_raise_TypeError("object has no len"); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, - "object of type '%s' has no len()", mp_obj_get_type_str(o_in))); + mp_raise_TypeError_varg( + "object of type '%s' has no len()", mp_obj_get_type_str(o_in)); } } else { return len; @@ -451,25 +451,24 @@ mp_obj_t mp_obj_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) { } if (value == MP_OBJ_NULL) { if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { - mp_raise_msg(&mp_type_TypeError, "object does not support item deletion"); + mp_raise_TypeError("object does not support item deletion"); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, - "'%s' object does not support item deletion", mp_obj_get_type_str(base))); + mp_raise_TypeError_varg( + "'%s' object does not support item deletion", mp_obj_get_type_str(base)); } } else if (value == MP_OBJ_SENTINEL) { if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, - "object is not subscriptable")); + mp_raise_TypeError("object is not subscriptable"); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, - "'%s' object is not subscriptable", mp_obj_get_type_str(base))); + mp_raise_TypeError_varg( + "'%s' object is not subscriptable", mp_obj_get_type_str(base)); } } else { if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { - mp_raise_msg(&mp_type_TypeError, "object does not support item assignment"); + mp_raise_TypeError("object does not support item assignment"); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, - "'%s' object does not support item assignment", mp_obj_get_type_str(base))); + mp_raise_TypeError_varg( + "'%s' object does not support item assignment", mp_obj_get_type_str(base)); } } } @@ -495,7 +494,7 @@ bool mp_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags) { void mp_get_buffer_raise(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags) { if (!mp_get_buffer(obj, bufinfo, flags)) { - mp_raise_msg(&mp_type_TypeError, "object with buffer protocol required"); + mp_raise_TypeError("object with buffer protocol required"); } } diff --git a/py/objarray.c b/py/objarray.c index 34acd15b3f..9cac5a303c 100644 --- a/py/objarray.c +++ b/py/objarray.c @@ -95,7 +95,7 @@ STATIC void array_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t STATIC mp_obj_array_t *array_new(char typecode, mp_uint_t n) { int typecode_size = mp_binary_get_size('@', typecode, NULL); if (typecode_size == 0) { - mp_raise_msg(&mp_type_ValueError, "bad typecode"); + mp_raise_ValueError("bad typecode"); } mp_obj_array_t *o = m_new_obj(mp_obj_array_t); #if MICROPY_PY_BUILTINS_BYTEARRAY && MICROPY_PY_ARRAY @@ -292,7 +292,7 @@ STATIC mp_obj_t array_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) // Otherwise, can only look for a scalar numeric value in an array if (MP_OBJ_IS_INT(rhs_in) || mp_obj_is_float(rhs_in)) { - mp_not_implemented(""); + mp_raise_NotImplementError(""); } return mp_const_false; @@ -382,7 +382,7 @@ STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value } else if (MP_OBJ_IS_TYPE(index_in, &mp_type_slice)) { mp_bound_slice_t slice; if (!mp_seq_get_fast_slice_indexes(o->len, index_in, &slice)) { - mp_not_implemented("only slices with step=1 (aka None) are supported"); + mp_raise_NotImplementError("only slices with step=1 (aka None) are supported"); } if (value != MP_OBJ_SENTINEL) { #if MICROPY_PY_ARRAY_SLICE_ASSIGN @@ -395,7 +395,7 @@ STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value mp_obj_array_t *src_slice = MP_OBJ_TO_PTR(value); if (item_sz != mp_binary_get_size('@', src_slice->typecode & TYPECODE_MASK, NULL)) { compat_error: - mp_raise_msg(&mp_type_ValueError, "lhs and rhs should be compatible"); + mp_raise_ValueError("lhs and rhs should be compatible"); } src_len = src_slice->len; src_items = src_slice->items; @@ -413,7 +413,7 @@ STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value src_len = bufinfo.len; src_items = bufinfo.buf; } else { - mp_not_implemented("array/bytes required on right side"); + mp_raise_NotImplementError("array/bytes required on right side"); } // TODO: check src/dst compat diff --git a/py/objcomplex.c b/py/objcomplex.c index b302b1d56d..c1f6400d91 100644 --- a/py/objcomplex.c +++ b/py/objcomplex.c @@ -194,7 +194,7 @@ mp_obj_t mp_obj_complex_binary_op(mp_uint_t op, mp_float_t lhs_real, mp_float_t } case MP_BINARY_OP_FLOOR_DIVIDE: case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE: - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "can't do truncated division of a complex number")); + mp_raise_TypeError("can't do truncated division of a complex number"); case MP_BINARY_OP_TRUE_DIVIDE: case MP_BINARY_OP_INPLACE_TRUE_DIVIDE: diff --git a/py/objdict.c b/py/objdict.c index 4942d37791..1c4a52efca 100644 --- a/py/objdict.c +++ b/py/objdict.c @@ -385,7 +385,7 @@ STATIC mp_obj_t dict_update(size_t n_args, const mp_obj_t *args, mp_map_t *kwarg if (key == MP_OBJ_STOP_ITERATION || value == MP_OBJ_STOP_ITERATION || stop != MP_OBJ_STOP_ITERATION) { - mp_raise_msg(&mp_type_ValueError, "dictionary update sequence has the wrong length"); + mp_raise_ValueError("dictionary update sequence has the wrong length"); } else { mp_map_lookup(&self->map, key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value; } diff --git a/py/objgenerator.c b/py/objgenerator.c index cbef9fea3d..8f4a16669c 100644 --- a/py/objgenerator.c +++ b/py/objgenerator.c @@ -105,7 +105,7 @@ mp_vm_return_kind_t mp_obj_gen_resume(mp_obj_t self_in, mp_obj_t send_value, mp_ } if (self->code_state.sp == self->code_state.state - 1) { if (send_value != mp_const_none) { - mp_raise_msg(&mp_type_TypeError, "can't send non-None value to a just-started generator"); + mp_raise_TypeError("can't send non-None value to a just-started generator"); } } else { *self->code_state.sp = send_value; @@ -157,7 +157,7 @@ STATIC mp_obj_t gen_resume_and_raise(mp_obj_t self_in, mp_obj_t send_value, mp_o case MP_VM_RETURN_YIELD: if (throw_value != MP_OBJ_NULL && mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(mp_obj_get_type(throw_value)), MP_OBJ_FROM_PTR(&mp_type_GeneratorExit))) { - mp_raise_msg(&mp_type_RuntimeError, "generator ignored GeneratorExit"); + mp_raise_RuntimeError("generator ignored GeneratorExit"); } return ret; @@ -209,7 +209,7 @@ STATIC mp_obj_t gen_instance_close(mp_obj_t self_in) { mp_obj_t ret; switch (mp_obj_gen_resume(self_in, mp_const_none, MP_OBJ_FROM_PTR(&mp_const_GeneratorExit_obj), &ret)) { case MP_VM_RETURN_YIELD: - mp_raise_msg(&mp_type_RuntimeError, "generator ignored GeneratorExit"); + mp_raise_RuntimeError("generator ignored GeneratorExit"); // Swallow StopIteration & GeneratorExit (== successful close), and re-raise any other case MP_VM_RETURN_EXCEPTION: diff --git a/py/objint.c b/py/objint.c index 5842a00a4d..edb2ecd873 100644 --- a/py/objint.c +++ b/py/objint.c @@ -322,15 +322,15 @@ mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value) { mp_obj_t mp_obj_new_int_from_float(mp_float_t val) { int cl = fpclassify(val); if (cl == FP_INFINITE) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OverflowError, "can't convert inf to int")); + mp_raise_msg(&mp_type_OverflowError, "can't convert inf to int"); } else if (cl == FP_NAN) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "can't convert NaN to int")); + mp_raise_ValueError("can't convert NaN to int"); } else { mp_fp_as_int_class_t icl = mp_classify_fp_as_int(val); if (icl == MP_FP_CLASS_FIT_SMALLINT) { return MP_OBJ_NEW_SMALL_INT((mp_int_t)val); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "float too big")); + mp_raise_ValueError("float too big"); } } } @@ -380,7 +380,7 @@ STATIC mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *args) { (void)n_args; if (args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little)) { - mp_not_implemented(""); + mp_raise_NotImplementError(""); } // get the buffer info @@ -405,7 +405,7 @@ STATIC mp_obj_t int_to_bytes(size_t n_args, const mp_obj_t *args) { (void)n_args; if (args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little)) { - mp_not_implemented(""); + mp_raise_NotImplementError(""); } mp_uint_t len = MP_OBJ_SMALL_INT_VALUE(args[1]); diff --git a/py/objint_longlong.c b/py/objint_longlong.c index f5b5d9c939..fadbc10fb4 100644 --- a/py/objint_longlong.c +++ b/py/objint_longlong.c @@ -241,7 +241,7 @@ mp_obj_t mp_obj_new_int_from_ll(long long val) { mp_obj_t mp_obj_new_int_from_ull(unsigned long long val) { // TODO raise an exception if the unsigned long long won't fit if (val >> (sizeof(unsigned long long) * 8 - 1) != 0) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OverflowError, "ulonglong too large")); + mp_raise_msg(&mp_type_OverflowError, "ulonglong too large")); } mp_obj_int_t *o = m_new_obj(mp_obj_int_t); o->base.type = &mp_type_int; @@ -253,9 +253,9 @@ mp_obj_t mp_obj_new_int_from_ull(unsigned long long val) { mp_obj_t mp_obj_new_int_from_float(mp_float_t val) { int cl = fpclassify(val); if (cl == FP_INFINITE) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OverflowError, "can't convert inf to int")); + mp_raise_msg(&mp_type_OverflowError, "can't convert inf to int")); } else if (cl == FP_NAN) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "can't convert NaN to int")); + mp_raise_ValueError("can't convert NaN to int"); } else { mp_fp_as_int_class_t icl = mp_classify_fp_as_int(val); if (icl == MP_FP_CLASS_FIT_SMALLINT) { @@ -263,7 +263,7 @@ mp_obj_t mp_obj_new_int_from_float(mp_float_t val) { } else if (icl == MP_FP_CLASS_FIT_LONGINT) { return mp_obj_new_int_from_ll((long long)val); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "float too big")); + mp_raise_ValueError("float too big"); } } } diff --git a/py/objint_mpz.c b/py/objint_mpz.c index eadf64fce7..25bdfda717 100644 --- a/py/objint_mpz.c +++ b/py/objint_mpz.c @@ -271,7 +271,7 @@ mp_obj_t mp_obj_int_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) { case MP_BINARY_OP_INPLACE_RSHIFT: { mp_int_t irhs = mp_obj_int_get_checked(rhs_in); if (irhs < 0) { - mp_raise_msg(&mp_type_ValueError, "negative shift count"); + mp_raise_ValueError("negative shift count"); } if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) { mpz_shl_inpl(&res->mpz, zlhs, irhs); @@ -354,9 +354,9 @@ mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value) { mp_obj_t mp_obj_new_int_from_float(mp_float_t val) { int cl = fpclassify(val); if (cl == FP_INFINITE) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OverflowError, "can't convert inf to int")); + mp_raise_msg(&mp_type_OverflowError, "can't convert inf to int"); } else if (cl == FP_NAN) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "can't convert NaN to int")); + mp_raise_ValueError("can't convert NaN to int"); } else { mp_fp_as_int_class_t icl = mp_classify_fp_as_int(val); if (icl == MP_FP_CLASS_FIT_SMALLINT) { diff --git a/py/objlist.c b/py/objlist.c index 6d4a20a507..d25ea4ac3e 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -156,7 +156,7 @@ STATIC mp_obj_t list_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) { mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in); mp_bound_slice_t slice; if (!mp_seq_get_fast_slice_indexes(self->len, index, &slice)) { - mp_not_implemented(""); + mp_raise_NotImplementError(""); } mp_int_t len_adj = slice.start - slice.stop; @@ -196,7 +196,7 @@ STATIC mp_obj_t list_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) { mp_obj_list_t *slice = MP_OBJ_TO_PTR(value); mp_bound_slice_t slice_out; if (!mp_seq_get_fast_slice_indexes(self->len, index, &slice_out)) { - mp_not_implemented(""); + mp_raise_NotImplementError(""); } mp_int_t len_adj = slice->len - (slice_out.stop - slice_out.start); //printf("Len adj: %d\n", len_adj); @@ -266,7 +266,7 @@ STATIC mp_obj_t list_pop(size_t n_args, const mp_obj_t *args) { mp_check_self(MP_OBJ_IS_TYPE(args[0], &mp_type_list)); mp_obj_list_t *self = MP_OBJ_TO_PTR(args[0]); if (self->len == 0) { - mp_raise_msg(&mp_type_IndexError, "pop from empty list"); + mp_raise_IndexError("pop from empty list"); } mp_uint_t index = mp_get_index(self->base.type, self->len, n_args == 1 ? MP_OBJ_NEW_SMALL_INT(-1) : args[1], false); mp_obj_t ret = self->items[index]; diff --git a/py/objnamedtuple.c b/py/objnamedtuple.c index 6f50d6cf96..19fd3c5dbc 100644 --- a/py/objnamedtuple.c +++ b/py/objnamedtuple.c @@ -64,7 +64,7 @@ void namedtuple_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { } else { // delete/store attribute // provide more detailed error message than we'd get by just returning - mp_raise_msg(&mp_type_AttributeError, "can't set attribute"); + mp_raise_AttributeError("can't set attribute"); } } @@ -75,13 +75,13 @@ mp_obj_t namedtuple_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { mp_arg_error_terse_mismatch(); } else if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NORMAL) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, + mp_raise_TypeError_varg( "function takes %d positional arguments but %d were given", - num_fields, n_args + n_kw)); + num_fields, n_args + n_kw); } else if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, + mp_raise_TypeError_varg( "%q() takes %d positional arguments but %d were given", - type->base.name, num_fields, n_args + n_kw)); + type->base.name, num_fields, n_args + n_kw); } } @@ -104,16 +104,16 @@ mp_obj_t namedtuple_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { mp_arg_error_terse_mismatch(); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, - "unexpected keyword argument '%q'", kw)); + mp_raise_TypeError_varg( + "unexpected keyword argument '%q'", kw); } } if (arg_objects[id] != MP_OBJ_NULL) { if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { mp_arg_error_terse_mismatch(); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, - "function got multiple values for argument '%q'", kw)); + mp_raise_TypeError_varg( + "function got multiple values for argument '%q'", kw); } } arg_objects[id] = args[i + 1]; diff --git a/py/objobject.c b/py/objobject.c index b33dc491c4..f9a7d17c34 100644 --- a/py/objobject.c +++ b/py/objobject.c @@ -50,7 +50,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(object___init___obj, object___init__); STATIC mp_obj_t object___new__(mp_obj_t cls) { if (!MP_OBJ_IS_TYPE(cls, &mp_type_type) || !mp_obj_is_instance_type((mp_obj_type_t*)MP_OBJ_TO_PTR(cls))) { - mp_raise_msg(&mp_type_TypeError, "__new__ arg must be a user-type"); + mp_raise_TypeError("__new__ arg must be a user-type"); } mp_obj_t o = MP_OBJ_SENTINEL; mp_obj_t res = mp_obj_instance_make_new(MP_OBJ_TO_PTR(cls), 1, 0, &o); diff --git a/py/objset.c b/py/objset.c index fc124fcd8c..97fce61d60 100644 --- a/py/objset.c +++ b/py/objset.c @@ -68,7 +68,7 @@ STATIC void check_set(mp_obj_t o) { if (MP_OBJ_IS_TYPE(o, &mp_type_frozenset)) { // Mutable method called on frozenset; emulate CPython behavior, eg: // AttributeError: 'frozenset' object has no attribute 'add' - mp_raise_msg(&mp_type_AttributeError, "'frozenset' has no such attribute"); + mp_raise_AttributeError("'frozenset' has no such attribute"); } #endif mp_check_self(MP_OBJ_IS_TYPE(o, &mp_type_set)); diff --git a/py/objstr.c b/py/objstr.c index f082e95591..c34b756e51 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -400,7 +400,7 @@ STATIC mp_obj_t bytes_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) { if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) { mp_bound_slice_t slice; if (!mp_seq_get_fast_slice_indexes(self_len, index, &slice)) { - mp_not_implemented("only slices with step=1 (aka None) are supported"); + mp_raise_NotImplementError("only slices with step=1 (aka None) are supported"); } return mp_obj_new_str_of_type(type, self_data + slice.start, slice.stop - slice.start); } @@ -609,7 +609,7 @@ STATIC mp_obj_t str_rsplit(size_t n_args, const mp_obj_t *args) { mp_int_t idx = splits; if (sep == mp_const_none) { - mp_not_implemented("rsplit(None,n)"); + mp_raise_NotImplementError("rsplit(None,n)"); } else { mp_uint_t sep_len; const char *sep_str = mp_obj_str_get_data(sep, &sep_len); @@ -727,7 +727,7 @@ STATIC mp_obj_t str_endswith(size_t n_args, const mp_obj_t *args) { GET_STR_DATA_LEN(args[0], str, str_len); GET_STR_DATA_LEN(args[1], suffix, suffix_len); if (n_args > 2) { - mp_not_implemented("start/end indices"); + mp_raise_NotImplementError("start/end indices"); } if (suffix_len > str_len) { @@ -950,8 +950,7 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar mp_raise_ValueError( "end of format while looking for conversion specifier"); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, - "unknown conversion specifier %c", *str)); + mp_raise_ValueError_varg("unknown conversion specifier %c", *str); } } } @@ -1008,7 +1007,7 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar } field_name = str_to_int(field_name, field_name_top, &index); if ((uint)index >= n_args - 1) { - mp_raise_msg(&mp_type_IndexError, "tuple index out of range"); + mp_raise_IndexError("tuple index out of range"); } arg = args[index + 1]; *arg_i = -1; @@ -1024,7 +1023,7 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar arg = key_elem->value; } if (field_name < field_name_top) { - mp_not_implemented("attributes not supported yet"); + mp_raise_NotImplementError("attributes not supported yet"); } } else { if (*arg_i < 0) { @@ -1036,7 +1035,7 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar } } if ((uint)*arg_i >= n_args - 1) { - mp_raise_msg(&mp_type_IndexError, "tuple index out of range"); + mp_raise_IndexError("tuple index out of range"); } arg = args[(*arg_i) + 1]; (*arg_i)++; @@ -1214,9 +1213,9 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { terse_str_format_value_error(); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, + mp_raise_ValueError_varg( "unknown format code '%c' for object of type '%s'", - type, mp_obj_get_type_str(arg))); + type, mp_obj_get_type_str(arg)); } } } @@ -1286,9 +1285,9 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { terse_str_format_value_error(); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, + mp_raise_ValueError_varg( "unknown format code '%c' for object of type 'float'", - type, mp_obj_get_type_str(arg))); + type, mp_obj_get_type_str(arg)); } } } else { @@ -1322,9 +1321,9 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { terse_str_format_value_error(); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, + mp_raise_ValueError_varg( "unknown format code '%c' for object of type 'str'", - type, mp_obj_get_type_str(arg))); + type, mp_obj_get_type_str(arg)); } } } @@ -1520,9 +1519,9 @@ not_enough_args: if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { terse_str_format_value_error(); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, + mp_raise_ValueError_varg( "unsupported format character '%c' (0x%x) at index %d", - *str, *str, str - start_str)); + *str, *str, str - start_str); } } } @@ -2061,9 +2060,9 @@ STATIC void bad_implicit_conversion(mp_obj_t self_in) { if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { mp_raise_TypeError("can't convert to str implicitly"); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, + mp_raise_TypeError_varg( "can't convert '%s' object to str implicitly", - mp_obj_get_type_str(self_in))); + mp_obj_get_type_str(self_in)); } } diff --git a/py/objstringio.c b/py/objstringio.c index a430fca3b7..feeb3ab929 100644 --- a/py/objstringio.c +++ b/py/objstringio.c @@ -39,7 +39,7 @@ #if MICROPY_CPYTHON_COMPAT STATIC void check_stringio_is_open(const mp_obj_stringio_t *o) { if (o->vstr == NULL) { - mp_raise_msg(&mp_type_ValueError, "I/O operation on closed file"); + mp_raise_ValueError("I/O operation on closed file"); } } #else diff --git a/py/objstrunicode.c b/py/objstrunicode.c index 8444a26892..b12c68e61f 100644 --- a/py/objstrunicode.c +++ b/py/objstrunicode.c @@ -131,7 +131,7 @@ const byte *str_index_to_ptr(const mp_obj_type_t *type, const byte *self_data, s if (MP_OBJ_IS_SMALL_INT(index)) { i = MP_OBJ_SMALL_INT_VALUE(index); } else if (!mp_obj_get_int_maybe(index, &i)) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "string indices must be integers, not %s", mp_obj_get_type_str(index))); + mp_raise_TypeError_varg("string indices must be integers, not %s", mp_obj_get_type_str(index)); } const byte *s, *top = self_data + self_len; if (i < 0) @@ -142,7 +142,7 @@ const byte *str_index_to_ptr(const mp_obj_type_t *type, const byte *self_data, s if (is_slice) { return self_data; } - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_IndexError, "string index out of range")); + mp_raise_IndexError("string index out of range"); } if (!UTF8_IS_CONT(*s)) { ++i; @@ -161,7 +161,7 @@ const byte *str_index_to_ptr(const mp_obj_type_t *type, const byte *self_data, s if (is_slice) { return top; } - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_IndexError, "string index out of range")); + mp_raise_IndexError("string index out of range"); } // Then check completion if (i-- == 0) { @@ -188,7 +188,7 @@ STATIC mp_obj_t str_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) { mp_obj_t ostart, ostop, ostep; mp_obj_slice_get(index, &ostart, &ostop, &ostep); if (ostep != mp_const_none && ostep != MP_OBJ_NEW_SMALL_INT(1)) { - mp_not_implemented("only slices with step=1 (aka None) are supported"); + mp_raise_NotImplementError("only slices with step=1 (aka None) are supported"); } const byte *pstart, *pstop; diff --git a/py/objtuple.c b/py/objtuple.c index c547da9406..409f051abe 100644 --- a/py/objtuple.c +++ b/py/objtuple.c @@ -181,7 +181,7 @@ mp_obj_t mp_obj_tuple_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) { if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) { mp_bound_slice_t slice; if (!mp_seq_get_fast_slice_indexes(self->len, index, &slice)) { - mp_not_implemented("only slices with step=1 (aka None) are supported"); + mp_raise_NotImplementError("only slices with step=1 (aka None) are supported"); } mp_obj_tuple_t *res = MP_OBJ_TO_PTR(mp_obj_new_tuple(slice.stop - slice.start, NULL)); mp_seq_copy(res->items, self->items + slice.start, res->len, mp_obj_t); diff --git a/py/objtype.c b/py/objtype.c index 581f246435..c107c83c99 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -314,10 +314,10 @@ mp_obj_t mp_obj_instance_make_new(const mp_obj_type_t *self, size_t n_args, size } if (init_ret != mp_const_none) { if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { - mp_raise_msg(&mp_type_TypeError, "__init__() should return None"); + mp_raise_TypeError("__init__() should return None"); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, - "__init__() should return None, not '%s'", mp_obj_get_type_str(init_ret))); + mp_raise_TypeError_varg("__init__() should return None, not '%s'", + mp_obj_get_type_str(init_ret)); } } @@ -510,7 +510,7 @@ STATIC void mp_obj_instance_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *des // the code. const mp_obj_t *proxy = mp_obj_property_get(member); if (proxy[0] == mp_const_none) { - mp_raise_msg(&mp_type_AttributeError, "unreadable attribute"); + mp_raise_AttributeError("unreadable attribute"); } else { dest[0] = mp_call_function_n_kw(proxy[0], 1, 0, &self_in); } @@ -713,10 +713,10 @@ mp_obj_t mp_obj_instance_call(mp_obj_t self_in, size_t n_args, size_t n_kw, cons mp_obj_t call = mp_obj_instance_get_call(self_in, member); if (call == MP_OBJ_NULL) { if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { - mp_raise_msg(&mp_type_TypeError, "object not callable"); + mp_raise_TypeError("object not callable"); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, - "'%s' object is not callable", mp_obj_get_type_str(self_in))); + mp_raise_TypeError_varg("'%s' object is not callable", + mp_obj_get_type_str(self_in)); } } mp_obj_instance_t *self = MP_OBJ_TO_PTR(self_in); @@ -795,7 +795,7 @@ STATIC mp_obj_t type_make_new(const mp_obj_type_t *type_in, size_t n_args, size_ return mp_obj_new_type(mp_obj_str_get_qstr(args[0]), args[1], args[2]); default: - mp_raise_msg(&mp_type_TypeError, "type takes 1 or 3 arguments"); + mp_raise_TypeError("type takes 1 or 3 arguments"); } } @@ -806,10 +806,9 @@ STATIC mp_obj_t type_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp if (self->make_new == NULL) { if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { - mp_raise_msg(&mp_type_TypeError, "cannot create instance"); + mp_raise_TypeError("cannot create instance"); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, - "cannot create '%q' instances", self->name)); + mp_raise_TypeError_varg("cannot create '%q' instances", self->name); } } @@ -894,10 +893,10 @@ mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict) // TODO: Verify with CPy, tested on function type if (t->make_new == NULL) { if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { - mp_raise_msg(&mp_type_TypeError, "type is not an acceptable base type"); + mp_raise_TypeError("type is not an acceptable base type"); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, - "type '%q' is not an acceptable base type", t->name)); + mp_raise_TypeError_varg( + "type '%q' is not an acceptable base type", t->name); } } } @@ -928,7 +927,7 @@ mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict) const mp_obj_type_t *native_base; uint num_native_bases = instance_count_native_bases(o, &native_base); if (num_native_bases > 1) { - mp_raise_msg(&mp_type_TypeError, "multiple bases have instance lay-out conflict"); + mp_raise_TypeError("multiple bases have instance lay-out conflict"); } mp_map_t *locals_map = &o->locals_dict->map; @@ -1075,7 +1074,7 @@ STATIC mp_obj_t mp_obj_is_subclass(mp_obj_t object, mp_obj_t classinfo) { } else if (MP_OBJ_IS_TYPE(classinfo, &mp_type_tuple)) { mp_obj_tuple_get(classinfo, &len, &items); } else { - mp_raise_msg(&mp_type_TypeError, "issubclass() arg 2 must be a class or a tuple of classes"); + mp_raise_TypeError("issubclass() arg 2 must be a class or a tuple of classes"); } for (uint i = 0; i < len; i++) { @@ -1089,7 +1088,7 @@ STATIC mp_obj_t mp_obj_is_subclass(mp_obj_t object, mp_obj_t classinfo) { STATIC mp_obj_t mp_builtin_issubclass(mp_obj_t object, mp_obj_t classinfo) { if (!MP_OBJ_IS_TYPE(object, &mp_type_type)) { - mp_raise_msg(&mp_type_TypeError, "issubclass() arg 1 must be a class"); + mp_raise_TypeError("issubclass() arg 1 must be a class"); } return mp_obj_is_subclass(object, classinfo); } diff --git a/py/parsenum.c b/py/parsenum.c index f3e1aad127..b47297877f 100644 --- a/py/parsenum.c +++ b/py/parsenum.c @@ -55,7 +55,7 @@ mp_obj_t mp_parse_num_integer(const char *restrict str_, size_t len, int base, m // check radix base if ((base != 0 && base < 2) || base > 36) { // this won't be reached if lex!=NULL - mp_raise_msg(&mp_type_ValueError, "int() arg 2 must be >= 2 and <= 36"); + mp_raise_ValueError("int() arg 2 must be >= 2 and <= 36"); } // skip leading space diff --git a/py/runtime.c b/py/runtime.c index c407a58a45..5ebee10104 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -24,6 +24,7 @@ * THE SOFTWARE. */ +#include #include #include #include @@ -245,11 +246,11 @@ mp_obj_t mp_unary_op(mp_uint_t op, mp_obj_t arg) { } } if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { - mp_raise_msg(&mp_type_TypeError, "unsupported type for operator"); + mp_raise_TypeError("unsupported type for operator"); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, + mp_raise_TypeError_varg( "unsupported type for %q: '%s'", - mp_unary_op_method_name[op], mp_obj_get_type_str(arg))); + mp_unary_op_method_name[op], mp_obj_get_type_str(arg)); } } } @@ -337,7 +338,7 @@ mp_obj_t mp_binary_op(mp_uint_t op, mp_obj_t lhs, mp_obj_t rhs) { case MP_BINARY_OP_INPLACE_LSHIFT: { if (rhs_val < 0) { // negative shift not allowed - mp_raise_msg(&mp_type_ValueError, "negative shift count"); + mp_raise_ValueError("negative shift count"); } else if (rhs_val >= (mp_int_t)BITS_PER_WORD || lhs_val > (MP_SMALL_INT_MAX >> rhs_val) || lhs_val < (MP_SMALL_INT_MIN >> rhs_val)) { // left-shift will overflow, so use higher precision integer lhs = mp_obj_new_int_from_ll(lhs_val); @@ -352,7 +353,7 @@ mp_obj_t mp_binary_op(mp_uint_t op, mp_obj_t lhs, mp_obj_t rhs) { case MP_BINARY_OP_INPLACE_RSHIFT: if (rhs_val < 0) { // negative shift not allowed - mp_raise_msg(&mp_type_ValueError, "negative shift count"); + mp_raise_ValueError("negative shift count"); } else { // standard precision is enough for right-shift if (rhs_val >= (mp_int_t)BITS_PER_WORD) { @@ -428,7 +429,7 @@ mp_obj_t mp_binary_op(mp_uint_t op, mp_obj_t lhs, mp_obj_t rhs) { lhs = mp_obj_new_float(lhs_val); goto generic_binary_op; #else - mp_raise_msg(&mp_type_ValueError, "negative power with no float support"); + mp_raise_ValueError("negative power with no float support"); #endif } else { mp_int_t ans = 1; @@ -529,10 +530,10 @@ mp_obj_t mp_binary_op(mp_uint_t op, mp_obj_t lhs, mp_obj_t rhs) { } if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { - mp_raise_msg(&mp_type_TypeError, "object not iterable"); + mp_raise_TypeError("object not iterable"); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, - "'%s' object is not iterable", mp_obj_get_type_str(rhs))); + mp_raise_TypeError_varg( + "'%s' object is not iterable", mp_obj_get_type_str(rhs)); } } @@ -551,11 +552,11 @@ generic_binary_op: unsupported_op: if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { - mp_raise_msg(&mp_type_TypeError, "unsupported type for operator"); + mp_raise_TypeError("unsupported type for operator"); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, + mp_raise_TypeError_varg( "unsupported types for %q: '%s', '%s'", - mp_binary_op_method_name[op], mp_obj_get_type_str(lhs), mp_obj_get_type_str(rhs))); + mp_binary_op_method_name[op], mp_obj_get_type_str(lhs), mp_obj_get_type_str(rhs)); } zero_division: @@ -593,10 +594,9 @@ mp_obj_t mp_call_function_n_kw(mp_obj_t fun_in, mp_uint_t n_args, mp_uint_t n_kw } if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { - mp_raise_msg(&mp_type_TypeError, "object not callable"); + mp_raise_TypeError("object not callable"); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, - "'%s' object is not callable", mp_obj_get_type_str(fun_in))); + mp_raise_TypeError_varg("'%s' object is not callable", mp_obj_get_type_str(fun_in)); } } @@ -824,17 +824,17 @@ void mp_unpack_sequence(mp_obj_t seq_in, mp_uint_t num, mp_obj_t *items) { too_short: if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { - mp_raise_msg(&mp_type_ValueError, "wrong number of values to unpack"); + mp_raise_ValueError("wrong number of values to unpack"); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, - "need more than %d values to unpack", (int)seq_len)); + mp_raise_ValueError_varg("need more than %d values to unpack", + (int)seq_len); } too_long: if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { - mp_raise_msg(&mp_type_ValueError, "wrong number of values to unpack"); + mp_raise_ValueError("wrong number of values to unpack"); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, - "too many values to unpack (expected %d)", (int)num)); + mp_raise_ValueError_varg("too many values to unpack (expected %d)", + (int)num); } } @@ -897,10 +897,10 @@ void mp_unpack_ex(mp_obj_t seq_in, mp_uint_t num_in, mp_obj_t *items) { too_short: if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { - mp_raise_msg(&mp_type_ValueError, "wrong number of values to unpack"); + mp_raise_ValueError("wrong number of values to unpack"); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, - "need more than %d values to unpack", (int)seq_len)); + mp_raise_ValueError_varg("need more than %d values to unpack", + (int)seq_len); } } @@ -936,10 +936,10 @@ STATIC mp_obj_t checked_fun_call(mp_obj_t self_in, size_t n_args, size_t n_kw, c const mp_obj_type_t *arg0_type = mp_obj_get_type(args[0]); if (arg0_type != self->type) { if (MICROPY_ERROR_REPORTING != MICROPY_ERROR_REPORTING_DETAILED) { - mp_raise_msg(&mp_type_TypeError, "argument has wrong type"); + mp_raise_TypeError("argument has wrong type"); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, - "argument should be a '%q' not a '%q'", self->type->name, arg0_type->name)); + mp_raise_TypeError_varg("argument should be a '%q' not a '%q'", + self->type->name, arg0_type->name); } } } @@ -1016,7 +1016,7 @@ void mp_convert_member_lookup(mp_obj_t self, const mp_obj_type_t *type, mp_obj_t // the code. const mp_obj_t *proxy = mp_obj_property_get(member); if (proxy[0] == mp_const_none) { - mp_raise_msg(&mp_type_AttributeError, "unreadable attribute"); + mp_raise_AttributeError("unreadable attribute"); } else { dest[0] = mp_call_function_n_kw(proxy[0], 1, 0, &self); } @@ -1074,7 +1074,7 @@ void mp_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest) { if (dest[0] == MP_OBJ_NULL) { // no attribute/method called attr if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { - mp_raise_msg(&mp_type_AttributeError, "no such attribute"); + mp_raise_AttributeError("no such attribute"); } else { // following CPython, we give a more detailed error message for type objects if (MP_OBJ_IS_TYPE(base, &mp_type_type)) { @@ -1131,7 +1131,7 @@ void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) { #endif } if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { - mp_raise_msg(&mp_type_AttributeError, "no such attribute"); + mp_raise_AttributeError("no such attribute"); } else { nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_AttributeError, "'%s' object has no attribute '%q'", @@ -1161,10 +1161,10 @@ mp_obj_t mp_getiter(mp_obj_t o_in) { // object not iterable if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { - mp_raise_msg(&mp_type_TypeError, "object not iterable"); + mp_raise_TypeError("object not iterable"); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, - "'%s' object is not iterable", mp_obj_get_type_str(o_in))); + mp_raise_TypeError_varg( + "'%s' object is not iterable", mp_obj_get_type_str(o_in)); } } @@ -1183,10 +1183,10 @@ mp_obj_t mp_iternext_allow_raise(mp_obj_t o_in) { return mp_call_method_n_kw(0, 0, dest); } else { if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { - mp_raise_msg(&mp_type_TypeError, "object not an iterator"); + mp_raise_TypeError("object not an iterator"); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, - "'%s' object is not an iterator", mp_obj_get_type_str(o_in))); + mp_raise_TypeError_varg("'%s' object is not an iterator", + mp_obj_get_type_str(o_in)); } } } @@ -1219,10 +1219,10 @@ mp_obj_t mp_iternext(mp_obj_t o_in) { } } else { if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { - mp_raise_msg(&mp_type_TypeError, "object not an iterator"); + mp_raise_TypeError("object not an iterator"); } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, - "'%s' object is not an iterator", mp_obj_get_type_str(o_in))); + mp_raise_TypeError_varg("'%s' object is not an iterator", + mp_obj_get_type_str(o_in)); } } } @@ -1354,7 +1354,7 @@ mp_obj_t mp_import_from(mp_obj_t module, qstr name) { if (dest[1] != MP_OBJ_NULL) { // Hopefully we can't import bound method from an object import_error: - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ImportError, "cannot import name %q", name)); + mp_raise_msg_varg(&mp_type_ImportError, "cannot import name %q", name); } if (dest[0] != MP_OBJ_NULL) { @@ -1454,27 +1454,67 @@ void *m_malloc_fail(size_t num_bytes) { mp_raise_msg(&mp_type_MemoryError, "memory allocation failed, heap is locked"); #endif } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_MemoryError, - "memory allocation failed, allocating %u bytes", (uint)num_bytes)); + mp_raise_msg_varg(&mp_type_MemoryError, + "memory allocation failed, allocating %u bytes", (uint)num_bytes); } } NORETURN void mp_raise_msg(const mp_obj_type_t *exc_type, const char *msg) { - nlr_raise(mp_obj_new_exception_msg(exc_type, msg)); + nlr_raise(mp_obj_new_exception_msg_varg(exc_type, 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); + va_end(argptr); + nlr_raise(exception); +} + +NORETURN void mp_raise_AttributeError(const char *msg) { + mp_raise_msg(&mp_type_AttributeError, msg); +} + +NORETURN void mp_raise_RuntimeError(const char *msg) { + mp_raise_msg(&mp_type_RuntimeError, msg); +} + +NORETURN void mp_raise_NotImplementError(const char *msg) { + mp_raise_msg(&mp_type_NotImplementedError, msg); +} + +NORETURN void mp_raise_ImportError(const char *msg) { + mp_raise_msg(&mp_type_ImportError, msg); +} + +NORETURN void mp_raise_IndexError(const char *msg) { + mp_raise_msg(&mp_type_IndexError, msg); } NORETURN void mp_raise_ValueError(const char *msg) { mp_raise_msg(&mp_type_ValueError, 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); + va_end(argptr); + nlr_raise(exception); +} + NORETURN void mp_raise_TypeError(const char *msg) { mp_raise_msg(&mp_type_TypeError, 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); + va_end(argptr); + nlr_raise(exception); +} + NORETURN void mp_raise_OSError(int errno_) { nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(errno_))); } - -NORETURN void mp_not_implemented(const char *msg) { - mp_raise_msg(&mp_type_NotImplementedError, msg); -} diff --git a/py/runtime.h b/py/runtime.h index 3532b838de..3e62a4ac67 100644 --- a/py/runtime.h +++ b/py/runtime.h @@ -135,11 +135,17 @@ mp_obj_t mp_import_from(mp_obj_t module, qstr name); void mp_import_all(mp_obj_t module); NORETURN void mp_raise_msg(const mp_obj_type_t *exc_type, const char *msg); -//NORETURN void nlr_raise_msg_varg(const mp_obj_type_t *exc_type, const char *fmt, ...); +NORETURN void mp_raise_msg_varg(const mp_obj_type_t *exc_type, const char *fmt, ...); NORETURN void mp_raise_ValueError(const char *msg); +NORETURN void mp_raise_ValueError_varg(const char *fmt, ...); NORETURN void mp_raise_TypeError(const char *msg); +NORETURN void mp_raise_TypeError_varg(const char *fmt, ...); +NORETURN void mp_raise_AttributeError(const char *msg); +NORETURN void mp_raise_RuntimeError(const char *msg); +NORETURN void mp_raise_NotImplementError(const char *msg); +NORETURN void mp_raise_ImportError(const char *msg); +NORETURN void mp_raise_IndexError(const char *msg); NORETURN void mp_raise_OSError(int errno_); -NORETURN void mp_not_implemented(const char *msg); // Raise NotImplementedError with given message NORETURN void mp_exc_recursion_depth(void); #if MICROPY_BUILTIN_METHOD_CHECK_SELF_ARG diff --git a/py/sequence.c b/py/sequence.c index bc2cfc077c..bb34eac4aa 100644 --- a/py/sequence.c +++ b/py/sequence.c @@ -256,7 +256,7 @@ mp_obj_t mp_seq_index_obj(const mp_obj_t *items, mp_uint_t len, mp_uint_t n_args } } - mp_raise_msg(&mp_type_ValueError, "object not in sequence"); + mp_raise_ValueError("object not in sequence"); } mp_obj_t mp_seq_count_obj(const mp_obj_t *items, mp_uint_t len, mp_obj_t value) { diff --git a/py/stackctrl.c b/py/stackctrl.c index 1843e7339e..c3ad7b21a2 100644 --- a/py/stackctrl.c +++ b/py/stackctrl.c @@ -52,8 +52,8 @@ void mp_stack_set_limit(mp_uint_t limit) { } void mp_exc_recursion_depth(void) { - nlr_raise(mp_obj_new_exception_arg1(&mp_type_RuntimeError, - MP_OBJ_NEW_QSTR(MP_QSTR_maximum_space_recursion_space_depth_space_exceeded))); + mp_raise_RuntimeError( + MP_OBJ_NEW_QSTR(MP_QSTR_maximum_space_recursion_space_depth_space_exceeded)); } void mp_stack_check(void) { diff --git a/py/stream.c b/py/stream.c index c915110e0b..4d43099a5e 100644 --- a/py/stream.c +++ b/py/stream.c @@ -142,7 +142,7 @@ STATIC mp_obj_t stream_read_generic(size_t n_args, const mp_obj_t *args, byte fl while (more_bytes > 0) { char *p = vstr_add_len(&vstr, more_bytes); if (p == NULL) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_MemoryError, "out of memory")); + mp_raise_msg(&mp_type_MemoryError, "out of memory"); } int error; mp_uint_t out_sz = mp_stream_read_exactly(args[0], p, more_bytes, &error); @@ -381,7 +381,7 @@ STATIC mp_obj_t stream_unbuffered_readline(size_t n_args, const mp_obj_t *args) while (max_size == -1 || max_size-- != 0) { char *p = vstr_add_len(&vstr, 1); if (p == NULL) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_MemoryError, "out of memory")); + mp_raise_msg(&mp_type_MemoryError, "out of memory"); } int error; diff --git a/shared-bindings/bitbangio/I2C.c b/shared-bindings/bitbangio/I2C.c index 5b772c9bff..e5c9831b5d 100644 --- a/shared-bindings/bitbangio/I2C.c +++ b/shared-bindings/bitbangio/I2C.c @@ -100,7 +100,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bitbangio_i2c_obj___exit___obj, 4, 4, static void check_lock(bitbangio_i2c_obj_t *self) { if (!shared_module_bitbangio_i2c_has_lock(self)) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Function requires I2C lock.")); + mp_raise_RuntimeError("Function requires lock"); } } diff --git a/shared-bindings/bitbangio/SPI.c b/shared-bindings/bitbangio/SPI.c index a5d3f358f2..83eb1b204e 100644 --- a/shared-bindings/bitbangio/SPI.c +++ b/shared-bindings/bitbangio/SPI.c @@ -33,6 +33,7 @@ #include "shared-bindings/microcontroller/Pin.h" #include "lib/utils/context_manager_helpers.h" +#include "py/mperrno.h" #include "py/runtime.h" //| .. currentmodule:: bitbangio @@ -113,7 +114,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bitbangio_spi_obj___exit___obj, 4, 4, static void check_lock(bitbangio_spi_obj_t *self) { if (!shared_module_bitbangio_spi_has_lock(self)) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Function requires SPI lock.")); + mp_raise_RuntimeError("Function requires lock"); } } @@ -136,15 +137,15 @@ STATIC mp_obj_t bitbangio_spi_configure(size_t n_args, const mp_obj_t *pos_args, uint8_t polarity = args[ARG_polarity].u_int; if (polarity != 0 && polarity != 1) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid polarity.")); + mp_raise_ValueError("Invalid polarity"); } uint8_t phase = args[ARG_phase].u_int; if (phase != 0 && phase != 1) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid phase.")); + mp_raise_ValueError("Invalid phase"); } uint8_t bits = args[ARG_bits].u_int; if (bits != 8 && bits != 9) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid number of bits.")); + mp_raise_ValueError("Invalid number of bits"); } shared_module_bitbangio_spi_configure(self, args[ARG_baudrate].u_int, polarity, phase, bits); @@ -183,7 +184,7 @@ STATIC mp_obj_t bitbangio_spi_write(mp_obj_t self_in, mp_obj_t wr_buf) { check_lock(self); bool ok = shared_module_bitbangio_spi_write(self, src.buf, src.len); if (!ok) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "SPI bus error")); + mp_raise_OSError(MP_EIO); } return mp_const_none; } @@ -200,7 +201,7 @@ STATIC mp_obj_t bitbangio_spi_readinto(size_t n_args, const mp_obj_t *args) { check_lock(args[0]); bool ok = shared_module_bitbangio_spi_read(args[0], bufinfo.buf, bufinfo.len); if (!ok) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "SPI bus error")); + mp_raise_OSError(MP_EIO); } return mp_const_none; } diff --git a/shared-bindings/nativeio/AnalogOut.c b/shared-bindings/nativeio/AnalogOut.c index 950faabb59..a6996ba18e 100644 --- a/shared-bindings/nativeio/AnalogOut.c +++ b/shared-bindings/nativeio/AnalogOut.c @@ -113,8 +113,7 @@ STATIC mp_obj_t nativeio_analogout_obj_set_value(mp_obj_t self_in, mp_obj_t valu nativeio_analogout_obj_t *self = MP_OBJ_TO_PTR(self_in); uint32_t v = mp_obj_get_int(value); if (v >= (1 << 16)) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, - "AnalogOut is only 16 bits. Value must be less than 65536.")); + mp_raise_ValueError("AnalogOut is only 16 bits. Value must be less than 65536."); } common_hal_nativeio_analogout_set_value(self, v); return mp_const_none; diff --git a/shared-bindings/nativeio/DigitalInOut.c b/shared-bindings/nativeio/DigitalInOut.c index b45086be1c..aa62f6a5fd 100644 --- a/shared-bindings/nativeio/DigitalInOut.c +++ b/shared-bindings/nativeio/DigitalInOut.c @@ -220,8 +220,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(nativeio_digitalinout_get_value_obj, nativeio_digitali STATIC mp_obj_t nativeio_digitalinout_obj_set_value(mp_obj_t self_in, mp_obj_t value) { nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); if (common_hal_nativeio_digitalinout_get_direction(self) == DIRECTION_IN) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, - "Cannot set value when direction is input.")); + mp_raise_AttributeError("Cannot set value when direction is input."); return mp_const_none; } common_hal_nativeio_digitalinout_set_value(self, mp_obj_is_true(value)); @@ -243,8 +242,7 @@ mp_obj_property_t nativeio_digitalinout_value_obj = { STATIC mp_obj_t nativeio_digitalinout_obj_get_drive_mode(mp_obj_t self_in) { nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); if (common_hal_nativeio_digitalinout_get_direction(self) == DIRECTION_IN) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, - "Drive mode not used when direction is input.")); + mp_raise_AttributeError("Drive mode not used when direction is input."); return mp_const_none; } enum digitalinout_drive_mode_t drive_mode = common_hal_nativeio_digitalinout_get_drive_mode(self); @@ -258,8 +256,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(nativeio_digitalinout_get_drive_mode_obj, nativeio_dig STATIC mp_obj_t nativeio_digitalinout_obj_set_drive_mode(mp_obj_t self_in, mp_obj_t drive_mode) { nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); if (common_hal_nativeio_digitalinout_get_direction(self) == DIRECTION_IN) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, - "Drive mode not used when direction is input.")); + mp_raise_AttributeError("Drive mode not used when direction is input."); return mp_const_none; } enum digitalinout_drive_mode_t c_drive_mode = DRIVE_MODE_PUSH_PULL; @@ -287,8 +284,7 @@ mp_obj_property_t nativeio_digitalinout_drive_mode_obj = { STATIC mp_obj_t nativeio_digitalinout_obj_get_pull(mp_obj_t self_in) { nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); if (common_hal_nativeio_digitalinout_get_direction(self) == DIRECTION_OUT) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, - "Pull not used when direction is output.")); + mp_raise_AttributeError("Pull not used when direction is output."); return mp_const_none; } enum digitalinout_pull_t pull = common_hal_nativeio_digitalinout_get_pull(self); @@ -304,8 +300,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(nativeio_digitalinout_get_pull_obj, nativeio_digitalin STATIC mp_obj_t nativeio_digitalinout_obj_set_pull(mp_obj_t self_in, mp_obj_t pull_obj) { nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); if (common_hal_nativeio_digitalinout_get_direction(self) == DIRECTION_OUT) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, - "Pull not used when direction is output.")); + mp_raise_AttributeError("Pull not used when direction is output."); return mp_const_none; } enum digitalinout_pull_t pull = PULL_NONE; diff --git a/shared-bindings/nativeio/I2C.c b/shared-bindings/nativeio/I2C.c index 43cf7b63d5..4b0f9a6402 100644 --- a/shared-bindings/nativeio/I2C.c +++ b/shared-bindings/nativeio/I2C.c @@ -109,7 +109,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_i2c___exit___obj, 4, 4, nati static void check_lock(nativeio_i2c_obj_t *self) { if (!common_hal_nativeio_i2c_has_lock(self)) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Function requires I2C lock.")); + mp_raise_RuntimeError("Function requires lock."); } } diff --git a/shared-bindings/nativeio/PWMOut.c b/shared-bindings/nativeio/PWMOut.c index 330ffb1876..bbb4e5a873 100644 --- a/shared-bindings/nativeio/PWMOut.c +++ b/shared-bindings/nativeio/PWMOut.c @@ -155,9 +155,7 @@ STATIC mp_obj_t nativeio_pwmout_obj_set_duty_cycle(mp_obj_t self_in, mp_obj_t du nativeio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in); mp_int_t duty = mp_obj_get_int(duty_cycle); if (duty < 0 || duty > 0xffff) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, - "PWM duty must be between 0 and 65536 (16 bit resolution), not %d", - duty)); + mp_raise_ValueError("PWM duty must be between 0 and 65536 (16 bit resolution)"); } common_hal_nativeio_pwmout_set_duty_cycle(self, duty); return mp_const_none; @@ -185,9 +183,9 @@ MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pwmout_get_frequency_obj, nativeio_pwmout_obj STATIC mp_obj_t nativeio_pwmout_obj_set_frequency(mp_obj_t self_in, mp_obj_t frequency) { nativeio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in); if (!common_hal_nativeio_pwmout_get_variable_frequency(self)) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_AttributeError, + mp_raise_AttributeError( "PWM frequency not writeable when variable_frequency is False on " - "construction.")); + "construction."); } common_hal_nativeio_pwmout_set_frequency(self, mp_obj_get_int(frequency)); return mp_const_none; diff --git a/shared-bindings/nativeio/SPI.c b/shared-bindings/nativeio/SPI.c index 4653343539..eb2f86e221 100644 --- a/shared-bindings/nativeio/SPI.c +++ b/shared-bindings/nativeio/SPI.c @@ -33,6 +33,7 @@ #include "shared-bindings/nativeio/SPI.h" #include "lib/utils/context_manager_helpers.h" +#include "py/mperrno.h" #include "py/nlr.h" #include "py/runtime.h" @@ -124,7 +125,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_spi_obj___exit___obj, 4, 4, static void check_lock(nativeio_spi_obj_t *self) { if (!common_hal_nativeio_spi_has_lock(self)) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Function requires SPI lock.")); + mp_raise_RuntimeError("Function requires lock"); } } @@ -147,20 +148,20 @@ STATIC mp_obj_t nativeio_spi_configure(size_t n_args, const mp_obj_t *pos_args, uint8_t polarity = args[ARG_polarity].u_int; if (polarity != 0 && polarity != 1) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid polarity.")); + mp_raise_ValueError("Invalid polarity"); } uint8_t phase = args[ARG_phase].u_int; if (phase != 0 && phase != 1) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid phase.")); + mp_raise_ValueError("Invalid phase"); } uint8_t bits = args[ARG_bits].u_int; if (bits != 8 && bits != 9) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid number of bits.")); + mp_raise_ValueError("Invalid number of bits"); } if (!common_hal_nativeio_spi_configure(self, args[ARG_baudrate].u_int, polarity, phase, bits)) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "SPI configure failed.")); + mp_raise_OSError(MP_EIO); } return mp_const_none; } @@ -222,7 +223,7 @@ STATIC mp_obj_t nativeio_spi_write(size_t n_args, const mp_obj_t *pos_args, mp_m bool ok = common_hal_nativeio_spi_write(self, ((uint8_t*)bufinfo.buf) + start, len); if (!ok) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "SPI bus error")); + mp_raise_OSError(MP_EIO); } return mp_const_none; } @@ -267,7 +268,7 @@ STATIC mp_obj_t nativeio_spi_readinto(size_t n_args, const mp_obj_t *pos_args, m bool ok = common_hal_nativeio_spi_read(self, ((uint8_t*)bufinfo.buf) + start, len, args[ARG_write_value].u_int); if (!ok) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "SPI bus error")); + mp_raise_OSError(MP_EIO); } return mp_const_none; } diff --git a/shared-bindings/nativeio/UART.c b/shared-bindings/nativeio/UART.c index f4921814bf..b4942b72f0 100644 --- a/shared-bindings/nativeio/UART.c +++ b/shared-bindings/nativeio/UART.c @@ -92,8 +92,7 @@ STATIC mp_obj_t nativeio_uart_make_new(const mp_obj_type_t *type, size_t n_args, uint8_t bits = args[ARG_bits].u_int; if (bits < 7 || bits > 9) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, - "bits must be 7, 8 or 9")); + mp_raise_ValueError("bits must be 7, 8 or 9"); } uart_parity_t parity = PARITY_NONE; @@ -105,8 +104,7 @@ STATIC mp_obj_t nativeio_uart_make_new(const mp_obj_type_t *type, size_t n_args, uint8_t stop = args[ARG_stop].u_int; if (stop != 1 && stop != 2) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, - "stop must be 1 or 2")); + mp_raise_ValueError("stop must be 1 or 2"); } common_hal_nativeio_uart_construct(self, tx, rx, diff --git a/shared-bindings/time/__init__.c b/shared-bindings/time/__init__.c index 36e63d36f2..9c4cee8e02 100644 --- a/shared-bindings/time/__init__.c +++ b/shared-bindings/time/__init__.c @@ -69,7 +69,7 @@ STATIC mp_obj_t time_sleep(mp_obj_t seconds_o) { int seconds = mp_obj_get_int(seconds_o); #endif if (seconds < 0) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "sleep length must be non-negative")); + mp_raise_ValueError("sleep length must be non-negative"); } common_hal_time_delay_ms(1000 * seconds); return mp_const_none; @@ -79,10 +79,10 @@ MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_obj, time_sleep); #if MICROPY_PY_COLLECTIONS mp_obj_t struct_time_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { if (n_args != 1) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "time.struct_time() takes exactly 1 argument")); + mp_raise_TypeError("time.struct_time() takes exactly 1 argument"); } if (!MP_OBJ_IS_TYPE(args[0], &mp_type_tuple) || ((mp_obj_tuple_t*) MP_OBJ_TO_PTR(args[0]))->len != 9) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "time.struct_time() takes a 9-sequence")); + mp_raise_TypeError("time.struct_time() takes a 9-sequence"); } mp_obj_tuple_t* tuple = MP_OBJ_TO_PTR(args[0]); diff --git a/unix/modjni.c b/unix/modjni.c index b474e26ea7..99efa47873 100644 --- a/unix/modjni.c +++ b/unix/modjni.c @@ -268,7 +268,7 @@ STATIC mp_obj_t jobject_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) return mp_const_none; } } - mp_not_implemented(""); + mp_raise_NotImplementError(""); } if (!JJ(IsInstanceOf, self->obj, List_class)) {