Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python3....
File: pickle.py
"""Create portable serialized representations of Python objects.
[0] Fix | Delete
[1] Fix | Delete
See module copyreg for a mechanism for registering custom picklers.
[2] Fix | Delete
See module pickletools source for extensive comments.
[3] Fix | Delete
[4] Fix | Delete
Classes:
[5] Fix | Delete
[6] Fix | Delete
Pickler
[7] Fix | Delete
Unpickler
[8] Fix | Delete
[9] Fix | Delete
Functions:
[10] Fix | Delete
[11] Fix | Delete
dump(object, file)
[12] Fix | Delete
dumps(object) -> string
[13] Fix | Delete
load(file) -> object
[14] Fix | Delete
loads(string) -> object
[15] Fix | Delete
[16] Fix | Delete
Misc variables:
[17] Fix | Delete
[18] Fix | Delete
__version__
[19] Fix | Delete
format_version
[20] Fix | Delete
compatible_formats
[21] Fix | Delete
[22] Fix | Delete
"""
[23] Fix | Delete
[24] Fix | Delete
from types import FunctionType
[25] Fix | Delete
from copyreg import dispatch_table
[26] Fix | Delete
from copyreg import _extension_registry, _inverted_registry, _extension_cache
[27] Fix | Delete
from itertools import islice
[28] Fix | Delete
from functools import partial
[29] Fix | Delete
import sys
[30] Fix | Delete
from sys import maxsize
[31] Fix | Delete
from struct import pack, unpack
[32] Fix | Delete
import re
[33] Fix | Delete
import io
[34] Fix | Delete
import codecs
[35] Fix | Delete
import _compat_pickle
[36] Fix | Delete
[37] Fix | Delete
__all__ = ["PickleError", "PicklingError", "UnpicklingError", "Pickler",
[38] Fix | Delete
"Unpickler", "dump", "dumps", "load", "loads"]
[39] Fix | Delete
[40] Fix | Delete
# Shortcut for use in isinstance testing
[41] Fix | Delete
bytes_types = (bytes, bytearray)
[42] Fix | Delete
[43] Fix | Delete
# These are purely informational; no code uses these.
[44] Fix | Delete
format_version = "4.0" # File format version we write
[45] Fix | Delete
compatible_formats = ["1.0", # Original protocol 0
[46] Fix | Delete
"1.1", # Protocol 0 with INST added
[47] Fix | Delete
"1.2", # Original protocol 1
[48] Fix | Delete
"1.3", # Protocol 1 with BINFLOAT added
[49] Fix | Delete
"2.0", # Protocol 2
[50] Fix | Delete
"3.0", # Protocol 3
[51] Fix | Delete
"4.0", # Protocol 4
[52] Fix | Delete
] # Old format versions we can read
[53] Fix | Delete
[54] Fix | Delete
# This is the highest protocol number we know how to read.
[55] Fix | Delete
HIGHEST_PROTOCOL = 4
[56] Fix | Delete
[57] Fix | Delete
# The protocol we write by default. May be less than HIGHEST_PROTOCOL.
[58] Fix | Delete
# We intentionally write a protocol that Python 2.x cannot read;
[59] Fix | Delete
# there are too many issues with that.
[60] Fix | Delete
DEFAULT_PROTOCOL = 3
[61] Fix | Delete
[62] Fix | Delete
class PickleError(Exception):
[63] Fix | Delete
"""A common base class for the other pickling exceptions."""
[64] Fix | Delete
pass
[65] Fix | Delete
[66] Fix | Delete
class PicklingError(PickleError):
[67] Fix | Delete
"""This exception is raised when an unpicklable object is passed to the
[68] Fix | Delete
dump() method.
[69] Fix | Delete
[70] Fix | Delete
"""
[71] Fix | Delete
pass
[72] Fix | Delete
[73] Fix | Delete
class UnpicklingError(PickleError):
[74] Fix | Delete
"""This exception is raised when there is a problem unpickling an object,
[75] Fix | Delete
such as a security violation.
[76] Fix | Delete
[77] Fix | Delete
Note that other exceptions may also be raised during unpickling, including
[78] Fix | Delete
(but not necessarily limited to) AttributeError, EOFError, ImportError,
[79] Fix | Delete
and IndexError.
[80] Fix | Delete
[81] Fix | Delete
"""
[82] Fix | Delete
pass
[83] Fix | Delete
[84] Fix | Delete
# An instance of _Stop is raised by Unpickler.load_stop() in response to
[85] Fix | Delete
# the STOP opcode, passing the object that is the result of unpickling.
[86] Fix | Delete
class _Stop(Exception):
[87] Fix | Delete
def __init__(self, value):
[88] Fix | Delete
self.value = value
[89] Fix | Delete
[90] Fix | Delete
# Jython has PyStringMap; it's a dict subclass with string keys
[91] Fix | Delete
try:
[92] Fix | Delete
from org.python.core import PyStringMap
[93] Fix | Delete
except ImportError:
[94] Fix | Delete
PyStringMap = 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 = b'(' # push special markobject on stack
[101] Fix | Delete
STOP = b'.' # every pickle ends with STOP
[102] Fix | Delete
POP = b'0' # discard topmost stack item
[103] Fix | Delete
POP_MARK = b'1' # discard stack top through topmost markobject
[104] Fix | Delete
DUP = b'2' # duplicate top stack item
[105] Fix | Delete
FLOAT = b'F' # push float object; decimal string argument
[106] Fix | Delete
INT = b'I' # push integer or bool; decimal string argument
[107] Fix | Delete
BININT = b'J' # push four-byte signed int
[108] Fix | Delete
BININT1 = b'K' # push 1-byte unsigned int
[109] Fix | Delete
LONG = b'L' # push long; decimal string argument
[110] Fix | Delete
BININT2 = b'M' # push 2-byte unsigned int
[111] Fix | Delete
NONE = b'N' # push None
[112] Fix | Delete
PERSID = b'P' # push persistent object; id is taken from string arg
[113] Fix | Delete
BINPERSID = b'Q' # " " " ; " " " " stack
[114] Fix | Delete
REDUCE = b'R' # apply callable to argtuple, both on stack
[115] Fix | Delete
STRING = b'S' # push string; NL-terminated string argument
[116] Fix | Delete
BINSTRING = b'T' # push string; counted binary string argument
[117] Fix | Delete
SHORT_BINSTRING= b'U' # " " ; " " " " < 256 bytes
[118] Fix | Delete
UNICODE = b'V' # push Unicode string; raw-unicode-escaped'd argument
[119] Fix | Delete
BINUNICODE = b'X' # " " " ; counted UTF-8 string argument
[120] Fix | Delete
APPEND = b'a' # append stack top to list below it
[121] Fix | Delete
BUILD = b'b' # call __setstate__ or __dict__.update()
[122] Fix | Delete
GLOBAL = b'c' # push self.find_class(modname, name); 2 string args
[123] Fix | Delete
DICT = b'd' # build a dict from stack items
[124] Fix | Delete
EMPTY_DICT = b'}' # push empty dict
[125] Fix | Delete
APPENDS = b'e' # extend list on stack by topmost stack slice
[126] Fix | Delete
GET = b'g' # push item from memo on stack; index is string arg
[127] Fix | Delete
BINGET = b'h' # " " " " " " ; " " 1-byte arg
[128] Fix | Delete
INST = b'i' # build & push class instance
[129] Fix | Delete
LONG_BINGET = b'j' # push item from memo on stack; index is 4-byte arg
[130] Fix | Delete
LIST = b'l' # build list from topmost stack items
[131] Fix | Delete
EMPTY_LIST = b']' # push empty list
[132] Fix | Delete
OBJ = b'o' # build & push class instance
[133] Fix | Delete
PUT = b'p' # store stack top in memo; index is string arg
[134] Fix | Delete
BINPUT = b'q' # " " " " " ; " " 1-byte arg
[135] Fix | Delete
LONG_BINPUT = b'r' # " " " " " ; " " 4-byte arg
[136] Fix | Delete
SETITEM = b's' # add key+value pair to dict
[137] Fix | Delete
TUPLE = b't' # build tuple from topmost stack items
[138] Fix | Delete
EMPTY_TUPLE = b')' # push empty tuple
[139] Fix | Delete
SETITEMS = b'u' # modify dict by adding topmost key+value pairs
[140] Fix | Delete
BINFLOAT = b'G' # push float; arg is 8-byte float encoding
[141] Fix | Delete
[142] Fix | Delete
TRUE = b'I01\n' # not an opcode; see INT docs in pickletools.py
[143] Fix | Delete
FALSE = b'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 = b'\x80' # identify pickle protocol
[148] Fix | Delete
NEWOBJ = b'\x81' # build object by applying cls.__new__ to argtuple
[149] Fix | Delete
EXT1 = b'\x82' # push object from extension registry; 1-byte index
[150] Fix | Delete
EXT2 = b'\x83' # ditto, but 2-byte index
[151] Fix | Delete
EXT4 = b'\x84' # ditto, but 4-byte index
[152] Fix | Delete
TUPLE1 = b'\x85' # build 1-tuple from stack top
[153] Fix | Delete
TUPLE2 = b'\x86' # build 2-tuple from two topmost stack items
[154] Fix | Delete
TUPLE3 = b'\x87' # build 3-tuple from three topmost stack items
[155] Fix | Delete
NEWTRUE = b'\x88' # push True
[156] Fix | Delete
NEWFALSE = b'\x89' # push False
[157] Fix | Delete
LONG1 = b'\x8a' # push long from < 256 bytes
[158] Fix | Delete
LONG4 = b'\x8b' # push really big long
[159] Fix | Delete
[160] Fix | Delete
_tuplesize2code = [EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3]
[161] Fix | Delete
[162] Fix | Delete
# Protocol 3 (Python 3.x)
[163] Fix | Delete
[164] Fix | Delete
BINBYTES = b'B' # push bytes; counted binary string argument
[165] Fix | Delete
SHORT_BINBYTES = b'C' # " " ; " " " " < 256 bytes
[166] Fix | Delete
[167] Fix | Delete
# Protocol 4
[168] Fix | Delete
SHORT_BINUNICODE = b'\x8c' # push short string; UTF-8 length < 256 bytes
[169] Fix | Delete
BINUNICODE8 = b'\x8d' # push very long string
[170] Fix | Delete
BINBYTES8 = b'\x8e' # push very long bytes string
[171] Fix | Delete
EMPTY_SET = b'\x8f' # push empty set on the stack
[172] Fix | Delete
ADDITEMS = b'\x90' # modify set by adding topmost stack items
[173] Fix | Delete
FROZENSET = b'\x91' # build frozenset from topmost stack items
[174] Fix | Delete
NEWOBJ_EX = b'\x92' # like NEWOBJ but work with keyword only arguments
[175] Fix | Delete
STACK_GLOBAL = b'\x93' # same as GLOBAL but using names on the stacks
[176] Fix | Delete
MEMOIZE = b'\x94' # store top of the stack in memo
[177] Fix | Delete
FRAME = b'\x95' # indicate the beginning of a new frame
[178] Fix | Delete
[179] Fix | Delete
__all__.extend([x for x in dir() if re.match("[A-Z][A-Z0-9_]+$", x)])
[180] Fix | Delete
[181] Fix | Delete
[182] Fix | Delete
class _Framer:
[183] Fix | Delete
[184] Fix | Delete
_FRAME_SIZE_TARGET = 64 * 1024
[185] Fix | Delete
[186] Fix | Delete
def __init__(self, file_write):
[187] Fix | Delete
self.file_write = file_write
[188] Fix | Delete
self.current_frame = None
[189] Fix | Delete
[190] Fix | Delete
def start_framing(self):
[191] Fix | Delete
self.current_frame = io.BytesIO()
[192] Fix | Delete
[193] Fix | Delete
def end_framing(self):
[194] Fix | Delete
if self.current_frame and self.current_frame.tell() > 0:
[195] Fix | Delete
self.commit_frame(force=True)
[196] Fix | Delete
self.current_frame = None
[197] Fix | Delete
[198] Fix | Delete
def commit_frame(self, force=False):
[199] Fix | Delete
if self.current_frame:
[200] Fix | Delete
f = self.current_frame
[201] Fix | Delete
if f.tell() >= self._FRAME_SIZE_TARGET or force:
[202] Fix | Delete
with f.getbuffer() as data:
[203] Fix | Delete
n = len(data)
[204] Fix | Delete
write = self.file_write
[205] Fix | Delete
write(FRAME)
[206] Fix | Delete
write(pack("<Q", n))
[207] Fix | Delete
write(data)
[208] Fix | Delete
f.seek(0)
[209] Fix | Delete
f.truncate()
[210] Fix | Delete
[211] Fix | Delete
def write(self, data):
[212] Fix | Delete
if self.current_frame:
[213] Fix | Delete
return self.current_frame.write(data)
[214] Fix | Delete
else:
[215] Fix | Delete
return self.file_write(data)
[216] Fix | Delete
[217] Fix | Delete
[218] Fix | Delete
class _Unframer:
[219] Fix | Delete
[220] Fix | Delete
def __init__(self, file_read, file_readline, file_tell=None):
[221] Fix | Delete
self.file_read = file_read
[222] Fix | Delete
self.file_readline = file_readline
[223] Fix | Delete
self.current_frame = None
[224] Fix | Delete
[225] Fix | Delete
def read(self, n):
[226] Fix | Delete
if self.current_frame:
[227] Fix | Delete
data = self.current_frame.read(n)
[228] Fix | Delete
if not data and n != 0:
[229] Fix | Delete
self.current_frame = None
[230] Fix | Delete
return self.file_read(n)
[231] Fix | Delete
if len(data) < n:
[232] Fix | Delete
raise UnpicklingError(
[233] Fix | Delete
"pickle exhausted before end of frame")
[234] Fix | Delete
return data
[235] Fix | Delete
else:
[236] Fix | Delete
return self.file_read(n)
[237] Fix | Delete
[238] Fix | Delete
def readline(self):
[239] Fix | Delete
if self.current_frame:
[240] Fix | Delete
data = self.current_frame.readline()
[241] Fix | Delete
if not data:
[242] Fix | Delete
self.current_frame = None
[243] Fix | Delete
return self.file_readline()
[244] Fix | Delete
if data[-1] != b'\n'[0]:
[245] Fix | Delete
raise UnpicklingError(
[246] Fix | Delete
"pickle exhausted before end of frame")
[247] Fix | Delete
return data
[248] Fix | Delete
else:
[249] Fix | Delete
return self.file_readline()
[250] Fix | Delete
[251] Fix | Delete
def load_frame(self, frame_size):
[252] Fix | Delete
if self.current_frame and self.current_frame.read() != b'':
[253] Fix | Delete
raise UnpicklingError(
[254] Fix | Delete
"beginning of a new frame before end of current frame")
[255] Fix | Delete
self.current_frame = io.BytesIO(self.file_read(frame_size))
[256] Fix | Delete
[257] Fix | Delete
[258] Fix | Delete
# Tools used for pickling.
[259] Fix | Delete
[260] Fix | Delete
def _getattribute(obj, name):
[261] Fix | Delete
for subpath in name.split('.'):
[262] Fix | Delete
if subpath == '<locals>':
[263] Fix | Delete
raise AttributeError("Can't get local attribute {!r} on {!r}"
[264] Fix | Delete
.format(name, obj))
[265] Fix | Delete
try:
[266] Fix | Delete
parent = obj
[267] Fix | Delete
obj = getattr(obj, subpath)
[268] Fix | Delete
except AttributeError:
[269] Fix | Delete
raise AttributeError("Can't get attribute {!r} on {!r}"
[270] Fix | Delete
.format(name, obj))
[271] Fix | Delete
return obj, parent
[272] Fix | Delete
[273] Fix | Delete
def whichmodule(obj, name):
[274] Fix | Delete
"""Find the module an object belong to."""
[275] Fix | Delete
module_name = getattr(obj, '__module__', None)
[276] Fix | Delete
if module_name is not None:
[277] Fix | Delete
return module_name
[278] Fix | Delete
# Protect the iteration by using a list copy of sys.modules against dynamic
[279] Fix | Delete
# modules that trigger imports of other modules upon calls to getattr.
[280] Fix | Delete
for module_name, module in list(sys.modules.items()):
[281] Fix | Delete
if module_name == '__main__' or module is None:
[282] Fix | Delete
continue
[283] Fix | Delete
try:
[284] Fix | Delete
if _getattribute(module, name)[0] is obj:
[285] Fix | Delete
return module_name
[286] Fix | Delete
except AttributeError:
[287] Fix | Delete
pass
[288] Fix | Delete
return '__main__'
[289] Fix | Delete
[290] Fix | Delete
def encode_long(x):
[291] Fix | Delete
r"""Encode a long to a two's complement little-endian binary string.
[292] Fix | Delete
Note that 0 is a special case, returning an empty string, to save a
[293] Fix | Delete
byte in the LONG1 pickling context.
[294] Fix | Delete
[295] Fix | Delete
>>> encode_long(0)
[296] Fix | Delete
b''
[297] Fix | Delete
>>> encode_long(255)
[298] Fix | Delete
b'\xff\x00'
[299] Fix | Delete
>>> encode_long(32767)
[300] Fix | Delete
b'\xff\x7f'
[301] Fix | Delete
>>> encode_long(-256)
[302] Fix | Delete
b'\x00\xff'
[303] Fix | Delete
>>> encode_long(-32768)
[304] Fix | Delete
b'\x00\x80'
[305] Fix | Delete
>>> encode_long(-128)
[306] Fix | Delete
b'\x80'
[307] Fix | Delete
>>> encode_long(127)
[308] Fix | Delete
b'\x7f'
[309] Fix | Delete
>>>
[310] Fix | Delete
"""
[311] Fix | Delete
if x == 0:
[312] Fix | Delete
return b''
[313] Fix | Delete
nbytes = (x.bit_length() >> 3) + 1
[314] Fix | Delete
result = x.to_bytes(nbytes, byteorder='little', signed=True)
[315] Fix | Delete
if x < 0 and nbytes > 1:
[316] Fix | Delete
if result[-1] == 0xff and (result[-2] & 0x80) != 0:
[317] Fix | Delete
result = result[:-1]
[318] Fix | Delete
return result
[319] Fix | Delete
[320] Fix | Delete
def decode_long(data):
[321] Fix | Delete
r"""Decode a long from a two's complement little-endian binary string.
[322] Fix | Delete
[323] Fix | Delete
>>> decode_long(b'')
[324] Fix | Delete
0
[325] Fix | Delete
>>> decode_long(b"\xff\x00")
[326] Fix | Delete
255
[327] Fix | Delete
>>> decode_long(b"\xff\x7f")
[328] Fix | Delete
32767
[329] Fix | Delete
>>> decode_long(b"\x00\xff")
[330] Fix | Delete
-256
[331] Fix | Delete
>>> decode_long(b"\x00\x80")
[332] Fix | Delete
-32768
[333] Fix | Delete
>>> decode_long(b"\x80")
[334] Fix | Delete
-128
[335] Fix | Delete
>>> decode_long(b"\x7f")
[336] Fix | Delete
127
[337] Fix | Delete
"""
[338] Fix | Delete
return int.from_bytes(data, byteorder='little', signed=True)
[339] Fix | Delete
[340] Fix | Delete
[341] Fix | Delete
# Pickling machinery
[342] Fix | Delete
[343] Fix | Delete
class _Pickler:
[344] Fix | Delete
[345] Fix | Delete
def __init__(self, file, protocol=None, *, fix_imports=True):
[346] Fix | Delete
"""This takes a binary file for writing a pickle data stream.
[347] Fix | Delete
[348] Fix | Delete
The optional *protocol* argument tells the pickler to use the
[349] Fix | Delete
given protocol; supported protocols are 0, 1, 2, 3 and 4. The
[350] Fix | Delete
default protocol is 3; a backward-incompatible protocol designed
[351] Fix | Delete
for Python 3.
[352] Fix | Delete
[353] Fix | Delete
Specifying a negative protocol version selects the highest
[354] Fix | Delete
protocol version supported. The higher the protocol used, the
[355] Fix | Delete
more recent the version of Python needed to read the pickle
[356] Fix | Delete
produced.
[357] Fix | Delete
[358] Fix | Delete
The *file* argument must have a write() method that accepts a
[359] Fix | Delete
single bytes argument. It can thus be a file object opened for
[360] Fix | Delete
binary writing, an io.BytesIO instance, or any other custom
[361] Fix | Delete
object that meets this interface.
[362] Fix | Delete
[363] Fix | Delete
If *fix_imports* is True and *protocol* is less than 3, pickle
[364] Fix | Delete
will try to map the new Python 3 names to the old module names
[365] Fix | Delete
used in Python 2, so that the pickle data stream is readable
[366] Fix | Delete
with Python 2.
[367] Fix | Delete
"""
[368] Fix | Delete
if protocol is None:
[369] Fix | Delete
protocol = DEFAULT_PROTOCOL
[370] Fix | Delete
if protocol < 0:
[371] Fix | Delete
protocol = HIGHEST_PROTOCOL
[372] Fix | Delete
elif not 0 <= protocol <= HIGHEST_PROTOCOL:
[373] Fix | Delete
raise ValueError("pickle protocol must be <= %d" % HIGHEST_PROTOCOL)
[374] Fix | Delete
try:
[375] Fix | Delete
self._file_write = file.write
[376] Fix | Delete
except AttributeError:
[377] Fix | Delete
raise TypeError("file must have a 'write' attribute")
[378] Fix | Delete
self.framer = _Framer(self._file_write)
[379] Fix | Delete
self.write = self.framer.write
[380] Fix | Delete
self.memo = {}
[381] Fix | Delete
self.proto = int(protocol)
[382] Fix | Delete
self.bin = protocol >= 1
[383] Fix | Delete
self.fast = 0
[384] Fix | Delete
self.fix_imports = fix_imports and protocol < 3
[385] Fix | Delete
[386] Fix | Delete
def clear_memo(self):
[387] Fix | Delete
"""Clears the pickler's "memo".
[388] Fix | Delete
[389] Fix | Delete
The memo is the data structure that remembers which objects the
[390] Fix | Delete
pickler has already seen, so that shared or recursive objects
[391] Fix | Delete
are pickled by reference and not by value. This method is
[392] Fix | Delete
useful when re-using picklers.
[393] Fix | Delete
"""
[394] Fix | Delete
self.memo.clear()
[395] Fix | Delete
[396] Fix | Delete
def dump(self, obj):
[397] Fix | Delete
"""Write a pickled representation of obj to the open file."""
[398] Fix | Delete
# Check whether Pickler was initialized correctly. This is
[399] Fix | Delete
# only needed to mimic the behavior of _pickle.Pickler.dump().
[400] Fix | Delete
if not hasattr(self, "_file_write"):
[401] Fix | Delete
raise PicklingError("Pickler.__init__() was not called by "
[402] Fix | Delete
"%s.__init__()" % (self.__class__.__name__,))
[403] Fix | Delete
if self.proto >= 2:
[404] Fix | Delete
self.write(PROTO + pack("<B", self.proto))
[405] Fix | Delete
if self.proto >= 4:
[406] Fix | Delete
self.framer.start_framing()
[407] Fix | Delete
self.save(obj)
[408] Fix | Delete
self.write(STOP)
[409] Fix | Delete
self.framer.end_framing()
[410] Fix | Delete
[411] Fix | Delete
def memoize(self, obj):
[412] Fix | Delete
"""Store an object in the memo."""
[413] Fix | Delete
[414] Fix | Delete
# The Pickler memo is a dictionary mapping object ids to 2-tuples
[415] Fix | Delete
# that contain the Unpickler memo key and the object being memoized.
[416] Fix | Delete
# The memo key is written to the pickle and will become
[417] Fix | Delete
# the key in the Unpickler's memo. The object is stored in the
[418] Fix | Delete
# Pickler memo so that transient objects are kept alive during
[419] Fix | Delete
# pickling.
[420] Fix | Delete
[421] Fix | Delete
# The use of the Unpickler memo length as the memo key is just a
[422] Fix | Delete
# convention. The only requirement is that the memo values be unique.
[423] Fix | Delete
# But there appears no advantage to any other scheme, and this
[424] Fix | Delete
# scheme allows the Unpickler memo to be implemented as a plain (but
[425] Fix | Delete
# growable) array, indexed by memo key.
[426] Fix | Delete
if self.fast:
[427] Fix | Delete
return
[428] Fix | Delete
assert id(obj) not in self.memo
[429] Fix | Delete
idx = len(self.memo)
[430] Fix | Delete
self.write(self.put(idx))
[431] Fix | Delete
self.memo[id(obj)] = idx, obj
[432] Fix | Delete
[433] Fix | Delete
# Return a PUT (BINPUT, LONG_BINPUT) opcode string, with argument i.
[434] Fix | Delete
def put(self, idx):
[435] Fix | Delete
if self.proto >= 4:
[436] Fix | Delete
return MEMOIZE
[437] Fix | Delete
elif self.bin:
[438] Fix | Delete
if idx < 256:
[439] Fix | Delete
return BINPUT + pack("<B", idx)
[440] Fix | Delete
else:
[441] Fix | Delete
return LONG_BINPUT + pack("<I", idx)
[442] Fix | Delete
else:
[443] Fix | Delete
return PUT + repr(idx).encode("ascii") + b'\n'
[444] Fix | Delete
[445] Fix | Delete
# Return a GET (BINGET, LONG_BINGET) opcode string, with argument i.
[446] Fix | Delete
def get(self, i):
[447] Fix | Delete
if self.bin:
[448] Fix | Delete
if i < 256:
[449] Fix | Delete
return BINGET + pack("<B", i)
[450] Fix | Delete
else:
[451] Fix | Delete
return LONG_BINGET + pack("<I", i)
[452] Fix | Delete
[453] Fix | Delete
return GET + repr(i).encode("ascii") + b'\n'
[454] Fix | Delete
[455] Fix | Delete
def save(self, obj, save_persistent_id=True):
[456] Fix | Delete
self.framer.commit_frame()
[457] Fix | Delete
[458] Fix | Delete
# Check for persistent id (defined by a subclass)
[459] Fix | Delete
pid = self.persistent_id(obj)
[460] Fix | Delete
if pid is not None and save_persistent_id:
[461] Fix | Delete
self.save_pers(pid)
[462] Fix | Delete
return
[463] Fix | Delete
[464] Fix | Delete
# Check the memo
[465] Fix | Delete
x = self.memo.get(id(obj))
[466] Fix | Delete
if x is not None:
[467] Fix | Delete
self.write(self.get(x[0]))
[468] Fix | Delete
return
[469] Fix | Delete
[470] Fix | Delete
# Check the type dispatch table
[471] Fix | Delete
t = type(obj)
[472] Fix | Delete
f = self.dispatch.get(t)
[473] Fix | Delete
if f is not None:
[474] Fix | Delete
f(self, obj) # Call unbound method with explicit self
[475] Fix | Delete
return
[476] Fix | Delete
[477] Fix | Delete
# Check private dispatch table if any, or else copyreg.dispatch_table
[478] Fix | Delete
reduce = getattr(self, 'dispatch_table', dispatch_table).get(t)
[479] Fix | Delete
if reduce is not None:
[480] Fix | Delete
rv = reduce(obj)
[481] Fix | Delete
else:
[482] Fix | Delete
# Check for a class with a custom metaclass; treat as regular class
[483] Fix | Delete
try:
[484] Fix | Delete
issc = issubclass(t, type)
[485] Fix | Delete
except TypeError: # t is not a class (old Boost; see SF #502085)
[486] Fix | Delete
issc = False
[487] Fix | Delete
if issc:
[488] Fix | Delete
self.save_global(obj)
[489] Fix | Delete
return
[490] Fix | Delete
[491] Fix | Delete
# Check for a __reduce_ex__ method, fall back to __reduce__
[492] Fix | Delete
reduce = getattr(obj, "__reduce_ex__", None)
[493] Fix | Delete
if reduce is not None:
[494] Fix | Delete
rv = reduce(self.proto)
[495] Fix | Delete
else:
[496] Fix | Delete
reduce = getattr(obj, "__reduce__", None)
[497] Fix | Delete
if reduce is not None:
[498] Fix | Delete
rv = reduce()
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function