lib/python/pickle/protocol1.rb in python-pickle-0.1.1 vs lib/python/pickle/protocol1.rb in python-pickle-0.2.0

- old
+ new

@@ -31,28 +31,102 @@ # # @api private # class Protocol1 < Protocol0 - # Opcodes for Pickle protocol version 1. + # The `EMPTY_TUPLE` opcode. # - # @see https://github.com/python/cpython/blob/main/Lib/pickletools.py - OPCODES = Protocol0::OPCODES + Set[ - 41, # EMPTY_TUPLE - 71, # BINFLOAT - 75, # BININT1 - 84, # BINSTRING - 85, # SHORT_BINSTRING - 88, # BINUNICODE - 93, # EMPTY_LIST - 101, # APPENDS - 113, # BINPUT - 117, # SETITEMS - 125 # EMPTY_DICT - ] + # @since 0.2.0 + # + # @see https://github.com/python/cpython/blob/v2.7/Lib/pickle.py#L140 + EMPTY_TUPLE = 41 + # The `BINFLOAT` opcode. # + # @since 0.2.0 + # + # @see https://github.com/python/cpython/blob/v2.7/Lib/pickle.py#L142 + BINFLOAT = 71 + + # The `BININT1` opcode. + # + # @since 0.2.0 + # + # @see https://github.com/python/cpython/blob/v2.7/Lib/pickle.py#L110 + BININT1 = 75 + + # The `BINSTRING` opcode. + # + # @since 0.2.0 + # + # @see https://github.com/python/cpython/blob/v2.7/Lib/pickle.py#L118 + BINSTRING = 84 + + # The `SHORT_BINSTRING` opcode. + # + # @since 0.2.0 + # + # @see https://github.com/python/cpython/blob/v2.7/Lib/pickle.py#L119 + SHORT_BINSTRING = 85 + + # The `BINUNICODE` opcode. + # + # @since 0.2.0 + # + # @see https://github.com/python/cpython/blob/v2.7/Lib/pickle.py#L121 + BINUNICODE = 88 + + # The `EMPTY_LIST` opcode. + # + # @since 0.2.0 + # + # @see https://github.com/python/cpython/blob/v2.7/Lib/pickle.py#L133 + EMPTY_LIST = 93 + + # The `APPENDS` opcode. + # + # @since 0.2.0 + # + # @see https://github.com/python/cpython/blob/v2.7/Lib/pickle.py#L127 + APPENDS = 101 + + # The `BINGET` opcode. + # + # @since 0.2.0 + # + # @see https://github.com/python/cpython/blob/v2.7/Lib/pickle.py#L138 + BINGET = 104 + + # The `LONG_BINGET` opcode. + # + # @since 0.2.0 + # + # @see https://github.com/python/cpython/blob/v2.7/Lib/pickle.py#L140 + LONG_BINGET = 106 + + # The `BINPUT` opcode. + # + # @since 0.2.0 + # + # @see https://github.com/python/cpython/blob/v2.7/Lib/pickle.py#L145 + BINPUT = 113 + + # The `SETITEMS` opcode. + # + # @since 0.2.0 + # + # @see https://github.com/python/cpython/blob/v2.7/Lib/pickle.py#L141 + SETITEMS = 117 + + # The `EMPTY_DICT` opcode. + # + # @since 0.2.0 + # + # @see https://github.com/python/cpython/blob/v2.7/Lib/pickle.py#L126 + EMPTY_DICT = 125 + + # # Reads an instruction from the pickle stream. # # @return [Instruction] # The decoded instruction. # @@ -62,90 +136,51 @@ def read_instruction case (opcode = @io.getbyte) # # Protocol 0 instructions # - when 40 # MARK - Instructions::MARK - when 46 # STOP - Instructions::STOP - when 48 # POP - Instructions::POP - when 49 # POP_MARK - Instructions::POP_MARK - when 50 # DUP - Instructions::DUP - when 70 # FLOAT - Instructions::Float.new(read_float) - when 73 # INT - Instructions::Int.new(read_int) - when 76 # LONG - Instructions::Long.new(read_long) - when 78 # NONE - Instructions::NONE - when 82 # REDUCE - Instructions::REDUCE - when 83 # STRING - Instructions::String.new(read_string) - when 86 # UNICODE - Instructions::String.new(read_unicode_string) - when 97 # APPEND - Instructions::APPEND - when 98 # BUILD - Instructions::BUILD - when 99 # GLOBAL - Instructions::Global.new(read_nl_string,read_nl_string) - when 100 # DICT - Instructions::DICT - when 103 # GET - Instructions::Get.new(read_int) - when 108 # LIST - Instructions::LIST - when 112 # PUT - Instructions::Put.new(read_int) - when 115 # SETITEM - Instructions::SETITEM - when 116 # TUPLE - Instructions::TUPLE + when MARK then Instructions::MARK + when STOP then Instructions::STOP + when POP then Instructions::POP + when POP_MARK then Instructions::POP_MARK + when DUP then Instructions::DUP + when FLOAT then read_float_instruction + when INT then read_int_instruction + when LONG then read_long_instruction + when NONE then Instructions::NONE + when REDUCE then Instructions::REDUCE + when STRING then read_string_instruction + when UNICODE then read_unicode_instruction + when APPEND then Instructions::APPEND + when BUILD then Instructions::BUILD + when GLOBAL then read_global_instruction + when DICT then Instructions::DICT + when GET then read_get_instruction + when LIST then Instructions::LIST + when PUT then read_put_instruction + when SETITEM then Instructions::SETITEM + when TUPLE then Instructions::TUPLE + when INST then read_inst_instruction + when OBJ then Instructions::OBJ + when PERSID then read_persid_instruction + when BINPERSID then Instructions::BINPERSID # # Protocol 1 instructions # - when 41 # EMPTY_TUPLE - Instructions::EMPTY_TUPLE - when 71 # BINFLOAT - Instructions::BinFloat.new(read_float64_be) - when 75 # BININT1 - Instructions::BinInt1.new(read_uint8) - when 84 # BINSTRING - length = read_uint32_le - string = @io.read(length) - - Instructions::BinString.new(length,string) - when 85 # SHORT_BINSTRING - length = read_uint8 - string = @io.read(length) - - Instructions::ShortBinString.new(length,string) - when 88 # BINUNICODE - length = read_uint32_le - string = @io.read(length).force_encoding(Encoding::UTF_8) - - Instructions::BinUnicode.new(length,string) - when 93 # EMPTY_LIST - Instructions::EMPTY_LIST - when 101 # APPENDS - Instructions::APPENDS - when 104 # BINGET - Instructions::BinGet.new(read_uint8) - when 106 # LONG_BINGET - Instructions::LongBinGet.new(read_uint32_le) - when 113 # BINPUT - Instructions::BinPut.new(read_uint8) - when 117 # SETITEMS - Instructions::SETITEMS - when 125 # EMPTY_DICT - Instructions::EMPTY_DICT + when EMPTY_TUPLE then Instructions::EMPTY_TUPLE + when BINFLOAT then read_binfloat_instruction + when BININT1 then read_binint1_instruction + when BINSTRING then read_binstring_instruction + when SHORT_BINSTRING then read_short_binstring_instruction + when BINUNICODE then read_binunicode_instruction + when EMPTY_LIST then Instructions::EMPTY_LIST + when APPENDS then Instructions::APPENDS + when BINGET then read_binget_instruction + when LONG_BINGET then read_long_binget_instruction + when BINPUT then read_binput_instruction + when SETITEMS then Instructions::SETITEMS + when EMPTY_DICT then Instructions::EMPTY_DICT else raise(InvalidFormat,"invalid opcode (#{opcode.inspect}) for protocol 1") end end @@ -174,9 +209,106 @@ # # @return [Integer] # def read_uint32_le @io.read(4).unpack1('L<') + end + + # + # Reads a `BINFLOAT` instruction. + # + # @return [Instructions::BinFloat] + # + # @since 0.2.0 + # + def read_binfloat_instruction + Instructions::BinFloat.new(read_float64_be) + end + + # + # Reads a `BININT1` instruction. + # + # @return [Instructions::BinInt1] + # + # @since 0.2.0 + # + def read_binint1_instruction + Instructions::BinInt1.new(read_uint8) + end + + # + # Reads a `BINSTRING` instruction. + # + # @return [Instructions::BinString] + # + # @since 0.2.0 + # + def read_binstring_instruction + length = read_uint32_le + string = @io.read(length) + + Instructions::BinString.new(length,string) + end + + # + # Reads a `SHORT_BINSTRING` instruction. + # + # @return [Instructions::ShortBinString] + # + # @since 0.2.0 + # + def read_short_binstring_instruction + length = read_uint8 + string = @io.read(length) + + Instructions::ShortBinString.new(length,string) + end + + # + # Reads a `BINUNICODE` instruction. + # + # @return [Instructions::BinUnicode] + # + # @since 0.2.0 + # + def read_binunicode_instruction + length = read_uint32_le + string = @io.read(length).force_encoding(Encoding::UTF_8) + + Instructions::BinUnicode.new(length,string) + end + + # + # Reads a `BINGET` instruction. + # + # @return [Instructions::BinGet] + # + # @since 0.2.0 + # + def read_binget_instruction + Instructions::BinGet.new(read_uint8) + end + + # + # Reads a `LONG_BINGET` instruction. + # + # @return [Instructions::LongBinGet] + # + # @since 0.2.0 + # + def read_long_binget_instruction + Instructions::LongBinGet.new(read_uint32_le) + end + + # + # Reads a `BINPUT` instruction. + # + # @return [Instructions::BinPut] + # + # @since 0.2.0 + # + def read_binput_instruction + Instructions::BinPut.new(read_uint8) end end end end