traceback — 异常和栈的回溯
优质
小牛编辑
134浏览
2023-12-01
Supporting Functions
# traceback_example.py
import traceback
import sys
def produce_exception(recursion_level=2):
sys.stdout.flush()
if recursion_level:
produce_exception(recursion_level - 1)
else:
raise RuntimeError()
def call_function(f, recursion_level=2):
if recursion_level:
return call_function(f, recursion_level - 1)
else:
return f()
Examining the Stack
# traceback_stacksummary.py
import traceback
import sys
from traceback_example import call_function
def f():
summary = traceback.StackSummary.extract(
traceback.walk_stack(None)
)
print(''.join(summary.format()))
print('Calling f() directly:')
f()
print()
print('Calling f() from 3 levels deep:')
call_function(f)
# traceback_framesummary.py
import traceback
import sys
from traceback_example import call_function
template = (
'{fs.filename:<26}:{fs.lineno}:{fs.name}:\n'
' {fs.line}'
)
def f():
summary = traceback.StackSummary.extract(
traceback.walk_stack(None)
)
for fs in summary:
print(template.format(fs=fs))
print('Calling f() directly:')
f()
print()
print('Calling f() from 3 levels deep:')
call_function(f)
TracebackException
# traceback_tracebackexception.py
import traceback
import sys
from traceback_example import produce_exception
print('with no exception:')
exc_type, exc_value, exc_tb = sys.exc_info()
tbe = traceback.TracebackException(exc_type, exc_value, exc_tb)
print(''.join(tbe.format()))
print('\nwith exception:')
try:
produce_exception()
except Exception as err:
exc_type, exc_value, exc_tb = sys.exc_info()
tbe = traceback.TracebackException(
exc_type, exc_value, exc_tb,
)
print(''.join(tbe.format()))
print('\nexception only:')
print(''.join(tbe.format_exception_only()))
Low-level Exception APIs
# traceback_print_exc.py
import traceback
import sys
from traceback_example import produce_exception
print('print_exc() with no exception:')
traceback.print_exc(file=sys.stdout)
print()
try:
produce_exception()
except Exception as err:
print('print_exc():')
traceback.print_exc(file=sys.stdout)
print()
print('print_exc(1):')
traceback.print_exc(limit=1, file=sys.stdout)
# traceback_print_exception.py
import traceback
import sys
from traceback_example import produce_exception
try:
produce_exception()
except Exception as err:
print('print_exception():')
exc_type, exc_value, exc_tb = sys.exc_info()
traceback.print_exception(exc_type, exc_value, exc_tb)
# traceback_format_exception.py
import traceback
import sys
from pprint import pprint
from traceback_example import produce_exception
try:
produce_exception()
except Exception as err:
print('format_exception():')
exc_type, exc_value, exc_tb = sys.exc_info()
pprint(
traceback.format_exception(exc_type, exc_value, exc_tb),
width=65,
)
# traceback_extract_tb.py
import traceback
import sys
import os
from traceback_example import produce_exception
template = '{filename:<23}:{linenum}:{funcname}:\n {source}'
try:
produce_exception()
except Exception as err:
print('format_exception():')
exc_type, exc_value, exc_tb = sys.exc_info()
for tb_info in traceback.extract_tb(exc_tb):
filename, linenum, funcname, source = tb_info
if funcname != '<module>':
funcname = funcname + '()'
print(template.format(
filename=os.path.basename(filename),
linenum=linenum,
source=source,
funcname=funcname)
)
Low-level Stack APIs
# traceback_print_stack.py
import traceback
import sys
from traceback_example import call_function
def f():
traceback.print_stack(file=sys.stdout)
print('Calling f() directly:')
f()
print()
print('Calling f() from 3 levels deep:')
call_function(f)
# traceback_format_stack.py
import traceback
import sys
from pprint import pprint
from traceback_example import call_function
def f():
return traceback.format_stack()
formatted_stack = call_function(f)
pprint(formatted_stack)
# traceback_extract_stack.py
import traceback
import sys
import os
from traceback_example import call_function
template = '{filename:<26}:{linenum}:{funcname}:\n {source}'
def f():
return traceback.extract_stack()
stack = call_function(f)
for filename, linenum, funcname, source in stack:
if funcname != '<module>':
funcname = funcname + '()'
print(template.format(
filename=os.path.basename(filename),
linenum=linenum,
source=source,
funcname=funcname)
)