summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnthon van der Neut <anthon@mnt.org>2021-08-21 14:20:29 +0200
committerAnthon van der Neut <anthon@mnt.org>2021-08-21 14:20:29 +0200
commitd97574a94bca9a6b62511ee14bb633a5fdc2e32e (patch)
tree70f59ec02c9336f107c18fd38f2c017dc42de1af
parent15e7b487ef04fe617ec695cf7980763b0b8e170f (diff)
downloadruamel.yaml-d97574a94bca9a6b62511ee14bb633a5fdc2e32e.tar.gz
fix for issue 393
-rw-r--r--constructor.py11
-rw-r--r--representer.py12
2 files changed, 23 insertions, 0 deletions
diff --git a/constructor.py b/constructor.py
index 563f07b..3cc76a3 100644
--- a/constructor.py
+++ b/constructor.py
@@ -1636,6 +1636,17 @@ class RoundTripConstructor(SafeConstructor):
else:
state = SafeConstructor.construct_mapping(self, node)
data.__dict__.update(state)
+ if node.anchor:
+ from ruamel.yaml.serializer import templated_id
+ from ruamel.yaml.anchor import Anchor
+
+ if not templated_id(node.anchor):
+ if not hasattr(data, Anchor.attrib):
+ a = Anchor()
+ setattr(data, Anchor.attrib, a)
+ else:
+ a = getattr(data, Anchor.attrib)
+ a.value = node.anchor
def construct_yaml_omap(self, node):
# type: (Any) -> Any
diff --git a/representer.py b/representer.py
index a27f457..f0aea0a 100644
--- a/representer.py
+++ b/representer.py
@@ -26,6 +26,7 @@ from ruamel.yaml.scalarint import ScalarInt, BinaryInt, OctalInt, HexInt, HexCap
from ruamel.yaml.scalarfloat import ScalarFloat
from ruamel.yaml.scalarbool import ScalarBoolean
from ruamel.yaml.timestamp import TimeStamp
+from ruamel.yaml.anchor import Anchor
import datetime
import sys
@@ -1080,6 +1081,17 @@ class RoundTripRepresenter(SafeRepresenter):
anchor = None
return SafeRepresenter.represent_bool(self, data, anchor=anchor)
+ def represent_yaml_object(self, tag, data, cls, flow_style=None):
+ if hasattr(data, '__getstate__'):
+ state = data.__getstate__()
+ else:
+ state = data.__dict__.copy()
+ anchor = state.pop(Anchor.attrib, None)
+ res = self.represent_mapping(tag, state, flow_style=flow_style)
+ if anchor is not None:
+ res.anchor = anchor
+ return res
+
RoundTripRepresenter.add_representer(type(None), RoundTripRepresenter.represent_none)