Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 10e1a0c

Browse files
authored
gh-112137: change dis output to display labels instead of offsets (#112138)
1 parent 790db85 commit 10e1a0c

File tree

5 files changed

+838
-815
lines changed

5 files changed

+838
-815
lines changed

Doc/library/dis.rst

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ interpreter.
5151
transparent for forward jumps but needs to be taken into account when
5252
reasoning about backward jumps.
5353

54+
.. versionchanged:: 3.13
55+
The output shows logical labels rather than instruction offsets
56+
for jump targets and exception handlers. The ``-O`` command line
57+
option and the ``show_offsets`` argument were added.
58+
5459
Example: Given the function :func:`!myfunc`::
5560

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

6469
>>> dis.dis(myfunc)
65-
2 0 RESUME 0
70+
2 RESUME 0
6671
<BLANKLINE>
67-
3 2 LOAD_GLOBAL 1 (len + NULL)
68-
12 LOAD_FAST 0 (alist)
69-
14 CALL 1
70-
22 RETURN_VALUE
72+
3 LOAD_GLOBAL 1 (len + NULL)
73+
LOAD_FAST 0 (alist)
74+
CALL 1
75+
RETURN_VALUE
7176

7277
(The "2" is a line number).
7378

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

8186
.. code-block:: sh
8287
83-
python -m dis [-h] [-C] [infile]
88+
python -m dis [-h] [-C] [-O] [infile]
8489
8590
The following options are accepted:
8691

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

95100
Show inline caches.
96101

102+
.. cmdoption:: -O, --show-offsets
103+
104+
Show offsets of instructions.
105+
97106
If :file:`infile` is specified, its disassembled code will be written to stdout.
98107
Otherwise, disassembly is performed on compiled source code recieved from stdin.
99108

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

109118
.. class:: Bytecode(x, *, first_line=None, current_offset=None,\
110-
show_caches=False, adaptive=False)
119+
show_caches=False, adaptive=False, show_offsets=False)
111120

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

144+
If *show_offsets* is ``True``, :meth:`.dis` will include instruction
145+
offsets in the output.
146+
135147
.. classmethod:: from_traceback(tb, *, show_caches=False)
136148

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

256268

257-
.. function:: distb(tb=None, *, file=None, show_caches=False, adaptive=False)
269+
.. function:: distb(tb=None, *, file=None, show_caches=False, adaptive=False,
270+
show_offset=False)
258271

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

285+
.. versionchanged:: 3.13
286+
Added the *show_offsets* parameter.
272287

273288
.. function:: disassemble(code, lasti=-1, *, file=None, show_caches=False, adaptive=False)
274-
disco(code, lasti=-1, *, file=None, show_caches=False, adaptive=False)
289+
disco(code, lasti=-1, *, file=None, show_caches=False, adaptive=False,
290+
show_offsets=False)
275291
276292
Disassemble a code object, indicating the last instruction if *lasti* was
277293
provided. The output is divided in the following columns:
@@ -296,6 +312,8 @@ operation is being performed, so the intermediate analysis object isn't useful:
296312
.. versionchanged:: 3.11
297313
Added the *show_caches* and *adaptive* parameters.
298314

315+
.. versionchanged:: 3.13
316+
Added the *show_offsets* parameter.
299317

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

Doc/whatsnew/3.13.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,15 @@ copy
168168
any user classes which define the :meth:`!__replace__` method.
169169
(Contributed by Serhiy Storchaka in :gh:`108751`.)
170170

171+
dis
172+
---
173+
174+
* Change the output of :mod:`dis` module functions to show logical
175+
labels for jump targets and exception handlers, rather than offsets.
176+
The offsets can be added with the new ``-O`` command line option or
177+
the ``show_offsets`` parameter.
178+
(Contributed by Irit Katriel in :gh:`112137`.)
179+
171180
dbm
172181
---
173182

0 commit comments

Comments
 (0)