Commit 48d867b4 authored by Damien George's avatar Damien George
Browse files

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

parent 1e70fda6
......@@ -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;
break;
default:
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid data bits"));
mp_raise_ValueError("invalid data bits");
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;
break;
default:
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid stop bits"));
mp_raise_ValueError("invalid stop bits");
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);
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
......
......@@ -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;
break;
default:
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
"invalid format"));
mp_raise_ValueError("invalid format");
}
return MP_OBJ_FROM_PTR(o);
......
......@@ -75,7 +75,7 @@ mp_obj_t mod_binascii_unhexlify(mp_obj_t data) {
mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ);
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_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)) {
hex_byte += unichar_xdigit_value(hex_ch);
} 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) {
hex_byte <<= 4;
......
......@@ -230,8 +230,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_msg(&mp_type_ImportError, "script compilation not supported");
}
STATIC void chop_component(const char *start, const char **end) {
......
......@@ -398,8 +398,7 @@ 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));
......
......@@ -25,7 +25,7 @@
*/
#include "py/builtin.h"
#include "py/nlr.h"
#include "py/runtime.h"
#if MICROPY_PY_BUILTINS_FLOAT && MICROPY_PY_MATH
......@@ -41,7 +41,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) \
......
......@@ -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) {
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)));
......
......@@ -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_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:
......
......@@ -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) {
// 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;
......
......@@ -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_msg(&mp_type_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_msg(&mp_type_IndexError, "string index out of range");
}
// Then check completion
if (i-- == 0) {
......
......@@ -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;
......
......@@ -90,7 +90,7 @@ STATIC void accel_start(void) {
}
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
......
......@@ -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);
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
......@@ -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;
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);
......
......@@ -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) {
self->bits = args[0].u_int;
} else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "unsupported bits"));
mp_raise_ValueError("unsupported bits");
}
// reset state of DAC
......
......@@ -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) {
init->WordLength = UART_WORDLENGTH_9B;
} 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
......
......@@ -134,7 +134,7 @@ STATIC ffi_type *get_ffi_type(mp_obj_t o_in)
}
// 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)
......@@ -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);
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);
......@@ -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);
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);
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);
......
......@@ -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) {
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;
......@@ -433,7 +433,7 @@ STATIC bool py2jvalue(const char **jtypesig, mp_obj_t arg, jvalue *out) {
}
out->l = NULL;
} 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;
......@@ -534,7 +534,7 @@ STATIC mp_obj_t call_method(jobject obj, const char *name, jarray methods, bool
ret = new_jobject(res);
} else {
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);
......@@ -550,13 +550,13 @@ next_method:
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) {
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;
......@@ -602,13 +602,13 @@ STATIC void create_jvm() {
void *libjvm = dlopen(LIBJVM_SO, RTLD_NOW | RTLD_GLOBAL);
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 st = _JNI_CreateJavaVM(&jvm, (void**)&env, &args);
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");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment