[python] traceback 으로 이상 스 택 을 가 져 오고 출력 이상 을 포맷 합 니 다.
test.py
#encoding:utf-8
import sys
import traceback
def testerr():
testerr2()
def testerr2():
a = "test"
b = 123
print(c) # ,
try:
testerr()
except Exception as e:
exc_type, exc_value, exc_traceback = sys.exc_info()
frame = traceback.extract_tb(exc_traceback) # FrameSummary
#
mapStack = map(str, frame)
sErrorStack = "
".join(mapStack)
# traceback.format_exc()
print(" :")
print(sErrorStack)
#
print(repr(e))
#
lasttb = None
while exc_traceback:
lasttb = exc_traceback
exc_traceback = exc_traceback.tb_next
print(" :")
print(lasttb.tb_frame.f_locals)
출력 결과:
이상 스 택: NameError ("name" c "is not defined") 부분 변수: {"a": "test", "b": 123}
장식 기 버 전
from functools import wraps
import sys
import traceback
def FormatError(func):
@wraps(func)
def Format(*args, **kwargs):
try:
func(*args, **kwargs)
except Exception as e:
exc_type, exc_value, exc_traceback = sys.exc_info()
frame = traceback.extract_tb(exc_traceback) # FrameSummary
#
mapStack = map(str, frame)
sErrorStack = "
".join(mapStack)
print(" :")
print(sErrorStack)
#
print(repr(e))
#
lasttb = None
while exc_traceback:
lasttb = exc_traceback
exc_traceback = exc_traceback.tb_next
print(" :")
print(lasttb.tb_frame.f_locals)
return Format
@FormatError
def testerr():
testerr2()
def testerr2():
a = "test"
b = 123
print(c) # ,
testerr()
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
파이썬을 사용하여 10진수를 bin, 8진수 및 16진수 형식으로 변환하는 방법은 무엇입니까?텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.