Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
36 changes: 27 additions & 9 deletions Doc/library/dis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ interpreter.
transparent for forward jumps but needs to be taken into account when
reasoning about backward jumps.

.. versionchanged:: 3.13
The output shows logical labels rather than instruction offsets
for jump targets and exception handlers. The ``-O`` command line
option and the ``show_offsets`` argument were added.

Example: Given the function :func:`!myfunc`::

def myfunc(alist):
Expand All @@ -62,12 +67,12 @@ the following command can be used to display the disassembly of
.. doctest::

>>> dis.dis(myfunc)
2 0 RESUME 0
2 RESUME 0
<BLANKLINE>
3 2 LOAD_GLOBAL 1 (len + NULL)
12 LOAD_FAST 0 (alist)
14 CALL 1
22 RETURN_VALUE
3 LOAD_GLOBAL 1 (len + NULL)
LOAD_FAST 0 (alist)
CALL 1
RETURN_VALUE

(The "2" is a line number).

Expand All @@ -80,7 +85,7 @@ The :mod:`dis` module can be invoked as a script from the command line:

.. code-block:: sh

python -m dis [-h] [-C] [infile]
python -m dis [-h] [-C] [-O] [infile]

The following options are accepted:

Expand All @@ -94,6 +99,10 @@ The following options are accepted:

Show inline caches.

.. cmdoption:: -O, --show-offsets

Show offsets of instructions.

If :file:`infile` is specified, its disassembled code will be written to stdout.
Otherwise, disassembly is performed on compiled source code recieved from stdin.

Expand All @@ -107,7 +116,7 @@ The bytecode analysis API allows pieces of Python code to be wrapped in a
code.

.. class:: Bytecode(x, *, first_line=None, current_offset=None,\
show_caches=False, adaptive=False)
show_caches=False, adaptive=False, show_offsets=False)

Analyse the bytecode corresponding to a function, generator, asynchronous
generator, coroutine, method, string of source code, or a code object (as
Expand All @@ -132,6 +141,9 @@ code.
If *adaptive* is ``True``, :meth:`.dis` will display specialized bytecode
that may be different from the original bytecode.

If *show_offsets* is ``True``, :meth:`.dis` will include instruction
offsets in the output.

.. classmethod:: from_traceback(tb, *, show_caches=False)

Construct a :class:`Bytecode` instance from the given traceback, setting
Expand Down Expand Up @@ -254,7 +266,8 @@ operation is being performed, so the intermediate analysis object isn't useful:
Added the *show_caches* and *adaptive* parameters.


.. function:: distb(tb=None, *, file=None, show_caches=False, adaptive=False)
.. function:: distb(tb=None, *, file=None, show_caches=False, adaptive=False,
show_offset=False)

Disassemble the top-of-stack function of a traceback, using the last
traceback if none was passed. The instruction causing the exception is
Expand All @@ -269,9 +282,12 @@ operation is being performed, so the intermediate analysis object isn't useful:
.. versionchanged:: 3.11
Added the *show_caches* and *adaptive* parameters.

.. versionchanged:: 3.13
Added the *show_offsets* parameter.

.. function:: disassemble(code, lasti=-1, *, file=None, show_caches=False, adaptive=False)
disco(code, lasti=-1, *, file=None, show_caches=False, adaptive=False)
disco(code, lasti=-1, *, file=None, show_caches=False, adaptive=False,
show_offsets=False)

Disassemble a code object, indicating the last instruction if *lasti* was
provided. The output is divided in the following columns:
Expand All @@ -296,6 +312,8 @@ operation is being performed, so the intermediate analysis object isn't useful:
.. versionchanged:: 3.11
Added the *show_caches* and *adaptive* parameters.

.. versionchanged:: 3.13
Added the *show_offsets* parameter.

.. function:: get_instructions(x, *, first_line=None, show_caches=False, adaptive=False)

Expand Down
9 changes: 9 additions & 0 deletions Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,15 @@ copy
any user classes which define the :meth:`!__replace__` method.
(Contributed by Serhiy Storchaka in :gh:`108751`.)

dis
---

* Change the output of :mod:`dis` module functions to show logical
labels for jump targets and exception handlers, rather than offsets.
The offsets can be added with the new ``-O`` command line option or
the ``show_offsets`` parameter.
(Contributed by Irit Katriel in :gh:`112137`.)

dbm
---

Expand Down
Loading