Skip to content

Commit

Permalink
Catch yaml syntax errors in core files
Browse files Browse the repository at this point in the history
  • Loading branch information
olofk committed Sep 17, 2023
1 parent 322d421 commit 8f208ea
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
16 changes: 8 additions & 8 deletions fusesoc/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,17 +159,17 @@ def yaml_fread(filepath, resolve_env_vars=False, remove_preamble=False):
with open(filepath) as f:
if remove_preamble:
f.readline()
if resolve_env_vars:
return yaml.load(os.path.expandvars(f.read()), Loader=YamlLoader)
else:
return yaml.load(f.read(), Loader=YamlLoader)
return yaml_read(f, resolve_env_vars)


def yaml_read(data, resolve_env_vars=False):
if resolve_env_vars:
return yaml.load(os.path.expandvars(data.read()), Loader=YamlLoader)
else:
return yaml.load(data, Loader=YamlLoader)
try:
if resolve_env_vars:
return yaml.load(os.path.expandvars(data.read()), Loader=YamlLoader)
else:
return yaml.load(data, Loader=YamlLoader)
except (yaml.parser.ParserError, yaml.scanner.ScannerError) as e:
raise SyntaxError(str(e))


def yaml_dump(data):
Expand Down
6 changes: 6 additions & 0 deletions tests/capi2_cores/misc/syntax_error.core
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CAPI=2:
name : ::syntax_error:0

targets:
parse_error:
toplevel : [
11 changes: 11 additions & 0 deletions tests/test_capi2.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,3 +723,14 @@ def test_core2parser():
capi2_readback_data = parser.read(tmpfile)
os.remove(tmpfile)
assert capi2_data == capi2_readback_data


def test_syntax_error():
from fusesoc.capi2.coreparser import Core2Parser

core_file = os.path.join(tests_dir, "capi2_cores", "misc", "syntax_error.core")

parser = Core2Parser()
with pytest.raises(SyntaxError) as excinfo:
parser.read(core_file)
assert "did not find expected node content" in str(excinfo.value)

0 comments on commit 8f208ea

Please sign in to comment.