dtlib: fix include_path edge case

The documentation says DT.__init__ takes any iterable for the
include_path, but this leads to bad results when you pass it something
other than a 'real' sequence (list/tuple/etc), like a generator:

>>> dt = DT('/tmp/foo.dts', (x for x in ['a', 'b', 'c']))
>>> repr(dt)
"DT(filename='/tmp/foo.dts', include_path=<generator object ...>)"

Make a copy in list form just to avoid things like this.

Add a test for this and relax the regular expression in the existing
test case related to this.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
This commit is contained in:
Martí Bolívar 2021-04-23 13:53:19 -07:00 committed by Kumar Gala
commit b13b5b8b3a
2 changed files with 11 additions and 5 deletions

View file

@ -708,7 +708,7 @@ class DT:
relative to the .dts file that contains the /include/ or /incbin/.
"""
self.filename = filename
self._include_path = include_path
self._include_path = list(include_path)
with open(filename, encoding="utf-8") as f:
self._file_contents = f.read()

View file

@ -2077,7 +2077,7 @@ foo: / {
def test_reprs():
'''Test the __repr__() functions.'''
dt = parse("""
dts = """
/dts-v1/;
/ {
@ -2086,16 +2086,22 @@ def test_reprs():
y = < 1 >;
};
};
""",
include_path=("foo", "bar"))
"""
assert re.fullmatch(r"DT\(filename='.*', include_path=\('foo', 'bar'\)\)",
dt = parse(dts, include_path=("foo", "bar"))
assert re.fullmatch(r"DT\(filename='.*', include_path=.'foo', 'bar'.\)",
repr(dt))
assert re.fullmatch("<Property 'x' at '/' in '.*'>",
repr(dt.root.props["x"]))
assert re.fullmatch("<Node /sub in '.*'>",
repr(dt.root.nodes["sub"]))
dt = parse(dts, include_path=iter(("foo", "bar")))
assert re.fullmatch(r"DT\(filename='.*', include_path=.'foo', 'bar'.\)",
repr(dt))
def test_names():
'''Tests for node/property names.'''