Hướng dẫn print traceback python - in truy nguyên python

Mã nguồn: lib/traceback.py Lib/traceback.py

Show

Mô -đun này cung cấp một giao diện tiêu chuẩn để trích xuất, định dạng và in dấu vết ngăn xếp của các chương trình Python. Nó chính xác bắt chước hành vi của trình thông dịch Python khi nó in một dấu vết ngăn xếp. Điều này rất hữu ích khi bạn muốn in các dấu vết ngăn xếp dưới điều khiển chương trình, chẳng hạn như trong một trình bao bọc trên mạng xung quanh trình thông dịch.

Mô -đun sử dụng các đối tượng Traceback - đây là loại đối tượng được lưu trữ trong biến sys.last_traceback và được trả về dưới dạng mục thứ ba từ sys.exc_info().

Mô -đun xác định các chức năng sau:

Traceback.print_tb (TB, LEDE = none, file = none) ¶print_tb(tb, limit=None, file=None)

In lên để giới hạn các mục nhập dấu vết từ TB đối tượng Traceback (bắt đầu từ khung người gọi) nếu giới hạn là dương. Nếu không, in các mục abs(limit) cuối cùng. Nếu giới hạn bị bỏ qua hoặc None, tất cả các mục được in. Nếu tệp bị bỏ qua hoặc None, đầu ra sẽ chuyển đến

import sys, traceback

def lumberjack():
    bright_side_of_life()

def bright_side_of_life():
    return tuple()[0]

try:
    lumberjack()
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info()
    print("*** print_tb:")
    traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
    print("*** print_exception:")
    traceback.print_exception(exc_value, limit=2, file=sys.stdout)
    print("*** print_exc:")
    traceback.print_exc(limit=2, file=sys.stdout)
    print("*** format_exc, first and last line:")
    formatted_lines = traceback.format_exc().splitlines()
    print(formatted_lines[0])
    print(formatted_lines[-1])
    print("*** format_exception:")
    print(repr(traceback.format_exception(exc_value)))
    print("*** extract_tb:")
    print(repr(traceback.extract_tb(exc_traceback)))
    print("*** format_tb:")
    print(repr(traceback.format_tb(exc_traceback)))
    print("*** tb_lineno:", exc_traceback.tb_lineno)
0; Nếu không, nó phải là một tệp mở hoặc đối tượng giống như tệp để nhận đầu ra.

Đã thay đổi trong phiên bản 3.5: Đã thêm hỗ trợ giới hạn âm.Added negative limit support.

Traceback.print_exception (exc, /, [value, tb,] giới hạn = none, file = none, chuỗi = true)print_exception(exc, /, [value, tb, ]limit=None, file=None, chain=True)

In thông tin ngoại lệ và ngăn xếp các mục nhập từ TB đối tượng Traceback sang tệp. Điều này khác với

import sys, traceback

def lumberjack():
    bright_side_of_life()

def bright_side_of_life():
    return tuple()[0]

try:
    lumberjack()
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info()
    print("*** print_tb:")
    traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
    print("*** print_exception:")
    traceback.print_exception(exc_value, limit=2, file=sys.stdout)
    print("*** print_exc:")
    traceback.print_exc(limit=2, file=sys.stdout)
    print("*** format_exc, first and last line:")
    formatted_lines = traceback.format_exc().splitlines()
    print(formatted_lines[0])
    print(formatted_lines[-1])
    print("*** format_exception:")
    print(repr(traceback.format_exception(exc_value)))
    print("*** extract_tb:")
    print(repr(traceback.extract_tb(exc_traceback)))
    print("*** format_tb:")
    print(repr(traceback.format_tb(exc_traceback)))
    print("*** tb_lineno:", exc_traceback.tb_lineno)
1 theo những cách sau:

  • Nếu TB không phải là None, nó sẽ in tiêu đề

    import sys, traceback
    
    def lumberjack():
        bright_side_of_life()
    
    def bright_side_of_life():
        return tuple()[0]
    
    try:
        lumberjack()
    except IndexError:
        exc_type, exc_value, exc_traceback = sys.exc_info()
        print("*** print_tb:")
        traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
        print("*** print_exception:")
        traceback.print_exception(exc_value, limit=2, file=sys.stdout)
        print("*** print_exc:")
        traceback.print_exc(limit=2, file=sys.stdout)
        print("*** format_exc, first and last line:")
        formatted_lines = traceback.format_exc().splitlines()
        print(formatted_lines[0])
        print(formatted_lines[-1])
        print("*** format_exception:")
        print(repr(traceback.format_exception(exc_value)))
        print("*** extract_tb:")
        print(repr(traceback.extract_tb(exc_traceback)))
        print("*** format_tb:")
        print(repr(traceback.format_tb(exc_traceback)))
        print("*** tb_lineno:", exc_traceback.tb_lineno)
    
    3

  • Nó in loại ngoại lệ và giá trị sau dấu vết ngăn xếp

  • Nếu loại (giá trị) là

    import sys, traceback
    
    def lumberjack():
        bright_side_of_life()
    
    def bright_side_of_life():
        return tuple()[0]
    
    try:
        lumberjack()
    except IndexError:
        exc_type, exc_value, exc_traceback = sys.exc_info()
        print("*** print_tb:")
        traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
        print("*** print_exception:")
        traceback.print_exception(exc_value, limit=2, file=sys.stdout)
        print("*** print_exc:")
        traceback.print_exc(limit=2, file=sys.stdout)
        print("*** format_exc, first and last line:")
        formatted_lines = traceback.format_exc().splitlines()
        print(formatted_lines[0])
        print(formatted_lines[-1])
        print("*** format_exception:")
        print(repr(traceback.format_exception(exc_value)))
        print("*** extract_tb:")
        print(repr(traceback.extract_tb(exc_traceback)))
        print("*** format_tb:")
        print(repr(traceback.format_tb(exc_traceback)))
        print("*** tb_lineno:", exc_traceback.tb_lineno)
    
    4 và giá trị có định dạng thích hợp, nó sẽ in dòng trong đó lỗi cú pháp xảy ra với một chăm sóc cho biết vị trí gần đúng của lỗi.

Vì Python 3.10, thay vì chuyển giá trị và bệnh lao, một đối tượng ngoại lệ có thể được truyền làm đối số đầu tiên. Nếu giá trị và bệnh lao được cung cấp, đối số đầu tiên bị bỏ qua để cung cấp khả năng tương thích ngược.

Đối số giới hạn tùy chọn có ý nghĩa tương tự như đối với

import sys, traceback

def lumberjack():
    bright_side_of_life()

def bright_side_of_life():
    return tuple()[0]

try:
    lumberjack()
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info()
    print("*** print_tb:")
    traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
    print("*** print_exception:")
    traceback.print_exception(exc_value, limit=2, file=sys.stdout)
    print("*** print_exc:")
    traceback.print_exc(limit=2, file=sys.stdout)
    print("*** format_exc, first and last line:")
    formatted_lines = traceback.format_exc().splitlines()
    print(formatted_lines[0])
    print(formatted_lines[-1])
    print("*** format_exception:")
    print(repr(traceback.format_exception(exc_value)))
    print("*** extract_tb:")
    print(repr(traceback.extract_tb(exc_traceback)))
    print("*** format_tb:")
    print(repr(traceback.format_tb(exc_traceback)))
    print("*** tb_lineno:", exc_traceback.tb_lineno)
