Skip to content

Commit b2f4321

Browse files
LukaszMrugalaaescolar
authored andcommitted
scripts: Loader change
CSafeLoader used instead of yaml.safe_load and SafeLoader. C implementation is faster. Signed-off-by: Lukasz Mrugala <lukaszx.mrugala@intel.com>
1 parent 3d00574 commit b2f4321

File tree

7 files changed

+38
-14
lines changed

7 files changed

+38
-14
lines changed

scripts/list_boards.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@
1515
import list_hardware
1616
from list_hardware import unique_paths
1717

18+
try:
19+
from yaml import CSafeLoader as SafeLoader
20+
except ImportError:
21+
from yaml import SafeLoader
22+
1823
BOARD_SCHEMA_PATH = str(Path(__file__).parent / 'schemas' / 'board-schema.yml')
1924
with open(BOARD_SCHEMA_PATH, 'r') as f:
20-
board_schema = yaml.safe_load(f.read())
25+
board_schema = yaml.load(f.read(), Loader=SafeLoader)
2126

2227
BOARD_YML = 'board.yml'
2328

@@ -178,7 +183,7 @@ def load_v2_boards(board_name, board_yml, systems):
178183
boards = []
179184
if board_yml.is_file():
180185
with board_yml.open('r') as f:
181-
b = yaml.safe_load(f.read())
186+
b = yaml.load(f.read(), Loader=SafeLoader)
182187

183188
try:
184189
pykwalify.core.Core(source_data=b, schema_data=board_schema).validate()

scripts/list_hardware.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,19 @@
1212
import yaml
1313
import re
1414

15+
try:
16+
from yaml import CSafeLoader as SafeLoader
17+
except ImportError:
18+
from yaml import SafeLoader
19+
1520

1621
SOC_SCHEMA_PATH = str(Path(__file__).parent / 'schemas' / 'soc-schema.yml')
1722
with open(SOC_SCHEMA_PATH, 'r') as f:
18-
soc_schema = yaml.safe_load(f.read())
23+
soc_schema = yaml.load(f.read(), Loader=SafeLoader)
1924

2025
ARCH_SCHEMA_PATH = str(Path(__file__).parent / 'schemas' / 'arch-schema.yml')
2126
with open(ARCH_SCHEMA_PATH, 'r') as f:
22-
arch_schema = yaml.safe_load(f.read())
27+
arch_schema = yaml.load(f.read(), Loader=SafeLoader)
2328

2429
SOC_YML = 'soc.yml'
2530
ARCHS_YML_PATH = PurePath('arch/archs.yml')
@@ -35,7 +40,7 @@ def __init__(self, folder='', soc_yaml=None):
3540
return
3641

3742
try:
38-
data = yaml.safe_load(soc_yaml)
43+
data = yaml.load(soc_yaml, Loader=SafeLoader)
3944
pykwalify.core.Core(source_data=data,
4045
schema_data=soc_schema).validate()
4146
except (yaml.YAMLError, pykwalify.errors.SchemaError) as e:
@@ -188,7 +193,7 @@ def find_v2_archs(args):
188193

189194
if Path(archs_yml).is_file():
190195
with Path(archs_yml).open('r') as f:
191-
archs = yaml.safe_load(f.read())
196+
archs = yaml.load(f.read(), Loader=SafeLoader)
192197

193198
try:
194199
pykwalify.core.Core(source_data=archs, schema_data=arch_schema).validate()

scripts/pylib/twister/twisterlib/quarantine.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@
55

66
import logging
77
import re
8+
import yaml
89

910
from pathlib import Path
10-
from yaml import safe_load
1111
from dataclasses import dataclass, field
1212

13+
try:
14+
from yaml import CSafeLoader as SafeLoader
15+
except ImportError:
16+
from yaml import SafeLoader
17+
1318

1419
logger = logging.getLogger(__name__)
1520

@@ -88,7 +93,7 @@ def __post_init__(self):
8893
def load_data_from_yaml(cls, filename: str | Path) -> QuarantineData:
8994
"""Load quarantine from yaml file."""
9095
with open(filename, 'r', encoding='UTF-8') as yaml_fd:
91-
qlist_raw_data: list[dict] = safe_load(yaml_fd)
96+
qlist_raw_data: list[dict] = yaml.load(yaml_fd, Loader=SafeLoader)
9297
try:
9398
if not qlist_raw_data:
9499
# in case of loading empty quarantine file

scripts/pylib/twister/twisterlib/runner.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@
4646
from twisterlib.testplan import change_skip_to_error_if_integration
4747
from twisterlib.harness import HarnessImporter, Pytest
4848

49+
try:
50+
from yaml import CSafeLoader as SafeLoader
51+
except ImportError:
52+
from yaml import SafeLoader
53+
4954
logger = logging.getLogger('twister')
5055
logger.setLevel(logging.DEBUG)
5156
import expr_parser
@@ -861,7 +866,7 @@ def _get_binaries_from_runners(self, domain='') -> List[str]:
861866
return []
862867

863868
with open(runners_file_path, 'r') as file:
864-
runners_content: dict = yaml.safe_load(file)
869+
runners_content: dict = yaml.load(file, Loader=SafeLoader)
865870

866871
if 'config' not in runners_content:
867872
return []
@@ -901,7 +906,7 @@ def _sanitize_runners_file(self):
901906

902907
with open(runners_file_path, 'rt') as file:
903908
runners_content_text = file.read()
904-
runners_content_yaml: dict = yaml.safe_load(runners_content_text)
909+
runners_content_yaml: dict = yaml.load(runners_content_text, Loader=SafeLoader)
905910

906911
if 'config' not in runners_content_yaml:
907912
return

scripts/pylib/twister/twisterlib/testplan.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import copy
1717
import shutil
1818
import random
19-
2019
import snippets
2120
from pathlib import Path
2221
from argparse import Namespace

scripts/tests/twister/test_runner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1805,7 +1805,7 @@ def mock_exists(fname):
18051805

18061806
with mock.patch('os.path.exists', mock_exists), \
18071807
mock.patch('builtins.open', mock.mock_open()), \
1808-
mock.patch('yaml.safe_load', return_value=runners_content):
1808+
mock.patch('yaml.load', return_value=runners_content):
18091809
if domain:
18101810
bins = pb._get_binaries_from_runners(domain)
18111811
else:

scripts/zephyr_module.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
from pathlib import Path, PurePath
2929
from collections import namedtuple
3030

31+
try:
32+
from yaml import CSafeLoader as SafeLoader
33+
except ImportError:
34+
from yaml import SafeLoader
35+
3136
METADATA_SCHEMA = '''
3237
## A pykwalify schema for basic validation of the structure of a
3338
## metadata YAML file.
@@ -156,7 +161,7 @@
156161
BLOB_NOT_PRESENT = 'D'
157162
BLOB_OUTDATED = 'M'
158163

159-
schema = yaml.safe_load(METADATA_SCHEMA)
164+
schema = yaml.load(METADATA_SCHEMA, Loader=SafeLoader)
160165

161166

162167
def validate_setting(setting, module_path, filename=None):
@@ -180,7 +185,7 @@ def process_module(module):
180185
module_path / MODULE_YML_PATH.with_suffix('.yaml')]:
181186
if Path(module_yml).is_file():
182187
with Path(module_yml).open('r') as f:
183-
meta = yaml.safe_load(f.read())
188+
meta = yaml.load(f.read(), Loader=SafeLoader)
184189

185190
try:
186191
pykwalify.core.Core(source_data=meta, schema_data=schema)\

0 commit comments

Comments
 (0)