[rtems-central commit] items: Use CSafeLoader if available

Sebastian Huber sebh at rtems.org
Tue May 9 13:45:26 UTC 2023


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

Author:    Sebastian Huber <sebastian.huber at embedded-brains.de>
Date:      Fri May  5 14:41:19 2023 +0200

items: Use CSafeLoader if available

---

 rtemsspec/items.py                      | 7 ++++++-
 rtemsspec/tests/test_items_itemcache.py | 6 +-----
 2 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/rtemsspec/items.py b/rtemsspec/items.py
index 1bc29dec..9beb180d 100644
--- a/rtemsspec/items.py
+++ b/rtemsspec/items.py
@@ -37,6 +37,11 @@ from typing import Any, Callable, Dict, Iterable, Iterator, List, Match, \
 import json
 import yaml
 
+try:
+    from yaml import CSafeLoader as SafeLoader
+except ImportError:  # pragma: no cover
+    from yaml import SafeLoader  # type: ignore
+
 
 class ItemGetValueContext(NamedTuple):
     """ Context used to get an item value. """
@@ -681,7 +686,7 @@ def _gather_spec_refinements(item: Item) -> Optional[_SpecType]:
 def _load_yaml_data(path: str, uid: str) -> Any:
     with open(path, "r", encoding="utf-8") as src:
         try:
-            data = yaml.safe_load(src.read())
+            data = yaml.load(src.read(), Loader=SafeLoader)
         except yaml.YAMLError as err:
             msg = ("YAML error while loading specification item file "
                    f"'{path}': {str(err)}")
diff --git a/rtemsspec/tests/test_items_itemcache.py b/rtemsspec/tests/test_items_itemcache.py
index 6cb35e3e..ba1ed859 100644
--- a/rtemsspec/tests/test_items_itemcache.py
+++ b/rtemsspec/tests/test_items_itemcache.py
@@ -108,11 +108,7 @@ def test_load_link_error(tmpdir):
 def test_load_yaml_error(tmpdir):
     config = create_item_cache_config_and_copy_spec(tmpdir,
                                                     "spec-item-cache-3")
-    match = r"""YAML 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:
-    :
-    \^"""
+    match = r"YAML error while loading specification item file '.*invalid.yml': while parsing a block mapping"
     with pytest.raises(IOError, match=match):
         ItemCache(config)
 



More information about the vc mailing list