tests: Add test for print_exception() function.

This commit is contained in:
Paul Sokolovsky 2014-12-10 20:35:10 +02:00 committed by Damien George
parent 6c3fc74656
commit e8487ea1be
1 changed files with 21 additions and 0 deletions

View File

@ -0,0 +1,21 @@
import io
import sys
if hasattr(sys, 'print_exception'):
print_exception = sys.print_exception
else:
import traceback
print_exception = lambda e, f: traceback.print_exception(None, e, None, file=f)
try:
1/0
except Exception as e:
print('caught')
buf = io.StringIO()
print_exception(e, buf)
s = buf.getvalue()
for l in s.split("\n"):
# uPy and CPy tracebacks differ in that CPy prints a source line for
# each traceback entry. In this case, we know that offending line
# has 4-space indent, so filter it out.
if not l.startswith(" "):
print(l)