-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from imnotjames/chore/optional-traceback
chore: make traceback module optional
- Loading branch information
Showing
2 changed files
with
64 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# SPDX-FileCopyrightText: 2019-2020 Damien P. George | ||
# | ||
# SPDX-License-Identifier: MIT | ||
# | ||
# MicroPython uasyncio module | ||
# MIT license; Copyright (c) 2019-2020 Damien P. George | ||
""" | ||
Fallback traceback module if the system traceback is missing. | ||
""" | ||
|
||
try: | ||
from typing import List | ||
except ImportError: | ||
pass | ||
|
||
import sys | ||
|
||
|
||
def _print_traceback(traceback, limit=None, file=sys.stderr) -> List[str]: | ||
if limit is None: | ||
if hasattr(sys, "tracebacklimit"): | ||
limit = sys.tracebacklimit | ||
|
||
n = 0 | ||
while traceback is not None: | ||
frame = traceback.tb_frame | ||
line_number = traceback.tb_lineno | ||
frame_code = frame.f_code | ||
filename = frame_code.co_filename | ||
name = frame_code.co_name | ||
print(' File "%s", line %d, in %s' % (filename, line_number, name), file=file) | ||
traceback = traceback.tb_next | ||
n = n + 1 | ||
if limit is not None and n >= limit: | ||
break | ||
|
||
|
||
def print_exception(exception, value=None, traceback=None, limit=None, file=sys.stderr): | ||
""" | ||
Print exception information and stack trace to file. | ||
""" | ||
if traceback: | ||
print("Traceback (most recent call last):", file=file) | ||
_print_traceback(traceback, limit=limit, file=file) | ||
|
||
if isinstance(exception, BaseException): | ||
exception_type = type(exception).__name__ | ||
elif hasattr(exception, "__name__"): | ||
exception_type = exception.__name__ | ||
else: | ||
exception_type = type(value).__name__ | ||
|
||
valuestr = str(value) | ||
if value is None or not valuestr: | ||
print(exception_type, file=file) | ||
else: | ||
print("%s: %s" % (str(exception_type), valuestr), file=file) |