Skip to main content

Convenience functions for the python execution stack.

Project description

I find the supplied python traceback facilities quite awkward. These functions provide convenient facilities.

Latest release 20250306: frame: accept an optional traceback object or exception as a source from which to obtain the stack, add optional limit to crop the stack.

caller(frame_index=-3)

Return the Frame of the caller's caller. Returns None if frame_index is out of range.

Useful frame_index values:

  • -1: caller, this function
  • -2: invoker, who wants to know the caller
  • -3: the calling function of the invoker

The default from_index value is -3.

frames(source=None, limit=None)

Return the current stack as a StackSummary instance, a list of FrameSummary instances. Crop the returned list at limit if it is not None.

If source is omitted or None, obtain the source stack from traceback.extract_stack(). Otherwise if source has a .tb_frame attribute (like a traceback object) or source has a .__traceback__ attribute (like an exception), call traceback.extract_stack with that to obtain the source stack.

stack_dump(f=None, indent=0, summary=None, skip=None, select=None, format_frame=None)

Recite current or supplied stack to f, default sys.stderr.

Parameters:

  • f: the output file object, default sys.stderr
  • indent: how many spaces to indent the stack lines, default 0
  • summary: the stack Frames to write, default obtained from the current stack
  • skip: the number of Frames to trim from the end of summary; if summary is None this defaults to 2 to trim the Frames for the stack_dump function and its call to frames(), otherwise the default is 0 to use the supplied Frames as is
  • select: if not None, select particular frames; if select is a str it must be present in the frame filename; otherwise select(frame) must be true

Release Log

Release 20250306: frame: accept an optional traceback object or exception as a source from which to obtain the stack, add optional limit to crop the stack.

Release 20240630:

  • The fallback StackSummary: add format_frame_summary(frame) static method.
  • stack_dump: updates to avoid a name conflict, expect to work off a StackSummary or a list of frames.

Release 20240519: Drop Frame.funcname property.

Release 20240412:

  • stack_dump: new select parameter to pick interesting frames for the listing.
  • Provide StackSummary and FrameSummary if Python too old.

Release 20220918: caller(): return None if the frame offset is out of range.

Release 20220429: caller: return None if the stack index is out of range.

Release 20190812: stack_dump(): trim the last 2 frames from the listing by default (they are part of stack_dump's implementation).

Release 20190101:

  • _Frame: rename .functionname to .funcname; caller: turn raw frames into Frames.
  • caller(): accept optional frame_index, default -3.

Release 20160827: Add stack_dump().

Release 20150115: PyPI metadata fixups.

Release 20150111: Tag for initial PyPI release of cs.py.stack.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cs_py_stack-20250306.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cs_py_stack-20250306-py2.py3-none-any.whl (4.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file cs_py_stack-20250306.tar.gz.

File metadata

  • Download URL: cs_py_stack-20250306.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for cs_py_stack-20250306.tar.gz
Algorithm Hash digest
SHA256 e92e647c838fbb42fdd84c724863126d61648627b14a74d5c1246e79e8ad2e84
MD5 ce557b21020cca4372abadf70d483b90
BLAKE2b-256 d21260553b8540566287e5d22a5841bc232b491864609829a82616fb48333c92

See more details on using hashes here.

File details

Details for the file cs_py_stack-20250306-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for cs_py_stack-20250306-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 f1817f04321e3f6fb85fa75a630af4fbdfd9c2fe8128a543d21e9e539e649a6b
MD5 1a174e8fdd3a5bf3aa05a1cfb33f381b
BLAKE2b-256 a6cecf132ea5a2bdb86ff597804be05f1d891ee6043117cccf1c9d96924e5e5d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page