Class: Python::Pickle::Protocol0 Private

Inherits:
Protocol
  • Object
show all
Defined in:
lib/python/pickle/protocol0.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Implements reading and writing of Python Pickle protocol 0.

Direct Known Subclasses

Protocol1

Constant Summary collapse

MARK =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The MARK opcode.

40
STOP =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The STOP opcode.

46
POP =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The POP opcode.

48
POP_MARK =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The POP_MARK opcode.

49
DUP =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The DUP opcode.

50
FLOAT =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The FLOAT opcode.

70
INT =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The INT opcode.

73
LONG =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The LONG opcode.

76
NONE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The NONE opcode.

78
PERSID =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The PERSID opcode.

80
BINPERSID =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The BINPERSID opcode.

81
REDUCE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The REDUCE opcode.

82
STRING =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The STRING opcode.

83
UNICODE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The UNICODE opcode.

86
APPEND =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The APPEND opcode.

97
BUILD =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The BUILD opcode.

98
GLOBAL =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The GLOBAL opcode.

99
DICT =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The DICT opcode.

100
GET =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The GET opcode.

103
INST =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The INST opcode.

105
LIST =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The LIST opcode.

108
OBJ =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The OBJ opcode.

111
PUT =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The PUT opcode.

112
SETITEM =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The SETITEM opcode.

115
TUPLE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The TUPLE opcode.

116

Instance Attribute Summary

Attributes inherited from Protocol

#io

Instance Method Summary collapse

Methods inherited from Protocol

#initialize, #read

Constructor Details

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

Instance Method Details

#read_escaped_charString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Reads an escaped character from the pickle stream.

Returns:

  • (String)

    The unescaped raw character.



282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/python/pickle/protocol0.rb', line 282

def read_escaped_char
  case (letter = @io.getc)
  when 'x'  then  read_hex_escaped_char
  when 't'  then "\t"
  when 'n'  then "\n"
  when 'r'  then "\r"
  when '\\' then '\\'
  when "'"  then "'"
  else
    bad_escape = letter.inspect[1..-2]

    raise(InvalidFormat,"invalid backslash escape character: \"\\#{bad_escape}\"")
  end
end

#read_floatFloat

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Reads a floating-point decimal from the pickle stream.

Returns:

  • (Float)

    The decoded float.

Raises:

  • (InvalidFormat)

    Encountered a non-numeric character or a premature end of the stream.



449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# File 'lib/python/pickle/protocol0.rb', line 449

def read_float
  new_string = String.new

  until @io.eof?
    case (char = @io.getc)
    when /[0-9\.]/
      new_string << char
    when "\n" # end-of-float
      return new_string.to_f
    else
      raise(InvalidFormat,"encountered a non-numeric character while reading a float: #{char.inspect}")
    end
  end

  raise(InvalidFormat,"unexpected end of stream while parsing a float: #{new_string.inspect}")
end

#read_float_instructionInstructions::Float

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Reads a FLOAT instruction.

Returns:

Since:

  • 0.2.0



539
540
541
# File 'lib/python/pickle/protocol0.rb', line 539

def read_float_instruction
  Instructions::Float.new(read_float)
end

#read_get_instructionInstructions::Get

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Reads a GET instruction.

Returns:

Since:

  • 0.2.0



616
617
618
# File 'lib/python/pickle/protocol0.rb', line 616

def read_get_instruction
  Instructions::Get.new(read_int)
end

#read_global_instructionInstructions::Global

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Reads a GLOBAL instruction.

Returns:

Since:

  • 0.2.0



594
595
596
# File 'lib/python/pickle/protocol0.rb', line 594

def read_global_instruction
  Instructions::Global.new(read_nl_string,read_nl_string)
end

#read_hex_escaped_charString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Reads a hex number from the pickle stream.

Parameters:

  • digits (Integer)

    The number of digits to read.

Returns:

  • (String)

    The decoded raw character.



264
265
266
267
268
269
270
271
272
273
274
# File 'lib/python/pickle/protocol0.rb', line 264

