Skip to content

Commit dce4c9d

Browse files
authored
Merge pull request #7046 from jepler/traceback-mod-improvements
Traceback module improvements
2 parents 8825e7f + febc7a8 commit dce4c9d

File tree

2 files changed

+81
-58
lines changed

2 files changed

+81
-58
lines changed

shared-bindings/traceback/__init__.c

Lines changed: 79 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,45 @@
3838
//| """
3939
//| ...
4040

41-
STATIC void traceback_exception_common(mp_print_t *print, mp_obj_t value, mp_obj_t tb_obj, mp_obj_t limit_obj) {
41+
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) {
42+
enum { ARG_exc, ARG_value, ARG_tb, ARG_limit, ARG_file, ARG_chain };
43+
static const mp_arg_t allowed_args[] = {
44+
{ MP_QSTR_, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = MP_OBJ_NULL} },
45+
{ MP_QSTR_value, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
46+
{ MP_QSTR_tb, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
47+
{ MP_QSTR_limit, MP_ARG_OBJ, {.u_obj = mp_const_none} },
48+
{ MP_QSTR_file, MP_ARG_OBJ, {.u_obj = mp_const_none} },
49+
{ MP_QSTR_chain, MP_ARG_BOOL, {.u_bool = true} },
50+
};
51+
52+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
53+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
54+
55+
mp_obj_t value = args[ARG_value].u_obj;
56+
if (value == MP_OBJ_NULL) {
57+
value = args[ARG_exc].u_obj;
58+
}
59+
mp_obj_t tb_obj = args[ARG_tb].u_obj;
60+
mp_obj_t limit_obj = args[ARG_limit].u_obj;
61+
62+
if (args[ARG_file].u_obj != mp_const_none) {
63+
if (!is_print_exception) {
64+
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
65+
mp_arg_error_terse_mismatch();
66+
#else
67+
mp_raise_msg_varg(&mp_type_TypeError, MP_ERROR_TEXT("unexpected keyword argument '%q'"), MP_QSTR_file);
68+
#endif
69+
70+
}
71+
#if MICROPY_PY_IO && MICROPY_PY_SYS_STDFILES
72+
mp_get_stream_raise(args[ARG_file].u_obj, MP_STREAM_OP_WRITE);
73+
print->data = MP_OBJ_TO_PTR(args[ARG_file].u_obj);
74+
print->print_strn = mp_stream_write_adaptor;
75+
#else
76+
mp_raise_NotImplementedError(translate("file write is not available"));
77+
#endif
78+
}
79+
4280
if (!mp_obj_is_exception_instance(value)) {
4381
mp_raise_TypeError(translate("invalid exception"));
4482
}
@@ -53,7 +91,9 @@ STATIC void traceback_exception_common(mp_print_t *print, mp_obj_t value, mp_obj
5391
mp_obj_exception_t *exc = mp_obj_exception_get_native(value);
5492
mp_obj_traceback_t *trace_backup = exc->traceback;
5593

56-
if (tb_obj != mp_const_none && print_tb) {
94+
if (tb_obj == MP_OBJ_NULL) {
95+
/* Print the traceback's exception as is */
96+
} else if (tb_obj != mp_const_none && print_tb) {
5797
exc->traceback = mp_arg_validate_type(tb_obj, &mp_type_traceback, MP_QSTR_tb);
5898
} else {
5999
exc->traceback = (mp_obj_traceback_t *)&mp_const_empty_traceback_obj;
@@ -64,14 +104,24 @@ STATIC void traceback_exception_common(mp_print_t *print, mp_obj_t value, mp_obj
64104
}
65105

66106
//| def format_exception(
67-
//| etype: Type[BaseException],
68-
//| value: BaseException,
69-
//| tb: TracebackType,
107+
//| exc: BaseException | Type[BaseException],
108+
//| /,
109+
//| value: Optional[BaseException] = None,
110+
//| tb: Optional[TracebackType] = None,
70111
//| limit: Optional[int] = None,
71112
//| chain: Optional[bool] = True,
72-
//| ) -> None:
113+
//| ) -> List[str]:
73114
//| """Format a stack trace and the exception information.
74115
//|
116+
//| If the exception value is passed in ``exc``, then this exception value and its
117+
//| associated traceback are used. This is compatible with CPython 3.10 and newer.
118+
//|
119+
//| If the exception value is passed in ``value``, then any value passed in for
120+
//| ``exc`` is ignored. ``value`` is used as the exception value and the
121+
//| traceback in the ``tb`` argument is used. In this case, if ``tb`` is None,
122+
//| no traceback will be shown. This is compatible with CPython 3.5 and
123+
//| newer.
124+
//|
75125
//| The arguments have the same meaning as the corresponding arguments
76126
//| to print_exception(). The return value is a list of strings, each
77127
//| ending in a newline and some containing internal newlines. When
@@ -80,54 +130,50 @@ STATIC void traceback_exception_common(mp_print_t *print, mp_obj_t value, mp_obj
80130
//|
81131
//| .. note:: Setting ``chain`` will have no effect as chained exceptions are not yet implemented.
82132
//|
83-
//| :param Type[BaseException] etype: This is ignored and inferred from the type of ``value``.
84-
//| :param BaseException value: The exception. Must be an instance of `BaseException`.
85-
//| :param TracebackType tb: The traceback object. If `None`, the traceback will not be printed.
133+
//| :param exc: The exception. Must be an instance of `BaseException`. Unused if value is specified.
134+
//| :param value: If specified, is used in place of ``exc``.
135+
//| :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.
86136
//| :param int limit: Print up to limit stack trace entries (starting from the caller’s frame) if limit is positive.
87137
//| Otherwise, print the last ``abs(limit)`` entries. If limit is omitted or None, all entries are printed.
88138
//| :param bool chain: If `True` then chained exceptions will be printed (note: not yet implemented).
89-
//|
90139
//| """
91-
//| ...
92140
//|
93141
STATIC mp_obj_t traceback_format_exception(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
94-
enum { ARG_etype, ARG_value, ARG_tb, ARG_limit, ARG_chain };
95-
static const mp_arg_t allowed_args[] = {
96-
{ MP_QSTR_etype, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = MP_OBJ_NULL} },
97-
{ MP_QSTR_value, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = MP_OBJ_NULL} },
98-
{ MP_QSTR_tb, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = MP_OBJ_NULL} },
99-
{ MP_QSTR_limit, MP_ARG_OBJ, {.u_obj = mp_const_none} },
100-
{ MP_QSTR_chain, MP_ARG_BOOL, {.u_bool = true} },
101-
};
102-
103-
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
104-
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
105-
106142
mp_print_t print;
107143
vstr_t vstr;
108144
vstr_init_print(&vstr, 0, &print);
109-
traceback_exception_common(&print, args[ARG_value].u_obj, args[ARG_tb].u_obj, args[ARG_limit].u_obj);
110-
return mp_obj_new_str_from_vstr(&mp_type_str, &vstr);
145+
traceback_exception_common(false, &print, n_args, pos_args, kw_args);
146+
mp_obj_t output = mp_obj_new_str_from_vstr(&mp_type_str, &vstr);
147+
return mp_obj_new_list(1, &output);
111148
}
112149

113150
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(traceback_format_exception_obj, 0, traceback_format_exception);
114151

115152
//| def print_exception(
116-
//| etype: Type[BaseException],
117-
//| value: BaseException,
118-
//| tb: TracebackType,
153+
//| exc: BaseException | Type[BaseException],
154+
//| /,
155+
//| value: Optional[BaseException] = None,
156+
//| tb: Optional[TracebackType] = None,
119157
//| limit: Optional[int] = None,
120158
//| file: Optional[io.FileIO] = None,
121159
//| chain: Optional[bool] = True,
122160
//| ) -> None:
123-
//|
124161
//| """Prints exception information and stack trace entries.
125162
//|
163+
//| If the exception value is passed in ``exc``, then this exception value and its
164+
//| associated traceback are used. This is compatible with CPython 3.10 and newer.
165+
//|
166+
//| If the exception value is passed in ``value``, then any value passed in for
167+
//| ``exc`` is ignored. ``value`` is used as the exception value and the
168+
//| traceback in the ``tb`` argument is used. In this case, if ``tb`` is None,
169+
//| no traceback will be shown. This is compatible with CPython 3.5 and
170+
//| newer.
171+
//|
126172
//| .. note:: Setting ``chain`` will have no effect as chained exceptions are not yet implemented.
127173
//|
128-
//| :param Type[BaseException] etype: This is ignored and inferred from the type of ``value``.
129-
//| :param BaseException value: The exception. Must be an instance of `BaseException`.
130-
//| :param TracebackType tb: The traceback object. If `None`, the traceback will not be printed.
174+
//| :param exc: The exception. Must be an instance of `BaseException`. Unused if value is specified.
175+
//| :param value: If specified, is used in place of ``exc``.
176+
//| :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.
131177
//| :param int limit: Print up to limit stack trace entries (starting from the caller’s frame) if limit is positive.
132178
//| Otherwise, print the last ``abs(limit)`` entries. If limit is omitted or None, all entries are printed.
133179
//| :param io.FileIO file: If file is omitted or `None`, the output goes to `sys.stderr`; otherwise it should be an open
@@ -139,31 +185,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(traceback_format_exception_obj, 0, traceback_f
139185
//|
140186

141187
STATIC mp_obj_t traceback_print_exception(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
142-
enum { ARG_etype, ARG_value, ARG_tb, ARG_limit, ARG_file, ARG_chain };
143-
static const mp_arg_t allowed_args[] = {
144-
{ MP_QSTR_etype, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = MP_OBJ_NULL} },
145-
{ MP_QSTR_value, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = MP_OBJ_NULL} },
146-
{ MP_QSTR_tb, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = MP_OBJ_NULL} },
147-
{ MP_QSTR_limit, MP_ARG_OBJ, {.u_obj = mp_const_none} },
148-
{ MP_QSTR_file, MP_ARG_OBJ, {.u_obj = mp_const_none} },
149-
{ MP_QSTR_chain, MP_ARG_BOOL, {.u_bool = true} },
150-
};
151-
152-
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
153-
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
154-
155188
mp_print_t print = mp_plat_print;
156-
if (args[ARG_file].u_obj != mp_const_none) {
157-
#if MICROPY_PY_IO && MICROPY_PY_SYS_STDFILES
158-
mp_get_stream_raise(args[ARG_file].u_obj, MP_STREAM_OP_WRITE);
159-
print.data = MP_OBJ_TO_PTR(args[ARG_file].u_obj);
160-
print.print_strn = mp_stream_write_adaptor;
161-
#else
162-
mp_raise_NotImplementedError(translate("file write is not available"));
163-
#endif
164-
}
165-
166-
traceback_exception_common(&print, args[ARG_value].u_obj, args[ARG_tb].u_obj, args[ARG_limit].u_obj);
189+
traceback_exception_common(true, &print, n_args, pos_args, kw_args);
167190
return mp_const_none;
168191
}
169192
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(traceback_print_exception_obj, 0, traceback_print_exception);

tests/circuitpython/traceback_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ def fun():
1515
print("\nNo Trace:")
1616
traceback.print_exception(None, exc, None)
1717
print("\nDefault Trace:")
18-
traceback.print_exception(None, exc, exc.__traceback__)
18+
traceback.print_exception(exc)
1919
print("\nLimit=1 Trace:")
2020
traceback.print_exception(None, exc, exc.__traceback__, limit=1)
2121
print("\nLimit=0 Trace:")
2222
traceback.print_exception(None, exc, exc.__traceback__, limit=0)
2323
print("\nLimit=-1 Trace:")
24-
traceback.print_exception(None, exc, exc.__traceback__, limit=-1)
24+
print("".join(traceback.format_exception(None, exc, exc.__traceback__, limit=-1)), end="")
2525

2626

2727
class NonNativeException(Exception):

0 commit comments

Comments
 (0)