cpython: d71251d9fbbe (original) (raw)

Mercurial > cpython

changeset 87342:d71251d9fbbe

Close #17916: dis.Bytecode based replacement for distb - Bytecode.from_traceback() alternate constructor - current_offset parameter and attribute Patch by Claudiu Popa [#17916]

Nick Coghlan ncoghlan@gmail.com
date Sat, 23 Nov 2013 00:57:00 +1000
parents 57fbab22ab4e
children de65df13ed50
files Doc/library/dis.rst Doc/whatsnew/3.4.rst Lib/dis.py Lib/test/test_dis.py Misc/NEWS
diffstat 5 files changed, 98 insertions(+), 4 deletions(-)[+] [-] Doc/library/dis.rst 12 Doc/whatsnew/3.4.rst 3 Lib/dis.py 17 Lib/test/test_dis.py 66 Misc/NEWS 4

line wrap: on

line diff

--- a/Doc/library/dis.rst +++ b/Doc/library/dis.rst @@ -44,7 +44,7 @@ The bytecode analysis API allows pieces :class:Bytecode object that provides easy access to details of the compiled code. -.. class:: Bytecode(x, *, first_line=None) +.. class:: Bytecode(x, *, first_line=None, current_offset=None) Analyse the bytecode corresponding to a function, method, string of source code, or a code object (as returned by :func:compile). @@ -59,6 +59,16 @@ compiled code. Otherwise, the source line information (if any) is taken directly from the disassembled code object.

+ .. data:: codeobj The compiled code object.

--- a/Doc/whatsnew/3.4.rst +++ b/Doc/whatsnew/3.4.rst @@ -385,7 +385,8 @@ The new :class:dis.Bytecode class prov inspecting bytecode, both in human-readable form and for iterating over instructions. -(Contributed by Nick Coghlan, Ryan Kelly and Thomas Kluyver in :issue:11816) +(Contributed by Nick Coghlan, Ryan Kelly and Thomas Kluyver in :issue:11816 +and Claudiu Popa in :issue:17916) doctest

--- a/Lib/dis.py +++ b/Lib/dis.py @@ -406,7 +406,7 @@ class Bytecode: Iterating over this yields the bytecode operations as Instruction instances. """

@@ -417,6 +417,7 @@ class Bytecode: self._cell_names = co.co_cellvars + co.co_freevars self._linestarts = dict(findlinestarts(co)) self._original_object = x

def iter(self): co = self.codeobj @@ -429,6 +430,13 @@ class Bytecode: return "{}({!r})".format(self.class.name, self._original_object)

+ def info(self): """Return formatted information about the code object.""" return _format_code_info(self.codeobj) @@ -436,13 +444,18 @@ class Bytecode: def dis(self): """Return a formatted view of the bytecode operations.""" co = self.codeobj

--- a/Lib/test/test_dis.py +++ b/Lib/test/test_dis.py @@ -10,6 +10,21 @@ import io import types import contextlib +def get_tb():

+

+ +TRACEBACK_CODE = get_tb().tb_frame.f_code + class _C: def init(self, x): self.x = x == 1 @@ -174,6 +189,46 @@ dis_compound_stmt_str = """[](#l4.25) 25 RETURN_VALUE """ +dis_traceback = """[](#l4.29)

+

+

+

+""" % (TRACEBACK_CODE.co_firstlineno + 1,

+ class DisTests(unittest.TestCase): def get_disassembly(self, func, lasti=-1, wrapper=True): @@ -758,6 +813,17 @@ class BytecodeTests(unittest.TestCase): actual = dis.Bytecode(_f).dis() self.assertEqual(actual, dis_f)

+

+

def test_main(): run_unittest(DisTests, DisWithFileTests, CodeInfoTests,

--- a/Misc/NEWS +++ b/Misc/NEWS @@ -65,6 +65,10 @@ Core and Builtins Library ------- +- Issue #17916: Added dis.Bytecode.from_traceback() and