Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/smexe_ro.../usr/lib64/python3....
File: copy.py
"""Generic (shallow and deep) copying operations.
[0] Fix | Delete
[1] Fix | Delete
Interface summary:
[2] Fix | Delete
[3] Fix | Delete
import copy
[4] Fix | Delete
[5] Fix | Delete
x = copy.copy(y) # make a shallow copy of y
[6] Fix | Delete
x = copy.deepcopy(y) # make a deep copy of y
[7] Fix | Delete
[8] Fix | Delete
For module specific errors, copy.Error is raised.
[9] Fix | Delete
[10] Fix | Delete
The difference between shallow and deep copying is only relevant for
[11] Fix | Delete
compound objects (objects that contain other objects, like lists or
[12] Fix | Delete
class instances).
[13] Fix | Delete
[14] Fix | Delete
- A shallow copy constructs a new compound object and then (to the
[15] Fix | Delete
extent possible) inserts *the same objects* into it that the
[16] Fix | Delete
original contains.
[17] Fix | Delete
[18] Fix | Delete
- A deep copy constructs a new compound object and then, recursively,
[19] Fix | Delete
inserts *copies* into it of the objects found in the original.
[20] Fix | Delete
[21] Fix | Delete
Two problems often exist with deep copy operations that don't exist
[22] Fix | Delete
with shallow copy operations:
[23] Fix | Delete
[24] Fix | Delete
a) recursive objects (compound objects that, directly or indirectly,
[25] Fix | Delete
contain a reference to themselves) may cause a recursive loop
[26] Fix | Delete
[27] Fix | Delete
b) because deep copy copies *everything* it may copy too much, e.g.
[28] Fix | Delete
administrative data structures that should be shared even between
[29] Fix | Delete
copies
[30] Fix | Delete
[31] Fix | Delete
Python's deep copy operation avoids these problems by:
[32] Fix | Delete
[33] Fix | Delete
a) keeping a table of objects already copied during the current
[34] Fix | Delete
copying pass
[35] Fix | Delete
[36] Fix | Delete
b) letting user-defined classes override the copying operation or the
[37] Fix | Delete
set of components copied
[38] Fix | Delete
[39] Fix | Delete
This version does not copy types like module, class, function, method,
[40] Fix | Delete
nor stack trace, stack frame, nor file, socket, window, nor array, nor
[41] Fix | Delete
any similar types.
[42] Fix | Delete
[43] Fix | Delete
Classes can use the same interfaces to control copying that they use
[44] Fix | Delete
to control pickling: they can define methods called __getinitargs__(),
[45] Fix | Delete
__getstate__() and __setstate__(). See the documentation for module
[46] Fix | Delete
"pickle" for information on these methods.
[47] Fix | Delete
"""
[48] Fix | Delete
[49] Fix | Delete
import types
[50] Fix | Delete
import weakref
[51] Fix | Delete
from copyreg import dispatch_table
[52] Fix | Delete
[53] Fix | Delete
class Error(Exception):
[54] Fix | Delete
pass
[55] Fix | Delete
error = Error # backward compatibility
[56] Fix | Delete
[57] Fix | Delete
try:
[58] Fix | Delete
from org.python.core import PyStringMap
[59] Fix | Delete
except ImportError:
[60] Fix | Delete
PyStringMap = None
[61] Fix | Delete
[62] Fix | Delete
__all__ = ["Error", "copy", "deepcopy"]
[63] Fix | Delete
[64] Fix | Delete
def copy(x):
[65] Fix | Delete
"""Shallow copy operation on arbitrary Python objects.
[66] Fix | Delete
[67] Fix | Delete
See the module's __doc__ string for more info.
[68] Fix | Delete
"""
[69] Fix | Delete
[70] Fix | Delete
cls = type(x)
[71] Fix | Delete
[72] Fix | Delete
copier = _copy_dispatch.get(cls)
[73] Fix | Delete
if copier:
[74] Fix | Delete
return copier(x)
[75] Fix | Delete
[76] Fix | Delete
try:
[77] Fix | Delete
issc = issubclass(cls, type)
[78] Fix | Delete
except TypeError: # cls is not a class
[79] Fix | Delete
issc = False
[80] Fix | Delete
if issc:
[81] Fix | Delete
# treat it as a regular class:
[82] Fix | Delete
return _copy_immutable(x)
[83] Fix | Delete
[84] Fix | Delete
copier = getattr(cls, "__copy__", None)
[85] Fix | Delete
if copier:
[86] Fix | Delete
return copier(x)
[87] Fix | Delete
[88] Fix | Delete
reductor = dispatch_table.get(cls)
[89] Fix | Delete
if reductor:
[90] Fix | Delete
rv = reductor(x)
[91] Fix | Delete
else:
[92] Fix | Delete
reductor = getattr(x, "__reduce_ex__", None)
[93] Fix | Delete
if reductor:
[94] Fix | Delete
rv = reductor(4)
[95] Fix | Delete
else:
[96] Fix | Delete
reductor = getattr(x, "__reduce__", None)
[97] Fix | Delete
if reductor:
[98] Fix | Delete
rv = reductor()
[99] Fix | Delete
else:
[100] Fix | Delete
raise Error("un(shallow)copyable object of type %s" % cls)
[101] Fix | Delete
[102] Fix | Delete
if isinstance(rv, str):
[103] Fix | Delete
return x
[104] Fix | Delete
return _reconstruct(x, None, *rv)
[105] Fix | Delete
[106] Fix | Delete
[107] Fix | Delete
_copy_dispatch = d = {}
[108] Fix | Delete
[109] Fix | Delete
def _copy_immutable(x):
[110] Fix | Delete
return x
[111] Fix | Delete
for t in (type(None), int, float, bool, complex, str, tuple,
[112] Fix | Delete
bytes, frozenset, type, range, slice,
[113] Fix | Delete
types.BuiltinFunctionType, type(Ellipsis), type(NotImplemented),
[114] Fix | Delete
types.FunctionType, weakref.ref):
[115] Fix | Delete
d[t] = _copy_immutable
[116] Fix | Delete
t = getattr(types, "CodeType", None)
[117] Fix | Delete
if t is not None:
[118] Fix | Delete
d[t] = _copy_immutable
[119] Fix | Delete
[120] Fix | Delete
d[list] = list.copy
[121] Fix | Delete
d[dict] = dict.copy
[122] Fix | Delete
d[set] = set.copy
[123] Fix | Delete
d[bytearray] = bytearray.copy
[124] Fix | Delete
[125] Fix | Delete
if PyStringMap is not None:
[126] Fix | Delete
d[PyStringMap] = PyStringMap.copy
[127] Fix | Delete
[128] Fix | Delete
del d, t
[129] Fix | Delete
[130] Fix | Delete
def deepcopy(x, memo=None, _nil=[]):
[131] Fix | Delete
"""Deep copy operation on arbitrary Python objects.
[132] Fix | Delete
[133] Fix | Delete
See the module's __doc__ string for more info.
[134] Fix | Delete
"""
[135] Fix | Delete
[136] Fix | Delete
if memo is None:
[137] Fix | Delete
memo = {}
[138] Fix | Delete
[139] Fix | Delete
d = id(x)
[140] Fix | Delete
y = memo.get(d, _nil)
[141] Fix | Delete
if y is not _nil:
[142] Fix | Delete
return y
[143] Fix | Delete
[144] Fix | Delete
cls = type(x)
[145] Fix | Delete
[146] Fix | Delete
copier = _deepcopy_dispatch.get(cls)
[147] Fix | Delete
if copier:
[148] Fix | Delete
y = copier(x, memo)
[149] Fix | Delete
else:
[150] Fix | Delete
try:
[151] Fix | Delete
issc = issubclass(cls, type)
[152] Fix | Delete
except TypeError: # cls is not a class (old Boost; see SF #502085)
[153] Fix | Delete
issc = 0
[154] Fix | Delete
if issc:
[155] Fix | Delete
y = _deepcopy_atomic(x, memo)
[156] Fix | Delete
else:
[157] Fix | Delete
copier = getattr(x, "__deepcopy__", None)
[158] Fix | Delete
if copier:
[159] Fix | Delete
y = copier(memo)
[160] Fix | Delete
else:
[161] Fix | Delete
reductor = dispatch_table.get(cls)
[162] Fix | Delete
if reductor:
[163] Fix | Delete
rv = reductor(x)
[164] Fix | Delete
else:
[165] Fix | Delete
reductor = getattr(x, "__reduce_ex__", None)
[166] Fix | Delete
if reductor:
[167] Fix | Delete
rv = reductor(4)
[168] Fix | Delete
else:
[169] Fix | Delete
reductor = getattr(x, "__reduce__", None)
[170] Fix | Delete
if reductor:
[171] Fix | Delete
rv = reductor()
[172] Fix | Delete
else:
[173] Fix | Delete
raise Error(
[174] Fix | Delete
"un(deep)copyable object of type %s" % cls)
[175] Fix | Delete
if isinstance(rv, str):
[176] Fix | Delete
y = x
[177] Fix | Delete
else:
[178] Fix | Delete
y = _reconstruct(x, memo, *rv)
[179] Fix | Delete
[180] Fix | Delete
# If is its own copy, don't memoize.
[181] Fix | Delete
if y is not x:
[182] Fix | Delete
memo[d] = y
[183] Fix | Delete
_keep_alive(x, memo) # Make sure x lives at least as long as d
[184] Fix | Delete
return y
[185] Fix | Delete
[186] Fix | Delete
_deepcopy_dispatch = d = {}
[187] Fix | Delete
[188] Fix | Delete
def _deepcopy_atomic(x, memo):
[189] Fix | Delete
return x
[190] Fix | Delete
d[type(None)] = _deepcopy_atomic
[191] Fix | Delete
d[type(Ellipsis)] = _deepcopy_atomic
[192] Fix | Delete
d[type(NotImplemented)] = _deepcopy_atomic
[193] Fix | Delete
d[int] = _deepcopy_atomic
[194] Fix | Delete
d[float] = _deepcopy_atomic
[195] Fix | Delete
d[bool] = _deepcopy_atomic
[196] Fix | Delete
d[complex] = _deepcopy_atomic
[197] Fix | Delete
d[bytes] = _deepcopy_atomic
[198] Fix | Delete
d[str] = _deepcopy_atomic
[199] Fix | Delete
try:
[200] Fix | Delete
d[types.CodeType] = _deepcopy_atomic
[201] Fix | Delete
except AttributeError:
[202] Fix | Delete
pass
[203] Fix | Delete
d[type] = _deepcopy_atomic
[204] Fix | Delete
d[types.BuiltinFunctionType] = _deepcopy_atomic
[205] Fix | Delete
d[types.FunctionType] = _deepcopy_atomic
[206] Fix | Delete
d[weakref.ref] = _deepcopy_atomic
[207] Fix | Delete
[208] Fix | Delete
def _deepcopy_list(x, memo, deepcopy=deepcopy):
[209] Fix | Delete
y = []
[210] Fix | Delete
memo[id(x)] = y
[211] Fix | Delete
append = y.append
[212] Fix | Delete
for a in x:
[213] Fix | Delete
append(deepcopy(a, memo))
[214] Fix | Delete
return y
[215] Fix | Delete
d[list] = _deepcopy_list
[216] Fix | Delete
[217] Fix | Delete
def _deepcopy_tuple(x, memo, deepcopy=deepcopy):
[218] Fix | Delete
y = [deepcopy(a, memo) for a in x]
[219] Fix | Delete
# We're not going to put the tuple in the memo, but it's still important we
[220] Fix | Delete
# check for it, in case the tuple contains recursive mutable structures.
[221] Fix | Delete
try:
[222] Fix | Delete
return memo[id(x)]
[223] Fix | Delete
except KeyError:
[224] Fix | Delete
pass
[225] Fix | Delete
for k, j in zip(x, y):
[226] Fix | Delete
if k is not j:
[227] Fix | Delete
y = tuple(y)
[228] Fix | Delete
break
[229] Fix | Delete
else:
[230] Fix | Delete
y = x
[231] Fix | Delete
return y
[232] Fix | Delete
d[tuple] = _deepcopy_tuple
[233] Fix | Delete
[234] Fix | Delete
def _deepcopy_dict(x, memo, deepcopy=deepcopy):
[235] Fix | Delete
y = {}
[236] Fix | Delete
memo[id(x)] = y
[237] Fix | Delete
for key, value in x.items():
[238] Fix | Delete
y[deepcopy(key, memo)] = deepcopy(value, memo)
[239] Fix | Delete
return y
[240] Fix | Delete
d[dict] = _deepcopy_dict
[241] Fix | Delete
if PyStringMap is not None:
[242] Fix | Delete
d[PyStringMap] = _deepcopy_dict
[243] Fix | Delete
[244] Fix | Delete
def _deepcopy_method(x, memo): # Copy instance methods
[245] Fix | Delete
return type(x)(x.__func__, deepcopy(x.__self__, memo))
[246] Fix | Delete
d[types.MethodType] = _deepcopy_method
[247] Fix | Delete
[248] Fix | Delete
del d
[249] Fix | Delete
[250] Fix | Delete
def _keep_alive(x, memo):
[251] Fix | Delete
"""Keeps a reference to the object x in the memo.
[252] Fix | Delete
[253] Fix | Delete
Because we remember objects by their id, we have
[254] Fix | Delete
to assure that possibly temporary objects are kept
[255] Fix | Delete
alive by referencing them.
[256] Fix | Delete
We store a reference at the id of the memo, which should
[257] Fix | Delete
normally not be used unless someone tries to deepcopy
[258] Fix | Delete
the memo itself...
[259] Fix | Delete
"""
[260] Fix | Delete
try:
[261] Fix | Delete
memo[id(memo)].append(x)
[262] Fix | Delete
except KeyError:
[263] Fix | Delete
# aha, this is the first one :-)
[264] Fix | Delete
memo[id(memo)]=[x]
[265] Fix | Delete
[266] Fix | Delete
def _reconstruct(x, memo, func, args,
[267] Fix | Delete
state=None, listiter=None, dictiter=None,
[268] Fix | Delete
deepcopy=deepcopy):
[269] Fix | Delete
deep = memo is not None
[270] Fix | Delete
if deep and args:
[271] Fix | Delete
args = (deepcopy(arg, memo) for arg in args)
[272] Fix | Delete
y = func(*args)
[273] Fix | Delete
if deep:
[274] Fix | Delete
memo[id(x)] = y
[275] Fix | Delete
[276] Fix | Delete
if state is not None:
[277] Fix | Delete
if deep:
[278] Fix | Delete
state = deepcopy(state, memo)
[279] Fix | Delete
if hasattr(y, '__setstate__'):
[280] Fix | Delete
y.__setstate__(state)
[281] Fix | Delete
else:
[282] Fix | Delete
if isinstance(state, tuple) and len(state) == 2:
[283] Fix | Delete
state, slotstate = state
[284] Fix | Delete
else:
[285] Fix | Delete
slotstate = None
[286] Fix | Delete
if state is not None:
[287] Fix | Delete
y.__dict__.update(state)
[288] Fix | Delete
if slotstate is not None:
[289] Fix | Delete
for key, value in slotstate.items():
[290] Fix | Delete
setattr(y, key, value)
[291] Fix | Delete
[292] Fix | Delete
if listiter is not None:
[293] Fix | Delete
if deep:
[294] Fix | Delete
for item in listiter:
[295] Fix | Delete
item = deepcopy(item, memo)
[296] Fix | Delete
y.append(item)
[297] Fix | Delete
else:
[298] Fix | Delete
for item in listiter:
[299] Fix | Delete
y.append(item)
[300] Fix | Delete
if dictiter is not None:
[301] Fix | Delete
if deep:
[302] Fix | Delete
for key, value in dictiter:
[303] Fix | Delete
key = deepcopy(key, memo)
[304] Fix | Delete
value = deepcopy(value, memo)
[305] Fix | Delete
y[key] = value
[306] Fix | Delete
else:
[307] Fix | Delete
for key, value in dictiter:
[308] Fix | Delete
y[key] = value
[309] Fix | Delete
return y
[310] Fix | Delete
[311] Fix | Delete
del types, weakref, PyStringMap
[312] Fix | Delete
[313] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function