Skip to content

Commit 59a2cd0

Browse files
adjustment some logic
1 parent 4b77fa2 commit 59a2cd0

File tree

1 file changed

+40
-33
lines changed

1 file changed

+40
-33
lines changed

Lib/traceback.py

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -227,36 +227,45 @@ def _traceback_to_tuples(tb):
227227

228228

229229
def _safe_string(value, what, func=str,
230-
exception_target=None, exception_exclude=None,
231-
_seen=threading.local()):
232-
if not hasattr(_seen, "_seen"):
233-
_seen._seen = set()
234-
if not hasattr(_seen, "times"):
235-
_seen.times = 0
230+
exception_target=None, exception_exclude=None):
236231
try:
237-
_seen.times += 1
238232
return func(value)
239233
except:
240234
if isinstance(exception_target, list):
241235
typ, val, tb = sys.exc_info()
242-
tb_tuple = _traceback_to_tuples(tb)
243-
if tb_tuple not in _seen._seen:
244-
_seen._seen.add(tb_tuple)
245-
if exception_exclude:
246-
_remove_exception(val, exception_exclude)
247-
msg = "".join(TracebackException(typ, val, tb).format())
248-
while msg.endswith("\n") or msg.endswith(" "):
249-
msg = msg[:-1]
250-
exception_target.append(
251-
f"\nException ignored in {what} {func.__name__}():"
252-
)
253-
exception_target.append(msg)
236+
_add_exception_note(typ, val, tb, f"{what} {func.__name__}()",
237+
exception_target, exception_exclude)
254238
return f"<{what} {func.__name__}() failed>"
255-
finally:
256-
_seen.times -= 1
257-
if _seen.times <= 0:
258-
_seen.times = 0
259-
_seen._seen.clear()
239+
240+
241+
_ADD_EXC_NOTE_LIMIT = 10
242+
243+
244+
def _add_exception_note(exc_type, exc_value, exc_tb, where,
245+
exception_target, exception_exclude=None, _seen=threading.local()):
246+
if not hasattr(_seen, "_seen"):
247+
_seen._seen = set()
248+
if not hasattr(_seen, "times"):
249+
_seen.times = 0
250+
if not isinstance(exception_target, list):
251+
return
252+
_seen.times += 1
253+
tb_tuple = _traceback_to_tuples(exc_tb)
254+
if tb_tuple not in _seen._seen and _seen.times <= _ADD_EXC_NOTE_LIMIT:
255+
_seen._seen.add(tb_tuple)
256+
if exception_exclude:
257+
_remove_exception(exc_value, exception_exclude)
258+
msg = "".join(TracebackException(exc_type, exc_value, exc_tb).format())
259+
while msg.endswith("\n") or msg.endswith(" "):
260+
msg = msg[:-1]
261+
exception_target.append(
262+
f"\nException ignored in {where}:"
263+
)
264+
exception_target.append(msg)
265+
_seen.times -= 1
266+
if _seen.times <= 0:
267+
_seen.times = 0
268+
_seen._seen.clear()
260269

261270
# --
262271

@@ -1205,7 +1214,8 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None,
12051214
i, "note", str, exception_target, exc_value
12061215
)
12071216
)
1208-
self.__notes__ = final_string_list + exception_target
1217+
self.__notes__ = final_string_list
1218+
self.exception_target = exception_target
12091219
if lookup_lines:
12101220
self._load_lines()
12111221
self.__suppress_context__ = (
@@ -1339,6 +1349,7 @@ def format_exception_only(self, *, show_group=False, _depth=0, **kwargs):
13391349
well, recursively, with indentation relative to their nesting depth.
13401350
"""
13411351
colorize = kwargs.get("colorize", False)
1352+
exception_target = kwargs.get("exception_target", True)
13421353

13431354
indent = 3 * _depth * ' '
13441355
if not self._have_exc_type:
@@ -1361,15 +1372,11 @@ def format_exception_only(self, *, show_group=False, _depth=0, **kwargs):
13611372
else:
13621373
yield from [indent + l for l in self._format_syntax_error(stype, colorize=colorize)]
13631374

1364-
if (
1365-
isinstance(self.__notes__, collections.abc.Sequence)
1366-
and not isinstance(self.__notes__, (str, bytes))
1367-
):
1368-
for note in self.__notes__:
1369-
note = _safe_string(note, 'note')
1375+
for note in self.__notes__:
1376+
yield from [indent + l + '\n' for l in note.split('\n')]
1377+
if exception_target:
1378+
for note in self.exception_target:
13701379
yield from [indent + l + '\n' for l in note.split('\n')]
1371-
elif self.__notes__ is not None:
1372-
yield indent + "{}\n".format(_safe_string(self.__notes__, '__notes__', func=repr))
13731380

13741381
if self.exceptions and show_group:
13751382
for ex in self.exceptions:

0 commit comments

Comments
 (0)