1. Nếu chuỗi là đúng (mặc định), thì các ngoại lệ được xích (thuộc tính
import sys, traceback

def lumberjack():
    bright_side_of_life()

def bright_side_of_life():
    return tuple()[0]

try:
    lumberjack()
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info()
    print("*** print_tb:")
    traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
    print("*** print_exception:")
    traceback.print_exception(exc_value, limit=2, file=sys.stdout)
    print("*** print_exc:")
    traceback.print_exc(limit=2, file=sys.stdout)
    print("*** format_exc, first and last line:")
    formatted_lines = traceback.format_exc().splitlines()
    print(formatted_lines[0])
    print(formatted_lines[-1])
    print("*** format_exception:")
    print(repr(traceback.format_exception(exc_value)))
    print("*** extract_tb:")
    print(repr(traceback.extract_tb(exc_traceback)))
    print("*** format_tb:")
    print(repr(traceback.format_tb(exc_traceback)))
    print("*** tb_lineno:", exc_traceback.tb_lineno)
6 hoặc
import sys, traceback

def lumberjack():
    bright_side_of_life()

def bright_side_of_life():
    return tuple()[0]

try:
    lumberjack()
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info()
    print("*** print_tb:")
    traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
    print("*** print_exception:")
    traceback.print_exception(exc_value, limit=2, file=sys.stdout)
    print("*** print_exc:")
    traceback.print_exc(limit=2, file=sys.stdout)
    print("*** format_exc, first and last line:")
    formatted_lines = traceback.format_exc().splitlines()
    print(formatted_lines[0])
    print(formatted_lines[-1])
    print("*** format_exception:")
    print(repr(traceback.format_exception(exc_value)))
    print("*** extract_tb:")
    print(repr(traceback.extract_tb(exc_traceback)))
    print("*** format_tb:")
    print(repr(traceback.format_tb(exc_traceback)))
    print("*** tb_lineno:", exc_traceback.tb_lineno)
7 của ngoại lệ) cũng sẽ được in, giống như bản thân trình thông dịch khi in một ngoại lệ chưa được xử lý.

Thay đổi trong phiên bản 3.5: Đối số ETYPE bị bỏ qua và suy ra từ loại giá trị.The etype argument is ignored and inferred from the type of value.

Đã thay đổi trong phiên bản 3.10: Tham số ETYPE đã được đổi tên thành Exc và hiện chỉ có vị trí.The etype parameter has been renamed to exc and is now positional-only.

Traceback.print_exc (giới hạn = none, file = none, chuỗi = true) ¶print_exc(limit=None, file=None, chain=True)

Đây là một tốc ký cho

import sys, traceback

def lumberjack():
    bright_side_of_life()

def bright_side_of_life():
    return tuple()[0]

try:
    lumberjack()
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info()
    print("*** print_tb:")
    traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
    print("*** print_exception:")
    traceback.print_exception(exc_value, limit=2, file=sys.stdout)
    print("*** print_exc:")
    traceback.print_exc(limit=2, file=sys.stdout)
    print("*** format_exc, first and last line:")
    formatted_lines = traceback.format_exc().splitlines()
    print(formatted_lines[0])
    print(formatted_lines[-1])
    print("*** format_exception:")
    print(repr(traceback.format_exception(exc_value)))
    print("*** extract_tb:")
    print(repr(traceback.extract_tb(exc_traceback)))
    print("*** format_tb:")
    print(repr(traceback.format_tb(exc_traceback)))
    print("*** tb_lineno:", exc_traceback.tb_lineno)
8.

Traceback.print_last (giới hạn = none, file = none, chuỗi = true) ¶print_last(limit=None, file=None, chain=True)

Đây là một tốc ký cho

import sys, traceback

def lumberjack():
    bright_side_of_life()

def bright_side_of_life():
    return tuple()[0]

try:
    lumberjack()
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info()
    print("*** print_tb:")
    traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
    print("*** print_exception:")
    traceback.print_exception(exc_value, limit=2, file=sys.stdout)
    print("*** print_exc:")
    traceback.print_exc(limit=2, file=sys.stdout)
    print("*** format_exc, first and last line:")
    formatted_lines = traceback.format_exc().splitlines()
    print(formatted_lines[0])
    print(formatted_lines[-1])
    print("*** format_exception:")
    print(repr(traceback.format_exception(exc_value)))
    print("*** extract_tb:")
    print(repr(traceback.extract_tb(exc_traceback)))
    print("*** format_tb:")
    print(repr(traceback.format_tb(exc_traceback)))
    print("*** tb_lineno:", exc_traceback.tb_lineno)
9. Nói chung, nó sẽ chỉ hoạt động sau khi một ngoại lệ đã đạt đến một lời nhắc tương tác (xem
*** print_tb:
  File "", line 10, in 
    lumberjack()
*** print_exception:
Traceback (most recent call last):
  File "", line 10, in 
    lumberjack()
  File "", line 4, in lumberjack
    bright_side_of_life()
IndexError: tuple index out of range
*** print_exc:
Traceback (most recent call last):
  File "", line 10, in 
    lumberjack()
  File "", line 4, in lumberjack
    bright_side_of_life()
IndexError: tuple index out of range
*** format_exc, first and last line:
Traceback (most recent call last):
IndexError: tuple index out of range
*** format_exception:
['Traceback (most recent call last):\n',
 '  File "", line 10, in \n    lumberjack()\n',
 '  File "", line 4, in lumberjack\n    bright_side_of_life()\n',
 '  File "", line 7, in bright_side_of_life\n    return tuple()[0]\n           ~~~~~~~^^^\n',
 'IndexError: tuple index out of range\n']
*** extract_tb:
[, line 10 in >,
 , line 4 in lumberjack>,
 , line 7 in bright_side_of_life>]
*** format_tb:
['  File "", line 10, in \n    lumberjack()\n',
 '  File "", line 4, in lumberjack\n    bright_side_of_life()\n',
 '  File "", line 7, in bright_side_of_life\n    return tuple()[0]\n           ~~~~~~~^^^\n']
*** tb_lineno: 10
0).

TraceBack.Print_Stack (f = none, giới hạn = none, file = none) ¶print_stack(f=None, limit=None, file=None)

In lên để giới hạn các mục nhập dấu vết (bắt đầu từ điểm gọi) nếu giới hạn là dương. Nếu không, in các mục abs(limit) cuối cùng. Nếu giới hạn bị bỏ qua hoặc None, tất cả các mục được in. Đối số F tùy chọn có thể được sử dụng để chỉ định khung ngăn xếp thay thế để bắt đầu. Đối số tệp tùy chọn có ý nghĩa tương tự như đối với

import sys, traceback

def lumberjack():
    bright_side_of_life()

def bright_side_of_life():
    return tuple()[0]

try:
    lumberjack()
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info()
    print("*** print_tb:")
    traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
    print("*** print_exception:")
    traceback.print_exception(exc_value, limit=2, file=sys.stdout)
    print("*** print_exc:")
    traceback.print_exc(limit=2, file=sys.stdout)
    print("*** format_exc, first and last line:")
    formatted_lines = traceback.format_exc().splitlines()
    print(formatted_lines[0])
    print(formatted_lines[-1])
    print("*** format_exception:")
    print(repr(traceback.format_exception(exc_value)))
    print("*** extract_tb:")
    print(repr(traceback.extract_tb(exc_traceback)))
    print("*** format_tb:")
    print(repr(traceback.format_tb(exc_traceback)))
    print("*** tb_lineno:", exc_traceback.tb_lineno)
1.

Đã thay đổi trong phiên bản 3.5: Đã thêm hỗ trợ giới hạn âm.Added negative limit support.

Traceback.print_exception (exc, /, [value, tb,] giới hạn = none, file = none, chuỗi = true)

In thông tin ngoại lệ và ngăn xếp các mục nhập từ TB đối tượng Traceback sang tệp. Điều này khác với

import sys, traceback

def lumberjack():
    bright_side_of_life()

def bright_side_of_life():
    return tuple()[0]

