Skip to content

Commit 6b985bb

Browse files
committed
copy: Support copy and deepcopy of OrderedDict objects.
Signed-off-by: Damien George <damien@micropython.org>
1 parent b89114c commit 6b985bb

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

copy/copy.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ class Error(Exception):
5757
pass
5858
error = Error # backward compatibility
5959

60+
try:
61+
from ucollections import OrderedDict
62+
except ImportError:
63+
OrderedDict = None
64+
6065
try:
6166
from org.python.core import PyStringMap
6267
except ImportError:
@@ -121,6 +126,8 @@ def _copy_with_constructor(x):
121126
return type(x)(x)
122127
for t in (list, dict, set):
123128
d[t] = _copy_with_constructor
129+
if OrderedDict is not None:
130+
d[OrderedDict] = _copy_with_constructor
124131

125132
def _copy_with_copy_method(x):
126133
return x.copy()
@@ -235,12 +242,14 @@ def _deepcopy_tuple(x, memo):
235242
d[tuple] = _deepcopy_tuple
236243

237244
def _deepcopy_dict(x, memo):
238-
y = {}
245+
y = type(x)()
239246
memo[id(x)] = y
240247
for key, value in x.items():
241248
y[deepcopy(key, memo)] = deepcopy(value, memo)
242249
return y
243250
d[dict] = _deepcopy_dict
251+
if OrderedDict is not None:
252+
d[OrderedDict] = _deepcopy_dict
244253
if PyStringMap is not None:
245254
d[PyStringMap] = _deepcopy_dict
246255

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy