Skip to content

Commit 521c753

Browse files
committed
Improve printing to include _bleio. prefix for type.
1 parent 5e857fd commit 521c753

File tree

2 files changed

+39
-16
lines changed

2 files changed

+39
-16
lines changed

shared-bindings/_bleio/__init__.c

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,12 @@
8484
//|
8585
//| Catch all exception for Bluetooth related errors.
8686
//|
87-
MP_DEFINE_EXCEPTION(BluetoothError, Exception)
87+
MP_DEFINE_BLEIO_EXCEPTION(BluetoothError, Exception)
8888

8989
NORETURN void mp_raise_bleio_BluetoothError(const compressed_string_t* fmt, ...) {
9090
va_list argptr;
9191
va_start(argptr,fmt);
92-
mp_obj_t exception = mp_obj_new_exception_msg_vlist(&mp_type_BluetoothError, fmt, argptr);
92+
mp_obj_t exception = mp_obj_new_exception_msg_vlist(&mp_type_bleio_BluetoothError, fmt, argptr);
9393
va_end(argptr);
9494
nlr_raise(exception);
9595
}
@@ -98,11 +98,11 @@ NORETURN void mp_raise_bleio_BluetoothError(const compressed_string_t* fmt, ...)
9898
//|
9999
//| Raised when a connection is unavailable.
100100
//|
101-
MP_DEFINE_EXCEPTION(ConnectionError, BluetoothError)
101+
MP_DEFINE_BLEIO_EXCEPTION(ConnectionError, bleio_BluetoothError)
102102
NORETURN void mp_raise_bleio_ConnectionError(const compressed_string_t* fmt, ...) {
103103
va_list argptr;
104104
va_start(argptr,fmt);
105-
mp_obj_t exception = mp_obj_new_exception_msg_vlist(&mp_type_ConnectionError, fmt, argptr);
105+
mp_obj_t exception = mp_obj_new_exception_msg_vlist(&mp_type_bleio_ConnectionError, fmt, argptr);
106106
va_end(argptr);
107107
nlr_raise(exception);
108108
}
@@ -112,25 +112,26 @@ NORETURN void mp_raise_bleio_ConnectionError(const compressed_string_t* fmt, ...
112112
//| Raised when a resource is used as the mismatched role. For example, if a local CCCD is
113113
//| attempted to be set but they can only be set when remote.
114114
//|
115-
MP_DEFINE_EXCEPTION(RoleError, BluetoothError)
115+
MP_DEFINE_BLEIO_EXCEPTION(RoleError, bleio_BluetoothError)
116116
NORETURN void mp_raise_bleio_RoleError(const compressed_string_t* msg) {
117-
mp_raise_msg(&mp_type_RoleError, msg);
117+
mp_raise_msg(&mp_type_bleio_RoleError, msg);
118118
}
119119

120120
//| .. class:: SecurityError(BluetoothError)
121121
//|
122122
//| Raised when a security related error occurs.
123123
//|
124-
MP_DEFINE_EXCEPTION(SecurityError, BluetoothError)
124+
MP_DEFINE_BLEIO_EXCEPTION(SecurityError, bleio_BluetoothError)
125125
NORETURN void mp_raise_bleio_SecurityError(const compressed_string_t* fmt, ...) {
126126
va_list argptr;
127127
va_start(argptr,fmt);
128-
mp_obj_t exception = mp_obj_new_exception_msg_vlist(&mp_type_SecurityError, fmt, argptr);
128+
mp_obj_t exception = mp_obj_new_exception_msg_vlist(&mp_type_bleio_SecurityError, fmt, argptr);
129129
va_end(argptr);
130130
nlr_raise(exception);
131131
}
132132

133133
STATIC const mp_rom_map_elem_t bleio_module_globals_table[] = {
134+
// Name must be the first entry so that the exception printing below is correct.
134135
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR__bleio) },
135136
{ MP_ROM_QSTR(MP_QSTR_Adapter), MP_ROM_PTR(&bleio_adapter_type) },
136137
{ MP_ROM_QSTR(MP_QSTR_Address), MP_ROM_PTR(&bleio_address_type) },
@@ -148,14 +149,24 @@ STATIC const mp_rom_map_elem_t bleio_module_globals_table[] = {
148149
{ MP_ROM_QSTR(MP_QSTR_adapter), MP_ROM_PTR(&common_hal_bleio_adapter_obj) },
149150

150151
// Errors
151-
{ MP_ROM_QSTR(MP_QSTR_BluetoothError), MP_ROM_PTR(&mp_type_BluetoothError) },
152-
{ MP_ROM_QSTR(MP_QSTR_ConnectionError), MP_ROM_PTR(&mp_type_ConnectionError) },
153-
{ MP_ROM_QSTR(MP_QSTR_RoleError), MP_ROM_PTR(&mp_type_RoleError) },
154-
{ MP_ROM_QSTR(MP_QSTR_SecurityError), MP_ROM_PTR(&mp_type_SecurityError) },
152+
{ MP_ROM_QSTR(MP_QSTR_BluetoothError), MP_ROM_PTR(&mp_type_bleio_BluetoothError) },
153+
{ MP_ROM_QSTR(MP_QSTR_ConnectionError), MP_ROM_PTR(&mp_type_bleio_ConnectionError) },
154+
{ MP_ROM_QSTR(MP_QSTR_RoleError), MP_ROM_PTR(&mp_type_bleio_RoleError) },
155+
{ MP_ROM_QSTR(MP_QSTR_SecurityError), MP_ROM_PTR(&mp_type_bleio_SecurityError) },
155156
};
156157

