all: Make more use of mp_raise_{msg,TypeError,ValueError} helpers.

This commit is contained in:
Damien George 2017-06-15 11:54:41 +10:00
parent 1e70fda69f
commit 48d867b4a6
17 changed files with 33 additions and 37 deletions

View File

@ -104,7 +104,7 @@ STATIC void pyb_uart_init_helper(pyb_uart_obj_t *self, size_t n_args, const mp_o
self->bits = 8; self->bits = 8;
break; break;
default: default:
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid data bits")); mp_raise_ValueError("invalid data bits");
break; break;
} }
@ -140,7 +140,7 @@ STATIC void pyb_uart_init_helper(pyb_uart_obj_t *self, size_t n_args, const mp_o
self->stop = 2; self->stop = 2;
break; break;
default: default:
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid stop bits")); mp_raise_ValueError("invalid stop bits");
break; break;
} }
@ -215,7 +215,7 @@ STATIC mp_uint_t pyb_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, i
pyb_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); pyb_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
if (self->uart_id == 1) { if (self->uart_id == 1) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "UART(1) can't read")); mp_raise_msg(&mp_type_OSError, "UART(1) can't read");
} }
// make sure we want at least 1 char // make sure we want at least 1 char

View File

@ -244,8 +244,7 @@ STATIC mp_obj_t framebuf_make_new(const mp_obj_type_t *type, size_t n_args, size
o->stride = (o->stride + 7) & ~7; o->stride = (o->stride + 7) & ~7;
break; break;
default: default:
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, mp_raise_ValueError("invalid format");
"invalid format"));
} }
return MP_OBJ_FROM_PTR(o); return MP_OBJ_FROM_PTR(o);

View File

@ -75,7 +75,7 @@ mp_obj_t mod_binascii_unhexlify(mp_obj_t data) {
mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ); mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ);
if ((bufinfo.len & 1) != 0) { if ((bufinfo.len & 1) != 0) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "odd-length string")); mp_raise_ValueError("odd-length string");
} }
vstr_t vstr; vstr_t vstr;
vstr_init_len(&vstr, bufinfo.len / 2); vstr_init_len(&vstr, bufinfo.len / 2);
@ -86,7 +86,7 @@ mp_obj_t mod_binascii_unhexlify(mp_obj_t data) {
if (unichar_isxdigit(hex_ch)) { if (unichar_isxdigit(hex_ch)) {
hex_byte += unichar_xdigit_value(hex_ch); hex_byte += unichar_xdigit_value(hex_ch);
} else { } else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "non-hex digit found")); mp_raise_ValueError("non-hex digit found");
} }
if (i & 1) { if (i & 1) {
hex_byte <<= 4; hex_byte <<= 4;

View File

@ -230,8 +230,7 @@ STATIC void do_load(mp_obj_t module_obj, vstr_t *file) {
#endif #endif
// If we get here then the file was not frozen and we can't compile scripts. // 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, mp_raise_msg(&mp_type_ImportError, "script compilation not supported");
"script compilation not supported"));
} }
STATIC void chop_component(const char *start, const char **end) { STATIC void chop_component(const char *start, const char **end) {

View File

@ -398,8 +398,7 @@ STATIC mp_obj_t mp_builtin_ord(mp_obj_t o_in) {
#endif #endif
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, mp_raise_TypeError("ord expects a character");
"ord expects a character"));
} else { } else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"ord() expected a character, but string of length %d found", (int)len)); "ord() expected a character, but string of length %d found", (int)len));

View File

@ -25,7 +25,7 @@
*/ */
#include "py/builtin.h" #include "py/builtin.h"
#include "py/nlr.h" #include "py/runtime.h"
#if MICROPY_PY_BUILTINS_FLOAT && MICROPY_PY_MATH #if MICROPY_PY_BUILTINS_FLOAT && MICROPY_PY_MATH
@ -41,7 +41,7 @@
/// working with floating-point numbers. /// working with floating-point numbers.
STATIC NORETURN void math_error(void) { 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) \ #define MATH_FUN_1(py_name, c_name) \

View File

@ -460,8 +460,7 @@ mp_obj_t mp_obj_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) {
} }
} else if (value == MP_OBJ_SENTINEL) { } else if (value == MP_OBJ_SENTINEL) {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, mp_raise_TypeError("object is not subscriptable");
"object is not subscriptable"));
} else { } else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"'%s' object is not subscriptable", mp_obj_get_type_str(base))); "'%s' object is not subscriptable", mp_obj_get_type_str(base)));

View File

@ -196,7 +196,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_FLOOR_DIVIDE:
case MP_BINARY_OP_INPLACE_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_TRUE_DIVIDE:
case MP_BINARY_OP_INPLACE_TRUE_DIVIDE: case MP_BINARY_OP_INPLACE_TRUE_DIVIDE:

View File

@ -247,7 +247,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) { 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 // TODO raise an exception if the unsigned long long won't fit
if (val >> (sizeof(unsigned long long) * 8 - 1) != 0) { 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); mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
o->base.type = &mp_type_int; o->base.type = &mp_type_int;

View File

@ -142,7 +142,7 @@ const byte *str_index_to_ptr(const mp_obj_type_t *type, const byte *self_data, s
if (is_slice) { if (is_slice) {
return self_data; return self_data;
} }
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_IndexError, "string index out of range")); mp_raise_msg(&mp_type_IndexError, "string index out of range");
} }
if (!UTF8_IS_CONT(*s)) { if (!UTF8_IS_CONT(*s)) {
++i; ++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) { if (is_slice) {
return top; return top;
} }
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_IndexError, "string index out of range")); mp_raise_msg(&mp_type_IndexError, "string index out of range");
} }
// Then check completion // Then check completion
if (i-- == 0) { if (i-- == 0) {

View File

@ -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) { while (more_bytes > 0) {
char *p = vstr_add_len(&vstr, more_bytes); char *p = vstr_add_len(&vstr, more_bytes);
if (p == NULL) { 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; int error;
mp_uint_t out_sz = mp_stream_read_exactly(args[0], p, more_bytes, &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) { while (max_size == -1 || max_size-- != 0) {
char *p = vstr_add_len(&vstr, 1); char *p = vstr_add_len(&vstr, 1);
if (p == NULL) { 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; int error;

View File

@ -90,7 +90,7 @@ STATIC void accel_start(void) {
} }
if (status != HAL_OK) { if (status != HAL_OK) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "accelerometer not found")); mp_raise_msg(&mp_type_OSError, "accelerometer not found");
} }
// set MMA to active mode // set MMA to active mode

View File

@ -473,7 +473,7 @@ STATIC mp_obj_t pyb_can_send(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_
pyb_buf_get_for_send(args[0].u_obj, &bufinfo, data); pyb_buf_get_for_send(args[0].u_obj, &bufinfo, data);
if (bufinfo.len > 8) { if (bufinfo.len > 8) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "CAN data field too long")); mp_raise_ValueError("CAN data field too long");
} }
// send the data // send the data
@ -738,7 +738,7 @@ STATIC mp_obj_t pyb_can_setfilter(mp_uint_t n_args, const mp_obj_t *pos_args, mp
return mp_const_none; return mp_const_none;
error: error:
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "CAN filter parameter error")); mp_raise_ValueError("CAN filter parameter error");
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_can_setfilter_obj, 1, pyb_can_setfilter); STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_can_setfilter_obj, 1, pyb_can_setfilter);

View File

@ -189,7 +189,7 @@ STATIC mp_obj_t pyb_dac_init_helper(pyb_dac_obj_t *self, mp_uint_t n_args, const
if (args[0].u_int == 8 || args[0].u_int == 12) { if (args[0].u_int == 8 || args[0].u_int == 12) {
self->bits = args[0].u_int; self->bits = args[0].u_int;
} else { } else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "unsupported bits")); mp_raise_ValueError("unsupported bits");
} }
// reset state of DAC // reset state of DAC

View File

@ -597,7 +597,7 @@ STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, mp_uint_t n_args, con
} else if (bits == 9) { } else if (bits == 9) {
init->WordLength = UART_WORDLENGTH_9B; init->WordLength = UART_WORDLENGTH_9B;
} else { } else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "unsupported combination of bits and parity")); mp_raise_ValueError("unsupported combination of bits and parity");
} }
// stop bits // stop bits

View File

