summaryrefslogtreecommitdiff
path: root/comments.py
diff options
context:
space:
mode:
authorAnthon van der Neut <anthon@mnt.org>2016-12-25 14:47:21 +0100
committerAnthon van der Neut <anthon@mnt.org>2016-12-25 14:47:21 +0100
commit43642051ac62148e815e924d5ac8907260c54823 (patch)
tree0e149280b460501dd2e2d99ab5e8480d049e5a94 /comments.py
parentbdf3f50a644166aeb1a0b8521aa986ae07b26c41 (diff)
downloadruamel.yaml-43642051ac62148e815e924d5ac8907260c54823.tar.gz
fix #84, deepcopy not working (reported by Peter Amstutz)0.13.5
added test, implemented __deepcopy__ on CommentedMap and CommentedSeq
Diffstat (limited to 'comments.py')
-rw-r--r--comments.py26
1 files changed, 26 insertions, 0 deletions
diff --git a/comments.py b/comments.py
index dc56594..e549a33 100644
--- a/comments.py
+++ b/comments.py
@@ -8,6 +8,7 @@ these are not really related, formatting could be factored out as
a separate base
"""
+import copy
from collections import MutableSet, Sized, Set # type: ignore
from ruamel.yaml.compat import ordereddict, PY2
@@ -296,6 +297,15 @@ class CommentedBase(object):
def yaml_set_tag(self, value):
self.tag.value = value
+ def copy_attributes(self, t, deep=False):
+ for a in [Comment.attrib, Format.attrib, LineCol.attrib, Anchor.attrib,
+ Tag.attrib, merge_attrib]:
+ if hasattr(self, a):
+ if deep:
+ setattr(t, a, copy.deepcopy(getattr(self, a)))
+ else:
+ setattr(t, a, getattr(self, a))
+
class CommentedSeq(list, CommentedBase):
__slots__ = Comment.attrib,
@@ -357,6 +367,14 @@ class CommentedSeq(list, CommentedBase):
pre_comments = self.ca.comment[1] = []
return pre_comments
+ def __deepcopy__(self, memo):
+ res = CommentedSeq()
+ memo[id(self)] = res
+ for k in self:
+ res.append(copy.deepcopy(k))
+ self.copy_attributes(res, deep=True)
+ return res
+
class CommentedKeySeq(tuple, CommentedBase):
"""This primarily exists to be able to roundtrip keys that are sequences"""
@@ -731,6 +749,14 @@ class CommentedMap(ordereddict, CommentedBase):
def add_yaml_merge(self, value):
self.merge.extend(value)
+ def __deepcopy__(self, memo):
+ res = CommentedMap()
+ memo[id(self)] = res
+ for k in self:
+ res[k] = copy.deepcopy(self[k])
+ self.copy_attributes(res, deep=True)
+ return res
+
class CommentedOrderedMap(CommentedMap):
__slots__ = Comment.attrib,