|
|
@@ -9,6 +9,8 @@ try:
|
|
|
b = a[6]
|
|
|
except KeyError:
|
|
|
actual = traceback.format_exc()
|
|
|
+ assert traceback.print_exc() is None
|
|
|
+ assert traceback.format_exc() == actual
|
|
|
|
|
|
expected = '''Traceback (most recent call last):
|
|
|
File "tests/802_traceback.py", line 9
|
|
|
@@ -20,4 +22,80 @@ if actual != expected:
|
|
|
print(actual)
|
|
|
print('--- EXPECTED RESULT ---')
|
|
|
print(expected)
|
|
|
- exit(1)
|
|
|
+ exit(1)
|
|
|
+
|
|
|
+
|
|
|
+def format_from_helper():
|
|
|
+ return traceback.format_exc()
|
|
|
+
|
|
|
+
|
|
|
+def format_from_try():
|
|
|
+ try:
|
|
|
+ return format_from_helper()
|
|
|
+ except Exception:
|
|
|
+ assert False
|
|
|
+
|
|
|
+
|
|
|
+assert traceback.format_exc() is None
|
|
|
+assert format_from_try() is None
|
|
|
+assert traceback.print_exc() is None
|
|
|
+
|
|
|
+try:
|
|
|
+ raise ValueError('outer')
|
|
|
+except ValueError:
|
|
|
+ outer = traceback.format_exc()
|
|
|
+ assert outer.endswith('ValueError: outer')
|
|
|
+ assert format_from_helper() == outer
|
|
|
+ assert format_from_try() == outer
|
|
|
+
|
|
|
+ # A try body has no exception of its own and must not hide its handler.
|
|
|
+ try:
|
|
|
+ assert traceback.format_exc() == outer
|
|
|
+ assert format_from_try() == outer
|
|
|
+ raise KeyError('inner')
|
|
|
+ except KeyError:
|
|
|
+ inner = traceback.format_exc()
|
|
|
+ assert inner.endswith("KeyError: 'inner'")
|
|
|
+ assert format_from_try() == inner
|
|
|
+
|
|
|
+ # Leaving the inner handler restores the outer exception.
|
|
|
+ assert traceback.format_exc() == outer
|
|
|
+ assert format_from_try() == outer
|
|
|
+
|
|
|
+assert traceback.format_exc() is None
|
|
|
+assert format_from_try() is None
|
|
|
+
|
|
|
+
|
|
|
+def handle_in_helper():
|
|
|
+ try:
|
|
|
+ raise RuntimeError('helper')
|
|
|
+ except RuntimeError:
|
|
|
+ actual = traceback.format_exc()
|
|
|
+ assert actual.endswith('RuntimeError: helper')
|
|
|
+ assert format_from_try() == actual
|
|
|
+
|
|
|
+
|
|
|
+try:
|
|
|
+ raise ValueError('caller')
|
|
|
+except ValueError:
|
|
|
+ caller = traceback.format_exc()
|
|
|
+ handle_in_helper()
|
|
|
+ assert traceback.format_exc() == caller
|
|
|
+ assert format_from_try() == caller
|
|
|
+
|
|
|
+
|
|
|
+class BrokenStr(Exception):
|
|
|
+ def __str__(self):
|
|
|
+ raise RuntimeError('str failed')
|
|
|
+
|
|
|
+
|
|
|
+try:
|
|
|
+ raise BrokenStr()
|
|
|
+except BrokenStr:
|
|
|
+ actual = traceback.format_exc()
|
|
|
+ assert actual.endswith('BrokenStr: <exception str() failed>')
|
|
|
+ assert format_from_try() == actual
|
|
|
+ assert traceback.print_exc() is None
|
|
|
+ assert traceback.format_exc() == actual
|
|
|
+
|
|
|
+assert traceback.format_exc() is None
|