Movatterモバイル変換


[0]ホーム

URL:


Previous PageUp One LevelNext PagePython Library ReferenceContentsModule IndexIndex
Previous:3.11.4 The interpreter stackUp:3. Python Runtime ServicesNext:3.12.1 Traceback Example

3.12traceback -- Print or retrieve a stack traceback

This module provides a standard interface to extract, format and printstack traces of Python programs. It exactly mimics the behavior ofthe Python interpreter when it prints a stack trace. This is usefulwhen you want to print stack traces under program control, such as in a``wrapper'' around the interpreter.

The module uses traceback objects -- this is the object type that isstored in the variablessys.exc_traceback (deprecated) andsys.last_traceback and returned as the third item fromsys.exc_info(). 

The module defines the following functions:

print_tb(traceback[, limit[, file]])
Print up tolimit stack trace entries fromtraceback. Iflimit is omitted orNone, all entries are printed.Iffile is omitted orNone, the output goes tosys.stderr; otherwise it should be an open file or file-likeobject to receive the output.

print_exception(type, value, traceback[, limit[, file]])
Print exception information and up tolimit stack trace entriesfromtraceback tofile.This differs fromprint_tb() in thefollowing ways: (1) iftraceback is notNone, it prints aheader "Traceback (most recent call last):"; (2) it prints theexceptiontype andvalue after the stack trace; (3) iftype isSyntaxError andvalue has theappropriate format, it prints the line where the syntax error occurredwith a caret indicating the approximate position of the error.

print_exc([limit[, file]])
This is a shorthand forprint_exception(sys.exc_type,sys.exc_value, sys.exc_traceback,limit,file). (Infact, it usessys.exc_info() to retrieve the sameinformation in a thread-safe way instead of using the deprecatedvariables.)

print_last([limit[, file]])
This is a shorthand forprint_exception(sys.last_type,sys.last_value, sys.last_traceback,limit,file).

print_stack([f[, limit[, file]]])
This function prints a stack trace from its invocation point. Theoptionalf argument can be used to specify an alternate stackframe to start. The optionallimit andfile arguments have thesame meaning as forprint_exception().

extract_tb(traceback[, limit])
Return a list of up tolimit ``pre-processed'' stack traceentries extracted from the traceback objecttraceback. It isuseful for alternate formatting of stack traces. Iflimit isomitted orNone, all entries are extracted. A``pre-processed'' stack trace entry is a quadruple (filename,line number,function name,text) representingthe information that is usually printed for a stack trace. Thetext is a string with leading and trailing whitespacestripped; if the source is not available it isNone.

extract_stack([f[, limit]])
Extract the raw traceback from the current stack frame. The returnvalue has the same format as forextract_tb(). Theoptionalf andlimit arguments have the same meaning asforprint_stack().

format_list(list)
Given a list of tuples as returned byextract_tb() orextract_stack(), return a list of strings ready forprinting. Each string in the resulting list corresponds to the itemwith the same index in the argument list. Each string ends in anewline; the strings may contain internal newlines as well, for thoseitems whose source text line is notNone.

format_exception_only(type, value)
Format the exception part of a traceback. The arguments are theexception type and value such as given bysys.last_type andsys.last_value. The return value is a list of strings, eachending in a newline. Normally, the list contains a single string;however, forSyntaxError exceptions, it contains severallines that (when printed) display detailed information about where thesyntax error occurred. The message indicating which exceptionoccurred is the always last string in the list.

format_exception(type, value, tb[, limit])
Format a stack trace and the exception information. The arguments have the same meaning as the corresponding arguments toprint_exception(). The return value is a list of strings,each ending in a newline and some containing internal newlines. Whenthese lines are concatenated and printed, exactly the same text isprinted as doesprint_exception().

format_tb(tb[, limit])
A shorthand forformat_list(extract_tb(tb,limit)).

format_stack([f[, limit]])
A shorthand forformat_list(extract_stack(f,limit)).

tb_lineno(tb)
This function returns the current line number set in the tracebackobject. This is normally the same as thetb.tb_linenofield of the object, but when optimization is used (the -O flag) thisfield is not updated correctly; this function calculates the correctvalue.


Subsections


Previous PageUp One LevelNext PagePython Library ReferenceContentsModule IndexIndex
Previous:3.11.4 The interpreter stackUp:3. Python Runtime ServicesNext:3.12.1 Traceback Example
Release 2.2.3, documentation updated on 30 May 2003.
SeeAbout this document... for information on suggesting changes.
[8]ページ先頭

©2009-2026 Movatter.jp