Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/imh-pyth.../lib/python3....
File: copyreg.py
"""Helper to provide extensibility for pickle.
[0] Fix | Delete
[1] Fix | Delete
This is only useful to add pickle support for extension types defined in
[2] Fix | Delete
C, not for instances of user-defined classes.
[3] Fix | Delete
"""
[4] Fix | Delete
[5] Fix | Delete
__all__ = ["pickle", "constructor",
[6] Fix | Delete
"add_extension", "remove_extension", "clear_extension_cache"]
[7] Fix | Delete
[8] Fix | Delete
dispatch_table = {}
[9] Fix | Delete
[10] Fix | Delete
def pickle(ob_type, pickle_function, constructor_ob=None):
[11] Fix | Delete
if not callable(pickle_function):
[12] Fix | Delete
raise TypeError("reduction functions must be callable")
[13] Fix | Delete
dispatch_table[ob_type] = pickle_function
[14] Fix | Delete
[15] Fix | Delete
# The constructor_ob function is a vestige of safe for unpickling.
[16] Fix | Delete
# There is no reason for the caller to pass it anymore.
[17] Fix | Delete
if constructor_ob is not None:
[18] Fix | Delete
constructor(constructor_ob)
[19] Fix | Delete
[20] Fix | Delete
def constructor(object):
[21] Fix | Delete
if not callable(object):
[22] Fix | Delete
raise TypeError("constructors must be callable")
[23] Fix | Delete
[24] Fix | Delete
# Example: provide pickling support for complex numbers.
[25] Fix | Delete
[26] Fix | Delete
try:
[27] Fix | Delete
complex
[28] Fix | Delete
except NameError:
[29] Fix | Delete
pass
[30] Fix | Delete
else:
[31] Fix | Delete
[32] Fix | Delete
def pickle_complex(c):
[33] Fix | Delete
return complex, (c.real, c.imag)
[34] Fix | Delete
[35] Fix | Delete
pickle(complex, pickle_complex, complex)
[36] Fix | Delete
[37] Fix | Delete
# Support for pickling new-style objects
[38] Fix | Delete
[39] Fix | Delete
def _reconstructor(cls, base, state):
[40] Fix | Delete
if base is object:
[41] Fix | Delete
obj = object.__new__(cls)
[42] Fix | Delete
else:
[43] Fix | Delete
obj = base.__new__(cls, state)
[44] Fix | Delete
if base.__init__ != object.__init__:
[45] Fix | Delete
base.__init__(obj, state)
[46] Fix | Delete
return obj
[47] Fix | Delete
[48] Fix | Delete
_HEAPTYPE = 1<<9
[49] Fix | Delete
_new_type = type(int.__new__)
[50] Fix | Delete
[51] Fix | Delete
# Python code for object.__reduce_ex__ for protocols 0 and 1
[52] Fix | Delete
[53] Fix | Delete
def _reduce_ex(self, proto):
[54] Fix | Delete
assert proto < 2
[55] Fix | Delete
cls = self.__class__
[56] Fix | Delete
for base in cls.__mro__:
[57] Fix | Delete
if hasattr(base, '__flags__') and not base.__flags__ & _HEAPTYPE:
[58] Fix | Delete
break
[59] Fix | Delete
new = base.__new__
[60] Fix | Delete
if isinstance(new, _new_type) and new.__self__ is base:
[61] Fix | Delete
break
[62] Fix | Delete
else:
[63] Fix | Delete
base = object # not really reachable
[64] Fix | Delete
if base is object:
[65] Fix | Delete
state = None
[66] Fix | Delete
else:
[67] Fix | Delete
if base is cls:
[68] Fix | Delete
raise TypeError(f"cannot pickle {cls.__name__!r} object")
[69] Fix | Delete
state = base(self)
[70] Fix | Delete
args = (cls, base, state)
[71] Fix | Delete
try:
[72] Fix | Delete
getstate = self.__getstate__
[73] Fix | Delete
except AttributeError:
[74] Fix | Delete
if getattr(self, "__slots__", None):
[75] Fix | Delete
raise TypeError(f"cannot pickle {cls.__name__!r} object: "
[76] Fix | Delete
f"a class that defines __slots__ without "
[77] Fix | Delete
f"defining __getstate__ cannot be pickled "
[78] Fix | Delete
f"with protocol {proto}") from None
[79] Fix | Delete
try:
[80] Fix | Delete
dict = self.__dict__
[81] Fix | Delete
except AttributeError:
[82] Fix | Delete
dict = None
[83] Fix | Delete
else:
[84] Fix | Delete
dict = getstate()
[85] Fix | Delete
if dict:
[86] Fix | Delete
return _reconstructor, args, dict
[87] Fix | Delete
else:
[88] Fix | Delete
return _reconstructor, args
[89] Fix | Delete
[90] Fix | Delete
# Helper for __reduce_ex__ protocol 2
[91] Fix | Delete
[92] Fix | Delete
def __newobj__(cls, *args):
[93] Fix | Delete
return cls.__new__(cls, *args)
[94] Fix | Delete
[95] Fix | Delete
def __newobj_ex__(cls, args, kwargs):
[96] Fix | Delete
"""Used by pickle protocol 4, instead of __newobj__ to allow classes with
[97] Fix | Delete
keyword-only arguments to be pickled correctly.
[98] Fix | Delete
"""
[99] Fix | Delete
return cls.__new__(cls, *args, **kwargs)
[100] Fix | Delete
[101] Fix | Delete
def _slotnames(cls):
[102] Fix | Delete
"""Return a list of slot names for a given class.
[103] Fix | Delete
[104] Fix | Delete
This needs to find slots defined by the class and its bases, so we
[105] Fix | Delete
can't simply return the __slots__ attribute. We must walk down
[106] Fix | Delete
the Method Resolution Order and concatenate the __slots__ of each
[107] Fix | Delete
class found there. (This assumes classes don't modify their
[108] Fix | Delete
__slots__ attribute to misrepresent their slots after the class is
[109] Fix | Delete
defined.)
[110] Fix | Delete
"""
[111] Fix | Delete
[112] Fix | Delete
# Get the value from a cache in the class if possible
[113] Fix | Delete
names = cls.__dict__.get("__slotnames__")
[114] Fix | Delete
if names is not None:
[115] Fix | Delete
return names
[116] Fix | Delete
[117] Fix | Delete
# Not cached -- calculate the value
[118] Fix | Delete
names = []
[119] Fix | Delete
if not hasattr(cls, "__slots__"):
[120] Fix | Delete
# This class has no slots
[121] Fix | Delete
pass
[122] Fix | Delete
else:
[123] Fix | Delete
# Slots found -- gather slot names from all base classes
[124] Fix | Delete
for c in cls.__mro__:
[125] Fix | Delete
if "__slots__" in c.__dict__:
[126] Fix | Delete
slots = c.__dict__['__slots__']
[127] Fix | Delete
# if class has a single slot, it can be given as a string
[128] Fix | Delete
if isinstance(slots, str):
[129] Fix | Delete
slots = (slots,)
[130] Fix | Delete
for name in slots:
[131] Fix | Delete
# special descriptors
[132] Fix | Delete
if name in ("__dict__", "__weakref__"):
[133] Fix | Delete
continue
[134] Fix | Delete
# mangled names
[135] Fix | Delete
elif name.startswith('__') and not name.endswith('__'):
[136] Fix | Delete
stripped = c.__name__.lstrip('_')
[137] Fix | Delete
if stripped:
[138] Fix | Delete
names.append('_%s%s' % (stripped, name))
[139] Fix | Delete
else:
[140] Fix | Delete
names.append(name)
[141] Fix | Delete
else:
[142] Fix | Delete
names.append(name)
[143] Fix | Delete
[144] Fix | Delete
# Cache the outcome in the class if at all possible
[145] Fix | Delete
try:
[146] Fix | Delete
cls.__slotnames__ = names
[147] Fix | Delete
except:
[148] Fix | Delete
pass # But don't die if we can't
[149] Fix | Delete
[150] Fix | Delete
return names
[151] Fix | Delete
[152] Fix | Delete
# A registry of extension codes. This is an ad-hoc compression
[153] Fix | Delete
# mechanism. Whenever a global reference to <module>, <name> is about
[154] Fix | Delete
# to be pickled, the (<module>, <name>) tuple is looked up here to see
[155] Fix | Delete
# if it is a registered extension code for it. Extension codes are
[156] Fix | Delete
# universal, so that the meaning of a pickle does not depend on
[157] Fix | Delete
# context. (There are also some codes reserved for local use that
[158] Fix | Delete
# don't have this restriction.) Codes are positive ints; 0 is
[159] Fix | Delete
# reserved.
[160] Fix | Delete
[161] Fix | Delete
_extension_registry = {} # key -> code
[162] Fix | Delete
_inverted_registry = {} # code -> key
[163] Fix | Delete
_extension_cache = {} # code -> object
[164] Fix | Delete
# Don't ever rebind those names: pickling grabs a reference to them when
[165] Fix | Delete
# it's initialized, and won't see a rebinding.
[166] Fix | Delete
[167] Fix | Delete
def add_extension(module, name, code):
[168] Fix | Delete
"""Register an extension code."""
[169] Fix | Delete
code = int(code)
[170] Fix | Delete
if not 1 <= code <= 0x7fffffff:
[171] Fix | Delete
raise ValueError("code out of range")
[172] Fix | Delete
key = (module, name)
[173] Fix | Delete
if (_extension_registry.get(key) == code and
[174] Fix | Delete
_inverted_registry.get(code) == key):
[175] Fix | Delete
return # Redundant registrations are benign
[176] Fix | Delete
if key in _extension_registry:
[177] Fix | Delete
raise ValueError("key %s is already registered with code %s" %
[178] Fix | Delete
(key, _extension_registry[key]))
[179] Fix | Delete
if code in _inverted_registry:
[180] Fix | Delete
raise ValueError("code %s is already in use for key %s" %
[181] Fix | Delete
(code, _inverted_registry[code]))
[182] Fix | Delete
_extension_registry[key] = code
[183] Fix | Delete
_inverted_registry[code] = key
[184] Fix | Delete
[185] Fix | Delete
def remove_extension(module, name, code):
[186] Fix | Delete
"""Unregister an extension code. For testing only."""
[187] Fix | Delete
key = (module, name)
[188] Fix | Delete
if (_extension_registry.get(key) != code or
[189] Fix | Delete
_inverted_registry.get(code) != key):
[190] Fix | Delete
raise ValueError("key %s is not registered with code %s" %
[191] Fix | Delete
(key, code))
[192] Fix | Delete
del _extension_registry[key]
[193] Fix | Delete
del _inverted_registry[code]
[194] Fix | Delete
if code in _extension_cache:
[195] Fix | Delete
del _extension_cache[code]
[196] Fix | Delete
[197] Fix | Delete
def clear_extension_cache():
[198] Fix | Delete
_extension_cache.clear()
[199] Fix | Delete
[200] Fix | Delete
# Standard extension code assignments
[201] Fix | Delete
[202] Fix | Delete
# Reserved ranges
[203] Fix | Delete
[204] Fix | Delete
# First Last Count Purpose
[205] Fix | Delete
# 1 127 127 Reserved for Python standard library
[206] Fix | Delete
# 128 191 64 Reserved for Zope
[207] Fix | Delete
# 192 239 48 Reserved for 3rd parties
[208] Fix | Delete
# 240 255 16 Reserved for private use (will never be assigned)
[209] Fix | Delete
# 256 Inf Inf Reserved for future assignment
[210] Fix | Delete
[211] Fix | Delete
# Extension codes are assigned by the Python Software Foundation.
[212] Fix | Delete
[213] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function