Skip to content

Commit 8f208ea

Browse files
committed
Catch yaml syntax errors in core files
1 parent 322d421 commit 8f208ea

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

fusesoc/utils.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,17 +159,17 @@ def yaml_fread(filepath, resolve_env_vars=False, remove_preamble=False):
159159
with open(filepath) as f:
160160
if remove_preamble:
161161
f.readline()
162-
if resolve_env_vars:
163-
return yaml.load(os.path.expandvars(f.read()), Loader=YamlLoader)
164-
else:
165-
return yaml.load(f.read(), Loader=YamlLoader)
162+
return yaml_read(f, resolve_env_vars)
166163

167164

168165
def yaml_read(data, resolve_env_vars=False):
169-
if resolve_env_vars:
170-
return yaml.load(os.path.expandvars(data.read()), Loader=YamlLoader)
171-
else:
172-
return yaml.load(data, Loader=YamlLoader)
166+
try:
167+
if resolve_env_vars:
168+
return yaml.load(os.path.expandvars(data.read()), Loader=YamlLoader)
169+
else:
170+
return yaml.load(data, Loader=YamlLoader)
171+
except (yaml.parser.ParserError, yaml.scanner.ScannerError) as e:
172+
raise SyntaxError(str(e))
173173

174174

175175
def yaml_dump(data):
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CAPI=2:
2+
name : ::syntax_error:0
3+
4+
targets:
5+
parse_error:
6+
toplevel : [

tests/test_capi2.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,3 +723,14 @@ def test_core2parser():
723723
capi2_readback_data = parser.read(tmpfile)
724724
os.remove(tmpfile)
725725
assert capi2_data == capi2_readback_data
726+
727+
728+
def test_syntax_error():
729+
from fusesoc.capi2.coreparser import Core2Parser
730+
731+
core_file = os.path.join(tests_dir, "capi2_cores", "misc", "syntax_error.core")
732+
733+
parser = Core2Parser()
734+
with pytest.raises(SyntaxError) as excinfo:
735+
parser.read(core_file)
736+
assert "did not find expected node content" in str(excinfo.value)

0 commit comments

Comments
 (0)