diff -r bf44913588b7 Lib/dis.py --- a/Lib/dis.py Mon Apr 11 00:40:08 2016 +0000 +++ b/Lib/dis.py Mon Apr 11 18:33:21 2016 +0800 @@ -13,7 +13,8 @@ "get_instructions", "Instruction", "Bytecode"] + _opcodes_all del _opcodes_all -_have_code = (types.MethodType, types.FunctionType, types.CodeType, type) +_have_code = (types.MethodType, types.FunctionType, types.CodeType, + classmethod, staticmethod, type) def _try_compile(source, name): """Attempts to compile the given source, first as an expression and diff -r bf44913588b7 Lib/test/test_dis.py --- a/Lib/test/test_dis.py Mon Apr 11 00:40:08 2016 +0000 +++ b/Lib/test/test_dis.py Mon Apr 11 18:33:21 2016 +0800 @@ -30,6 +30,14 @@ def __init__(self, x): self.x = x == 1 + @staticmethod + def sm(x): + x = x == 1 + + @classmethod + def cm(cls, x): + cls.x = x == 1 + dis_c_instance_method = """\ %-4d 0 LOAD_FAST 1 (x) 3 LOAD_CONST 1 (1) @@ -49,6 +57,37 @@ 15 LOAD_CONST 0 (0) 18 RETURN_VALUE """ +# class disassemble info has an extra newline at end +dis_c = """\ +Disassembly of %s: + %-4d 0 LOAD_FAST 1 (x) + 3 LOAD_CONST 1 (1) + 6 COMPARE_OP 2 (==) + 9 LOAD_FAST 0 (self) + 12 STORE_ATTR 0 (x) + 15 LOAD_CONST 0 (None) + 18 RETURN_VALUE + +Disassembly of %s: + %-4d 0 LOAD_FAST 1 (x) + 3 LOAD_CONST 1 (1) + 6 COMPARE_OP 2 (==) + 9 LOAD_FAST 0 (cls) + 12 STORE_ATTR 0 (x) + 15 LOAD_CONST 0 (None) + 18 RETURN_VALUE + +Disassembly of %s: + %-4d 0 LOAD_FAST 0 (x) + 3 LOAD_CONST 1 (1) + 6 COMPARE_OP 2 (==) + 9 STORE_FAST 0 (x) + 12 LOAD_CONST 0 (None) + 15 RETURN_VALUE + +""" % (_C.__init__.__name__, _C.__init__.__code__.co_firstlineno + 1, + _C.cm.__name__, _C.cm.__code__.co_firstlineno + 2, + _C.sm.__name__, _C.sm.__code__.co_firstlineno + 2) def _f(a): print(a) @@ -82,17 +121,17 @@ pass dis_bug708901 = """\ - %-4d 0 SETUP_LOOP 23 (to 26) +%3d 0 SETUP_LOOP 23 (to 26) 3 LOAD_GLOBAL 0 (range) 6 LOAD_CONST 1 (1) - %-4d 9 LOAD_CONST 2 (10) +%3d 9 LOAD_CONST 2 (10) 12 CALL_FUNCTION 2 (2 positional, 0 keyword pair) 15 GET_ITER >> 16 FOR_ITER 6 (to 25) 19 STORE_FAST 0 (res) - %-4d 22 JUMP_ABSOLUTE 16 +%3d 22 JUMP_ABSOLUTE 16 >> 25 POP_BLOCK >> 26 LOAD_CONST 0 (None) 29 RETURN_VALUE @@ -311,6 +350,9 @@ def test_disassemble_bytes(self): self.do_disassembly_test(_f.__code__.co_code, dis_f_co_code) + def test_disassemble_class(self): + self.do_disassembly_test(_C, dis_c) + def test_disassemble_method(self): self.do_disassembly_test(_C(1).__init__, dis_c_instance_method)