157158
STATIC MP_DEFINE_CONST_DICT(bleio_module_globals, bleio_module_globals_table);
158159

160+
void bleio_exception_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
161+
mp_print_kind_t k = kind & ~PRINT_EXC_SUBCLASS;
162+
bool is_subclass = kind & PRINT_EXC_SUBCLASS;
163+
if (!is_subclass && (k == PRINT_REPR || k == PRINT_EXC)) {
164+
mp_print_str(print, qstr_str(MP_OBJ_QSTR_VALUE(bleio_module_globals_table[0].value)));
165+
mp_print_str(print, ".");
166+
}
167+
mp_obj_exception_print(print, o_in, kind);
168+
}
169+
159170
const mp_obj_module_t bleio_module = {
160171
.base = { &mp_type_module },
161172
.globals = (mp_obj_dict_t*)&bleio_module_globals,

shared-bindings/_bleio/__init__.h

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,22 @@
3838

3939
extern bleio_adapter_obj_t common_hal_bleio_adapter_obj;
4040

41-
extern const mp_obj_type_t mp_type_BluetoothError;
42-
extern const mp_obj_type_t mp_type_ConnectionError;
43-
extern const mp_obj_type_t mp_type_RoleError;
44-
extern const mp_obj_type_t mp_type_SecurityError;
41+
void bleio_exception_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind);
42+
43+
#define MP_DEFINE_BLEIO_EXCEPTION(exc_name, base_name) \
44+
const mp_obj_type_t mp_type_bleio_ ## exc_name = { \
45+
{ &mp_type_type }, \
46+
.name = MP_QSTR_ ## exc_name, \
47+
.print = bleio_exception_print, \
48+
.make_new = mp_obj_exception_make_new, \
49+
.attr = mp_obj_exception_attr, \
50+
.parent = &mp_type_ ## base_name, \
51+
};
52+
53+
extern const mp_obj_type_t mp_type_bleio_BluetoothError;
54+
extern const mp_obj_type_t mp_type_bleio_ConnectionError;
55+
extern const mp_obj_type_t mp_type_bleio_RoleError;
56+
extern const mp_obj_type_t mp_type_bleio_SecurityError;
4557

4658
NORETURN void mp_raise_bleio_BluetoothError(const compressed_string_t* msg, ...);
4759
NORETURN void mp_raise_bleio_ConnectionError(const compressed_string_t* msg, ...);

0 commit comments

Comments
 (0)