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

- old
+ new

@@ -11,15 +11,19 @@ require 'python/pickle/instructions/tuple' require 'python/pickle/instructions/list' require 'python/pickle/instructions/none' require 'python/pickle/instructions/append' require 'python/pickle/instructions/global' +require 'python/pickle/instructions/obj' +require 'python/pickle/instructions/inst' require 'python/pickle/instructions/reduce' require 'python/pickle/instructions/build' require 'python/pickle/instructions/pop' require 'python/pickle/instructions/pop_mark' require 'python/pickle/instructions/dup' +require 'python/pickle/instructions/persid' +require 'python/pickle/instructions/bin_persid' require 'python/pickle/instructions/stop' require 'python/pickle/exceptions' require 'set' @@ -30,90 +34,221 @@ # # @api private # class Protocol0 < Protocol - # Opcodes for Pickle protocol version 0. + # The `MARK` opcode. # - # @see https://github.com/python/cpython/blob/main/Lib/pickletools.py - OPCODES = Set[ - 40, # MARK - 46, # STOP - 48, # POP - 49, # POP_MARK - 50, # DUP - 70, # FLOAT - 73, # INT - 76, # LONG - 78, # NONE - 82, # REDUCE - 83, # STRING - 86, # UNICODE - 97, # APPEND - 98, # BUILD - 99, # GLOBAL - 100, # DICT - 103, # GET - 108, # LIST - 112, # PUT - 115, # SETITEM - 116 # TUPLE - ] + # @since 0.2.0 + # + # @see https://github.com/python/cpython/blob/v2.7/Lib/pickle.py#L102 + MARK = 40 + # The `STOP` opcode. # + # @since 0.2.0 + # + # @see https://github.com/python/cpython/blob/v2.7/Lib/pickle.py#L103 + STOP = 46 + + # The `POP` opcode. + # + # @since 0.2.0 + # + # @see https://github.com/python/cpython/blob/v2.7/Lib/pickle.py#L104 + POP = 48 + + # The `POP_MARK` opcode. + # + # @since 0.2.0 + # + # @see https://github.com/python/cpython/blob/v2.7/Lib/pickle.py#L105 + POP_MARK = 49 + + # The `DUP` opcode. + # + # @since 0.2.0 + # + # @see https://github.com/python/cpython/blob/v2.7/Lib/pickle.py#L106 + DUP = 50 + + # The `FLOAT` opcode. + # + # @since 0.2.0 + # + # @see https://github.com/python/cpython/blob/v2.7/Lib/pickle.py#L107 + FLOAT = 70 + + # The `INT` opcode. + # + # @since 0.2.0 + # + # @see https://github.com/python/cpython/blob/v2.7/Lib/pickle.py#L108 + INT = 73 + + # The `LONG` opcode. + # + # @since 0.2.0 + # + # @see https://github.com/python/cpython/blob/v2.7/Lib/pickle.py#L111 + LONG = 76 + + # The `NONE` opcode. + # + # @since 0.2.0 + # + # @see https://github.com/python/cpython/blob/v2.7/Lib/pickle.py#L113 + NONE = 78 + + # The `PERSID` opcode. + # + # @since 0.2.0 + # + # @see https://github.com/python/cpython/blob/v2.7/Lib/pickle.py#L114 + PERSID = 80 + + # The `BINPERSID` opcode. + # + # @since 0.2.0 + # + # @see https://github.com/python/cpython/blob/v2.7/Lib/pickle.py#L115 + BINPERSID = 81 + + # The `REDUCE` opcode. + # + # @since 0.2.0 + # + # @see https://github.com/python/cpython/blob/v2.7/Lib/pickle.py#L116 + REDUCE = 82 + + # The `STRING` opcode. + # + # @since 0.2.0 + # + # @see https://github.com/python/cpython/blob/v2.7/Lib/pickle.py#L117 + STRING = 83 + + # The `UNICODE` opcode. + # + # @since 0.2.0 + # + # @see https://github.com/python/cpython/blob/v2.7/Lib/pickle.py#L120 + UNICODE = 86 + + # The `APPEND` opcode. + # + # @since 0.2.0 + # + # @see https://github.com/python/cpython/blob/v2.7/Lib/pickle.py#L122 + APPEND = 97 + + # The `BUILD` opcode. + # + # @since 0.2.0 + # + # @see https://github.com/python/cpython/blob/v2.7/Lib/pickle.py#L123 + BUILD = 98 + + # The `GLOBAL` opcode. + # + # @since 0.2.0 + # + # @see https://github.com/python/cpython/blob/v2.7/Lib/pickle.py#L124 + GLOBAL = 99 + + # The `DICT` opcode. + # + # @since 0.2.0 + # + # @see https://github.com/python/cpython/blob/v2.7/Lib/pickle.py#L125 + DICT = 100 + + # The `GET` opcode. + # + # @since 0.2.0 + # + # @see https://github.com/python/cpython/blob/v2.7/Lib/pickle.py#L128 + GET = 103 + + # The `INST` opcode. + # + # @since 0.2.0 + # + # @see https://github.com/python/cpython/blob/v2.7/Lib/pickle.py#L130 + INST = 105 + + # The `LIST` opcode. + # + # @since 0.2.0 + # + # @see https://github.com/python/cpython/blob/v2.7/Lib/pickle.py#L132 + LIST = 108 + + # The `OBJ` opcode. + # + # @since 0.2.0 + # + # @see https://github.com/python/cpython/blob/v2.7/Lib/pickle.py#L137 + OBJ = 111 + + # The `PUT` opcode. + # + # @since 0.2.0 + # + # @see https://github.com/python/cpython/blob/v2.7/Lib/pickle.py#L135 + PUT = 112 + + # The `SETITEM` opcode. + # + # @since 0.2.0 + # + # @see https://github.com/python/cpython/blob/v2.7/Lib/pickle.py#L138 + SETITEM = 115 + + # The `TUPLE` opcode. + # + # @since 0.2.0 + # + # @see https://github.com/python/cpython/blob/v2.7/Lib/pickle.py#L139 + TUPLE = 116 + + # # Reads an instruction from the pickle stream. # # @return [Instruction] # The decoded instruction. # # @raise [InvalidFormat] # The pickle stream could not be parsed. # def read_instruction case (opcode = @io.getbyte) - 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 else raise(InvalidFormat,"invalid opcode (#{opcode.inspect}) for protocol 0") end end @@ -390,9 +525,119 @@ raise(InvalidFormat,"encountered a non-numeric character while reading a long integer: #{char.inspect}") end end raise(InvalidFormat,"unexpected end of stream while parsing a long integer: #{new_string.inspect}") + end + + # + # Reads a `FLOAT` instruction. + # + # @return [Instructions::Float] + # + # @since 0.2.0 + # + def read_float_instruction + Instructions::Float.new(read_float) + end + + # + # Reads a `INT` instruction. + # + # @return [Instructions::Int] + # + # @since 0.2.0 + # + def read_int_instruction + Instructions::Int.new(read_int) + end + + # + # Reads a `LONG` instruction. + # + # @return [Instructions::Long] + # + # @since 0.2.0 + # + def read_long_instruction + Instructions::Long.new(read_long) + end + + # + # Reads a `STRING` instruction. + # + # @return [Instructions::String] + # + # @since 0.2.0 + # + def read_string_instruction + Instructions::String.new(read_string) + end + + # + # Reads a `UNICODE` instruction. + # + # @return [Instructions::Unicode] + # + # @since 0.2.0 + # + def read_unicode_instruction + Instructions::String.new(read_unicode_string) + end + + # + # Reads a `GLOBAL` instruction. + # + # @return [Instructions::Global] + # + # @since 0.2.0 + # + def read_global_instruction + Instructions::Global.new(read_nl_string,read_nl_string) + end + + # + # Reads a `INST` instruction. + # + # @return [Instructions::Inst] + # + # @since 0.2.0 + # + def read_inst_instruction + Instructions::Inst.new(read_nl_string,read_nl_string) + end + + # + # Reads a `GET` instruction. + # + # @return [Instructions::Get] + # + # @since 0.2.0 + # + def read_get_instruction + Instructions::Get.new(read_int) + end + + # + # Reads a `PUT` instruction. + # + # @return [Instructions::Put] + # + # @since 0.2.0 + # + def read_put_instruction + Instructions::Put.new(read_int) + end + + # + # Reads a `PERSID` instruction. + # + # @return [Instructions::PersID] + # + # @since 0.2.0 + # + def read_persid_instruction + Instructions::PersID.new(read_nl_string) end end end end