Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/smexe_ro.../lib64/python2....
File: pickle.py
"""Create portable serialized representations of Python objects.
[0] Fix | Delete
[1] Fix | Delete
See module cPickle for a (much) faster implementation.
[2] Fix | Delete
See module copy_reg for a mechanism for registering custom picklers.
[3] Fix | Delete
See module pickletools source for extensive comments.
[4] Fix | Delete
[5] Fix | Delete
Classes:
[6] Fix | Delete
[7] Fix | Delete
Pickler
[8] Fix | Delete
Unpickler
[9] Fix | Delete
[10] Fix | Delete
Functions:
[11] Fix | Delete
[12] Fix | Delete
dump(object, file)
[13] Fix | Delete
dumps(object) -> string
[14] Fix | Delete
load(file) -> object
[15] Fix | Delete
loads(string) -> object
[16] Fix | Delete
[17] Fix | Delete
Misc variables:
[18] Fix | Delete
[19] Fix | Delete
__version__
[20] Fix | Delete
format_version
[21] Fix | Delete
compatible_formats
[22] Fix | Delete
[23] Fix | Delete
"""
[24] Fix | Delete
[25] Fix | Delete
__version__ = "$Revision: 72223 $" # Code version
[26] Fix | Delete
[27] Fix | Delete
from types import *
[28] Fix | Delete
from copy_reg import dispatch_table
[29] Fix | Delete
from copy_reg import _extension_registry, _inverted_registry, _extension_cache
[30] Fix | Delete
import marshal
[31] Fix | Delete
import sys
[32] Fix | Delete
import struct
[33] Fix | Delete
import re
[34] Fix | Delete
[35] Fix | Delete
__all__ = ["PickleError", "PicklingError", "UnpicklingError", "Pickler",
[36] Fix | Delete
"Unpickler", "dump", "dumps", "load", "loads"]
[37] Fix | Delete
[38] Fix | Delete
# These are purely informational; no code uses these.
[39] Fix | Delete
format_version = "2.0" # File format version we write
[40] Fix | Delete
compatible_formats = ["1.0", # Original protocol 0
[41] Fix | Delete
"1.1", # Protocol 0 with INST added
[42] Fix | Delete
"1.2", # Original protocol 1
[43] Fix | Delete
"1.3", # Protocol 1 with BINFLOAT added
[44] Fix | Delete
"2.0", # Protocol 2
[45] Fix | Delete
] # Old format versions we can read
[46] Fix | Delete
[47] Fix | Delete
# Keep in synch with cPickle. This is the highest protocol number we
[48] Fix | Delete
# know how to read.
[49] Fix | Delete
HIGHEST_PROTOCOL = 2
[50] Fix | Delete
[51] Fix | Delete
# Why use struct.pack() for pickling but marshal.loads() for
[52] Fix | Delete
# unpickling? struct.pack() is 40% faster than marshal.dumps(), but
[53] Fix | Delete
# marshal.loads() is twice as fast as struct.unpack()!
[54] Fix | Delete
mloads = marshal.loads
[55] Fix | Delete
[56] Fix | Delete
class PickleError(Exception):
[57] Fix | Delete
"""A common base class for the other pickling exceptions."""
[58] Fix | Delete
pass
[59] Fix | Delete
[60] Fix | Delete
class PicklingError(PickleError):
[61] Fix | Delete
"""This exception is raised when an unpicklable object is passed to the
[62] Fix | Delete
dump() method.
[63] Fix | Delete
[64] Fix | Delete
"""
[65] Fix | Delete
pass
[66] Fix | Delete
[67] Fix | Delete
class UnpicklingError(PickleError):
[68] Fix | Delete
"""This exception is raised when there is a problem unpickling an object,
[69] Fix | Delete
such as a security violation.
[70] Fix | Delete
[71] Fix | Delete
Note that other exceptions may also be raised during unpickling, including
[72] Fix | Delete
(but not necessarily limited to) AttributeError, EOFError, ImportError,
[73] Fix | Delete
and IndexError.
[74] Fix | Delete
[75] Fix | Delete
"""
[76] Fix | Delete
pass
[77] Fix | Delete
[78] Fix | Delete
# An instance of _Stop is raised by Unpickler.load_stop() in response to
[79] Fix | Delete
# the STOP opcode, passing the object that is the result of unpickling.
[80] Fix | Delete
class _Stop(Exception):
[81] Fix | Delete
def __init__(self, value):
[82] Fix | Delete
self.value = value
[83] Fix | Delete
[84] Fix | Delete
# Jython has PyStringMap; it's a dict subclass with string keys
[85] Fix | Delete
try:
[86] Fix | Delete
from org.python.core import PyStringMap
[87] Fix | Delete
except ImportError:
[88] Fix | Delete
PyStringMap = None
[89] Fix | Delete
[90] Fix | Delete
# UnicodeType may or may not be exported (normally imported from types)
[91] Fix | Delete
try:
[92] Fix | Delete
UnicodeType
[93] Fix | Delete
except NameError:
[94] Fix | Delete
UnicodeType = None
[95] Fix | Delete
[96] Fix | Delete
# Pickle opcodes. See pickletools.py for extensive docs. The listing
[97] Fix | Delete
# here is in kind-of alphabetical order of 1-character pickle code.
[98] Fix | Delete
# pickletools groups them by purpose.
[99] Fix | Delete
[100] Fix | Delete
MARK = '(' # push special markobject on stack
[101] Fix | Delete
STOP = '.' # every pickle ends with STOP
[102] Fix | Delete
POP = '0' # discard topmost stack item
[103] Fix | Delete
POP_MARK = '1' # discard stack top through topmost markobject
[104] Fix | Delete
DUP = '2' # duplicate top stack item
[105] Fix | Delete
FLOAT = 'F' # push float object; decimal string argument
[106] Fix | Delete
INT = 'I' # push integer or bool; decimal string argument
[107] Fix | Delete
BININT = 'J' # push four-byte signed int
[108] Fix | Delete
BININT1 = 'K' # push 1-byte unsigned int
[109] Fix | Delete
LONG = 'L' # push long; decimal string argument
[110] Fix | Delete
BININT2 = 'M' # push 2-byte unsigned int
[111] Fix | Delete
NONE = 'N' # push None
[112] Fix | Delete
PERSID = 'P' # push persistent object; id is taken from string arg
[113] Fix | Delete
BINPERSID = 'Q' # " " " ; " " " " stack
[114] Fix | Delete
REDUCE = 'R' # apply callable to argtuple, both on stack
[115] Fix | Delete
STRING = 'S' # push string; NL-terminated string argument
[116] Fix | Delete
BINSTRING = 'T' # push string; counted binary string argument
[117] Fix | Delete
SHORT_BINSTRING = 'U' # " " ; " " " " < 256 bytes
[118] Fix | Delete
UNICODE = 'V' # push Unicode string; raw-unicode-escaped'd argument
[119] Fix | Delete
BINUNICODE = 'X' # " " " ; counted UTF-8 string argument
[120] Fix | Delete
APPEND = 'a' # append stack top to list below it
[121] Fix | Delete
BUILD = 'b' # call __setstate__ or __dict__.update()
[122] Fix | Delete
GLOBAL = 'c' # push self.find_class(modname, name); 2 string args
[123] Fix | Delete
DICT = 'd' # build a dict from stack items
[124] Fix | Delete
EMPTY_DICT = '}' # push empty dict
[125] Fix | Delete
APPENDS = 'e' # extend list on stack by topmost stack slice
[126] Fix | Delete
GET = 'g' # push item from memo on stack; index is string arg
[127] Fix | Delete
BINGET = 'h' # " " " " " " ; " " 1-byte arg
[128] Fix | Delete
INST = 'i' # build & push class instance
[129] Fix | Delete
LONG_BINGET = 'j' # push item from memo on stack; index is 4-byte arg
[130] Fix | Delete
LIST = 'l' # build list from topmost stack items
[131] Fix | Delete
EMPTY_LIST = ']' # push empty list
[132] Fix | Delete
OBJ = 'o' # build & push class instance
[133] Fix | Delete
PUT = 'p' # store stack top in memo; index is string arg
[134] Fix | Delete
BINPUT = 'q' # " " " " " ; " " 1-byte arg
[135] Fix | Delete
LONG_BINPUT = 'r' # " " " " " ; " " 4-byte arg
[136] Fix | Delete
SETITEM = 's' # add key+value pair to dict
[137] Fix | Delete
TUPLE = 't' # build tuple from topmost stack items
[138] Fix | Delete
EMPTY_TUPLE = ')' # push empty tuple
[139] Fix | Delete
SETITEMS = 'u' # modify dict by adding topmost key+value pairs
[140] Fix | Delete
BINFLOAT = 'G' # push float; arg is 8-byte float encoding
[141] Fix | Delete
[142] Fix | Delete
TRUE = 'I01\n' # not an opcode; see INT docs in pickletools.py
[143] Fix | Delete
FALSE = 'I00\n' # not an opcode; see INT docs in pickletools.py
[144] Fix | Delete
[145] Fix | Delete
# Protocol 2
[146] Fix | Delete
[147] Fix | Delete
PROTO = '\x80' # identify pickle protocol
[148] Fix | Delete
NEWOBJ = '\x81' # build object by applying cls.__new__ to argtuple
[149] Fix | Delete
EXT1 = '\x82' # push object from extension registry; 1-byte index
[150] Fix | Delete
EXT2 = '\x83' # ditto, but 2-byte index
[151] Fix | Delete
EXT4 = '\x84' # ditto, but 4-byte index
[152] Fix | Delete
TUPLE1 = '\x85' # build 1-tuple from stack top
[153] Fix | Delete
TUPLE2 = '\x86' # build 2-tuple from two topmost stack items
[154] Fix | Delete
TUPLE3 = '\x87' # build 3-tuple from three topmost stack items
[155] Fix | Delete
NEWTRUE = '\x88' # push True
[156] Fix | Delete
NEWFALSE = '\x89' # push False
[157] Fix | Delete
LONG1 = '\x8a' # push long from < 256 bytes
[158] Fix | Delete
LONG4 = '\x8b' # push really big long
[159] Fix | Delete
[160] Fix | Delete
_tuplesize2code = [EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3]
[161] Fix | Delete
[162] Fix | Delete
[163] Fix | Delete
__all__.extend([x for x in dir() if re.match("[A-Z][A-Z0-9_]+$",x)])
[164] Fix | Delete
del x
[165] Fix | Delete
[166] Fix | Delete
[167] Fix | Delete
# Pickling machinery
[168] Fix | Delete
[169] Fix | Delete
class Pickler:
[170] Fix | Delete
[171] Fix | Delete
def __init__(self, file, protocol=None):
[172] Fix | Delete
"""This takes a file-like object for writing a pickle data stream.
[173] Fix | Delete
[174] Fix | Delete
The optional protocol argument tells the pickler to use the
[175] Fix | Delete
given protocol; supported protocols are 0, 1, 2. The default
[176] Fix | Delete
protocol is 0, to be backwards compatible. (Protocol 0 is the
[177] Fix | Delete
only protocol that can be written to a file opened in text
[178] Fix | Delete
mode and read back successfully. When using a protocol higher
[179] Fix | Delete
than 0, make sure the file is opened in binary mode, both when
[180] Fix | Delete
pickling and unpickling.)
[181] Fix | Delete
[182] Fix | Delete
Protocol 1 is more efficient than protocol 0; protocol 2 is
[183] Fix | Delete
more efficient than protocol 1.
[184] Fix | Delete
[185] Fix | Delete
Specifying a negative protocol version selects the highest
[186] Fix | Delete
protocol version supported. The higher the protocol used, the
[187] Fix | Delete
more recent the version of Python needed to read the pickle
[188] Fix | Delete
produced.
[189] Fix | Delete
[190] Fix | Delete
The file parameter must have a write() method that accepts a single
[191] Fix | Delete
string argument. It can thus be an open file object, a StringIO
[192] Fix | Delete
object, or any other custom object that meets this interface.
[193] Fix | Delete
[194] Fix | Delete
"""
[195] Fix | Delete
if protocol is None:
[196] Fix | Delete
protocol = 0
[197] Fix | Delete
if protocol < 0:
[198] Fix | Delete
protocol = HIGHEST_PROTOCOL
[199] Fix | Delete
elif not 0 <= protocol <= HIGHEST_PROTOCOL:
[200] Fix | Delete
raise ValueError("pickle protocol must be <= %d" % HIGHEST_PROTOCOL)
[201] Fix | Delete
self.write = file.write
[202] Fix | Delete
self.memo = {}
[203] Fix | Delete
self.proto = int(protocol)
[204] Fix | Delete
self.bin = protocol >= 1
[205] Fix | Delete
self.fast = 0
[206] Fix | Delete
[207] Fix | Delete
def clear_memo(self):
[208] Fix | Delete
"""Clears the pickler's "memo".
[209] Fix | Delete
[210] Fix | Delete
The memo is the data structure that remembers which objects the
[211] Fix | Delete
pickler has already seen, so that shared or recursive objects are
[212] Fix | Delete
pickled by reference and not by value. This method is useful when
[213] Fix | Delete
re-using picklers.
[214] Fix | Delete
[215] Fix | Delete
"""
[216] Fix | Delete
self.memo.clear()
[217] Fix | Delete
[218] Fix | Delete
def dump(self, obj):
[219] Fix | Delete
"""Write a pickled representation of obj to the open file."""
[220] Fix | Delete
if self.proto >= 2:
[221] Fix | Delete
self.write(PROTO + chr(self.proto))
[222] Fix | Delete
self.save(obj)
[223] Fix | Delete
self.write(STOP)
[224] Fix | Delete
[225] Fix | Delete
def memoize(self, obj):
[226] Fix | Delete
"""Store an object in the memo."""
[227] Fix | Delete
[228] Fix | Delete
# The Pickler memo is a dictionary mapping object ids to 2-tuples
[229] Fix | Delete
# that contain the Unpickler memo key and the object being memoized.
[230] Fix | Delete
# The memo key is written to the pickle and will become
[231] Fix | Delete
# the key in the Unpickler's memo. The object is stored in the
[232] Fix | Delete
# Pickler memo so that transient objects are kept alive during
[233] Fix | Delete
# pickling.
[234] Fix | Delete
[235] Fix | Delete
# The use of the Unpickler memo length as the memo key is just a
[236] Fix | Delete
# convention. The only requirement is that the memo values be unique.
[237] Fix | Delete
# But there appears no advantage to any other scheme, and this
[238] Fix | Delete
# scheme allows the Unpickler memo to be implemented as a plain (but
[239] Fix | Delete
# growable) array, indexed by memo key.
[240] Fix | Delete
if self.fast:
[241] Fix | Delete
return
[242] Fix | Delete
assert id(obj) not in self.memo
[243] Fix | Delete
memo_len = len(self.memo)
[244] Fix | Delete
self.write(self.put(memo_len))
[245] Fix | Delete
self.memo[id(obj)] = memo_len, obj
[246] Fix | Delete
[247] Fix | Delete
# Return a PUT (BINPUT, LONG_BINPUT) opcode string, with argument i.
[248] Fix | Delete
def put(self, i, pack=struct.pack):
[249] Fix | Delete
if self.bin:
[250] Fix | Delete
if i < 256:
[251] Fix | Delete
return BINPUT + chr(i)
[252] Fix | Delete
else:
[253] Fix | Delete
return LONG_BINPUT + pack("<i", i)
[254] Fix | Delete
[255] Fix | Delete
return PUT + repr(i) + '\n'
[256] Fix | Delete
[257] Fix | Delete
# Return a GET (BINGET, LONG_BINGET) opcode string, with argument i.
[258] Fix | Delete
def get(self, i, pack=struct.pack):
[259] Fix | Delete
if self.bin:
[260] Fix | Delete
if i < 256:
[261] Fix | Delete
return BINGET + chr(i)
[262] Fix | Delete
else:
[263] Fix | Delete
return LONG_BINGET + pack("<i", i)
[264] Fix | Delete
[265] Fix | Delete
return GET + repr(i) + '\n'
[266] Fix | Delete
[267] Fix | Delete
def save(self, obj):
[268] Fix | Delete
# Check for persistent id (defined by a subclass)
[269] Fix | Delete
pid = self.persistent_id(obj)
[270] Fix | Delete
if pid is not None:
[271] Fix | Delete
self.save_pers(pid)
[272] Fix | Delete
return
[273] Fix | Delete
[274] Fix | Delete
# Check the memo
[275] Fix | Delete
x = self.memo.get(id(obj))
[276] Fix | Delete
if x:
[277] Fix | Delete
self.write(self.get(x[0]))
[278] Fix | Delete
return
[279] Fix | Delete
[280] Fix | Delete
# Check the type dispatch table
[281] Fix | Delete
t = type(obj)
[282] Fix | Delete
f = self.dispatch.get(t)
[283] Fix | Delete
if f:
[284] Fix | Delete
f(self, obj) # Call unbound method with explicit self
[285] Fix | Delete
return
[286] Fix | Delete
[287] Fix | Delete
# Check copy_reg.dispatch_table
[288] Fix | Delete
reduce = dispatch_table.get(t)
[289] Fix | Delete
if reduce:
[290] Fix | Delete
rv = reduce(obj)
[291] Fix | Delete
else:
[292] Fix | Delete
# Check for a class with a custom metaclass; treat as regular class
[293] Fix | Delete
try:
[294] Fix | Delete
issc = issubclass(t, TypeType)
[295] Fix | Delete
except TypeError: # t is not a class (old Boost; see SF #502085)
[296] Fix | Delete
issc = 0
[297] Fix | Delete
if issc:
[298] Fix | Delete
self.save_global(obj)
[299] Fix | Delete
return
[300] Fix | Delete
[301] Fix | Delete
# Check for a __reduce_ex__ method, fall back to __reduce__
[302] Fix | Delete
reduce = getattr(obj, "__reduce_ex__", None)
[303] Fix | Delete
if reduce:
[304] Fix | Delete
rv = reduce(self.proto)
[305] Fix | Delete
else:
[306] Fix | Delete
reduce = getattr(obj, "__reduce__", None)
[307] Fix | Delete
if reduce:
[308] Fix | Delete
rv = reduce()
[309] Fix | Delete
else:
[310] Fix | Delete
raise PicklingError("Can't pickle %r object: %r" %
[311] Fix | Delete
(t.__name__, obj))
[312] Fix | Delete
[313] Fix | Delete
# Check for string returned by reduce(), meaning "save as global"
[314] Fix | Delete
if type(rv) is StringType:
[315] Fix | Delete
self.save_global(obj, rv)
[316] Fix | Delete
return
[317] Fix | Delete
[318] Fix | Delete
# Assert that reduce() returned a tuple
[319] Fix | Delete
if type(rv) is not TupleType:
[320] Fix | Delete
raise PicklingError("%s must return string or tuple" % reduce)
[321] Fix | Delete
[322] Fix | Delete
# Assert that it returned an appropriately sized tuple
[323] Fix | Delete
l = len(rv)
[324] Fix | Delete
if not (2 <= l <= 5):
[325] Fix | Delete
raise PicklingError("Tuple returned by %s must have "
[326] Fix | Delete
"two to five elements" % reduce)
[327] Fix | Delete
[328] Fix | Delete
# Save the reduce() output and finally memoize the object
[329] Fix | Delete
self.save_reduce(obj=obj, *rv)
[330] Fix | Delete
[331] Fix | Delete
def persistent_id(self, obj):
[332] Fix | Delete
# This exists so a subclass can override it
[333] Fix | Delete
return None
[334] Fix | Delete
[335] Fix | Delete
def save_pers(self, pid):
[336] Fix | Delete
# Save a persistent id reference
[337] Fix | Delete
if self.bin:
[338] Fix | Delete
self.save(pid)
[339] Fix | Delete
self.write(BINPERSID)
[340] Fix | Delete
else:
[341] Fix | Delete
self.write(PERSID + str(pid) + '\n')
[342] Fix | Delete
[343] Fix | Delete
def save_reduce(self, func, args, state=None,
[344] Fix | Delete
listitems=None, dictitems=None, obj=None):
[345] Fix | Delete
# This API is called by some subclasses
[346] Fix | Delete
[347] Fix | Delete
# Assert that args is a tuple or None
[348] Fix | Delete
if not isinstance(args, TupleType):
[349] Fix | Delete
raise PicklingError("args from reduce() should be a tuple")
[350] Fix | Delete
[351] Fix | Delete
# Assert that func is callable
[352] Fix | Delete
if not hasattr(func, '__call__'):
[353] Fix | Delete
raise PicklingError("func from reduce should be callable")
[354] Fix | Delete
[355] Fix | Delete
save = self.save
[356] Fix | Delete
write = self.write
[357] Fix | Delete
[358] Fix | Delete
# Protocol 2 special case: if func's name is __newobj__, use NEWOBJ
[359] Fix | Delete
if self.proto >= 2 and getattr(func, "__name__", "") == "__newobj__":
[360] Fix | Delete
# A __reduce__ implementation can direct protocol 2 to
[361] Fix | Delete
# use the more efficient NEWOBJ opcode, while still
[362] Fix | Delete
# allowing protocol 0 and 1 to work normally. For this to
[363] Fix | Delete
# work, the function returned by __reduce__ should be
[364] Fix | Delete
# called __newobj__, and its first argument should be a
[365] Fix | Delete
# new-style class. The implementation for __newobj__
[366] Fix | Delete
# should be as follows, although pickle has no way to
[367] Fix | Delete
# verify this:
[368] Fix | Delete
#
[369] Fix | Delete
# def __newobj__(cls, *args):
[370] Fix | Delete
# return cls.__new__(cls, *args)
[371] Fix | Delete
#
[372] Fix | Delete
# Protocols 0 and 1 will pickle a reference to __newobj__,
[373] Fix | Delete
# while protocol 2 (and above) will pickle a reference to
[374] Fix | Delete
# cls, the remaining args tuple, and the NEWOBJ code,
[375] Fix | Delete
# which calls cls.__new__(cls, *args) at unpickling time
[376] Fix | Delete
# (see load_newobj below). If __reduce__ returns a
[377] Fix | Delete
# three-tuple, the state from the third tuple item will be
[378] Fix | Delete
# pickled regardless of the protocol, calling __setstate__
[379] Fix | Delete
# at unpickling time (see load_build below).
[380] Fix | Delete
#
[381] Fix | Delete
# Note that no standard __newobj__ implementation exists;
[382] Fix | Delete
# you have to provide your own. This is to enforce
[383] Fix | Delete
# compatibility with Python 2.2 (pickles written using
[384] Fix | Delete
# protocol 0 or 1 in Python 2.3 should be unpicklable by
[385] Fix | Delete
# Python 2.2).
[386] Fix | Delete
cls = args[0]
[387] Fix | Delete
if not hasattr(cls, "__new__"):
[388] Fix | Delete
raise PicklingError(
[389] Fix | Delete
"args[0] from __newobj__ args has no __new__")
[390] Fix | Delete
if obj is not None and cls is not obj.__class__:
[391] Fix | Delete
raise PicklingError(
[392] Fix | Delete
"args[0] from __newobj__ args has the wrong class")
[393] Fix | Delete
args = args[1:]
[394] Fix | Delete
save(cls)
[395] Fix | Delete
save(args)
[396] Fix | Delete
write(NEWOBJ)
[397] Fix | Delete
else:
[398] Fix | Delete
save(func)
[399] Fix | Delete
save(args)
[400] Fix | Delete
write(REDUCE)
[401] Fix | Delete
[402] Fix | Delete
if obj is not None:
[403] Fix | Delete
# If the object is already in the memo, this means it is
[404] Fix | Delete
# recursive. In this case, throw away everything we put on the
[405] Fix | Delete
# stack, and fetch the object back from the memo.
[406] Fix | Delete
if id(obj) in self.memo:
[407] Fix | Delete
write(POP + self.get(self.memo[id(obj)][0]))
[408] Fix | Delete
else:
[409] Fix | Delete
self.memoize(obj)
[410] Fix | Delete
[411] Fix | Delete
# More new special cases (that work with older protocols as
[412] Fix | Delete
# well): when __reduce__ returns a tuple with 4 or 5 items,
[413] Fix | Delete
# the 4th and 5th item should be iterators that provide list
[414] Fix | Delete
# items and dict items (as (key, value) tuples), or None.
[415] Fix | Delete
[416] Fix | Delete
if listitems is not None:
[417] Fix | Delete
self._batch_appends(listitems)
[418] Fix | Delete
[419] Fix | Delete
if dictitems is not None:
[420] Fix | Delete
self._batch_setitems(dictitems)
[421] Fix | Delete
[422] Fix | Delete
if state is not None:
[423] Fix | Delete
save(state)
[424] Fix | Delete
write(BUILD)
[425] Fix | Delete
[426] Fix | Delete
# Methods below this point are dispatched through the dispatch table
[427] Fix | Delete
[428] Fix | Delete
dispatch = {}
[429] Fix | Delete
[430] Fix | Delete
def save_none(self, obj):
[431] Fix | Delete
self.write(NONE)
[432] Fix | Delete
dispatch[NoneType] = save_none
[433] Fix | Delete
[434] Fix | Delete
def save_bool(self, obj):
[435] Fix | Delete
if self.proto >= 2:
[436] Fix | Delete
self.write(obj and NEWTRUE or NEWFALSE)
[437] Fix | Delete
else:
[438] Fix | Delete
self.write(obj and TRUE or FALSE)
[439] Fix | Delete
dispatch[bool] = save_bool
[440] Fix | Delete
[441] Fix | Delete
def save_int(self, obj, pack=struct.pack):
[442] Fix | Delete
if self.bin:
[443] Fix | Delete
# If the int is small enough to fit in a signed 4-byte 2's-comp
[444] Fix | Delete
# format, we can store it more efficiently than the general
[445] Fix | Delete
# case.
[446] Fix | Delete
# First one- and two-byte unsigned ints:
[447] Fix | Delete
if obj >= 0:
[448] Fix | Delete
if obj <= 0xff:
[449] Fix | Delete
self.write(BININT1 + chr(obj))
[450] Fix | Delete
return
[451] Fix | Delete
if obj <= 0xffff:
[452] Fix | Delete
self.write("%c%c%c" % (BININT2, obj&0xff, obj>>8))
[453] Fix | Delete
return
[454] Fix | Delete
# Next check for 4-byte signed ints:
[455] Fix | Delete
high_bits = obj >> 31 # note that Python shift sign-extends
[456] Fix | Delete
if high_bits == 0 or high_bits == -1:
[457] Fix | Delete
# All high bits are copies of bit 2**31, so the value
[458] Fix | Delete
# fits in a 4-byte signed int.
[459] Fix | Delete
self.write(BININT + pack("<i", obj))
[460] Fix | Delete
return
[461] Fix | Delete
# Text pickle, or int too big to fit in signed 4-byte format.
[462] Fix | Delete
self.write(INT + repr(obj) + '\n')
[463] Fix | Delete
dispatch[IntType] = save_int
[464] Fix | Delete
[465] Fix | Delete
def save_long(self, obj, pack=struct.pack):
[466] Fix | Delete
if self.proto >= 2:
[467] Fix | Delete
bytes = encode_long(obj)
[468] Fix | Delete
n = len(bytes)
[469] Fix | Delete
if n < 256:
[470] Fix | Delete
self.write(LONG1 + chr(n) + bytes)
[471] Fix | Delete
else:
[472] Fix | Delete
self.write(LONG4 + pack("<i", n) + bytes)
[473] Fix | Delete
return
[474] Fix | Delete
self.write(LONG + repr(obj) + '\n')
[475] Fix | Delete
dispatch[LongType] = save_long
[476] Fix | Delete
[477] Fix | Delete
def save_float(self, obj, pack=struct.pack):
[478] Fix | Delete
if self.bin:
[479] Fix | Delete
self.write(BINFLOAT + pack('>d', obj))
[480] Fix | Delete
else:
[481] Fix | Delete
self.write(FLOAT + repr(obj) + '\n')
[482] Fix | Delete
dispatch[FloatType] = save_float
[483] Fix | Delete
[484] Fix | Delete
def save_string(self, obj, pack=struct.pack):
[485] Fix | Delete
if self.bin:
[486] Fix | Delete
n = len(obj)
[487] Fix | Delete
if n < 256:
[488] Fix | Delete
self.write(SHORT_BINSTRING + chr(n) + obj)
[489] Fix | Delete
else:
[490] Fix | Delete
self.write(BINSTRING + pack("<i", n) + obj)
[491] Fix | Delete
else:
[492] Fix | Delete
self.write(STRING + repr(obj) + '\n')
[493] Fix | Delete
self.memoize(obj)
[494] Fix | Delete
dispatch[StringType] = save_string
[495] Fix | Delete
[496] Fix | Delete
def save_unicode(self, obj, pack=struct.pack):
[497] Fix | Delete
if self.bin:
[498] Fix | Delete
encoding = obj.encode('utf-8')
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function