diff configmix/yaml.py @ 134:2f2e819e8d17

Check the return type of the JSON and YAML loading functions: they must be a dict alike
author Franz Glasner <hg@dom66.de>
date Thu, 05 Apr 2018 09:12:29 +0200
parents 99e7b10c8aa8
children b7b0cea8ec6e
line wrap: on
line diff
--- a/configmix/yaml.py	Wed Apr 04 23:51:07 2018 +0200
+++ b/configmix/yaml.py	Thu Apr 05 09:12:29 2018 +0200
@@ -151,16 +151,34 @@
 
 
 def load(stream, Loader=ConfigLoader):
-    return yaml.load(stream, Loader)
+    data = yaml.load(stream, Loader)
+    if OrderedDict:
+        if not isinstance(data, OrderedDict):
+            raise TypeError("YAML root object must be a mapping")
+    return data
 
 
 def load_all(stream, Loader=ConfigLoader):
-    return yaml.load_all(stream, Loader)
+    data_all = yaml.load_all(stream, Loader)
+    if OrderedDict:
+        for data in data_all:
+            if not isinstance(data, OrderedDict):
+                raise TypeError("YAML root object must be a mapping")
+    return data_all
 
 
 def safe_load(stream):
-    return yaml.load(stream, Loader=ConfigSafeLoader)
+    data = yaml.load(stream, Loader=ConfigSafeLoader)
+    if OrderedDict:
+        if not isinstance(data, OrderedDict):
+            raise TypeError("YAML root object must be a mapping")
+    return data
 
 
 def safe_load_all(stream):
-    return yaml.load_all(stream, Loader=ConfigSafeLoader)
+    data_all = yaml.load_all(stream, Loader=ConfigSafeLoader)
+    if OrderedDict:
+        for data in data_all:
+            if not isinstance(data, OrderedDict):
+                raise TypeError("YAML root object must be a mapping")
+    return data_all