Skip to content

Commit 000df1a

Browse files
LisaLisa
authored andcommitted
- Several bug fixes
- Pages.py deepcopy replaced by ordereddict init because of deepcopy failure - Debug mode main added
1 parent 9ebffbd commit 000df1a

26 files changed

Lines changed: 753 additions & 26 deletions

docs/uPyEasy RESTful API.doc

108 KB
Binary file not shown.

modules/esp32/copy.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../unix/copy.py

modules/esp32/types.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../unix/types.py

modules/esp32/upyeasy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../src

modules/stm32/copy.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../unix/copy.py

modules/stm32/types.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../unix/types.py

modules/stm32/upyeasy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../src

modules/unix/copy.py

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

0 commit comments

Comments
 (0)