[rtems-central commit] items: Improve YAML parser error messages

Sebastian Huber sebh at rtems.org
Fri Nov 20 13:04:20 UTC 2020


Module:    rtems-central
Branch:    master
Commit:    805ebf0e54a3f730e0d61fff7bb18037198b2ed3
Changeset: http://git.rtems.org/rtems-central/commit/?id=805ebf0e54a3f730e0d61fff7bb18037198b2ed3

Author:    Sebastian Huber <sebastian.huber at embedded-brains.de>
Date:      Fri Nov 20 14:03:06 2020 +0100

items: Improve YAML parser error messages

---

 rtemsspec/items.py                            |  7 ++++++-
 rtemsspec/tests/spec-item-cache-3/invalid.yml |  1 +
 rtemsspec/tests/test_items_itemcache.py       | 12 ++++++++++++
 3 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/rtemsspec/items.py b/rtemsspec/items.py
index 6e87245..5b7a95a 100644
--- a/rtemsspec/items.py
+++ b/rtemsspec/items.py
@@ -497,7 +497,12 @@ def _gather_spec_refinements(item: Item) -> Optional[_SpecType]:
 
 def _load_item(path: str, uid: str) -> Any:
     with open(path, "r") as src:
-        data = yaml.safe_load(src.read())
+        try:
+            data = yaml.safe_load(src.read())
+        except yaml.parser.ParserError as err:
+            msg = ("YAML parser error while loading specification item file "
+                   f"'{path}': {str(err)}")
+            raise IOError(msg) from err
         data["_file"] = os.path.abspath(path)
         data["_uid"] = uid
     return data
diff --git a/rtemsspec/tests/spec-item-cache-3/invalid.yml b/rtemsspec/tests/spec-item-cache-3/invalid.yml
new file mode 100644
index 0000000..397db75
--- /dev/null
+++ b/rtemsspec/tests/spec-item-cache-3/invalid.yml
@@ -0,0 +1 @@
+:
diff --git a/rtemsspec/tests/test_items_itemcache.py b/rtemsspec/tests/test_items_itemcache.py
index cb0969e..05affc2 100644
--- a/rtemsspec/tests/test_items_itemcache.py
+++ b/rtemsspec/tests/test_items_itemcache.py
@@ -84,6 +84,18 @@ def test_load_link_error(tmpdir):
         ItemCache(config)
 
 
+def test_load_parser_error(tmpdir):
+    config = create_item_cache_config_and_copy_spec(tmpdir,
+                                                    "spec-item-cache-3")
+    match = r"""YAML parser error while loading specification item file '.*invalid.yml': while parsing a block mapping
+expected <block end>, but found ':'
+  in "<unicode string>", line 1, column 1:
+    :
+    \^"""
+    with pytest.raises(IOError, match=match):
+        ItemCache(config)
+
+
 class Mapper(ItemMapper):
     def __init__(self, item):
         super().__init__(item)



More information about the vc mailing list