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

Skip to content

Commit 0bce6e7

Browse files
committed
whatsnew: expand 'dis' entry.
Also add one missing versionadded.
1 parent 985b8db commit 0bce6e7

2 files changed

Lines changed: 52 additions & 8 deletions

File tree

Doc/library/dis.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ the following command can be used to display the disassembly of
4040
Bytecode analysis
4141
-----------------
4242

43+
.. versionadded:: 3.4
44+
4345
The bytecode analysis API allows pieces of Python code to be wrapped in a
4446
:class:`Bytecode` object that provides easy access to details of the
4547
compiled code.

Doc/whatsnew/3.4.rst

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -558,15 +558,57 @@ differences between single use, reusable and reentrant context managers.
558558
dis
559559
---
560560

561+
Functions :func:`~dis.show_code`, :func:`~dis.dis`, :func:`~dis.distb`, and
562+
:func:`~dis.disassemble` now accept a keyword-only *file* argument that
563+
controls where they write their output.
564+
561565
The :mod:`dis` module is now built around an :class:`~dis.Instruction` class
562-
that provides details of individual bytecode operations and a
563-
:func:`~dis.get_instructions` iterator that emits the Instruction stream for a
564-
given piece of Python code. The various display tools in the :mod:`dis`
565-
module have been updated to be based on these new components.
566-
567-
The new :class:`dis.Bytecode` class provides an object-oriented API for
568-
inspecting bytecode, both in human-readable form and for iterating over
569-
instructions.
566+
that provides object oriented access to the details of each individual bytecode
567+
operation.
568+
569+
A new method, :func:`~dis.get_instructions`, provides an iterator that emits
570+
the Instruction stream for a given piece of Python code. Thus it is now
571+
possible to write a program that inspects and manipulates a bytecode
572+
object in ways different from those provided by the :mod:`~dis` module
573+
itself. For example::
574+
575+
>>> import dis
576+
>>> for instr in dis.get_instructions(lambda x: x + 1):
577+
... print(instr.opname)
578+
LOAD_FAST
579+
LOAD_CONST
580+
BINARY_ADD
581+
RETURN_VALUE
582+
583+
The various display tools in the :mod:`dis` module have been rewritten to use
584+
these new components.
585+
586+
In addition, a new application-friendly class :class:`~dis.Bytecode` provides
587+
an object-oriented API for inspecting bytecode in both in human-readable form
588+
and for iterating over instructions. The :class:`~dis.Bytecode` constructor
589+
takes the same arguments that :func:`~dis.get_instruction` does (plus an
590+
optional *current_offset*), and the resulting object can be iterated to produce
591+
:class:`~dis.Instruction` objects. But it also has a :mod:`~dis.Bytecode.dis`
592+
method, equivalent to calling :mod:`~dis.dis` on the constructor argument, but
593+
returned as a multi-line string::
594+
595+
>>> bytecode = dis.Bytecode(lambda x: x +1, current_offset=3)
596+
>>> for instr in bytecode:
597+
... print('{} ({})'.format(instr.opname, instr.opcode))
598+
LOAD_FAST (124)
599+
LOAD_CONST (100)
600+
BINARY_ADD (23)
601+
RETURN_VALUE (83)
602+
>>> bytecode.dis().splitlines() # doctest: +NORMALIZE_WHITESPACE
603+
[' 1 0 LOAD_FAST 0 (x)',
604+
' --> 3 LOAD_CONST 1 (1)',
605+
' 6 BINARY_ADD',
606+
' 7 RETURN_VALUE']
607+
608+
:class:`~dis.Bytecode` also has a class method,
609+
:meth:`~dis.Bytecode.from_traceback`, that provides the ability to manipulate a
610+
traceback (that is, ``print(Bytecode.from_traceback(tb).dis())`` is equivalent
611+
to ``distb(tb)``).
570612

571613
(Contributed by Nick Coghlan, Ryan Kelly and Thomas Kluyver in :issue:`11816`
572614
and Claudiu Popa in :issue:`17916`)

0 commit comments

Comments
 (0)