try:
    lumberjack()
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info()
    print("*** print_tb:")
    traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
    print("*** print_exception:")
    traceback.print_exception(exc_value, limit=2, file=sys.stdout)
    print("*** print_exc:")
    traceback.print_exc(limit=2, file=sys.stdout)
    print("*** format_exc, first and last line:")
    formatted_lines = traceback.format_exc().splitlines()
    print(formatted_lines[0])
    print(formatted_lines[-1])
    print("*** format_exception:")
    print(repr(traceback.format_exception(exc_value)))
    print("*** extract_tb:")
    print(repr(traceback.extract_tb(exc_traceback)))
    print("*** format_tb:")
    print(repr(traceback.format_tb(exc_traceback)))
    print("*** tb_lineno:", exc_traceback.tb_lineno)
1 theo những cách sau:

Nếu TB không phải là None, nó sẽ in tiêu đề
import sys, traceback

def lumberjack():
    bright_side_of_life()

def bright_side_of_life():
    return tuple()[0]

try:
    lumberjack()
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info()
    print("*** print_tb:")
    traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
    print("*** print_exception:")
    traceback.print_exception(exc_value, limit=2, file=sys.stdout)
    print("*** print_exc:")
    traceback.print_exc(limit=2, file=sys.stdout)
    print("*** format_exc, first and last line:")
    formatted_lines = traceback.format_exc().splitlines()
    print(formatted_lines[0])
    print(formatted_lines[-1])
    print("*** format_exception:")
    print(repr(traceback.format_exception(exc_value)))
    print("*** extract_tb:")
    print(repr(traceback.extract_tb(exc_traceback)))
    print("*** format_tb:")
    print(repr(traceback.format_tb(exc_traceback)))
    print("*** tb_lineno:", exc_traceback.tb_lineno)
3
format_list(extracted_list)

Nó in loại ngoại lệ và giá trị sau dấu vết ngăn xếp

Nếu loại (giá trị) là
import sys, traceback

def lumberjack():
    bright_side_of_life()

def bright_side_of_life():
    return tuple()[0]

try:
    lumberjack()
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info()
    print("*** print_tb:")
    traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
    print("*** print_exception:")
    traceback.print_exception(exc_value, limit=2, file=sys.stdout)
    print("*** print_exc:")
    traceback.print_exc(limit=2, file=sys.stdout)
    print("*** format_exc, first and last line:")
    formatted_lines = traceback.format_exc().splitlines()
    print(formatted_lines[0])
    print(formatted_lines[-1])
    print("*** format_exception:")
    print(repr(traceback.format_exception(exc_value)))
    print("*** extract_tb:")
    print(repr(traceback.extract_tb(exc_traceback)))
    print("*** format_tb:")
    print(repr(traceback.format_tb(exc_traceback)))
    print("*** tb_lineno:", exc_traceback.tb_lineno)
4 và giá trị có định dạng thích hợp, nó sẽ in dòng trong đó lỗi cú pháp xảy ra với một chăm sóc cho biết vị trí gần đúng của lỗi.
format_exception_only(exc, /[, value])

Vì Python 3.10, thay vì chuyển giá trị và bệnh lao, một đối tượng ngoại lệ có thể được truyền làm đối số đầu tiên. Nếu giá trị và bệnh lao được cung cấp, đối số đầu tiên bị bỏ qua để cung cấp khả năng tương thích ngược.

Vì Python 3.10, thay vì giá trị vượt qua, một đối tượng ngoại lệ có thể được truyền như là đối số đầu tiên. Nếu giá trị được cung cấp, đối số đầu tiên bị bỏ qua để cung cấp khả năng tương thích ngược.

Đã thay đổi trong phiên bản 3.10: Tham số ETYPE đã được đổi tên thành Exc và hiện chỉ có vị trí.The etype parameter has been renamed to exc and is now positional-only.

Traceback.format_exception (exc, /, [value, tb,] giới hạn = none, chuỗi = true) ¶format_exception(exc, /, [value, tb, ]limit=None, chain=True)

Định dạng một dấu vết ngăn xếp và thông tin ngoại lệ. Các đối số có cùng ý nghĩa với các đối số tương ứng với

>>> import traceback
>>> traceback.format_list([('spam.py', 3, '', 'spam.eggs()'),
...                        ('eggs.py', 42, 'eggs', 'return "bacon"')])
['  File "spam.py", line 3, in \n    spam.eggs()\n',
 '  File "eggs.py", line 42, in eggs\n    return "bacon"\n']
>>> an_error = IndexError('tuple index out of range')
>>> traceback.format_exception_only(type(an_error), an_error)
['IndexError: tuple index out of range\n']
1. Giá trị trả về là một danh sách các chuỗi, mỗi chuỗi kết thúc trong một dòng mới và một số có chứa các dòng bên trong. Khi các dòng này được nối và in, chính xác cùng một văn bản được in như
>>> import traceback
>>> traceback.format_list([('spam.py', 3, '', 'spam.eggs()'),
...                        ('eggs.py', 42, 'eggs', 'return "bacon"')])
['  File "spam.py", line 3, in \n    spam.eggs()\n',
 '  File "eggs.py", line 42, in eggs\n    return "bacon"\n']
>>> an_error = IndexError('tuple index out of range')
>>> traceback.format_exception_only(type(an_error), an_error)
['IndexError: tuple index out of range\n']
1.

Thay đổi trong phiên bản 3.5: Đối số ETYPE bị bỏ qua và suy ra từ loại giá trị.The etype argument is ignored and inferred from the type of value.

Đã thay đổi trong phiên bản 3.10: Chức năng này hành vi và chữ ký này đã được sửa đổi để khớp với

>>> import traceback
>>> traceback.format_list([('spam.py', 3, '', 'spam.eggs()'),
...                        ('eggs.py', 42, 'eggs', 'return "bacon"')])
['  File "spam.py", line 3, in \n    spam.eggs()\n',
 '  File "eggs.py", line 42, in eggs\n    return "bacon"\n']
>>> an_error = IndexError('tuple index out of range')
>>> traceback.format_exception_only(type(an_error), an_error)
['IndexError: tuple index out of range\n']
1.This function’s behavior and signature were modified to match
>>> import traceback
>>> traceback.format_list([('spam.py', 3, '', 'spam.eggs()'),
...                        ('eggs.py', 42, 'eggs', 'return "bacon"')])
['  File "spam.py", line 3, in \n    spam.eggs()\n',
 '  File "eggs.py", line 42, in eggs\n    return "bacon"\n']
>>> an_error = IndexError('tuple index out of range')
>>> traceback.format_exception_only(type(an_error), an_error)
['IndexError: tuple index out of range\n']
1.

Traceback.format_exc (giới hạn = none, chuỗi = true) ¶format_exc(limit=None, chain=True)

Điều này giống như

>>> import traceback
>>> traceback.format_list([('spam.py', 3, '', 'spam.eggs()'),
...                        ('eggs.py', 42, 'eggs', 'return "bacon"')])
['  File "spam.py", line 3, in \n    spam.eggs()\n',
 '  File "eggs.py", line 42, in eggs\n    return "bacon"\n']
>>> an_error = IndexError('tuple index out of range')
>>> traceback.format_exception_only(type(an_error), an_error)
['IndexError: tuple index out of range\n']
4 nhưng trả về một chuỗi thay vì in vào một tệp.

Traceback.format_tb (tb, giới hạn = không) ¶format_tb(tb, limit=None)

Một tốc ký cho

>>> import traceback
>>> traceback.format_list([('spam.py', 3, '', 'spam.eggs()'),
...                        ('eggs.py', 42, 'eggs', 'return "bacon"')])
['  File "spam.py", line 3, in \n    spam.eggs()\n',
 '  File "eggs.py", line 42, in eggs\n    return "bacon"\n']
>>> an_error = IndexError('tuple index out of range')
>>> traceback.format_exception_only(type(an_error), an_error)
['IndexError: tuple index out of range\n']
5.

