• Kirill Smelkov's avatar
    encoder/decoder: Teach ogórek about tuple · f98f54b1
    Kirill Smelkov authored
    It is true that tuples and lists are very similar, and when e.g. a
    python function accepts tuple as input, and then uses only indexes
    access, it can be substituted with list.
    
    However some functions do `isinstance(..., tuple)` etc, and e.g. for a
    go program which wants to produce pickle for such python programs there
    is currently no way to generate pickle with tuple opcodes.
    
    So to solve this teach ogórek about tuples:
    
    - introduce simple Tuple type which has []interface{} as underlying
    - when decoding tuple opcodes produce this Tuple instead of
      []interface{} used previously.
    - when encoding encode Tuple with tuple opcodes.
    
    Otherwise Tuple can be seen and behaves like a regular go []interface{}
    slice. In fact the only difference is that runtime type of Tuple is
    different from runtime type of []interface{} but otherwise both values
    are the same and can be casted to each other if/when needed freely.
    
    NOTE opReduce decoder is adjusted to always require tuple, not list,
    because with e.g. cPickle:
    
        "c__main__\nf\n]R." -> TypeError('argument list must be a tuple', ...)
        "c__main__\nf\n)R." -> ok
    
    ( the first one uses empty list - "]", and the second one empty tuple - ")" )
    
    So it is ok and compatible to always require args to be tuple for
    reduce.
    f98f54b1
encode.go 6.81 KB