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
This commit is contained in:
Damien George
2020-02-11 11:48:28 +11:00
parent 97eca38c4f
commit ad7213d3c3
53 changed files with 234 additions and 242 deletions

View File

@@ -606,13 +606,13 @@ bool ble_drv_advertise_data(ubluepy_advertise_data_t * p_adv_params) {
#if (BLUETOOTH_SD == 110)
if ((err_code = sd_ble_gap_adv_data_set(adv_data, byte_pos, NULL, 0)) != 0) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
"Can not apply advertisment data. status: 0x" HEX2_FMT, (uint16_t)err_code));
mp_raise_msg_varg(&mp_type_OSError,
"Can not apply advertisment data. status: 0x" HEX2_FMT, (uint16_t)err_code);
}
#else
if ((err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data, &m_adv_params)) != 0) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
"Can not apply advertisment data. status: 0x" HEX2_FMT, (uint16_t)err_code));
mp_raise_msg_varg(&mp_type_OSError,
"Can not apply advertisment data. status: 0x" HEX2_FMT, (uint16_t)err_code);
}
#endif
BLE_DRIVER_LOG("Set Adv data size: " UINT_FMT "\n", byte_pos);
@@ -626,8 +626,8 @@ bool ble_drv_advertise_data(ubluepy_advertise_data_t * p_adv_params) {
err_code = sd_ble_gap_adv_start(m_adv_handle, conf_tag);
#endif
if (err_code != 0) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
"Can not start advertisment. status: 0x" HEX2_FMT, (uint16_t)err_code));
mp_raise_msg_varg(&mp_type_OSError,
"Can not start advertisment. status: 0x" HEX2_FMT, (uint16_t)err_code);
}
m_adv_in_progress = true;
@@ -641,13 +641,13 @@ void ble_drv_advertise_stop(void) {
#if (BLUETOOTH_SD == 110)
if ((err_code = sd_ble_gap_adv_stop()) != 0) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
"Can not stop advertisment. status: 0x" HEX2_FMT, (uint16_t)err_code));
mp_raise_msg_varg(&mp_type_OSError,
"Can not stop advertisment. status: 0x" HEX2_FMT, (uint16_t)err_code);
}
#else
if ((err_code = sd_ble_gap_adv_stop(m_adv_handle)) != 0) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
"Can not stop advertisment. status: 0x" HEX2_FMT, (uint16_t)err_code));
mp_raise_msg_varg(&mp_type_OSError,
"Can not stop advertisment. status: 0x" HEX2_FMT, (uint16_t)err_code);
}
#endif
}
@@ -666,8 +666,8 @@ void ble_drv_attr_s_read(uint16_t conn_handle, uint16_t handle, uint16_t len, ui
handle,
&gatts_value);
if (err_code != 0) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
"Can not read attribute value. status: 0x" HEX2_FMT, (uint16_t)err_code));
mp_raise_msg_varg(&mp_type_OSError,
"Can not read attribute value. status: 0x" HEX2_FMT, (uint16_t)err_code);
}
}
@@ -683,8 +683,8 @@ void ble_drv_attr_s_write(uint16_t conn_handle, uint16_t handle, uint16_t len, u
uint32_t err_code = sd_ble_gatts_value_set(conn_handle, handle, &gatts_value);
if (err_code != 0) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
"Can not write attribute value. status: 0x" HEX2_FMT, (uint16_t)err_code));
mp_raise_msg_varg(&mp_type_OSError,
"Can not write attribute value. status: 0x" HEX2_FMT, (uint16_t)err_code);
}
}
@@ -707,8 +707,8 @@ void ble_drv_attr_s_notify(uint16_t conn_handle, uint16_t handle, uint16_t len,
BLE_DRIVER_LOG("Request TX, m_tx_in_progress: %u\n", m_tx_in_progress);
uint32_t err_code;
if ((err_code = sd_ble_gatts_hvx(conn_handle, &hvx_params)) != 0) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
"Can not notify attribute value. status: 0x" HEX2_FMT, (uint16_t)err_code));
mp_raise_msg_varg(&mp_type_OSError,
"Can not notify attribute value. status: 0x" HEX2_FMT, (uint16_t)err_code);
}
m_tx_in_progress++;
BLE_DRIVER_LOG("Queued TX, m_tx_in_progress: %u\n", m_tx_in_progress);
@@ -746,8 +746,8 @@ void ble_drv_attr_c_read(uint16_t conn_handle, uint16_t handle, mp_obj_t obj, bl
handle,
0);
if (err_code != 0) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
"Can not read attribute value. status: 0x" HEX2_FMT, (uint16_t)err_code));
mp_raise_msg_varg(&mp_type_OSError,
"Can not read attribute value. status: 0x" HEX2_FMT, (uint16_t)err_code);
}
while (gattc_char_data_handle != NULL) {
@@ -776,8 +776,8 @@ void ble_drv_attr_c_write(uint16_t conn_handle, uint16_t handle, uint16_t len, u
uint32_t err_code = sd_ble_gattc_write(conn_handle, &write_params);
if (err_code != 0) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
"Can not write attribute value. status: 0x" HEX2_FMT, (uint16_t)err_code));
mp_raise_msg_varg(&mp_type_OSError,
"Can not write attribute value. status: 0x" HEX2_FMT, (uint16_t)err_code);
}
while (m_write_done != true) {
@@ -807,8 +807,8 @@ void ble_drv_scan_start(bool cont) {
p_scan_params = NULL;
}
if ((err_code = sd_ble_gap_scan_start(p_scan_params, &scan_buffer)) != 0) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
"Can not start scanning. status: 0x" HEX2_FMT, (uint16_t)err_code));
mp_raise_msg_varg(&mp_type_OSError,
"Can not start scanning. status: 0x" HEX2_FMT, (uint16_t)err_code);
}
}
@@ -853,8 +853,8 @@ void ble_drv_connect(uint8_t * p_addr, uint8_t addr_type) {
&scan_params,
&conn_params,
conn_tag)) != 0) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
"Can not connect. status: 0x" HEX2_FMT, (uint16_t)err_code));
mp_raise_msg_varg(&mp_type_OSError,
"Can not connect. status: 0x" HEX2_FMT, (uint16_t)err_code);
}
}

View File

@@ -375,7 +375,7 @@ STATIC mp_obj_t pin_obj_init_helper(const pin_obj_t *self, mp_uint_t n_args, con
NRF_GPIO_PIN_S0S1,
NRF_GPIO_PIN_NOSENSE);
} else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid pin mode: %d", mode));
mp_raise_msg_varg(&mp_type_ValueError, "invalid pin mode: %d", mode);
}
return mp_const_none;