Class: Python::Pickle::Protocol2

Inherits:
Protocol1 show all
Defined in:
lib/python/pickle/protocol2.rb

Direct Known Subclasses

Protocol3

Constant Summary collapse

PROTO =

The PROTO opcode.

128
NEWOBJ =

The NEWOBJ opcode.

129
EXT1 =

The EXT1 opcode.

130
EXT2 =

The EXT2 opcode.

131
EXT4 =

The EXT4 opcode.

132
TUPLE1 =

The TUPLE1 opcode.

133
TUPLE2 =

The TUPLE2 opcode.

134
TUPLE3 =

The TUPLE3 opcode.

135
NEWTRUE =

The NEWTRUE opcode.

136
NEWFALSE =

The NEWFALSE opcode.

137
LONG1 =

The LONG1 opcode.

138
LONG4 =

The LONG4 opcode.

139

Constants inherited from Protocol1

Python::Pickle::Protocol1::APPENDS, Python::Pickle::Protocol1::BINFLOAT, Python::Pickle::Protocol1::BINGET, Python::Pickle::Protocol1::BININT1, Python::Pickle::Protocol1::BINPUT, Python::Pickle::Protocol1::BINSTRING, Python::Pickle::Protocol1::BINUNICODE, Python::Pickle::Protocol1::EMPTY_DICT, Python::Pickle::Protocol1::EMPTY_LIST, Python::Pickle::Protocol1::EMPTY_TUPLE, Python::Pickle::Protocol1::LONG_BINGET, Python::Pickle::Protocol1::SETITEMS, Python::Pickle::Protocol1::SHORT_BINSTRING

Constants inherited from Protocol0

Python::Pickle::Protocol0::APPEND, Python::Pickle::Protocol0::BINPERSID, Python::Pickle::Protocol0::BUILD, Python::Pickle::Protocol0::DICT, Python::Pickle::Protocol0::DUP, Python::Pickle::Protocol0::FLOAT, Python::Pickle::Protocol0::GET, Python::Pickle::Protocol0::GLOBAL, Python::Pickle::Protocol0::INST, Python::Pickle::Protocol0::INT, Python::Pickle::Protocol0::LIST, Python::Pickle::Protocol0::LONG, Python::Pickle::Protocol0::MARK, Python::Pickle::Protocol0::NONE, Python::Pickle::Protocol0::OBJ, Python::Pickle::Protocol0::PERSID, Python::Pickle::Protocol0::POP, Python::Pickle::Protocol0::POP_MARK, Python::Pickle::Protocol0::PUT, Python::Pickle::Protocol0::REDUCE, Python::Pickle::Protocol0::SETITEM, Python::Pickle::Protocol0::STOP, Python::Pickle::Protocol0::STRING, Python::Pickle::Protocol0::TUPLE, Python::Pickle::Protocol0::UNICODE

Instance Attribute Summary

Attributes inherited from Protocol

#io

Instance Method Summary collapse

Methods inherited from Protocol1

#read_binfloat_instruction, #read_binget_instruction, #read_binint1_instruction, #read_binput_instruction, #read_binstring_instruction, #read_binunicode_instruction, #read_float64_be, #read_long_binget_instruction, #read_short_binstring_instruction, #read_uint32_le, #read_uint8

Methods inherited from Protocol0

#read_escaped_char, #read_float, #read_float_instruction, #read_get_instruction, #read_global_instruction, #read_hex_escaped_char, #read_inst_instruction, #read_int, #read_int_instruction, #read_long, #read_long_instruction, #read_nl_string, #read_persid_instruction, #read_put_instruction, #read_string, #read_string_instruction, #read_unicode_escaped_char, #read_unicode_escaped_char16, #read_unicode_escaped_char32, #read_unicode_instruction, #read_unicode_string

Methods inherited from Protocol

#initialize, #read

Constructor Details

This class inherits a constructor from Python::Pickle::Protocol

Instance Method Details

#read_ext1_instructionInstructions::Ext1

Reads a EXT1 instruction.

Returns:

Since:

  • 0.2.0



254
255
256
# File 'lib/python/pickle/protocol2.rb', line 254

