diff -r e3c9a47a83fb Lib/dis.py --- a/Lib/dis.py Mon Apr 11 17:33:27 2016 +0300 +++ b/Lib/dis.py Mon Apr 11 23:28:52 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 e3c9a47a83fb Lib/test/test_dis.py --- a/Lib/test/test_dis.py Mon Apr 11 17:33:27 2016 +0300 +++ b/Lib/test/test_dis.py Mon Apr 11 23:28:52 2016 +0800 @@ -30,8 +30,16 @@ 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) +%3d 0 LOAD_FAST 1 (x) 3 LOAD_CONST 1 (1) 6 COMPARE_OP 2 (==) 9 LOAD_FAST 0 (self) @@ -49,18 +57,49 @@ 15 LOAD_CONST 0 (0) 18 RETURN_VALUE """ +# class disassemble info has an extra newline at end +dis_c = """\ +Disassembly of %s: +%3d 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: +%3d 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: +%3d 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) return 1 dis_f = """\ - %-4d 0 LOAD_GLOBAL 0 (print) +%3d 0 LOAD_GLOBAL 0 (print) 3 LOAD_FAST 0 (a) 6 CALL_FUNCTION 1 (1 positional, 0 keyword pair) 9 POP_TOP - %-4d 10 LOAD_CONST 1 (1) +%3d 10 LOAD_CONST 1 (1) 13 RETURN_VALUE """ % (_f.__code__.co_firstlineno + 1, _f.__code__.co_firstlineno + 2) @@ -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 @@ -191,16 +230,16 @@ """ dis_traceback = """\ - %-4d 0 SETUP_EXCEPT 12 (to 15) +%3d 0 SETUP_EXCEPT 12 (to 15) - %-4d 3 LOAD_CONST 1 (1) +%3d 3 LOAD_CONST 1 (1) 6 LOAD_CONST 2 (0) --> 9 BINARY_TRUE_DIVIDE 10 POP_TOP 11 POP_BLOCK 12 JUMP_FORWARD 46 (to 61) - %-4d >> 15 DUP_TOP +%3d >> 15 DUP_TOP 16 LOAD_GLOBAL 0 (Exception) 19 COMPARE_OP 10 (exception match) 22 POP_JUMP_IF_FALSE 60 @@ -209,7 +248,7 @@ 29 POP_TOP 30 SETUP_FINALLY 14 (to 47) - %-4d 33 LOAD_FAST 0 (e) +%3d 33 LOAD_FAST 0 (e) 36 LOAD_ATTR 1 (__traceback__) 39 STORE_FAST 1 (tb) 42 POP_BLOCK @@ -222,7 +261,7 @@ 57 JUMP_FORWARD 1 (to 61) >> 60 END_FINALLY - %-4d >> 61 LOAD_FAST 1 (tb) +%3d >> 61 LOAD_FAST 1 (tb) 64 RETURN_VALUE """ % (TRACEBACK_CODE.co_firstlineno + 1, TRACEBACK_CODE.co_firstlineno + 2, @@ -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)