Traceback.format_stack (f = none, giới hạn = không) ¶format_stack(f=None, limit=None)

Một tốc ký cho

>>> import traceback
>>> traceback.format_list([('spam.py', 3, '', 'spam.eggs()'),
...                        ('eggs.py', 42, 'eggs', 'return "bacon"')])
['  File "spam.py", line 3, in \n    spam.eggs()\n',
 '  File "eggs.py", line 42, in eggs\n    return "bacon"\n']
>>> an_error = IndexError('tuple index out of range')
>>> traceback.format_exception_only(type(an_error), an_error)
['IndexError: tuple index out of range\n']
6.

TraceBack.Clear_Frames (TB) ¶clear_frames(tb)

Xóa các biến cục bộ của tất cả các khung ngăn xếp trong TB theo dõi bằng cách gọi phương thức

>>> import traceback
>>> traceback.format_list([('spam.py', 3, '', 'spam.eggs()'),
...                        ('eggs.py', 42, 'eggs', 'return "bacon"')])
['  File "spam.py", line 3, in \n    spam.eggs()\n',
 '  File "eggs.py", line 42, in eggs\n    return "bacon"\n']
>>> an_error = IndexError('tuple index out of range')
>>> traceback.format_exception_only(type(an_error), an_error)
['IndexError: tuple index out of range\n']
7 của mỗi đối tượng khung.

Mới trong phiên bản 3.4.

Traceback.walk_stack (f) ¶walk_stack(f)

Đi bộ ngăn xếp theo

>>> import traceback
>>> traceback.format_list([('spam.py', 3, '', 'spam.eggs()'),
...                        ('eggs.py', 42, 'eggs', 'return "bacon"')])
['  File "spam.py", line 3, in \n    spam.eggs()\n',
 '  File "eggs.py", line 42, in eggs\n    return "bacon"\n']
>>> an_error = IndexError('tuple index out of range')
>>> traceback.format_exception_only(type(an_error), an_error)
['IndexError: tuple index out of range\n']
8 từ khung đã cho, mang lại khung và số đường cho mỗi khung hình. Nếu F là None, ngăn xếp hiện tại được sử dụng. Người trợ giúp này được sử dụng với sys.last_traceback0.

Mới trong phiên bản 3.5.

Traceback.walk_tb (TB) ¶walk_tb(tb)

Đi bộ theo dõi sau sys.last_traceback1 mang lại khung và số đường cho mỗi khung hình. Người trợ giúp này được sử dụng với sys.last_traceback0.

Mới trong phiên bản 3.5.

Traceback.walk_tb (TB) ¶

Đi bộ theo dõi sau sys.last_traceback1 mang lại khung và số đường cho mỗi khung hình. Người trợ giúp này được sử dụng với sys.last_traceback0.

Mới trong phiên bản 3.5.

Traceback.walk_tb (TB) ¶

Đi bộ theo dõi sau sys.last_traceback1 mang lại khung và số đường cho mỗi khung hình. Người trợ giúp này được sử dụng với sys.last_traceback0.traceback.TracebackException(exc_type, exc_value, exc_traceback, *, limit=None, lookup_lines=True, capture_locals=False, compact=False)

Mô -đun cũng xác định các lớp sau:

sys.last_traceback3 Đối tượng

Các đối tượng sys.last_traceback3 được tạo từ các ngoại lệ thực tế để thu thập dữ liệu để in sau này theo kiểu nhẹ.

