Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions 39 Doc/library/traceback.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,29 @@ The module defines the following functions:

.. function:: print_exc(limit=None, file=None, chain=True)

This is a shorthand for ``print_exception(*sys.exc_info(), limit, file,
Print exception up to *limit* stack trace entries from *tb* to *file*.

The optional argument *limit* defines how many entries are printed. If it
is omitted or ``None``, all the entries are printed.
If the optional argument *file* is omitted or ``None``, the outuput goes to
``sys.stderr``; otherwise *file* should be an open file or file-like object
with a write() method.

It is a shorthand for ``print_exception(*sys.exc_info(), limit, file,
chain)``.


.. function:: print_last(limit=None, file=None, chain=True)

This is a shorthand for ``print_exception(sys.last_type, sys.last_value,
Print the last exception and the stack trace.

The optional argument *limit* defines how many entries are printed. If it
is omitted or ``None``, all the entries are printed.
If the optional argument *file* is omitted or ``None``, the output goes to
``sys.stderr``; otherwise *file* should be an open file or file-like object
with a write() method.

It is a shorthand for ``print_exception(sys.last_type, sys.last_value,
sys.last_traceback, limit, file, chain)``. In general it will work only
after an exception has reached an interactive prompt (see
:data:`sys.last_type`).
Expand Down Expand Up @@ -152,12 +168,27 @@ The module defines the following functions:

.. function:: format_tb(tb, limit=None)

A shorthand for ``format_list(extract_tb(tb, limit))``.
Return an :func:`extract_tb` object which returns a list of pre-processed
entries from *tb*.

The argument *tb* is the traceback to be foramtted.
The optional argument *limit* defines how many entries are formatted.
If it is omitted or ``None``, all entries are formatted ready to be
printed.

It is a shorthand for ``format_list(extract_tb(tb, limit))``.


.. function:: format_stack(f=None, limit=None)

A shorthand for ``format_list(extract_stack(f, limit))``.
Return a :func:`format_list` object which is a list of strings with the
messages of the traceback formatted ready to be printed.

The optional argument *f* is the stack frame from which to start.
The optional argument *limit* defines how many entries are formatted.
If it is omitted or ``None``, all entries are formatted.

It is a shorthand for ``format_list(extract_stack(f, limit))``.

.. function:: clear_frames(tb)

Expand Down
44 changes: 39 additions & 5 deletions 44 Lib/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,17 @@ def print_tb(tb, limit=None, file=None):
print_list(extract_tb(tb, limit=limit), file=file)

def format_tb(tb, limit=None):
"""A shorthand for 'format_list(extract_tb(tb, limit))'."""
"""Return an 'extract_tb' object which returns a list of pre-processed
entries from 'tb'.

The argument 'tb' is the traceback to be foramtted.
The optional argument 'limit' defines how many entries are formatted.
If it is omitted or None, then all entries are formatted ready to be
printed.

It is a shorthand for 'format_list(extract_tb(tb, limit))',
which returns a list of strings ready for printing.
"""
return extract_tb(tb, limit=limit).format()

def extract_tb(tb, limit=None):
Expand Down Expand Up @@ -159,16 +169,32 @@ def _some_str(value):
# --

def print_exc(limit=None, file=None, chain=True):
"""Shorthand for 'print_exception(*sys.exc_info(), limit, file)'."""
"""Print exception up to 'limit' stack trace entries from 'tb' to 'file'.

The optional argument 'limit' defines how many entries are printed.
If the optional argument 'file' is omitted or None, the output goes to
sys.stderr; otherwise 'file' should be an open file or file-like object
with a write() method.

It is a shorthand for 'print_exception(*sys.exc_info(), limit, file)'.
"""
print_exception(*sys.exc_info(), limit=limit, file=file, chain=chain)

def format_exc(limit=None, chain=True):
"""Like print_exc() but return a string."""
return "".join(format_exception(*sys.exc_info(), limit=limit, chain=chain))

def print_last(limit=None, file=None, chain=True):
"""This is a shorthand for 'print_exception(sys.last_type,
sys.last_value, sys.last_traceback, limit, file)'."""
"""Print the last exception and the stack trace.

The optional argument 'limit' defines how many entries are printed.
If the optional argument 'file' is omitted or None, the oputput goes to
sys.stderr; otherwise 'file' should be an open file or file-like object
with a write() method.

It is a shorthand for 'print_exception(sys.last_type,
sys.last_value, sys.last_traceback, limit, file)'.
"""
if not hasattr(sys, "last_type"):
raise ValueError("no last exception")
print_exception(sys.last_type, sys.last_value, sys.last_traceback,
Expand All @@ -191,7 +217,15 @@ def print_stack(f=None, limit=None, file=None):


def format_stack(f=None, limit=None):
"""Shorthand for 'format_list(extract_stack(f, limit))'."""
"""Return a 'format_list' object which is a list of strings with the
messages of the traceback formatted ready to be printed.

The optional argument 'f' is the stack frame from which to start.
The optional argument 'limit' defines how many entries are formatted.
If it is omitted or None, then all entries are formatted.

It is a shorthand for 'format_list(extract_stack(f, limit))'.
"""
if f is None:
f = sys._getframe().f_back
return format_list(extract_stack(f, limit=limit))
Expand Down
1 change: 1 addition & 0 deletions 1 Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Juancarlo Añez
Chris Angelico
Jérémy Anger
Jon Anglin
Michele Angrisano
Ankur Ankan
Heidi Annexstad
Ramchandra Apte
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Make the docstring of :meth:`format_tb` more clean than the previous one.
Now it's clear that :meth:`format_tb` returns a list of strings.
Morty Proxy This is a proxified and sanitized view of the page, visit original site.