util: Add properties_print_helper

This commit is contained in:
Jeff Epler 2023-05-03 09:11:17 -05:00
parent c22fd2a18e
commit bc03e03b9e
No known key found for this signature in database
GPG Key ID: D5BF15AB975AB4DE
2 changed files with 16 additions and 0 deletions

View File

@ -37,5 +37,17 @@ void raise_deinited_error(void) {
mp_raise_ValueError(translate("Object has been deinitialized and can no longer be used. Create a new object."));
}
void properties_print_helper(const mp_print_t *print, mp_obj_t self_in, const mp_arg_t *properties, size_t n_properties) {
const mp_obj_type_t *type = mp_obj_get_type(self_in);
mp_printf(print, "%q(", type->name);
for (size_t i = 0; i < n_properties; i++) {
if (i > 0) {
mp_print_str(print, ", ");
}
mp_printf(print, "%q=", properties[i].qst);
mp_obj_print_helper(print, mp_load_attr(self_in, properties[i].qst), PRINT_REPR);
}
mp_print_str(print, ")");
}
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_UTIL_H

View File

@ -27,7 +27,11 @@
#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_UTIL_H
#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_UTIL_H
#include "py/mpprint.h"
#include "py/runtime.h"
void raise_deinited_error(void);
void properties_print_helper(const mp_print_t *print, mp_obj_t self_in, const mp_arg_t *properties, size_t n_properties);
#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_UTIL_H