classTraceBack.TracebackException (exc_type, exc_value, exc_traceback, *, giới hạn = không

Nắm bắt một ngoại lệ cho kết xuất sau này. giới hạn, tra cứu_lines và capture_locals cũng như đối với lớp

*** print_tb:
  File "", line 10, in 
    lumberjack()
*** print_exception:
Traceback (most recent call last):
  File "", line 10, in 
    lumberjack()
  File "", line 4, in lumberjack
    bright_side_of_life()
IndexError: tuple index out of range
*** print_exc:
Traceback (most recent call last):
  File "", line 10, in 
    lumberjack()
  File "", line 4, in lumberjack
    bright_side_of_life()
IndexError: tuple index out of range
*** format_exc, first and last line:
Traceback (most recent call last):
IndexError: tuple index out of range
*** format_exception:
['Traceback (most recent call last):\n',
 '  File "", line 10, in \n    lumberjack()\n',
 '  File "", line 4, in lumberjack\n    bright_side_of_life()\n',
 '  File "", line 7, in bright_side_of_life\n    return tuple()[0]\n           ~~~~~~~^^^\n',
 'IndexError: tuple index out of range\n']
*** extract_tb:
[, line 10 in >,
 , line 4 in lumberjack>,
 , line 7 in bright_side_of_life>]
*** format_tb:
['  File "", line 10, in \n    lumberjack()\n',
 '  File "", line 4, in lumberjack\n    bright_side_of_life()\n',
 '  File "", line 7, in bright_side_of_life\n    return tuple()[0]\n           ~~~~~~~^^^\n']
*** tb_lineno: 10
4.

Nếu Compact là đúng, chỉ có dữ liệu được yêu cầu bằng phương thức ________ 53 sys.last_traceback7 được lưu trong các thuộc tính của lớp. Cụ thể, trường
import sys, traceback

def lumberjack():
    bright_side_of_life()

def bright_side_of_life():
    return tuple()[0]

try:
    lumberjack()
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info()
    print("*** print_tb:")
    traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
    print("*** print_exception:")
    traceback.print_exception(exc_value, limit=2, file=sys.stdout)
    print("*** print_exc:")
    traceback.print_exc(limit=2, file=sys.stdout)
    print("*** format_exc, first and last line:")
    formatted_lines = traceback.format_exc().splitlines()
    print(formatted_lines[0])
    print(formatted_lines[-1])
    print("*** format_exception:")
    print(repr(traceback.format_exception(exc_value)))
    print("*** extract_tb:")
    print(repr(traceback.extract_tb(exc_traceback)))
    print("*** format_tb:")
    print(repr(traceback.format_tb(exc_traceback)))
    print("*** tb_lineno:", exc_traceback.tb_lineno)
7 chỉ được tính toán nếu
import sys, traceback

def lumberjack():
    bright_side_of_life()

def bright_side_of_life():
    return tuple()[0]

try:
    lumberjack()
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info()
    print("*** print_tb:")
    traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
    print("*** print_exception:")
    traceback.print_exception(exc_value, limit=2, file=sys.stdout)
    print("*** print_exc:")
    traceback.print_exc(limit=2, file=sys.stdout)
    print("*** format_exc, first and last line:")
    formatted_lines = traceback.format_exc().splitlines()
    print(formatted_lines[0])
    print(formatted_lines[-1])
    print("*** format_exception:")
    print(repr(traceback.format_exception(exc_value)))
    print("*** extract_tb:")
    print(repr(traceback.extract_tb(exc_traceback)))
    print("*** format_tb:")
    print(repr(traceback.format_tb(exc_traceback)))
    print("*** tb_lineno:", exc_traceback.tb_lineno)
6 là Nonesys.exc_info()1 là sai.

Lưu ý rằng khi người dân địa phương bị bắt, chúng cũng được hiển thị trong dấu vết.

__gây ra__¶

A sys.last_traceback3 của

import sys, traceback

def lumberjack():
    bright_side_of_life()

def bright_side_of_life():
    return tuple()[0]

try:
    lumberjack()
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info()
    print("*** print_tb:")
    traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
    print("*** print_exception:")
    traceback.print_exception(exc_value, limit=2, file=sys.stdout)
    print("*** print_exc:")
    traceback.print_exc(limit=2, file=sys.stdout)
    print("*** format_exc, first and last line:")
    formatted_lines = traceback.format_exc().splitlines()
    print(formatted_lines[0])
    print(formatted_lines[-1])
    print("*** format_exception:")
    print(repr(traceback.format_exception(exc_value)))
    print("*** extract_tb:")
    print(repr(traceback.extract_tb(exc_traceback)))
    print("*** format_tb:")
    print(repr(traceback.format_tb(exc_traceback)))
    print("*** tb_lineno:", exc_traceback.tb_lineno)
6 gốc.

__định nghĩa bài văn__¶

A sys.last_traceback3 của

import sys, traceback

def lumberjack():
    bright_side_of_life()

def bright_side_of_life():
    return tuple()[0]

try:
    lumberjack()
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info()
    print("*** print_tb:")
    traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
    print("*** print_exception:")
    traceback.print_exception(exc_value, limit=2, file=sys.stdout)
    print("*** print_exc:")
    traceback.print_exc(limit=2, file=sys.stdout)
    print("*** format_exc, first and last line:")
    formatted_lines = traceback.format_exc().splitlines()
    print(formatted_lines[0])
    print(formatted_lines[-1])
    print("*** format_exception:")
    print(repr(traceback.format_exception(exc_value)))
    print("*** extract_tb:")
    print(repr(traceback.extract_tb(exc_traceback)))
    print("*** format_tb:")
    print(repr(traceback.format_tb(exc_traceback)))
    print("*** tb_lineno:", exc_traceback.tb_lineno)
7 gốc.

__Suppress_context__¶

Giá trị sys.exc_info()1 từ ngoại lệ ban đầu.

__notes__¶

Giá trị sys.exc_info()7 từ ngoại lệ ban đầu hoặc None nếu ngoại lệ không có bất kỳ ghi chú nào. Nếu nó không phải là None, nó được định dạng trong dấu vết sau chuỗi ngoại lệ.

Mới trong phiên bản 3.11.

cây rơm¶

A

*** print_tb:
  File "", line 10, in 
    lumberjack()
*** print_exception:
Traceback (most recent call last):
  File "", line 10, in 
    lumberjack()
  File "", line 4, in lumberjack
    bright_side_of_life()
IndexError: tuple index out of range
*** print_exc:
Traceback (most recent call last):
  File "", line 10, in 
    lumberjack()
  File "", line 4, in lumberjack
    bright_side_of_life()
IndexError: tuple index out of range
*** format_exc, first and last line:
Traceback (most recent call last):
IndexError: tuple index out of range
*** format_exception:
['Traceback (most recent call last):\n',
 '  File "", line 10, in \n    lumberjack()\n',
 '  File "", line 4, in lumberjack\n    bright_side_of_life()\n',
 '  File "", line 7, in bright_side_of_life\n    return tuple()[0]\n           ~~~~~~~^^^\n',
 'IndexError: tuple index out of range\n']
*** extract_tb:
[, line 10 in >,
 , line 4 in lumberjack>,
 , line 7 in bright_side_of_life>]
*** format_tb:
['  File "", line 10, in \n    lumberjack()\n',
 '  File "", line 4, in lumberjack\n    bright_side_of_life()\n',
 '  File "", line 7, in bright_side_of_life\n    return tuple()[0]\n           ~~~~~~~^^^\n']
*** tb_lineno: 10
4 đại diện cho Traceback.

exc_type¶

Lớp của Traceback ban đầu.

Tên tệp

Đối với lỗi cú pháp - Tên tệp nơi xảy ra lỗi.

vải mỏng

Đối với lỗi cú pháp - Số dòng nơi xảy ra lỗi.

chữ¶

Đối với lỗi cú pháp - văn bản xảy ra lỗi.

bù lại¶ from_exception(exc, *, limit=None, lookup_lines=True, capture_locals=False)

Mô -đun cũng xác định các lớp sau:

Các đối tượng sys.last_traceback3 được tạo từ các ngoại lệ thực tế để thu thập dữ liệu để in sau này theo kiểu nhẹ.

classTraceBack.TracebackException (exc_type, exc_value, exc_traceback, *, giới hạn = không(*, file=None, chain=True)

Nắm bắt một ngoại lệ cho kết xuất sau này. giới hạn, tra cứu_lines và capture_locals cũng như đối với lớp

*** print_tb:
  File "", line 10, in 
    lumberjack()
*** print_exception:
Traceback (most recent call last):
  File "", line 10, in 
    lumberjack()
  File "", line 4, in lumberjack
    bright_side_of_life()
IndexError: tuple index out of range
*** print_exc:
Traceback (most recent call last):
  File "", line 10, in 
    lumberjack()
  File "", line 4, in lumberjack
    bright_side_of_life()
IndexError: tuple index out of range
*** format_exc, first and last line:
Traceback (most recent call last):
IndexError: tuple index out of range
*** format_exception:
['Traceback (most recent call last):\n',
 '  File "", line 10, in \n    lumberjack()\n',
 '  File "", line 4, in lumberjack\n    bright_side_of_life()\n',
 '  File "", line 7, in bright_side_of_life\n    return tuple()[0]\n           ~~~~~~~^^^\n',
 'IndexError: tuple index out of range\n']
*** extract_tb:
[, line 10 in >,
 , line 4 in lumberjack>,
 , line 7 in bright_side_of_life>]
*** format_tb:
['  File "", line 10, in \n    lumberjack()\n',
 '  File "", line 4, in lumberjack\n    bright_side_of_life()\n',
 '  File "", line 7, in bright_side_of_life\n    return tuple()[0]\n           ~~~~~~~^^^\n']
*** tb_lineno: 10
4.

__Suppress_context__¶

Giá trị sys.exc_info()1 từ ngoại lệ ban đầu.(*, chain=True)

__notes__¶

Giá trị sys.exc_info()7 từ ngoại lệ ban đầu hoặc None nếu ngoại lệ không có bất kỳ ghi chú nào. Nếu nó không phải là None, nó được định dạng trong dấu vết sau chuỗi ngoại lệ.

Mới trong phiên bản 3.11.

cây rơm¶

A
*** print_tb:
  File "", line 10, in 
    lumberjack()
*** print_exception:
Traceback (most recent call last):
  File "", line 10, in 
    lumberjack()
  File "", line 4, in lumberjack
    bright_side_of_life()
IndexError: tuple index out of range
*** print_exc:
Traceback (most recent call last):
  File "", line 10, in 
    lumberjack()
  File "", line 4, in lumberjack
    bright_side_of_life()
IndexError: tuple index out of range
*** format_exc, first and last line:
Traceback (most recent call last):
IndexError: tuple index out of range
*** format_exception:
['Traceback (most recent call last):\n',
 '  File "", line 10, in \n    lumberjack()\n',
 '  File "", line 4, in lumberjack\n    bright_side_of_life()\n',
 '  File "", line 7, in bright_side_of_life\n    return tuple()[0]\n           ~~~~~~~^^^\n',
 'IndexError: tuple index out of range\n']
*** extract_tb:
[, line 10 in >,
 , line 4 in lumberjack>,
 , line 7 in bright_side_of_life>]
*** format_tb:
['  File "", line 10, in \n    lumberjack()\n',
 '  File "", line 4, in lumberjack\n    bright_side_of_life()\n',
 '  File "", line 7, in bright_side_of_life\n    return tuple()[0]\n           ~~~~~~~^^^\n']
*** tb_lineno: 10
4 đại diện cho Traceback.
()

exc_type¶

Lớp của Traceback ban đầu.

Tên tệp

cây rơm¶

A

*** print_tb:
  File "", line 10, in 
    lumberjack()
*** print_exception:
Traceback (most recent call last):
  File "", line 10, in 
    lumberjack()
  File "", line 4, in lumberjack
    bright_side_of_life()
IndexError: tuple index out of range
*** print_exc:
Traceback (most recent call last):
  File "", line 10, in 
    lumberjack()
  File "", line 4, in lumberjack
    bright_side_of_life()
IndexError: tuple index out of range
*** format_exc, first and last line:
Traceback (most recent call last):
IndexError: tuple index out of range
*** format_exception:
['Traceback (most recent call last):\n',
 '  File "", line 10, in \n    lumberjack()\n',
 '  File "", line 4, in lumberjack\n    bright_side_of_life()\n',
 '  File "", line 7, in bright_side_of_life\n    return tuple()[0]\n           ~~~~~~~^^^\n',
 'IndexError: tuple index out of range\n']
*** extract_tb:
[, line 10 in >,
 , line 4 in lumberjack>,
 , line 7 in bright_side_of_life>]
*** format_tb:
['  File "", line 10, in \n    lumberjack()\n',
 '  File "", line 4, in lumberjack\n    bright_side_of_life()\n',
 '  File "", line 7, in bright_side_of_life\n    return tuple()[0]\n           ~~~~~~~^^^\n']
*** tb_lineno: 10
4 đại diện cho Traceback.Added the compact parameter.

Mới trong phiên bản 3.5.

*** print_tb:
  File "", line 10, in 
    lumberjack()
*** print_exception:
Traceback (most recent call last):
  File "", line 10, in 
    lumberjack()
  File "", line 4, in lumberjack
    bright_side_of_life()
IndexError: tuple index out of range
*** print_exc:
Traceback (most recent call last):
  File "", line 10, in 
    lumberjack()
  File "", line 4, in lumberjack
    bright_side_of_life()
IndexError: tuple index out of range
*** format_exc, first and last line:
Traceback (most recent call last):
IndexError: tuple index out of range
*** format_exception:
['Traceback (most recent call last):\n',
 '  File "", line 10, in \n    lumberjack()\n',
 '  File "", line 4, in lumberjack\n    bright_side_of_life()\n',
 '  File "", line 7, in bright_side_of_life\n    return tuple()[0]\n           ~~~~~~~^^^\n',
 'IndexError: tuple index out of range\n']
*** extract_tb:
[, line 10 in >,
 , line 4 in lumberjack>,
 , line 7 in bright_side_of_life>]
*** format_tb:
['  File "", line 10, in \n    lumberjack()\n',
 '  File "", line 4, in lumberjack\n    bright_side_of_life()\n',
 '  File "", line 7, in bright_side_of_life\n    return tuple()[0]\n           ~~~~~~~^^^\n']
*** tb_lineno: 10
4 Các đối tượng đại diện cho một ngăn xếp cuộc gọi sẵn sàng để định dạng.

classstraceback.stacksummary¶traceback.StackSummary

Xây dựng một đối tượng

*** print_tb:
  File "", line 10, in 
    lumberjack()
*** print_exception:
Traceback (most recent call last):
  File "", line 10, in 
    lumberjack()
  File "", line 4, in lumberjack
    bright_side_of_life()
IndexError: tuple index out of range
*** print_exc:
Traceback (most recent call last):
  File "", line 10, in 
    lumberjack()
  File "", line 4, in lumberjack
    bright_side_of_life()
IndexError: tuple index out of range
*** format_exc, first and last line:
Traceback (most recent call last):
IndexError: tuple index out of range
*** format_exception:
['Traceback (most recent call last):\n',
 '  File "", line 10, in \n    lumberjack()\n',
 '  File "", line 4, in lumberjack\n    bright_side_of_life()\n',
 '  File "", line 7, in bright_side_of_life\n    return tuple()[0]\n           ~~~~~~~^^^\n',
 'IndexError: tuple index out of range\n']
*** extract_tb:
[, line 10 in >,
 , line 4 in lumberjack>,
 , line 7 in bright_side_of_life>]
*** format_tb:
['  File "", line 10, in \n    lumberjack()\n',
 '  File "", line 4, in lumberjack\n    bright_side_of_life()\n',
 '  File "", line 7, in bright_side_of_life\n    return tuple()[0]\n           ~~~~~~~^^^\n']
*** tb_lineno: 10
4 từ một trình tạo khung (như được trả về bởi None2 hoặc None3).

Nếu giới hạn được cung cấp, chỉ có nhiều khung hình này được lấy từ frame_gen. Nếu Lookup_Lines là None4, các đối tượng

*** print_tb:
  File "", line 10, in 
    lumberjack()
*** print_exception:
Traceback (most recent call last):
  File "", line 10, in 
    lumberjack()
  File "", line 4, in lumberjack
    bright_side_of_life()
IndexError: tuple index out of range
*** print_exc:
Traceback (most recent call last):
  File "", line 10, in 
    lumberjack()
  File "", line 4, in lumberjack
    bright_side_of_life()
IndexError: tuple index out of range
*** format_exc, first and last line:
Traceback (most recent call last):
IndexError: tuple index out of range
*** format_exception:
['Traceback (most recent call last):\n',
 '  File "", line 10, in \n    lumberjack()\n',
 '  File "", line 4, in lumberjack\n    bright_side_of_life()\n',
 '  File "", line 7, in bright_side_of_life\n    return tuple()[0]\n           ~~~~~~~^^^\n',
 'IndexError: tuple index out of range\n']
*** extract_tb:
[, line 10 in >,
 , line 4 in lumberjack>,
 , line 7 in bright_side_of_life>]
*** format_tb:
['  File "", line 10, in \n    lumberjack()\n',
 '  File "", line 4, in lumberjack\n    bright_side_of_life()\n',
 '  File "", line 7, in bright_side_of_life\n    return tuple()[0]\n           ~~~~~~~^^^\n']
*** tb_lineno: 10
6 đã trả lại sẽ chưa đọc các dòng của chúng, khiến chi phí tạo ra giá rẻ hơn (có thể có giá trị nếu nó không thực sự được định dạng). Nếu Capture_Locals là abs(limit)4, các biến cục bộ trong mỗi
*** print_tb:
  File "", line 10, in 
    lumberjack()
*** print_exception:
Traceback (most recent call last):
  File "", line 10, in 
    lumberjack()
  File "", line 4, in lumberjack
    bright_side_of_life()
IndexError: tuple index out of range
*** print_exc:
Traceback (most recent call last):
  File "", line 10, in 
    lumberjack()
  File "", line 4, in lumberjack
    bright_side_of_life()
IndexError: tuple index out of range
*** format_exc, first and last line:
Traceback (most recent call last):
IndexError: tuple index out of range
*** format_exception:
['Traceback (most recent call last):\n',
 '  File "", line 10, in \n    lumberjack()\n',
 '  File "", line 4, in lumberjack\n    bright_side_of_life()\n',
 '  File "", line 7, in bright_side_of_life\n    return tuple()[0]\n           ~~~~~~~^^^\n',
 'IndexError: tuple index out of range\n']
*** extract_tb:
[, line 10 in >,
 , line 4 in lumberjack>,
 , line 7 in bright_side_of_life>]
*** format_tb:
['  File "", line 10, in \n    lumberjack()\n',
 '  File "", line 4, in lumberjack\n    bright_side_of_life()\n',
 '  File "", line 7, in bright_side_of_life\n    return tuple()[0]\n           ~~~~~~~^^^\n']
*** tb_lineno: 10
6 được chụp dưới dạng biểu diễn đối tượng.

classmethodfrom_list (a_list) ¶from_list(a_list)

Xây dựng một đối tượng

*** print_tb:
  File "", line 10, in 
    lumberjack()
*** print_exception:
Traceback (most recent call last):
  File "", line 10, in 
    lumberjack()
  File "", line 4, in lumberjack
    bright_side_of_life()
IndexError: tuple index out of range
*** print_exc:
Traceback (most recent call last):
  File "", line 10, in 
    lumberjack()
  File "", line 4, in lumberjack
    bright_side_of_life()
IndexError: tuple index out of range
*** format_exc, first and last line:
Traceback (most recent call last):
IndexError: tuple index out of range
*** format_exception:
['Traceback (most recent call last):\n',
 '  File "", line 10, in \n    lumberjack()\n',
 '  File "", line 4, in lumberjack\n    bright_side_of_life()\n',
 '  File "", line 7, in bright_side_of_life\n    return tuple()[0]\n           ~~~~~~~^^^\n',
 'IndexError: tuple index out of range\n']
*** extract_tb:
[, line 10 in >,
 , line 4 in lumberjack>,
 , line 7 in bright_side_of_life>]
*** format_tb:
['  File "", line 10, in \n    lumberjack()\n',
 '  File "", line 4, in lumberjack\n    bright_side_of_life()\n',
 '  File "", line 7, in bright_side_of_life\n    return tuple()[0]\n           ~~~~~~~^^^\n']
*** tb_lineno: 10
4 từ danh sách các đối tượng
*** print_tb:
  File "", line 10, in 
    lumberjack()
*** print_exception:
Traceback (most recent call last):
  File "", line 10, in 
    lumberjack()
  File "", line 4, in lumberjack
    bright_side_of_life()
IndexError: tuple index out of range
*** print_exc:
Traceback (most recent call last):
  File "", line 10, in 
    lumberjack()
  File "", line 4, in lumberjack
    bright_side_of_life()
IndexError: tuple index out of range
*** format_exc, first and last line:
Traceback (most recent call last):
IndexError: tuple index out of range
*** format_exception:
['Traceback (most recent call last):\n',
 '  File "", line 10, in \n    lumberjack()\n',
 '  File "", line 4, in lumberjack\n    bright_side_of_life()\n',
 '  File "", line 7, in bright_side_of_life\n    return tuple()[0]\n           ~~~~~~~^^^\n',
 'IndexError: tuple index out of range\n']
*** extract_tb:
[, line 10 in >,
 , line 4 in lumberjack>,
 , line 7 in bright_side_of_life>]
*** format_tb:
['  File "", line 10, in \n    lumberjack()\n',
 '  File "", line 4, in lumberjack\n    bright_side_of_life()\n',
 '  File "", line 7, in bright_side_of_life\n    return tuple()[0]\n           ~~~~~~~^^^\n']
*** tb_lineno: 10
6 hoặc danh sách các bộ dữ liệu kiểu cũ. Mỗi tuple phải là 4-tuple với tên tệp, vải lanh, tên, dòng là các yếu tố.

định dạng()¶()

Trả về một danh sách các chuỗi sẵn sàng để in. Mỗi chuỗi trong danh sách kết quả tương ứng với một khung hình duy nhất từ ​​ngăn xếp. Mỗi chuỗi kết thúc trong một dòng mới; Các chuỗi cũng có thể chứa các dòng nội bộ, cho các mục đó với các dòng văn bản nguồn.

Đối với các chuỗi dài của cùng một khung và dòng, một vài lần lặp lại đầu tiên được hiển thị, theo sau là một dòng tóm tắt nêu số lượng chính xác các lần lặp lại tiếp theo.

Thay đổi trong phiên bản 3.6: Các chuỗi dài của các khung lặp lại hiện được viết tắt.Long sequences of repeated frames are now abbreviated.

format_frame_summary (frame_summary) ¶(frame_summary)

Trả về một chuỗi để in một trong các khung liên quan đến ngăn xếp. Phương pháp này được gọi cho mỗi đối tượng

*** print_tb:
  File "", line 10, in 
    lumberjack()
*** print_exception:
Traceback (most recent call last):
  File "", line 10, in 
    lumberjack()
  File "", line 4, in lumberjack
    bright_side_of_life()
IndexError: tuple index out of range
*** print_exc:
Traceback (most recent call last):
  File "", line 10, in 
    lumberjack()
  File "", line 4, in lumberjack
    bright_side_of_life()
IndexError: tuple index out of range
*** format_exc, first and last line:
Traceback (most recent call last):
IndexError: tuple index out of range
*** format_exception:
['Traceback (most recent call last):\n',
 '  File "", line 10, in \n    lumberjack()\n',
 '  File "", line 4, in lumberjack\n    bright_side_of_life()\n',
 '  File "", line 7, in bright_side_of_life\n    return tuple()[0]\n           ~~~~~~~^^^\n',
 'IndexError: tuple index out of range\n']
*** extract_tb:
[, line 10 in >,
 , line 4 in lumberjack>,
 , line 7 in bright_side_of_life>]
*** format_tb:
['  File "", line 10, in \n    lumberjack()\n',
 '  File "", line 4, in lumberjack\n    bright_side_of_life()\n',
 '  File "", line 7, in bright_side_of_life\n    return tuple()[0]\n           ~~~~~~~^^^\n']
*** tb_lineno: 10
6 được in bởi None2. Nếu nó trả về None, khung được bỏ qua từ đầu ra.

Mới trong phiên bản 3.11.

Mới trong phiên bản 3.5.

Một đối tượng

*** print_tb:
  File "", line 10, in 
    lumberjack()
*** print_exception:
Traceback (most recent call last):
  File "", line 10, in 
    lumberjack()
  File "", line 4, in lumberjack
    bright_side_of_life()
IndexError: tuple index out of range
*** print_exc:
Traceback (most recent call last):
  File "", line 10, in 
    lumberjack()
  File "", line 4, in lumberjack
    bright_side_of_life()
IndexError: tuple index out of range
*** format_exc, first and last line:
Traceback (most recent call last):
IndexError: tuple index out of range
*** format_exception:
['Traceback (most recent call last):\n',
 '  File "", line 10, in \n    lumberjack()\n',
 '  File "", line 4, in lumberjack\n    bright_side_of_life()\n',
 '  File "", line 7, in bright_side_of_life\n    return tuple()[0]\n           ~~~~~~~^^^\n',
 'IndexError: tuple index out of range\n']
*** extract_tb:
[, line 10 in >,
 , line 4 in lumberjack>,
 , line 7 in bright_side_of_life>]
*** format_tb:
['  File "", line 10, in \n    lumberjack()\n',
 '  File "", line 4, in lumberjack\n    bright_side_of_life()\n',
 '  File "", line 7, in bright_side_of_life\n    return tuple()[0]\n           ~~~~~~~^^^\n']
*** tb_lineno: 10
6 đại diện cho một khung hình duy nhất trong một dấu vết.

classTraceBack.Framesummary (fileName, lineno, name, lookup_line = true, locals = none, line = none) ¶ traceback.FrameSummary(filename, lineno, name, lookup_line=True, locals=None, line=None)

Đại diện cho một khung duy nhất trong dấu vết hoặc ngăn xếp đang được định dạng hoặc in. Nó có thể tùy ý có một phiên bản chuỗi của các khung hình địa phương có trong đó. Nếu Tra cứu_line là None4, mã nguồn không được tra cứu cho đến khi

*** print_tb:
  File "", line 10, in 
    lumberjack()
*** print_exception:
Traceback (most recent call last):
  File "", line 10, in 
    lumberjack()
  File "", line 4, in lumberjack
    bright_side_of_life()
IndexError: tuple index out of range
*** print_exc:
Traceback (most recent call last):
  File "", line 10, in 
    lumberjack()
  File "", line 4, in lumberjack
    bright_side_of_life()
IndexError: tuple index out of range
*** format_exc, first and last line:
Traceback (most recent call last):
IndexError: tuple index out of range
*** format_exception:
['Traceback (most recent call last):\n',
 '  File "", line 10, in \n    lumberjack()\n',
 '  File "", line 4, in lumberjack\n    bright_side_of_life()\n',
 '  File "", line 7, in bright_side_of_life\n    return tuple()[0]\n           ~~~~~~~^^^\n',
 'IndexError: tuple index out of range\n']
*** extract_tb:
[, line 10 in >,
 , line 4 in lumberjack>,
 , line 7 in bright_side_of_life>]
*** format_tb:
['  File "", line 10, in \n    lumberjack()\n',
 '  File "", line 4, in lumberjack\n    bright_side_of_life()\n',
 '  File "", line 7, in bright_side_of_life\n    return tuple()[0]\n           ~~~~~~~^^^\n']
*** tb_lineno: 10
6 có thuộc tính
>>> import traceback
>>> def another_function():
...     lumberstack()
...
>>> def lumberstack():
...     traceback.print_stack()
...     print(repr(traceback.extract_stack()))
...     print(repr(traceback.format_stack()))
...
>>> another_function()
  File "", line 10, in 
    another_function()
  File "", line 3, in another_function
    lumberstack()
  File "", line 6, in lumberstack
    traceback.print_stack()
[('', 10, '', 'another_function()'),
 ('', 3, 'another_function', 'lumberstack()'),
 ('', 7, 'lumberstack', 'print(repr(traceback.extract_stack()))')]
['  File "", line 10, in \n    another_function()\n',
 '  File "", line 3, in another_function\n    lumberstack()\n',
 '  File "", line 8, in lumberstack\n    print(repr(traceback.format_stack()))\n']
0 được truy cập (cũng xảy ra khi đúc nó thành một bộ thuật).
>>> import traceback
>>> def another_function():
...     lumberstack()
...
>>> def lumberstack():
...     traceback.print_stack()
...     print(repr(traceback.extract_stack()))
...     print(repr(traceback.format_stack()))
...
>>> another_function()
  File "", line 10, in 
    another_function()
  File "", line 3, in another_function
    lumberstack()
  File "", line 6, in lumberstack
    traceback.print_stack()
[('', 10, '', 'another_function()'),
 ('', 3, 'another_function', 'lumberstack()'),
 ('', 7, 'lumberstack', 'print(repr(traceback.extract_stack()))')]
['  File "", line 10, in \n    another_function()\n',
 '  File "", line 3, in another_function\n    lumberstack()\n',
 '  File "", line 8, in lumberstack\n    print(repr(traceback.format_stack()))\n']
0 có thể được cung cấp trực tiếp, và sẽ ngăn chặn việc tra cứu dòng xảy ra. Người dân địa phương là một từ điển biến cục bộ tùy chọn và nếu được cung cấp, các biểu diễn biến được lưu trữ trong bản tóm tắt để hiển thị sau.

Ví dụ về dấu vết

Ví dụ đơn giản này thực hiện một vòng lặp in-eval cơ bản, tương tự (nhưng ít hữu ích hơn) vòng thông dịch tương tác Python tiêu chuẩn. Để thực hiện đầy đủ hơn các vòng thông dịch, hãy tham khảo mô -đun

import sys, traceback

def lumberjack():
    bright_side_of_life()

def bright_side_of_life():
    return tuple()[0]

try:
    lumberjack()
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info()
    print("*** print_tb:")
    traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
    print("*** print_exception:")
    traceback.print_exception(exc_value, limit=2, file=sys.stdout)
    print("*** print_exc:")
    traceback.print_exc(limit=2, file=sys.stdout)
    print("*** format_exc, first and last line:")
    formatted_lines = traceback.format_exc().splitlines()
    print(formatted_lines[0])
    print(formatted_lines[-1])
    print("*** format_exception:")
    print(repr(traceback.format_exception(exc_value)))
    print("*** extract_tb:")
    print(repr(traceback.extract_tb(exc_traceback)))
    print("*** format_tb:")
    print(repr(traceback.format_tb(exc_traceback)))
    print("*** tb_lineno:", exc_traceback.tb_lineno)
00.

import sys, traceback

def run_user_code(envdir):
    source = input(">>> ")
    try:
        exec(source, envdir)
    except Exception:
        print("Exception in user code:")
        print("-"*60)
        traceback.print_exc(file=sys.stdout)
        print("-"*60)

envdir = {}
while True:
    run_user_code(envdir)

Ví dụ sau đây cho thấy các cách khác nhau để in và định dạng ngoại lệ và dấu vết:

import sys, traceback

def lumberjack():
    bright_side_of_life()

def bright_side_of_life():
    return tuple()[0]

try:
    lumberjack()
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info()
    print("*** print_tb:")
    traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
    print("*** print_exception:")
    traceback.print_exception(exc_value, limit=2, file=sys.stdout)
    print("*** print_exc:")
    traceback.print_exc(limit=2, file=sys.stdout)
    print("*** format_exc, first and last line:")
    formatted_lines = traceback.format_exc().splitlines()
    print(formatted_lines[0])
    print(formatted_lines[-1])
    print("*** format_exception:")
    print(repr(traceback.format_exception(exc_value)))
    print("*** extract_tb:")
    print(repr(traceback.extract_tb(exc_traceback)))
    print("*** format_tb:")
    print(repr(traceback.format_tb(exc_traceback)))
    print("*** tb_lineno:", exc_traceback.tb_lineno)

Đầu ra cho ví dụ sẽ trông tương tự như thế này:

*** print_tb:
  File "", line 10, in 
    lumberjack()
*** print_exception:
Traceback (most recent call last):
  File "", line 10, in 
    lumberjack()
  File "", line 4, in lumberjack
    bright_side_of_life()
IndexError: tuple index out of range
*** print_exc:
Traceback (most recent call last):
  File "", line 10, in 
    lumberjack()
  File "", line 4, in lumberjack
    bright_side_of_life()
IndexError: tuple index out of range
*** format_exc, first and last line:
Traceback (most recent call last):
IndexError: tuple index out of range
*** format_exception:
['Traceback (most recent call last):\n',
 '  File "", line 10, in \n    lumberjack()\n',
 '  File "", line 4, in lumberjack\n    bright_side_of_life()\n',
 '  File "", line 7, in bright_side_of_life\n    return tuple()[0]\n           ~~~~~~~^^^\n',
 'IndexError: tuple index out of range\n']
*** extract_tb:
[, line 10 in >,
 , line 4 in lumberjack>,
 , line 7 in bright_side_of_life>]
*** format_tb:
['  File "", line 10, in \n    lumberjack()\n',
 '  File "", line 4, in lumberjack\n    bright_side_of_life()\n',
 '  File "", line 7, in bright_side_of_life\n    return tuple()[0]\n           ~~~~~~~^^^\n']
*** tb_lineno: 10

Ví dụ sau đây cho thấy các cách khác nhau để in và định dạng ngăn xếp:

>>> import traceback
>>> def another_function():
...     lumberstack()
...
>>> def lumberstack():
...     traceback.print_stack()
...     print(repr(traceback.extract_stack()))
...     print(repr(traceback.format_stack()))
...
>>> another_function()
  File "", line 10, in 
    another_function()
  File "", line 3, in another_function
    lumberstack()
  File "", line 6, in lumberstack
    traceback.print_stack()
[('', 10, '', 'another_function()'),
 ('', 3, 'another_function', 'lumberstack()'),
 ('', 7, 'lumberstack', 'print(repr(traceback.extract_stack()))')]
['  File "", line 10, in \n    another_function()\n',
 '  File "", line 3, in another_function\n    lumberstack()\n',
 '  File "", line 8, in lumberstack\n    print(repr(traceback.format_stack()))\n']

Ví dụ cuối cùng này cho thấy một vài chức năng định dạng cuối cùng:

>>> import traceback
>>> traceback.format_list([('spam.py', 3, '', 'spam.eggs()'),
...                        ('eggs.py', 42, 'eggs', 'return "bacon"')])
['  File "spam.py", line 3, in \n    spam.eggs()\n',
 '  File "eggs.py", line 42, in eggs\n    return "bacon"\n']
>>> an_error = IndexError('tuple index out of range')
>>> traceback.format_exception_only(type(an_error), an_error)
['IndexError: tuple index out of range\n']