def read_ext1_instruction
  Instructions::Ext1.new(read_uint8)
end

#read_ext2_instructionInstructions::Ext2

Reads a EXT2 instruction.

Returns:

Since:

  • 0.2.0



265
266
267
# File 'lib/python/pickle/protocol2.rb', line 265

def read_ext2_instruction
  Instructions::Ext2.new(read_uint16_le)
end

#read_ext4_instructionInstructions::Ext4

Reads a EXT4 instruction.

Returns:

Since:

  • 0.2.0



276
277
278
# File 'lib/python/pickle/protocol2.rb', line 276

def read_ext4_instruction
  Instructions::Ext4.new(read_uint32_le)
end

#read_instructionInstruction

Reads an instruction from the pickle stream.

Returns:

Raises:



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/python/pickle/protocol2.rb', line 112

def read_instruction
  case (opcode = @io.getbyte)
  #
  # Protocol 0 instructions
  #
  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 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
  #
  # Protocol 2 instructions
  #
  when PROTO    then read_proto_instruction
  when NEWOBJ   then Instructions::NEWOBJ
  when EXT1     then read_ext1_instruction
  when EXT2     then read_ext2_instruction
  when EXT4     then read_ext4_instruction
  when TUPLE1   then Instructions::TUPLE1
  when TUPLE2   then Instructions::TUPLE2
  when TUPLE3   then Instructions::TUPLE3
  when NEWTRUE  then Instructions::NEWTRUE
  when NEWFALSE then Instructions::NEWFALSE
  when LONG1    then read_long1_instruction
  when LONG4    then read_long4_instruction
  else
    raise(InvalidFormat,"invalid opcode (#{opcode.inspect}) for protocol 2")
  end
end

#read_int_le(length) ⇒ Integer

Reads and unpacks a signed integer of arbitrary length.

Parameters:

  • length (Integer)

    The number of bytes to read.

Returns:

  • (Integer)

    The decoded long integer.



197
198
199
200
201
202
203
204
205
# File 'lib/python/pickle/protocol2.rb', line 197

def read_int_le(length)
  data = @io.read(length)

  if data.bytesize < length
    raise(InvalidFormat,"premature end of string")
  end

  return unpack_int_le(data)
end

#read_long1_instructionInstructions::Long1

Reads a LONG1 instruction.

Returns:

Since:

  • 0.2.0



287
288
289
290
291
292
# File 'lib/python/pickle/protocol2.rb', line 287

def read_long1_instruction
  length = read_uint8
  long   = read_int_le(length)

  Instructions::Long1.new(length,long)
end

#read_long4_instructionInstructions::Long4

Reads a LONG4 instruction.

Returns:

Since:

  • 0.2.0



301
302
303
304
305
306
# File 'lib/python/pickle/protocol2.rb', line 301

def read_long4_instruction
  length = read_uint32_le
  long   = read_int_le(length)

  Instructions::Long4.new(length,long)
end

#read_proto_instructionInstructions::Proto

Reads a PROTO instruction.

Returns:

Since:

  • 0.2.0



243
244
245
# File 'lib/python/pickle/protocol2.rb', line 243

def read_proto_instruction
  Instructions::Proto.new(read_uint8)
end

#read_uint16_leInteger

Reads an unsigned 16bit integer in little-endian byte-order.

Returns:

  • (Integer)

    The decoded integer.



184
185
186
# File 'lib/python/pickle/protocol2.rb', line 184

def read_uint16_le
  @io.read(2).unpack1('S<')
end

#unpack_int_le(data) ⇒ Integer

Decodes a packed twos-complement long value of arbitrary length.

Parameters:

  • data (String)

    The packed long to decode.

Returns:

  • (Integer)

    The unpacked long.



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/python/pickle/protocol2.rb', line 216

def unpack_int_le(data)
  return 0 if data.empty?

  long  = 0
  shift = 0

  data.each_byte do |b|
    long |= b << shift
    shift += 8
  end

  max_signed = (1 << (shift-1))

  if long >= max_signed
    long -= (1 << shift)
  end

  return long
end