py: Overhaul and simplify printf/pfenv mechanism.
Previous to this patch the printing mechanism was a bit of a tangled
mess. This patch attempts to consolidate printing into one interface.
All (non-debug) printing now uses the mp_print* family of functions,
mainly mp_printf. All these functions take an mp_print_t structure as
their first argument, and this structure defines the printing backend
through the "print_strn" function of said structure.
Printing from the uPy core can reach the platform-defined print code via
two paths: either through mp_sys_stdout_obj (defined pert port) in
conjunction with mp_stream_write; or through the mp_plat_print structure
which uses the MP_PLAT_PRINT_STRN macro to define how string are printed
on the platform. The former is only used when MICROPY_PY_IO is defined.
With this new scheme printing is generally more efficient (less layers
to go through, less arguments to pass), and, given an mp_print_t*
structure, one can call mp_print_str for efficiency instead of
mp_printf("%s", ...). Code size is also reduced by around 200 bytes on
Thumb2 archs.
This commit is contained in:
19
py/objset.c
19
py/objset.c
@@ -79,7 +79,7 @@ STATIC void check_set(mp_obj_t o) {
|
||||
}
|
||||
}
|
||||
|
||||
STATIC void set_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
|
||||
STATIC void set_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
||||
(void)kind;
|
||||
mp_obj_set_t *self = self_in;
|
||||
#if MICROPY_PY_BUILTINS_FROZENSET
|
||||
@@ -88,37 +88,36 @@ STATIC void set_print(void (*print)(void *env, const char *fmt, ...), void *env,
|
||||
if (self->set.used == 0) {
|
||||
#if MICROPY_PY_BUILTINS_FROZENSET
|
||||
if (is_frozen) {
|
||||
print(env, "frozen");
|
||||
mp_print_str(print, "frozen");
|
||||
}
|
||||
#endif
|
||||
print(env, "set()");
|
||||
mp_print_str(print, "set()");
|
||||
return;
|
||||
}
|
||||
bool first = true;
|
||||
#if MICROPY_PY_BUILTINS_FROZENSET
|
||||
if (is_frozen) {
|
||||
print(env, "frozenset(");
|
||||
mp_print_str(print, "frozenset(");
|
||||
}
|
||||
#endif
|
||||
print(env, "{");
|
||||
mp_print_str(print, "{");
|
||||
for (mp_uint_t i = 0; i < self->set.alloc; i++) {
|
||||
if (MP_SET_SLOT_IS_FILLED(&self->set, i)) {
|
||||
if (!first) {
|
||||
print(env, ", ");
|
||||
mp_print_str(print, ", ");
|
||||
}
|
||||
first = false;
|
||||
mp_obj_print_helper(print, env, self->set.table[i], PRINT_REPR);
|
||||
mp_obj_print_helper(print, self->set.table[i], PRINT_REPR);
|
||||
}
|
||||
}
|
||||
print(env, "}");
|
||||
mp_print_str(print, "}");
|
||||
#if MICROPY_PY_BUILTINS_FROZENSET
|
||||
if (is_frozen) {
|
||||
print(env, ")");
|
||||
mp_print_str(print, ")");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
STATIC mp_obj_t set_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
|
||||
mp_arg_check_num(n_args, n_kw, 0, 1, false);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user