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