@classmethod<EOL><INDENT>def from_code(cls, code_object):<DEDENT>
<EOL>if isinstance( code_object, types.FunctionType ) :<EOL><INDENT>code_object = code_object.__code__<EOL><DEDENT>co_code = code_object.co_code<EOL>labels = dict((addr, Label()) for addr in findlabels(co_code))<EOL>linestarts = dict(cls._findlinestarts(code_object))<EOL>cellfree = code_object.co_cellvars + code_object.co_freevars<EOL>code = CodeList()   <EOL>n = len(co_code)    <EOL>i = <NUM_LIT:0>               <EOL>extended_arg = <NUM_LIT:0>    <EOL>while i < n:<EOL><INDENT>op = Opcode( co_code[i] )<EOL>if i in labels:<EOL><INDENT>code.append((labels[i], None))<EOL><DEDENT>if i in linestarts:<EOL><INDENT>code.append((SetLineno, linestarts[i]))<EOL><DEDENT>i += <NUM_LIT:1> <EOL>if op not in hasargx :<EOL><INDENT>code.append((op, None))<EOL><DEDENT>else:<EOL><INDENT>if op in hascode :<EOL><INDENT>if len(code) >= <NUM_LIT:2>and code[-<NUM_LIT:2>][<NUM_LIT:0>] == LOAD_CONSTand code[-<NUM_LIT:1>][<NUM_LIT:0>] == LOAD_CONSTand isinstance( code[-<NUM_LIT:2>][<NUM_LIT:1>], types.CodeType ) :<EOL><INDENT>code[-<NUM_LIT:2>] = ( Opcode(LOAD_CONST), Code.from_code( code[-<NUM_LIT:2>][<NUM_LIT:1>] ) )<EOL><DEDENT>else :<EOL><INDENT>raise ValueError(<EOL>'<STR_LIT>'<EOL>)<EOL><DEDENT><DEDENT>arg = co_code[i] + co_code[i+<NUM_LIT:1>]*<NUM_LIT> + extended_arg<EOL>extended_arg = <NUM_LIT:0> <EOL>i += <NUM_LIT:2> <EOL>if op == opcode.EXTENDED_ARG:<EOL><INDENT>extended_arg = arg << <NUM_LIT:16><EOL><DEDENT>elif op in hasconst:<EOL><INDENT>code.append((op, code_object.co_consts[arg]))<EOL><DEDENT>elif op in hasname:<EOL><INDENT>code.append((op, code_object.co_names[arg]))<EOL><DEDENT>elif op in hasjabs:<EOL><INDENT>code.append((op, labels[arg]))<EOL><DEDENT>elif op in hasjrel:<EOL><INDENT>code.append((op, labels[i + arg]))<EOL><DEDENT>elif op in haslocal:<EOL><INDENT>code.append((op, code_object.co_varnames[arg]))<EOL><DEDENT>elif op in hascompare:<EOL><INDENT>code.append((op, cmp_op[arg]))<EOL><DEDENT>elif op in hasfree:<EOL><INDENT>code.append((op, cellfree[arg]))<EOL><DEDENT>else:<EOL><INDENT>code.append((op, arg))<EOL><DEDENT><DEDENT><DEDENT>varargs = bool(code_object.co_flags & CO_VARARGS)<EOL>varkwargs = bool(code_object.co_flags & CO_VARKEYWORDS)<EOL>newlocals = bool(code_object.co_flags & CO_NEWLOCALS)<EOL>nargs = code_object.co_argcount + code_object.co_kwonlyargcount + varargs + varkwargs<EOL>args = code_object.co_varnames[ : nargs ]<EOL>docstring = None<EOL>if code_object.co_consts and isinstance(code_object.co_consts[<NUM_LIT:0>], str):<EOL><INDENT>docstring = code_object.co_consts[<NUM_LIT:0>]<EOL><DEDENT>return cls( code = code,<EOL>freevars = code_object.co_freevars,<EOL>args = args,<EOL>varargs = varargs,<EOL>varkwargs = varkwargs,<EOL>kwonlyargcount = code_object.co_kwonlyargcount,<EOL>newlocals = newlocals,<EOL>coflags = code_object.co_flags,<EOL>name = code_object.co_name,<EOL>filename = code_object.co_filename,<EOL>firstlineno = code_object.co_firstlineno,<EOL>docstring = docstring<EOL>)<EOL>
Disassemble a Python code object and make a Code object from the bits.
This is the expected way to make a Code instance. But you are welcome
to call Code() directly if you wish.