def disassemble(co, lasti=-1):
    """Disassemble a code object."""
    # Taken from dis.disassemble, returns disassembled code instead of printing
    # it (the fuck python ?).
    # Also, unicodified.
    # Also, use % operator instead of string operations.
    # Also, one statement per line.
    out = StringIO()
    code = co.co_code
    labels = dis.findlabels(code)
    linestarts = dict(dis.findlinestarts(co))
    n = len(code)
    i = 0
    extended_arg = 0
    free = None
    while i < n:
        c = code[i]
        op = ord(c)
        if i in linestarts:
            if i > 0:
                print(end=u'\n', file=out)
            print(u'%3d' % linestarts[i], end=u' ', file=out)
        else:
            print(u'   ', end=u' ', file=out)

        if i == lasti:
            print(u'-->', end=u' ', file=out)
        else:
            print(u'   ', end=u' ', file=out)
        if i in labels:
            print(u'>>', end=u' ', file=out)
        else:
            print(u'  ', end=u' ', file=out)
        print(u'%4i' % i, end=u' ', file=out)
        print(u'%-20s' % dis.opname[op], end=u' ', file=out)
        i = i + 1
        if op >= dis.HAVE_ARGUMENT:
            oparg = ord(code[i]) + ord(code[i + 1]) * 256 + extended_arg
            extended_arg = 0
            i = i + 2
            if op == dis.EXTENDED_ARG:
                extended_arg = oparg * 65536
            print(u'%5i' % oparg, end=u' ', file=out)
            if op in dis.hasconst:
                print(u'(%r)' % co.co_consts[oparg], end=u' ', file=out)
            elif op in dis.hasname:
                print(u'(%s)' % co.co_names[oparg], end=u' ', file=out)
            elif op in dis.hasjrel:
                print(u'(to %r)' % (i + oparg), end=u' ', file=out)
            elif op in dis.haslocal:
                print(u'(%s)' % co.co_varnames[oparg], end=u' ', file=out)
            elif op in dis.hascompare:
                print(u'(%s)' % dis.cmp_op[oparg], end=u' ', file=out)
            elif op in dis.hasfree:
                if free is None:
                    free = co.co_cellvars + co.co_freevars
                print(u'(%s)' % free[oparg], end=u' ', file=out)
        print(end=u'\n', file=out)
    return out.getvalue()
def disassemble ( co , lasti = - 1 ) : out = StringIO ( ) code = co . co_code labels = dis . findlabels ( code ) linestarts = dict ( dis . findlinestarts ( co ) ) n = len ( code ) i = 0 extended_arg = 0 free = None while i < n : c = code [ i ] op = ord ( c ) if i in linestarts : if i > 0 : print ( end = u'\n' , file = out ) print ( u'%3d' % linestarts [ i ] , end = u' ' , file = out ) else : print ( u'   ' , end = u' ' , file = out ) if i == lasti : print ( u'-->' , end = u' ' , file = out ) else : print ( u'   ' , end = u' ' , file = out ) if i in labels : print ( u'>>' , end = u' ' , file = out ) else : print ( u'  ' , end = u' ' , file = out ) print ( u'%4i' % i , end = u' ' , file = out ) print ( u'%-20s' % dis . opname [ op ] , end = u' ' , file = out ) i = i + 1 if op >= dis . HAVE_ARGUMENT : oparg = ord ( code [ i ] ) + ord ( code [ i + 1 ] ) * 256 + extended_arg extended_arg = 0 i = i + 2 if op == dis . EXTENDED_ARG : extended_arg = oparg * 65536 print ( u'%5i' % oparg , end = u' ' , file = out ) if op in dis . hasconst : print ( u'(%r)' % co . co_consts [ oparg ] , end = u' ' , file = out ) elif op in dis . hasname : print ( u'(%s)' % co . co_names [ oparg ] , end = u' ' , file = out ) elif op in dis . hasjrel : print ( u'(to %r)' % ( i + oparg ) , end = u' ' , file = out ) elif op in dis . haslocal : print ( u'(%s)' % co . co_varnames [ oparg ] , end = u' ' , file = out ) elif op in dis . hascompare : print ( u'(%s)' % dis . cmp_op [ oparg ] , end = u' ' , file = out ) elif op in dis . hasfree : if free is None : free = co . co_cellvars + co . co_freevars print ( u'(%s)' % free [ oparg ] , end = u' ' , file = out ) print ( end = u'\n' , file = out ) return out . getvalue ( )
Disassemble a code object.