def to_code(self):
co_argcount = len(self.args) - self.varargs - self.varkwargs - self.kwonlyargcount<EOL>co_kwonlyargcount = self.kwonlyargcount<EOL>co_stacksize = self._compute_stacksize()<EOL>co_flags = self._compute_flags()<EOL>co_consts = [self.docstring]<EOL>co_names = []<EOL>co_varnames = list(self.args)<EOL>co_freevars = tuple(self.freevars)<EOL>cellvars = set( arg for op, arg in self.code<EOL>if isopcode(op)<EOL>and op in hasfree<EOL>and arg not in co_freevars<EOL>)<EOL>co_cellvars = [x for x in self.args if x in cellvars]<EOL>def index(seq, item, eq=operator.eq, can_append=True):<EOL><INDENT>"""<STR_LIT>"""<EOL>for i, x in enumerate(seq):<EOL><INDENT>if eq(x, item):<EOL><INDENT>return i<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if can_append:<EOL><INDENT>seq.append(item)<EOL>return len(seq) - <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>raise IndexError("<STR_LIT>")<EOL><DEDENT><DEDENT><DEDENT>jumps = []<EOL>label_pos = {}<EOL>lastlineno = self.firstlineno<EOL>lastlinepos = <NUM_LIT:0><EOL>co_code = array('<STR_LIT:B>')<EOL>co_lnotab = array('<STR_LIT:B>')<EOL>for i, (op, arg) in enumerate(self.code):<EOL><INDENT>if isinstance(op, Label):<EOL><INDENT>label_pos[op] = len(co_code)<EOL><DEDENT>elif isinstance( op, SetLinenoType ) :<EOL><INDENT>incr_lineno = arg - lastlineno<EOL>incr_pos = len(co_code) - lastlinepos<EOL>lastlineno = arg<EOL>lastlinepos = len(co_code)<EOL>if incr_lineno == <NUM_LIT:0> and incr_pos == <NUM_LIT:0>:<EOL><INDENT>co_lnotab.append(<NUM_LIT:0>)<EOL>co_lnotab.append(<NUM_LIT:0>)<EOL><DEDENT>else:<EOL><INDENT>while incr_pos > <NUM_LIT:255>:<EOL><INDENT>co_lnotab.append(<NUM_LIT:255>)<EOL>co_lnotab.append(<NUM_LIT:0>)<EOL>incr_pos -= <NUM_LIT:255><EOL><DEDENT>while incr_lineno > <NUM_LIT:255>:<EOL><INDENT>co_lnotab.append(incr_pos)<EOL>co_lnotab.append(<NUM_LIT:255>)<EOL>incr_pos = <NUM_LIT:0><EOL>incr_lineno -= <NUM_LIT:255><EOL><DEDENT>if incr_pos or incr_lineno:<EOL><INDENT>co_lnotab.append(incr_pos)<EOL>co_lnotab.append(incr_lineno)<EOL><DEDENT><DEDENT><DEDENT>elif op == opcode.EXTENDED_ARG:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>elif not op in hasarg:<EOL><INDENT>co_code.append(op)<EOL><DEDENT>else:<EOL><INDENT>if op in hasconst:<EOL><INDENT>if isinstance(arg, Code) :<EOL><INDENT>if i < len(self.code)-<NUM_LIT:2>and self.code[i+<NUM_LIT:2>][<NUM_LIT:0>] in hascode :<EOL><INDENT>arg = arg.to_code()<EOL><DEDENT>else :<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT><DEDENT>arg = index(co_consts, arg, operator.is_)<EOL><DEDENT>elif op in hasname:<EOL><INDENT>arg = index(co_names, arg)<EOL><DEDENT>elif op in hasjump:<EOL><INDENT>jumps.append((len(co_code), arg))<EOL>arg = <NUM_LIT:0><EOL><DEDENT>elif op in haslocal:<EOL><INDENT>arg = index(co_varnames, arg)<EOL><DEDENT>elif op in hascompare:<EOL><INDENT>arg = index(cmp_op, arg, can_append=False)<EOL><DEDENT>elif op in hasfree:<EOL><INDENT>try:<EOL><INDENT>arg = index(co_freevars, arg, can_append=False)+ len(cellvars)<EOL><DEDENT>except IndexError:<EOL><INDENT>arg = index(co_cellvars, arg)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>pass<EOL><DEDENT>if arg > <NUM_LIT>:<EOL><INDENT>co_code.append(opcode.EXTENDED_ARG)<EOL>co_code.append((arg >> <NUM_LIT:16>) & <NUM_LIT>)<EOL>co_code.append((arg >> <NUM_LIT>) & <NUM_LIT>)<EOL><DEDENT>co_code.append(op)<EOL>co_code.append(arg & <NUM_LIT>)<EOL>co_code.append((arg >> <NUM_LIT:8>) & <NUM_LIT>)<EOL><DEDENT><DEDENT>for pos, label in jumps:<EOL><INDENT>jump = label_pos[label]<EOL>if co_code[pos] in hasjrel:<EOL><INDENT>jump -= pos+<NUM_LIT:3><EOL><DEDENT>if jump > <NUM_LIT>:<EOL><INDENT>raise NotImplementedError("<STR_LIT>")<EOL><DEDENT>co_code[pos+<NUM_LIT:1>] = jump & <NUM_LIT><EOL>co_code[pos+<NUM_LIT:2>] = (jump >> <NUM_LIT:8>) & <NUM_LIT><EOL><DEDENT>co_code = co_code.tostring()<EOL>co_lnotab = co_lnotab.tostring()<EOL>co_consts = tuple(co_consts)<EOL>co_names = tuple(co_names)<EOL>co_varnames = tuple(co_varnames)<EOL>co_nlocals = len(co_varnames)<EOL>co_cellvars = tuple(co_cellvars)<EOL>return types.CodeType(co_argcount, co_kwonlyargcount, co_nlocals, co_stacksize, co_flags,<EOL>co_code,<EOL>co_consts, co_names, co_varnames,<EOL>self.filename, self.name, self.firstlineno, co_lnotab,<EOL>co_freevars, co_cellvars)<EOL>
Assemble a Python code object from this Code object.