def read_hex_escaped_char
  string = @io.read(2)

  unless string =~ /\A[0-9a-fA-F]{2}\z/
    bad_hex = string.inspect[1..-2]

    raise(InvalidFormat,"invalid hex escape character: \"\\x#{bad_hex}\"")
  end

  return string.to_i(16).chr
end

#read_inst_instructionInstructions::Inst

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Reads a INST instruction.

Returns:

Since:

  • 0.2.0



605
606
607
# File 'lib/python/pickle/protocol0.rb', line 605

def read_inst_instruction
  Instructions::Inst.new(read_nl_string,read_nl_string)
end

#read_instructionInstruction

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Reads an instruction from the pickle stream.

Returns:

Raises:



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/python/pickle/protocol0.rb', line 223

def read_instruction
  case (opcode = @io.getbyte)
  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

#read_intInteger, ...

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Reads an integer from the pickle stream.

Returns:

  • (Integer, true, false)

    The decoded Integer. If the integer is 00, then false will be returned. If the integer is 01, then true will be returned.

Raises:

  • (InvalidFormat)

    Encountered a non-numeric character or a premature end of the stream.



477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
# File 'lib/python/pickle/protocol0.rb', line 477

def read_int
  new_string = String.new

  until @io.eof?
    case (char = @io.getc)
    when /[0-9]/
      new_string << char
    when "\n" # end-of-integer
      return case new_string
             when '00' then false
             when '01' then true
             else           new_string.to_i
             end
    else
      raise(InvalidFormat,"encountered a non-numeric character while reading an integer: #{char.inspect}")
    end
  end

  raise(InvalidFormat,"unexpected end of stream while parsing an integer: #{new_string.inspect}")
end

#read_int_instructionInstructions::Int

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Reads a INT instruction.

Returns:

Since:

  • 0.2.0



550
551
552
# File 'lib/python/pickle/protocol0.rb', line 550

def read_int_instruction
  Instructions::Int.new(read_int)
end

#read_longInteger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Reads a long integer.

Returns:

  • (Integer)

    The decoded Integer.

Raises:

  • (InvalidFormat)

    Encountered a non-numeric character or a premature end of the stream.



507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
# File 'lib/python/pickle/protocol0.rb', line 507

def read_long
  new_string = String.new

  until @io.eof?
    case (char = @io.getc)
    when /[0-9]/
      new_string << char
    when 'L'
      newline = @io.getc

      if newline == nil
        raise(InvalidFormat,"unexpected end of stream after the end of an integer")
      elsif newline != "\n"
        raise(InvalidFormat,"expected a '\\n' character following the integer, but was #{newline.inspect}")
      end

      return new_string.to_i
    else
      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

#read_long_instructionInstructions::Long

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Reads a LONG instruction.

Returns:

Since:

  • 0.2.0



561
562
563
# File 'lib/python/pickle/protocol0.rb', line 561

def read_long_instruction
  Instructions::Long.new(read_long)
end

#read_nl_stringString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Reads a newline terminated string from the pickle string.

Returns:

  • (String)

    The read string.

Raises:



306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/python/pickle/protocol0.rb', line 306

def read_nl_string
  new_string = String.new

  until @io.eof?
    case (char = @io.getc)
    when "\n"
      return new_string
    else
      new_string << char
    end
  end

  raise(InvalidFormat,"unexpected end of stream after the end of a newline terminated string")
end

#read_persid_instructionInstructions::PersID

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Reads a PERSID instruction.

Returns:

Since:

  • 0.2.0



638
639
640
# File 'lib/python/pickle/protocol0.rb', line 638

def read_persid_instruction
  Instructions::PersID.new(read_nl_string)
end

#read_put_instructionInstructions::Put

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Reads a PUT instruction.

Returns:

Since:

  • 0.2.0



627
628
629
# File 'lib/python/pickle/protocol0.rb', line 627

