diff --git a/fusesoc/utils.py b/fusesoc/utils.py index 7c7e20ea..8c7176c2 100644 --- a/fusesoc/utils.py +++ b/fusesoc/utils.py @@ -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): diff --git a/tests/capi2_cores/misc/syntax_error.core b/tests/capi2_cores/misc/syntax_error.core new file mode 100644 index 00000000..ea104f3b --- /dev/null +++ b/tests/capi2_cores/misc/syntax_error.core @@ -0,0 +1,6 @@ +CAPI=2: +name : ::syntax_error:0 + +targets: + parse_error: + toplevel : [ diff --git a/tests/test_capi2.py b/tests/test_capi2.py index f58ea26b..58b0df88 100644 --- a/tests/test_capi2.py +++ b/tests/test_capi2.py @@ -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)