From c6bbb0e4f6c356a17da0e43a17dd0eb491f27c14 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 13 Oct 2022 08:44:31 -0500 Subject: [PATCH 1/6] test format_exception too --- tests/circuitpython/traceback_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/circuitpython/traceback_test.py b/tests/circuitpython/traceback_test.py index 6ae73db6ae..6f51ceede8 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:") - traceback.print_exception(None, exc, exc.__traceback__, limit=-1) + print(traceback.format_exception(None, exc, exc.__traceback__, limit=-1), end="") class NonNativeException(Exception): From fc991c262c4a9ce5023e8a3552c7cef3539b1e93 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 13 Oct 2022 08:44:57 -0500 Subject: [PATCH 2/6] traceback: share more code between format & print exception --- shared-bindings/traceback/__init__.c | 76 ++++++++++++++-------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/shared-bindings/traceback/__init__.c b/shared-bindings/traceback/__init__.c index 566ac57218..08744d8009 100644 --- a/shared-bindings/traceback/__init__.c +++ b/shared-bindings/traceback/__init__.c @@ -38,7 +38,42 @@ //| """ //| ... -STATIC void traceback_exception_common(mp_print_t *print, mp_obj_t value, mp_obj_t tb_obj, mp_obj_t limit_obj) { +STATIC void traceback_exception_common(bool is_print_exception, mp_print_t *print, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum { ARG_etype, ARG_value, ARG_tb, ARG_limit, ARG_file, ARG_chain }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_etype, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = MP_OBJ_NULL} }, + { MP_QSTR_value, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = MP_OBJ_NULL} }, + { MP_QSTR_tb, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = MP_OBJ_NULL} }, + { MP_QSTR_limit, MP_ARG_OBJ, {.u_obj = mp_const_none} }, + { MP_QSTR_file, MP_ARG_OBJ, {.u_obj = mp_const_none} }, + { MP_QSTR_chain, MP_ARG_BOOL, {.u_bool = true} }, + }; + + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + mp_obj_t value = args[ARG_value].u_obj; + mp_obj_t tb_obj = args[ARG_tb].u_obj; + mp_obj_t limit_obj = args[ARG_limit].u_obj; + + if (args[ARG_file].u_obj != mp_const_none) { + if (!is_print_exception) { + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE + mp_arg_error_terse_mismatch(); + #else + mp_raise_msg_varg(&mp_type_TypeError, MP_ERROR_TEXT("unexpected keyword argument '%q'"), MP_QSTR_file); + #endif + + } + #if MICROPY_PY_IO && MICROPY_PY_SYS_STDFILES + mp_get_stream_raise(args[ARG_file].u_obj, MP_STREAM_OP_WRITE); + print->data = MP_OBJ_TO_PTR(args[ARG_file].u_obj); + print->print_strn = mp_stream_write_adaptor; + #else + mp_raise_NotImplementedError(translate("file write is not available")); + #endif + } + if (!mp_obj_is_exception_instance(value)) { mp_raise_TypeError(translate("invalid exception")); } @@ -91,22 +126,10 @@ STATIC void traceback_exception_common(mp_print_t *print, mp_obj_t value, mp_obj //| ... //| STATIC mp_obj_t traceback_format_exception(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - enum { ARG_etype, ARG_value, ARG_tb, ARG_limit, ARG_chain }; - static const mp_arg_t allowed_args[] = { - { MP_QSTR_etype, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = MP_OBJ_NULL} }, - { MP_QSTR_value, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = MP_OBJ_NULL} }, - { MP_QSTR_tb, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = MP_OBJ_NULL} }, - { MP_QSTR_limit, MP_ARG_OBJ, {.u_obj = mp_const_none} }, - { MP_QSTR_chain, MP_ARG_BOOL, {.u_bool = true} }, - }; - - mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; - mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - mp_print_t print; vstr_t vstr; vstr_init_print(&vstr, 0, &print); - traceback_exception_common(&print, args[ARG_value].u_obj, args[ARG_tb].u_obj, args[ARG_limit].u_obj); + traceback_exception_common(false, &print, n_args, pos_args, kw_args); return mp_obj_new_str_from_vstr(&mp_type_str, &vstr); } @@ -139,31 +162,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(traceback_format_exception_obj, 0, traceback_f //| STATIC mp_obj_t traceback_print_exception(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - enum { ARG_etype, ARG_value, ARG_tb, ARG_limit, ARG_file, ARG_chain }; - static const mp_arg_t allowed_args[] = { - { MP_QSTR_etype, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = MP_OBJ_NULL} }, - { MP_QSTR_value, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = MP_OBJ_NULL} }, - { MP_QSTR_tb, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = MP_OBJ_NULL} }, - { MP_QSTR_limit, MP_ARG_OBJ, {.u_obj = mp_const_none} }, - { MP_QSTR_file, MP_ARG_OBJ, {.u_obj = mp_const_none} }, - { MP_QSTR_chain, MP_ARG_BOOL, {.u_bool = true} }, - }; - - mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; - mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - mp_print_t print = mp_plat_print; - if (args[ARG_file].u_obj != mp_const_none) { - #if MICROPY_PY_IO && MICROPY_PY_SYS_STDFILES - mp_get_stream_raise(args[ARG_file].u_obj, MP_STREAM_OP_WRITE); - print.data = MP_OBJ_TO_PTR(args[ARG_file].u_obj); - print.print_strn = mp_stream_write_adaptor; - #else - mp_raise_NotImplementedError(translate("file write is not available")); - #endif - } - - traceback_exception_common(&print, args[ARG_value].u_obj, args[ARG_tb].u_obj, args[ARG_limit].u_obj); + traceback_exception_common(true, &print, n_args, pos_args, kw_args); return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(traceback_print_exception_obj, 0, traceback_print_exception); From 47759294e0ba9187ab6cf9a151d3ddb41a37e73a Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 13 Oct 2022 08:51:41 -0500 Subject: [PATCH 3/6] code changes for supporting 1-arg print_exception --- shared-bindings/traceback/__init__.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/shared-bindings/traceback/__init__.c b/shared-bindings/traceback/__init__.c index 08744d8009..b2221a4c8e 100644 --- a/shared-bindings/traceback/__init__.c +++ b/shared-bindings/traceback/__init__.c @@ -39,11 +39,11 @@ //| ... STATIC void traceback_exception_common(bool is_print_exception, mp_print_t *print, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - enum { ARG_etype, ARG_value, ARG_tb, ARG_limit, ARG_file, ARG_chain }; + enum { ARG_exc, ARG_value, ARG_tb, ARG_limit, ARG_file, ARG_chain }; static const mp_arg_t allowed_args[] = { - { MP_QSTR_etype, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = MP_OBJ_NULL} }, - { MP_QSTR_value, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = MP_OBJ_NULL} }, - { MP_QSTR_tb, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = MP_OBJ_NULL} }, + { MP_QSTR_, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = MP_OBJ_NULL} }, + { MP_QSTR_value, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, + { MP_QSTR_tb, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, { MP_QSTR_limit, MP_ARG_OBJ, {.u_obj = mp_const_none} }, { MP_QSTR_file, MP_ARG_OBJ, {.u_obj = mp_const_none} }, { MP_QSTR_chain, MP_ARG_BOOL, {.u_bool = true} }, @@ -53,6 +53,9 @@ STATIC void traceback_exception_common(bool is_print_exception, mp_print_t *prin mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); mp_obj_t value = args[ARG_value].u_obj; + if (value == MP_OBJ_NULL) { + value = args[ARG_exc].u_obj; + } mp_obj_t tb_obj = args[ARG_tb].u_obj; mp_obj_t limit_obj = args[ARG_limit].u_obj; @@ -88,7 +91,9 @@ STATIC void traceback_exception_common(bool is_print_exception, mp_print_t *prin mp_obj_exception_t *exc = mp_obj_exception_get_native(value); mp_obj_traceback_t *trace_backup = exc->traceback; - if (tb_obj != mp_const_none && print_tb) { + if (tb_obj == MP_OBJ_NULL) { + /* Print the traceback's exception as is */ + } else if (tb_obj != mp_const_none && print_tb) { exc->traceback = mp_arg_validate_type(tb_obj, &mp_type_traceback, MP_QSTR_tb); } else { exc->traceback = (mp_obj_traceback_t *)&mp_const_empty_traceback_obj; From 448eb1b70b93c35b611aefdff486cd12f1fc7570 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 13 Oct 2022 09:24:46 -0500 Subject: [PATCH 4/6] Document the 3.10-style calling pattern --- shared-bindings/traceback/__init__.c | 49 +++++++++++++++++++--------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/shared-bindings/traceback/__init__.c b/shared-bindings/traceback/__init__.c index b2221a4c8e..bc5776db2b 100644 --- a/shared-bindings/traceback/__init__.c +++ b/shared-bindings/traceback/__init__.c @@ -104,14 +104,24 @@ STATIC void traceback_exception_common(bool is_print_exception, mp_print_t *prin } //| def format_exception( -//| etype: Type[BaseException], -//| value: BaseException, -//| tb: TracebackType, +//| exc: BaseException | Type[BaseException], +//| /, +//| value: Optional[BaseException] = None, +//| tb: Optional[TracebackType] = None, //| limit: Optional[int] = None, //| chain: Optional[bool] = True, -//| ) -> None: +//| ) -> str: //| """Format a stack trace and the exception information. //| +//| If the exception value is passed in ``exc``, then this exception value and its +//| associated traceback are used. This is compatible with CPython 3.10 and newer. +//| +//| If the exception value is passed in ``value``, then any value passed in for +//| ``exc`` is ignored. ``value`` is used as the exception value and the +//| traceback in the ``tb`` argument is used. In this case, if ``tb`` is None, +//| no traceback will be shown. This is compatible with CPython 3.5 and +//| newer. +//| //| The arguments have the same meaning as the corresponding arguments //| to print_exception(). The return value is a list of strings, each //| ending in a newline and some containing internal newlines. When @@ -120,15 +130,13 @@ STATIC void traceback_exception_common(bool is_print_exception, mp_print_t *prin //| //| .. note:: Setting ``chain`` will have no effect as chained exceptions are not yet implemented. //| -//| :param Type[BaseException] etype: This is ignored and inferred from the type of ``value``. -//| :param BaseException value: The exception. Must be an instance of `BaseException`. -//| :param TracebackType tb: The traceback object. If `None`, the traceback will not be printed. +//| :param exc: The exception. Must be an instance of `BaseException`. Unused if value is specified. +//| :param value: If specified, is used in place of ``exc``. +//| :param TracebackType tb: When value is alsp specified, ``tb`` is used in place of the exception's own traceback. If `None`, the traceback will not be printed. //| :param int limit: Print up to limit stack trace entries (starting from the caller’s frame) if limit is positive. //| Otherwise, print the last ``abs(limit)`` entries. If limit is omitted or None, all entries are printed. //| :param bool chain: If `True` then chained exceptions will be printed (note: not yet implemented). -//| //| """ -//| ... //| STATIC mp_obj_t traceback_format_exception(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { mp_print_t print; @@ -141,21 +149,30 @@ STATIC mp_obj_t traceback_format_exception(size_t n_args, const mp_obj_t *pos_ar STATIC MP_DEFINE_CONST_FUN_OBJ_KW(traceback_format_exception_obj, 0, traceback_format_exception); //| def print_exception( -//| etype: Type[BaseException], -//| value: BaseException, -//| tb: TracebackType, +//| exc: BaseException | Type[BaseException], +//| /, +//| value: Optional[BaseException] = None, +//| tb: Optional[TracebackType] = None, //| limit: Optional[int] = None, //| file: Optional[io.FileIO] = None, //| chain: Optional[bool] = True, //| ) -> None: -//| //| """Prints exception information and stack trace entries. //| +//| If the exception value is passed in ``exc``, then this exception value and its +//| associated traceback are used. This is compatible with CPython 3.10 and newer. +//| +//| If the exception value is passed in ``value``, then any value passed in for +//| ``exc`` is ignored. ``value`` is used as the exception value and the +//| traceback in the ``tb`` argument is used. In this case, if ``tb`` is None, +//| no traceback will be shown. This is compatible with CPython 3.5 and +//| newer. +//| //| .. note:: Setting ``chain`` will have no effect as chained exceptions are not yet implemented. //| -//| :param Type[BaseException] etype: This is ignored and inferred from the type of ``value``. -//| :param BaseException value: The exception. Must be an instance of `BaseException`. -//| :param TracebackType tb: The traceback object. If `None`, the traceback will not be printed. +//| :param exc: The exception. Must be an instance of `BaseException`. Unused if value is specified. +//| :param value: If specified, is used in place of ``exc``. +//| :param tb: When value is alsp specified, ``tb`` is used in place of the exception's own traceback. If `None`, the traceback will not be printed. //| :param int limit: Print up to limit stack trace entries (starting from the caller’s frame) if limit is positive. //| Otherwise, print the last ``abs(limit)`` entries. If limit is omitted or None, all entries are printed. //| :param io.FileIO file: If file is omitted or `None`, the output goes to `sys.stderr`; otherwise it should be an open From 9ecb905061c1d5864009029098b485280015c937 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 13 Oct 2022 09:24:56 -0500 Subject: [PATCH 5/6] Test new-style exception printing --- tests/circuitpython/traceback_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/circuitpython/traceback_test.py b/tests/circuitpython/traceback_test.py index 6f51ceede8..5377f1dca4 100644 --- a/tests/circuitpython/traceback_test.py +++ b/tests/circuitpython/traceback_test.py @@ -15,7 +15,7 @@ except Exception as exc: print("\nNo Trace:") traceback.print_exception(None, exc, None) print("\nDefault Trace:") - traceback.print_exception(None, exc, exc.__traceback__) + traceback.print_exception(exc) print("\nLimit=1 Trace:") traceback.print_exception(None, exc, exc.__traceback__, limit=1) print("\nLimit=0 Trace:") From febc7a8514f9efbff8394f808483757375aca2e4 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 13 Oct 2022 09:29:21 -0500 Subject: [PATCH 6/6] 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):