From bbe132092ed299275f9da7a86319fdf03b31861f Mon Sep 17 00:00:00 2001 From: Sebastian Huber Date: Fri, 5 May 2023 14:41:19 +0200 Subject: 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 , but found ':' - in "", 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) -- cgit v1.2.3