Commit ac17fe72 authored by Daniel Silverstone's avatar Daniel Silverstone

_yaml.py::node_get(): Only construct path on error

We don't need to spend the CPU constructing path except when there is
an error which needs it, so don't.
Signed-off-by: Daniel Silverstone's avatarDaniel Silverstone <daniel.silverstone@codethink.co.uk>
parent 906612a0
Pipeline #54623635 passed with stages
in 53 minutes and 11 seconds
......@@ -452,7 +452,6 @@ _sentinel = object()
def node_get(node, expected_type, key, indices=None, *, default_value=_sentinel, allow_none=False):
assert type(node) is Node
path = key
if indices is None:
if default_value is _sentinel:
value = node[0].get(key, Node(default_value, None, 0, 0))
......@@ -471,7 +470,6 @@ def node_get(node, expected_type, key, indices=None, *, default_value=_sentinel,
value = value[0][index]
if type(value) is not Node:
value = (value,)
path += '[{:d}]'.format(index)
# Optionally allow None as a valid value for any type
if value[0] is None and (allow_none or default_value is None):
......@@ -499,6 +497,12 @@ def node_get(node, expected_type, key, indices=None, *, default_value=_sentinel,
raise ValueError()
except (ValueError, TypeError):
provenance = node_get_provenance(node, key=key, indices=indices)
if indices:
path = [key]
path.extend("[{:d}]".format(i) for i in indices)
path = "".join(path)
else:
path = key
raise LoadError(LoadErrorReason.INVALID_DATA,
"{}: Value of '{}' is not of the expected type '{}'"
.format(provenance, path, expected_type.__name__))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment