mirror of
https://github.com/micropython/micropython.git
synced 2026-01-07 12:40:15 +01:00
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:
36
py/obj.c
36
py/obj.c
@@ -36,7 +36,7 @@
|
||||
#include "py/runtime0.h"
|
||||
#include "py/runtime.h"
|
||||
#include "py/stackctrl.h"
|
||||
#include "py/pfenv.h"
|
||||
//#include "py/pfenv.h"
|
||||
#include "py/stream.h" // for mp_obj_print
|
||||
|
||||
mp_obj_type_t *mp_obj_get_type(mp_const_obj_t o_in) {
|
||||
@@ -54,20 +54,20 @@ const char *mp_obj_get_type_str(mp_const_obj_t o_in) {
|
||||
return qstr_str(mp_obj_get_type(o_in)->name);
|
||||
}
|
||||
|
||||
void mp_obj_print_helper(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
|
||||
void mp_obj_print_helper(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
|
||||
// There can be data structures nested too deep, or just recursive
|
||||
MP_STACK_CHECK();
|
||||
#ifndef NDEBUG
|
||||
if (o_in == NULL) {
|
||||
print(env, "(nil)");
|
||||
mp_print_str(print, "(nil)");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
mp_obj_type_t *type = mp_obj_get_type(o_in);
|
||||
if (type->print != NULL) {
|
||||
type->print(print, env, o_in, kind);
|
||||
type->print((mp_print_t*)print, o_in, kind);
|
||||
} else {
|
||||
print(env, "<%s>", qstr_str(type->name));
|
||||
mp_printf(print, "<%s>", qstr_str(type->name));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,41 +75,41 @@ void mp_obj_print(mp_obj_t o_in, mp_print_kind_t kind) {
|
||||
#if MICROPY_PY_IO
|
||||
// defined per port; type of these is irrelevant, just need pointer
|
||||
extern struct _mp_dummy_t mp_sys_stdout_obj;
|
||||
pfenv_t pfenv;
|
||||
pfenv.data = &mp_sys_stdout_obj;
|
||||
pfenv.print_strn = (void (*)(void *, const char *, mp_uint_t))mp_stream_write;
|
||||
mp_obj_print_helper((void (*)(void *env, const char *fmt, ...))pfenv_printf, &pfenv, o_in, kind);
|
||||
mp_print_t print;
|
||||
print.data = &mp_sys_stdout_obj;
|
||||
print.print_strn = (mp_print_strn_t)mp_stream_write;
|
||||
mp_obj_print_helper(&print, o_in, kind);
|
||||
#else
|
||||
mp_obj_print_helper(printf_wrapper, NULL, o_in, kind);
|
||||
mp_obj_print_helper(&mp_plat_print, o_in, kind);
|
||||
#endif
|
||||
}
|
||||
|
||||
// helper function to print an exception with traceback
|
||||
void mp_obj_print_exception(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t exc) {
|
||||
void mp_obj_print_exception(const mp_print_t *print, mp_obj_t exc) {
|
||||
if (mp_obj_is_exception_instance(exc)) {
|
||||
mp_uint_t n, *values;
|
||||
mp_obj_exception_get_traceback(exc, &n, &values);
|
||||
if (n > 0) {
|
||||
assert(n % 3 == 0);
|
||||
print(env, "Traceback (most recent call last):\n");
|
||||
mp_print_str(print, "Traceback (most recent call last):\n");
|
||||
for (int i = n - 3; i >= 0; i -= 3) {
|
||||
#if MICROPY_ENABLE_SOURCE_LINE
|
||||
print(env, " File \"%s\", line %d", qstr_str(values[i]), (int)values[i + 1]);
|
||||
mp_printf(print, " File \"%s\", line %d", qstr_str(values[i]), (int)values[i + 1]);
|
||||
#else
|
||||
print(env, " File \"%s\"", qstr_str(values[i]));
|
||||
mp_printf(print, " File \"%s\"", qstr_str(values[i]));
|
||||
#endif
|
||||
// the block name can be NULL if it's unknown
|
||||
qstr block = values[i + 2];
|
||||
if (block == MP_QSTR_NULL) {
|
||||
print(env, "\n");
|
||||
mp_print_str(print, "\n");
|
||||
} else {
|
||||
print(env, ", in %s\n", qstr_str(block));
|
||||
mp_printf(print, ", in %s\n", qstr_str(block));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
mp_obj_print_helper(print, env, exc, PRINT_EXC);
|
||||
print(env, "\n");
|
||||
mp_obj_print_helper(print, exc, PRINT_EXC);
|
||||
mp_print_str(print, "\n");
|
||||
}
|
||||
|
||||
bool mp_obj_is_true(mp_obj_t arg) {
|
||||
|
||||
Reference in New Issue
Block a user