Blob Blame History Raw
From c4b3c8976c7eeeaf960f6116af0fbf5af7cd3b3e Mon Sep 17 00:00:00 2001
From: Orion Poplawski <orion@nwra.com>
Date: Sun, 17 Jul 2022 21:05:34 -0600
Subject: [PATCH] Use updated ruamel.yaml API

---
 conda/common/serialize.py | 52 +++++++++++++++++++++++++--------------
 conda_env/env.py          |  6 +----
 2 files changed, 34 insertions(+), 24 deletions(-)

diff --git a/conda/common/serialize.py b/conda/common/serialize.py
index d850ce6700..43153f1bcd 100644
--- a/conda/common/serialize.py
+++ b/conda/common/serialize.py
@@ -20,14 +20,15 @@ def get_yaml():
     except ImportError:  # pragma: no cover
         try:
             import ruamel.yaml as yaml
+            from ruamel.yaml.compat import StringIO
         except ImportError:
             raise ImportError("No yaml library available.\n"
                               "To proceed, conda install "
                               "ruamel_yaml")
-    return yaml
+    return (yaml, StringIO)
 
 
-yaml = get_yaml()
+(yaml, StringIO) = get_yaml()
 
 
 def represent_ordereddict(dumper, data):
@@ -47,7 +48,8 @@ def represent_ordereddict(dumper, data):
 
 
 def yaml_round_trip_load(string):
-    return yaml.round_trip_load(string, version="1.2")
+    yinst = yaml.YAML(typ='rt')
+    return yinst.load(string)
 
 
 def yaml_safe_load(string):
@@ -57,22 +59,34 @@ def yaml_safe_load(string):
         {'key': 'value'}
 
     """
-    return yaml.safe_load(string, version="1.2")
-
-
-def yaml_round_trip_dump(object):
-    """dump object to string"""
-    return yaml.round_trip_dump(
-        object, block_seq_indent=2, default_flow_style=False, indent=2
-    )
-
-
-def yaml_safe_dump(object):
-    """dump object to string"""
-    return yaml.safe_dump(
-        object, block_seq_indent=2, default_flow_style=False, indent=2
-    )
-
+    yinst=yaml.YAML(typ='safe', pure=True)
+    return yinst.load(string)
+
+
+def yaml_round_trip_dump(object, stream=None):
+    """dump object to string or stream"""
+    yinst = yaml.YAML(typ='rt')
+    yinst.indent(mapping=2, offset=2, sequence=4)
+    inefficient = False
+    if stream is None:
+        inefficient = True
+        stream = StringIO()
+    yinst.dump(object, stream)
+    if inefficient:
+        return stream.getvalue()
+
+def yaml_safe_dump(object, stream=None):
+    """dump object to string or stream"""
+    yinst=yaml.YAML(typ='safe', pure=True)
+    yinst.indent(mapping=2, offset=2, sequence=4)
+    yinst.default_flow_style = False
+    inefficient = False
+    if stream is None:
+        inefficient = True
+        stream = StringIO()
+    yinst.dump(object, stream)
+    if inefficient:
+        return stream.getvalue()
 
 def json_load(string):
     return json.loads(string)
diff --git a/conda_env/env.py b/conda_env/env.py
index c494b3d855..142731ada9 100644
--- a/conda_env/env.py
+++ b/conda_env/env.py
@@ -257,13 +257,9 @@ def to_dict(self, stream=None):
 
     def to_yaml(self, stream=None):
         d = self.to_dict()
-        out = yaml_safe_dump(d)
+        out = yaml_safe_dump(d, stream)
         if stream is None:
             return out
-        try:
-            stream.write(bytes(out, encoding="utf-8"))
-        except TypeError:
-            stream.write(out)
 
     def save(self):
         with open(self.filename, "wb") as fp: