MatthieuDartiailh/bytecode

Re-implement bytecode on top of codetype

Closed this issue · 1 comments

Using the builtin code type can guard against future changes to the bytecode format. This is from an SO question:

MyCode= CodeType(
        0,
        0,
        0,
        3,
        64,
        bytes([101, 0, 0,    #Load print function
               101, 1, 0,    #Load name 'a'
               101, 2, 0,    #Load name 'b'
               23,           #Take first two stack elements and store their sum
               131, 1, 0,    #Call first element in the stack with one positional argument
               1,            #Pop top of stack
               101, 0, 0,    #Load print function
               101, 1, 0,    #Load name 'a'
               101, 2, 0,    #Load name 'b'
               20,           #Take first two stack elements and store their product
               131, 1, 0,    #Call first element in the stack with one positional argument
               1,            #Pop top of stack
               100, 0, 0,    #Load constant None
               83]),         #Return top of stack
        (None,),
        ('print', 'a', 'b'),
        (),
        'PersonalCodeObject',
        'MyCode',
        1,
        bytes([14,1]),
        (),
        () )

It is already so (look at https://github.com/haypo/bytecode/blob/master/bytecode/concrete.py in ConcreteBytecode.to_code method). And actually does not solve everything (the example you posted works on 3.5 not 3.6 that use wordcode (2 byes per instruction)).