Skip to content

Commit f317420

Browse files
committed
chore: make traceback module optional
some boards don't provide traceback so we can add a minimal implementation that works _good enough_ in those cases so asyncio can still be used
1 parent da943a7 commit f317420

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

asyncio/core.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616
"""
1717

1818
from adafruit_ticks import ticks_ms as ticks, ticks_diff, ticks_add
19-
import sys, select, traceback
19+
import sys, select
20+
21+
try:
22+
from traceback import print_exception
23+
except:
24+
from .traceback import print_exception
2025

2126
# Import TaskQueue and Task, preferring built-in C code over Python code
2227
try:
@@ -371,7 +376,7 @@ def default_exception_handler(loop, context):
371376
"""The default exception handler that is called."""
372377

373378
exc = context["exception"]
374-
traceback.print_exception(None, exc, exc.__traceback__)
379+
print_exception(None, exc, exc.__traceback__)
375380

376381
def call_exception_handler(context):
377382
"""Call the current exception handler. The argument *context* is passed through

asyncio/traceback.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
try:
2+
from typing import List
3+
except ImportError:
4+
pass
5+
6+
import sys
7+
8+
9+
def _print_err(*args):
10+
print(*args, file=sys.stderr)
11+
12+
13+
def _print_traceback(traceback, limit=None) -> List[str]:
14+
if limit is None:
15+
if hasattr(sys, "tracebacklimit"):
16+
limit = sys.tracebacklimit
17+
18+
n = 0
19+
while traceback is not None:
20+
frame = traceback.tb_frame
21+
line_number = traceback.tb_lineno
22+
frame_code = frame.f_code
23+
filename = frame_code.co_filename
24+
name = frame_code.co_name
25+
_print_err(' File "%s", line %d, in %s' % (filename, line_number, name))
26+
traceback = traceback.tb_next
27+
n = n + 1
28+
if limit is not None and n >= limit:
29+
break
30+
31+
32+
def print_exception(exception, value, traceback):
33+
if traceback:
34+
_print_err("Traceback (most recent call last):")
35+
_print_traceback(traceback)
36+
37+
if isinstance(exception, Exception) or isinstance(exception, BaseException):
38+
exception_type = type(exception).__name__
39+
elif hasattr(exception, "__name__"):
40+
exception_type = exception.__name__
41+
else:
42+
exception_type = type(value).__name__
43+
44+
valuestr = str(value)
45+
if value is None or not valuestr:
46+
_print_err(exception_type)
47+
else:
48+
_print_err("%s: %s" % (str(exception_type), valuestr))

0 commit comments

Comments
 (0)