@ -134,7 +134,7 @@ STATIC ffi_type *get_ffi_type(mp_obj_t o_in)
} }
// TODO: Support actual libffi type objects // TODO: Support actual libffi type objects
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "Unknown type")); mp_raise_TypeError("Unknown type");
} }
STATIC mp_obj_t return_ffi_value(ffi_arg val, char type) STATIC mp_obj_t return_ffi_value(ffi_arg val, char type)
@ -203,7 +203,7 @@ STATIC mp_obj_t make_func(mp_obj_t rettype_in, void *func, mp_obj_t argtypes_in)
int res = ffi_prep_cif(&o->cif, FFI_DEFAULT_ABI, nparams, char2ffi_type(*rettype), o->params); int res = ffi_prep_cif(&o->cif, FFI_DEFAULT_ABI, nparams, char2ffi_type(*rettype), o->params);
if (res != FFI_OK) { if (res != FFI_OK) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Error in ffi_prep_cif")); mp_raise_ValueError("Error in ffi_prep_cif");
} }
return MP_OBJ_FROM_PTR(o); return MP_OBJ_FROM_PTR(o);
@ -261,12 +261,12 @@ STATIC mp_obj_t mod_ffi_callback(mp_obj_t rettype_in, mp_obj_t func_in, mp_obj_t
int res = ffi_prep_cif(&o->cif, FFI_DEFAULT_ABI, nparams, char2ffi_type(*rettype), o->params); int res = ffi_prep_cif(&o->cif, FFI_DEFAULT_ABI, nparams, char2ffi_type(*rettype), o->params);
if (res != FFI_OK) { if (res != FFI_OK) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Error in ffi_prep_cif")); mp_raise_ValueError("Error in ffi_prep_cif");
} }
res = ffi_prep_closure_loc(o->clo, &o->cif, call_py_func, MP_OBJ_TO_PTR(func_in), o->func); res = ffi_prep_closure_loc(o->clo, &o->cif, call_py_func, MP_OBJ_TO_PTR(func_in), o->func);
if (res != FFI_OK) { if (res != FFI_OK) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "ffi_prep_closure_loc")); mp_raise_ValueError("ffi_prep_closure_loc");
} }
return MP_OBJ_FROM_PTR(o); return MP_OBJ_FROM_PTR(o);

View File

@ -159,7 +159,7 @@ STATIC void jclass_attr(mp_obj_t self_in, qstr attr_in, mp_obj_t *dest) {
STATIC mp_obj_t jclass_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) { STATIC mp_obj_t jclass_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
if (n_kw != 0) { if (n_kw != 0) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "kwargs not supported")); mp_raise_TypeError("kwargs not supported");
} }
mp_obj_jclass_t *self = self_in; mp_obj_jclass_t *self = self_in;
@ -433,7 +433,7 @@ STATIC bool py2jvalue(const char **jtypesig, mp_obj_t arg, jvalue *out) {
} }
out->l = NULL; out->l = NULL;
} else { } else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "arg type not supported")); mp_raise_TypeError("arg type not supported");
} }
*jtypesig = arg_type; *jtypesig = arg_type;
@ -534,7 +534,7 @@ STATIC mp_obj_t call_method(jobject obj, const char *name, jarray methods, bool
ret = new_jobject(res); ret = new_jobject(res);
} else { } else {
JJ(ReleaseStringUTFChars, name_o, decl); JJ(ReleaseStringUTFChars, name_o, decl);
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "cannot handle return type")); mp_raise_TypeError("cannot handle return type");
} }
JJ(ReleaseStringUTFChars, name_o, decl); JJ(ReleaseStringUTFChars, name_o, decl);
@ -550,13 +550,13 @@ next_method:
JJ(DeleteLocalRef, meth); JJ(DeleteLocalRef, meth);
} }
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "method not found")); mp_raise_TypeError("method not found");
} }
STATIC mp_obj_t jmethod_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) { STATIC mp_obj_t jmethod_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
if (n_kw != 0) { if (n_kw != 0) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "kwargs not supported")); mp_raise_TypeError("kwargs not supported");
} }
mp_obj_jmethod_t *self = self_in; mp_obj_jmethod_t *self = self_in;
@ -602,13 +602,13 @@ STATIC void create_jvm() {
void *libjvm = dlopen(LIBJVM_SO, RTLD_NOW | RTLD_GLOBAL); void *libjvm = dlopen(LIBJVM_SO, RTLD_NOW | RTLD_GLOBAL);
if (!libjvm) { if (!libjvm) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "unable to load libjvm.so, use LD_LIBRARY_PATH")); mp_raise_msg(&mp_type_OSError, "unable to load libjvm.so, use LD_LIBRARY_PATH");
} }
int (*_JNI_CreateJavaVM)(void*, void**, void*) = dlsym(libjvm, "JNI_CreateJavaVM"); int (*_JNI_CreateJavaVM)(void*, void**, void*) = dlsym(libjvm, "JNI_CreateJavaVM");
int st = _JNI_CreateJavaVM(&jvm, (void**)&env, &args); int st = _JNI_CreateJavaVM(&jvm, (void**)&env, &args);
if (st < 0 || !env) { if (st < 0 || !env) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "unable to create JVM")); mp_raise_msg(&mp_type_OSError, "unable to create JVM");
} }
Class_class = JJ(FindClass, "java/lang/Class"); Class_class = JJ(FindClass, "java/lang/Class");