From febc7a8514f9efbff8394f808483757375aca2e4 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 13 Oct 2022 09:29:21 -0500 Subject: [PATCH] format_traceback: Return list, as documented, and compatible with CPython --- shared-bindings/traceback/__init__.c | 5 +++-- tests/circuitpython/traceback_test.py | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/shared-bindings/traceback/__init__.c b/shared-bindings/traceback/__init__.c index bc5776db2b..eeb7bc458d 100644 --- a/shared-bindings/traceback/__init__.c +++ b/shared-bindings/traceback/__init__.c @@ -110,7 +110,7 @@ STATIC void traceback_exception_common(bool is_print_exception, mp_print_t *prin //| tb: Optional[TracebackType] = None, //| limit: Optional[int] = None, //| chain: Optional[bool] = True, -//| ) -> str: +//| ) -> List[str]: //| """Format a stack trace and the exception information. //| //| If the exception value is passed in ``exc``, then this exception value and its @@ -143,7 +143,8 @@ STATIC mp_obj_t traceback_format_exception(size_t n_args, const mp_obj_t *pos_ar vstr_t vstr; vstr_init_print(&vstr, 0, &print); traceback_exception_common(false, &print, n_args, pos_args, kw_args); - return mp_obj_new_str_from_vstr(&mp_type_str, &vstr); + mp_obj_t output = mp_obj_new_str_from_vstr(&mp_type_str, &vstr); + return mp_obj_new_list(1, &output); } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(traceback_format_exception_obj, 0, traceback_format_exception); diff --git a/tests/circuitpython/traceback_test.py b/tests/circuitpython/traceback_test.py index 5377f1dca4..17c54c857a 100644 --- a/tests/circuitpython/traceback_test.py +++ b/tests/circuitpython/traceback_test.py @@ -21,7 +21,7 @@ except Exception as exc: print("\nLimit=0 Trace:") traceback.print_exception(None, exc, exc.__traceback__, limit=0) print("\nLimit=-1 Trace:") - print(traceback.format_exception(None, exc, exc.__traceback__, limit=-1), end="") + print("".join(traceback.format_exception(None, exc, exc.__traceback__, limit=-1)), end="") class NonNativeException(Exception):