def read_put_instruction
  Instructions::Put.new(read_int)
end

#read_stringString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Reads an ASCII string from the pickle stream.

Returns:

  • (String)

    The decoded raw string.



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/python/pickle/protocol0.rb', line 327

def read_string
  new_string = String.new(encoding: Encoding::ASCII_8BIT)

  unless @io.getc == "'"
    raise(InvalidFormat,"cannot find beginning single-quote of string")
  end

  until @io.eof?
    case (char = @io.getc)
    when "\\"
      new_string << read_escaped_char
    when "'" # end-of-string
      break
    else
      new_string << char
    end
  end

  newline = @io.getc

  if newline == nil
    raise(InvalidFormat,"unexpected end of stream after the end of a single-quoted string")
  elsif newline != "\n"
    raise(InvalidFormat,"expected a '\\n' character following the string, but was #{newline.inspect}")
  end

  return new_string
end

#read_string_instructionInstructions::String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Reads a STRING instruction.

Returns:

Since:

  • 0.2.0



572
573
574
# File 'lib/python/pickle/protocol0.rb', line 572

def read_string_instruction
  Instructions::String.new(read_string)
end

#read_unicode_escaped_charString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Reads a unicode escaped character from the pickle stream.

Returns:

  • (String)

    The unescaped raw unicode character.



404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/python/pickle/protocol0.rb', line 404

def read_unicode_escaped_char
  case (letter = @io.getc)
  when 'x'  then read_hex_escaped_char
  when 'u'  then read_unicode_escaped_char16
  when 'U'  then read_unicode_escaped_char32
  when "\\" then "\\"
  else
    bad_escape = letter.inspect[1..-2]

    raise(InvalidFormat,"invalid unicode escape character: \"\\#{bad_escape}\"")
  end
end

#read_unicode_escaped_char16String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Reads a short unicode escaped character.

Returns:

  • (String)

    The decoded UTF-8 character.

Raises:



365
366
367
368
369
370
371
372
373
374
375
# File 'lib/python/pickle/protocol0.rb', line 365

def read_unicode_escaped_char16
  string = @io.read(4)

  unless string =~ /\A[0-9a-fA-F]{4}\z/
    bad_unicode = string.inspect[1..-2]

    raise(InvalidFormat,"invalid unicode escape character: \"\\u#{bad_unicode}\"")
  end

  return string.to_i(16).chr(Encoding::UTF_8)
end

#read_unicode_escaped_char32String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Reads a long unicode escaped character.

Returns:

  • (String)

    The decoded UTF-8 character.

Raises:



386
387
388
389
390
391
392
393
394
395
396
# File 'lib/python/pickle/protocol0.rb', line 386

def read_unicode_escaped_char32
  string = @io.read(8)

  unless string =~ /\A[0-9a-fA-F]{8}\z/
    bad_unicode = string.inspect[1..-2]

    raise(InvalidFormat,"invalid unicode escape character: \"\\U#{bad_unicode}\"")
  end

  return string.to_i(16).chr(Encoding::UTF_8)
end

#read_unicode_instructionInstructions::Unicode

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Reads a UNICODE instruction.

Returns:

  • (Instructions::Unicode)

Since:

  • 0.2.0



583
584
585
# File 'lib/python/pickle/protocol0.rb', line 583

def read_unicode_instruction
  Instructions::String.new(read_unicode_string)
end

#read_unicode_stringString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Reads a unicode String from the pickle stream.

Returns:

  • (String)

    The decoded raw unicode String.

Raises:



423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# File 'lib/python/pickle/protocol0.rb', line 423

def read_unicode_string
  new_string = String.new(encoding: Encoding::UTF_8)

  until @io.eof?
    case (char = @io.getc)
    when "\\" # backslash escaped character
      new_string << read_unicode_escaped_char
    when "\n" # end-of-string
      return new_string
    else
      new_string << char
    end
  end

  raise(InvalidFormat,"unexpected end of stream while parsing unicode string: #{new_string.inspect}")
end