Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../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
if issubclass(cls, type):
[77] Fix | Delete
# treat it as a regular class:
[78] Fix | Delete
return _copy_immutable(x)
[79] Fix | Delete
[80] Fix | Delete
copier = getattr(cls, "__copy__", None)
[81] Fix | Delete
if copier is not None:
[82] Fix | Delete
return copier(x)
[83] Fix | Delete
[84] Fix | Delete
reductor = dispatch_table.get(cls)
[85] Fix | Delete
if reductor is not None:
[86] Fix | Delete
rv = reductor(x)
[87] Fix | Delete
else:
[88] Fix | Delete
reductor = getattr(x, "__reduce_ex__", None)
[89] Fix | Delete
if reductor is not None:
[90] Fix | Delete
rv = reductor(4)
[91] Fix | Delete
else:
[92] Fix | Delete
reductor = getattr(x, "__reduce__", None)
[93] Fix | Delete
if reductor:
[94] Fix | Delete
rv = reductor()
[95] Fix | Delete
else:
[96] Fix | Delete
raise Error("un(shallow)copyable object of type %s" % cls)
[97] Fix | Delete
[98] Fix | Delete
if isinstance(rv, str):
[99] Fix | Delete
return x
[100] Fix | Delete
return _reconstruct(x, None, *rv)
[101] Fix | Delete
[102] Fix | Delete
[103] Fix | Delete
_copy_dispatch = d = {}
[104] Fix | Delete
[105] Fix | Delete
def _copy_immutable(x):
[106] Fix | Delete
return x
[107] Fix | Delete
for t in (type(None), int, float, bool, complex, str, tuple,
[108] Fix | Delete
bytes, frozenset, type, range, slice, property,
[109] Fix | Delete
types.BuiltinFunctionType, type(Ellipsis), type(NotImplemented),
[110] Fix | Delete
types.FunctionType, weakref.ref):
[111] Fix | Delete
d[t] = _copy_immutable
[112] Fix | Delete
t = getattr(types, "CodeType", None)
[113] Fix | Delete
if t is not None:
[114] Fix | Delete
d[t] = _copy_immutable
[115] Fix | Delete
[116] Fix | Delete
d[list] = list.copy
[117] Fix | Delete
d[dict] = dict.copy
[118] Fix | Delete
d[set] = set.copy
[119] Fix | Delete
d[bytearray] = bytearray.copy
[120] Fix | Delete
[121] Fix | Delete
if PyStringMap is not None:
[122] Fix | Delete
d[PyStringMap] = PyStringMap.copy
[123] Fix | Delete
[124] Fix | Delete
del d, t
[125] Fix | Delete
[126] Fix | Delete
def deepcopy(x, memo=None, _nil=[]):
[127] Fix | Delete
"""Deep copy operation on arbitrary Python objects.
[128] Fix | Delete
[129] Fix | Delete
See the module's __doc__ string for more info.
[130] Fix | Delete
"""
[131] Fix | Delete
[132] Fix | Delete
if memo is None:
[133] Fix | Delete
memo = {}
[134] Fix | Delete
[135] Fix | Delete
d = id(x)
[136] Fix | Delete
y = memo.get(d, _nil)
[137] Fix | Delete
if y is not _nil:
[138] Fix | Delete
return y
[139] Fix | Delete
[140] Fix | Delete
cls = type(x)
[141] Fix | Delete
[142] Fix | Delete
copier = _deepcopy_dispatch.get(cls)
[143] Fix | Delete
if copier is not None:
[144] Fix | Delete
y = copier(x, memo)
[145] Fix | Delete
else:
[146] Fix | Delete
if issubclass(cls, type):
[147] Fix | Delete
y = _deepcopy_atomic(x, memo)
[148] Fix | Delete
else:
[149] Fix | Delete
copier = getattr(x, "__deepcopy__", None)
[150] Fix | Delete
if copier is not None:
[151] Fix | Delete
y = copier(memo)
[152] Fix | Delete
else:
[153] Fix | Delete
reductor = dispatch_table.get(cls)
[154] Fix | Delete
if reductor:
[155] Fix | Delete
rv = reductor(x)
[156] Fix | Delete
else:
[157] Fix | Delete
reductor = getattr(x, "__reduce_ex__", None)
[158] Fix | Delete
if reductor is not None:
[159] Fix | Delete
rv = reductor(4)
[160] Fix | Delete
else:
[161] Fix | Delete
reductor = getattr(x, "__reduce__", None)
[162] Fix | Delete
if reductor:
[163] Fix | Delete
rv = reductor()
[164] Fix | Delete
else:
[165] Fix | Delete
raise Error(
[166] Fix | Delete
"un(deep)copyable object of type %s" % cls)
[167] Fix | Delete
if isinstance(rv, str):
[168] Fix | Delete
y = x
[169] Fix | Delete
else:
[170] Fix | Delete
y = _reconstruct(x, memo, *rv)
[171] Fix | Delete
[172] Fix | Delete
# If is its own copy, don't memoize.
[173] Fix | Delete
if y is not x:
[174] Fix | Delete
memo[d] = y
[175] Fix | Delete
_keep_alive(x, memo) # Make sure x lives at least as long as d
[176] Fix | Delete
return y
[177] Fix | Delete
[178] Fix | Delete
_deepcopy_dispatch = d = {}
[179] Fix | Delete
[180] Fix | Delete
def _deepcopy_atomic(x, memo):
[181] Fix | Delete
return x
[182] Fix | Delete
d[type(None)] = _deepcopy_atomic
[183] Fix | Delete
d[type(Ellipsis)] = _deepcopy_atomic
[184] Fix | Delete
d[type(NotImplemented)] = _deepcopy_atomic
[185] Fix | Delete
d[int] = _deepcopy_atomic
[186] Fix | Delete
d[float] = _deepcopy_atomic
[187] Fix | Delete
d[bool] = _deepcopy_atomic
[188] Fix | Delete
d[complex] = _deepcopy_atomic
[189] Fix | Delete
d[bytes] = _deepcopy_atomic
[190] Fix | Delete
d[str] = _deepcopy_atomic
[191] Fix | Delete
d[types.CodeType] = _deepcopy_atomic
[192] Fix | Delete
d[type] = _deepcopy_atomic
[193] Fix | Delete
d[types.BuiltinFunctionType] = _deepcopy_atomic
[194] Fix | Delete
d[types.FunctionType] = _deepcopy_atomic
[195] Fix | Delete
d[weakref.ref] = _deepcopy_atomic
[196] Fix | Delete
d[property] = _deepcopy_atomic
[197] Fix | Delete
[198] Fix | Delete
def _deepcopy_list(x, memo, deepcopy=deepcopy):
[199] Fix | Delete
y = []
[200] Fix | Delete
memo[id(x)] = y
[201] Fix | Delete
append = y.append
[202] Fix | Delete
for a in x:
[203] Fix | Delete
append(deepcopy(a, memo))
[204] Fix | Delete
return y
[205] Fix | Delete
d[list] = _deepcopy_list
[206] Fix | Delete
[207] Fix | Delete
def _deepcopy_tuple(x, memo, deepcopy=deepcopy):
[208] Fix | Delete
y = [deepcopy(a, memo) for a in x]
[209] Fix | Delete
# We're not going to put the tuple in the memo, but it's still important we
[210] Fix | Delete
# check for it, in case the tuple contains recursive mutable structures.
[211] Fix | Delete
try:
[212] Fix | Delete
return memo[id(x)]
[213] Fix | Delete
except KeyError:
[214] Fix | Delete
pass
[215] Fix | Delete
for k, j in zip(x, y):
[216] Fix | Delete
if k is not j:
[217] Fix | Delete
y = tuple(y)
[218] Fix | Delete
break
[219] Fix | Delete
else:
[220] Fix | Delete
y = x
[221] Fix | Delete
return y
[222] Fix | Delete
d[tuple] = _deepcopy_tuple
[223] Fix | Delete
[224] Fix | Delete
def _deepcopy_dict(x, memo, deepcopy=deepcopy):
[225] Fix | Delete
y = {}
[226] Fix | Delete
memo[id(x)] = y
[227] Fix | Delete
for key, value in x.items():
[228] Fix | Delete
y[deepcopy(key, memo)] = deepcopy(value, memo)
[229] Fix | Delete
return y
[230] Fix | Delete
d[dict] = _deepcopy_dict
[231] Fix | Delete
if PyStringMap is not None:
[232] Fix | Delete
d[PyStringMap] = _deepcopy_dict
[233] Fix | Delete
[234] Fix | Delete
def _deepcopy_method(x, memo): # Copy instance methods
[235] Fix | Delete
return type(x)(x.__func__, deepcopy(x.__self__, memo))
[236] Fix | Delete
d[types.MethodType] = _deepcopy_method
[237] Fix | Delete
[238] Fix | Delete
del d
[239] Fix | Delete
[240] Fix | Delete
def _keep_alive(x, memo):
[241] Fix | Delete
"""Keeps a reference to the object x in the memo.
[242] Fix | Delete
[243] Fix | Delete
Because we remember objects by their id, we have
[244] Fix | Delete
to assure that possibly temporary objects are kept
[245] Fix | Delete
alive by referencing them.
[246] Fix | Delete
We store a reference at the id of the memo, which should
[247] Fix | Delete
normally not be used unless someone tries to deepcopy
[248] Fix | Delete
the memo itself...
[249] Fix | Delete
"""
[250] Fix | Delete
try:
[251] Fix | Delete
memo[id(memo)].append(x)
[252] Fix | Delete
except KeyError:
[253] Fix | Delete
# aha, this is the first one :-)
[254] Fix | Delete
memo[id(memo)]=[x]
[255] Fix | Delete
[256] Fix | Delete
def _reconstruct(x, memo, func, args,
[257] Fix | Delete
state=None, listiter=None, dictiter=None,
[258] Fix | Delete
deepcopy=deepcopy):
[259] Fix | Delete
deep = memo is not None
[260] Fix | Delete
if deep and args:
[261] Fix | Delete
args = (deepcopy(arg, memo) for arg in args)
[262] Fix | Delete
y = func(*args)
[263] Fix | Delete
if deep:
[264] Fix | Delete
memo[id(x)] = y
[265] Fix | Delete
[266] Fix | Delete
if state is not None:
[267] Fix | Delete
if deep:
[268] Fix | Delete
state = deepcopy(state, memo)
[269] Fix | Delete
if hasattr(y, '__setstate__'):
[270] Fix | Delete
y.__setstate__(state)
[271] Fix | Delete
else:
[272] Fix | Delete
if isinstance(state, tuple) and len(state) == 2:
[273] Fix | Delete
state, slotstate = state
[274] Fix | Delete
else:
[275] Fix | Delete
slotstate = None
[276] Fix | Delete
if state is not None:
[277] Fix | Delete
y.__dict__.update(state)
[278] Fix | Delete
if slotstate is not None:
[279] Fix | Delete
for key, value in slotstate.items():
[280] Fix | Delete
setattr(y, key, value)
[281] Fix | Delete
[282] Fix | Delete
if listiter is not None:
[283] Fix | Delete
if deep:
[284] Fix | Delete
for item in listiter:
[285] Fix | Delete
item = deepcopy(item, memo)
[286] Fix | Delete
y.append(item)
[287] Fix | Delete
else:
[288] Fix | Delete
for item in listiter:
[289] Fix | Delete
y.append(item)
[290] Fix | Delete
if dictiter is not None:
[291] Fix | Delete
if deep:
[292] Fix | Delete
for key, value in dictiter:
[293] Fix | Delete
key = deepcopy(key, memo)
[294] Fix | Delete
value = deepcopy(value, memo)
[295] Fix | Delete
y[key] = value
[296] Fix | Delete
else:
[297] Fix | Delete
for key, value in dictiter:
[298] Fix | Delete
y[key] = value
[299] Fix | Delete
return y
[300] Fix | Delete
[301] Fix | Delete
del types, weakref, PyStringMap
[302] Fix | Delete
[303] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function