Skip to content

Commit 84b7edd

Browse files
committed
dtlib: (cosmetic) use filename and line number on all messages
Take advantage of the new 'filename' and 'lineno' attributes of Node and Property objects to provide more informative error and status messages by printing the actual source file reference of the corresponding object, always using the unquoted file:line format in error messages. No functional change is introduced by this commit. Signed-off-by: Luca Burelli <l.burelli@arduino.cc>
1 parent e7d07ee commit 84b7edd

File tree

3 files changed

+21
-21
lines changed

3 files changed

+21
-21
lines changed

scripts/dts/python-devicetree/src/devicetree/dtlib.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def __repr__(self):
247247
Returns some information about the Node instance. Called automatically
248248
if the Node instance is evaluated.
249249
"""
250-
return f"<Node {self.path} in '{self.dt.filename}'>"
250+
return f"<Node {self.path} in {self.filename}:{self.lineno}>"
251251

252252
# See Property.type
253253
class Type(enum.IntEnum):
@@ -437,7 +437,7 @@ def to_num(self, signed=False) -> int:
437437
"""
438438
if self.type is not Type.NUM:
439439
_err(f"expected property '{self.name}' on {self.node.path} in "
440-
f"{self.node.dt.filename} to be assigned with "
440+
f"{self.filename}:{self.lineno} to be assigned with "
441441
f"'{self.name} = < (number) >;', not '{self}'")
442442

443443
return int.from_bytes(self.value, "big", signed=signed)
@@ -457,7 +457,7 @@ def to_nums(self, signed=False) -> list[int]:
457457
"""
458458
if self.type not in (Type.NUM, Type.NUMS):
459459
_err(f"expected property '{self.name}' on {self.node.path} in "
460-
f"{self.node.dt.filename} to be assigned with "
460+
f"{self.filename}:{self.lineno} to be assigned with "
461461
f"'{self.name} = < (number) (number) ... >;', not '{self}'")
462462

463463
return [int.from_bytes(self.value[i:i + 4], "big", signed=signed)
@@ -475,7 +475,7 @@ def to_bytes(self) -> bytes:
475475
"""
476476
if self.type is not Type.BYTES:
477477
_err(f"expected property '{self.name}' on {self.node.path} "
478-
f"in {self.node.dt.filename} to be assigned with "
478+
f"in {self.filename}:{self.lineno} to be assigned with "
479479
f"'{self.name} = [ (byte) (byte) ... ];', not '{self}'")
480480

481481
return self.value
@@ -494,14 +494,14 @@ def to_string(self) -> str:
494494
"""
495495
if self.type is not Type.STRING:
496496
_err(f"expected property '{self.name}' on {self.node.path} "
497-
f"in {self.node.dt.filename} to be assigned with "
497+
f"in {self.filename}:{self.lineno} to be assigned with "
498498
f"'{self.name} = \"string\";', not '{self}'")
499499

500500
try:
501501
ret = self.value.decode("utf-8")[:-1] # Strip null
502502
except UnicodeDecodeError:
503503
_err(f"value of property '{self.name}' ({self.value!r}) "
504-
f"on {self.node.path} in {self.node.dt.filename} "
504+
f"on {self.node.path} in {self.filename}:{self.lineno} "
505505
"is not valid UTF-8")
506506

507507
return ret # The separate 'return' appeases the type checker.
@@ -519,14 +519,14 @@ def to_strings(self) -> list[str]:
519519
"""
520520
if self.type not in (Type.STRING, Type.STRINGS):
521521
_err(f"expected property '{self.name}' on {self.node.path} in "
522-
f"{self.node.dt.filename} to be assigned with "
522+
f"{self.filename}:{self.lineno} to be assigned with "
523523
f"'{self.name} = \"string\", \"string\", ... ;', not '{self}'")
524524

525525
try:
526526
ret = self.value.decode("utf-8").split("\0")[:-1]
527527
except UnicodeDecodeError:
528528
_err(f"value of property '{self.name}' ({self.value!r}) "
529-
f"on {self.node.path} in {self.node.dt.filename} "
529+
f"on {self.node.path} in {self.filename}:{self.lineno} "
530530
"is not valid UTF-8")
531531

532532
return ret # The separate 'return' appeases the type checker.
@@ -542,7 +542,7 @@ def to_node(self) -> Node:
542542
"""
543543
if self.type is not Type.PHANDLE:
544544
_err(f"expected property '{self.name}' on {self.node.path} in "
545-
f"{self.node.dt.filename} to be assigned with "
545+
f"{self.filename}:{self.lineno} to be assigned with "
546546
f"'{self.name} = < &foo >;', not '{self}'")
547547

548548
return self.node.dt.phandle2node[int.from_bytes(self.value, "big")]
@@ -567,7 +567,7 @@ def type_ok():
567567

568568
if not type_ok():
569569
_err(f"expected property '{self.name}' on {self.node.path} in "
570-
f"{self.node.dt.filename} to be assigned with "
570+
f"{self.filename}:{self.lineno} to be assigned with "
571571
f"'{self.name} = < &foo &bar ... >;', not '{self}'")
572572

573573
return [self.node.dt.phandle2node[int.from_bytes(self.value[i:i + 4],
@@ -588,23 +588,23 @@ def to_path(self) -> Node:
588588
"""
589589
if self.type not in (Type.PATH, Type.STRING):
590590
_err(f"expected property '{self.name}' on {self.node.path} in "
591-
f"{self.node.dt.filename} to be assigned with either "
591+
f"{self.filename}:{self.lineno} to be assigned with either "
592592
f"'{self.name} = &foo' or '{self.name} = \"/path/to/node\"', "
593593
f"not '{self}'")
594594

595595
try:
596596
path = self.value.decode("utf-8")[:-1]
597597
except UnicodeDecodeError:
598598
_err(f"value of property '{self.name}' ({self.value!r}) "
599-
f"on {self.node.path} in {self.node.dt.filename} "
599+
f"on {self.node.path} in {self.filename}:{self.lineno} "
600600
"is not valid UTF-8")
601601

602602
try:
603603
ret = self.node.dt.get_node(path)
604604
except DTError:
605605
_err(f"property '{self.name}' on {self.node.path} in "
606-
f"{self.node.dt.filename} points to the non-existent node "
607-
f'"{path}"')
606+
f"{self.filename}:{self.lineno} points to the non-existent "
607+
f'node "{path}"')
608608

609609
return ret # The separate 'return' appeases the type checker.
610610

@@ -678,8 +678,8 @@ def __str__(self):
678678
return s + ";"
679679

680680
def __repr__(self):
681-
return (f"<Property '{self.name}' at '{self.node.path}' in "
682-
f"'{self.node.dt.filename}'>")
681+
return (f"<Property '{self.name}' on {self.node.path} in "
682+
f"{self.filename}:{self.lineno}>")
683683

684684
#
685685
# Internal functions

scripts/dts/python-devicetree/tests/test_dtlib.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2166,9 +2166,9 @@ def test_reprs():
21662166

21672167
assert re.fullmatch(r"DT\(filename='.*', include_path=.'foo', 'bar'.\)",
21682168
repr(dt))
2169-
assert re.fullmatch("<Property 'x' at '/' in '.*'>",
2169+
assert re.fullmatch("<Property 'x' on / in .*:5>",
21702170
repr(dt.root.props["x"]))
2171-
assert re.fullmatch("<Node /sub in '.*'>",
2171+
assert re.fullmatch("<Node /sub in .*:6>",
21722172
repr(dt.root.nodes["sub"]))
21732173

21742174
dt = parse(dts, include_path=iter(("foo", "bar")))

scripts/dts/python-devicetree/tests/test_edtlib.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,7 @@ def test_slice_errs(tmp_path):
826826
};
827827
""",
828828
dts_file,
829-
f"'reg' property in <Node /sub in '{dts_file}'> has length 4, which is not evenly divisible by 12 (= 4*(<#address-cells> (= 1) + <#size-cells> (= 2))). Note that #*-cells properties come either from the parent node or from the controller (in the case of 'interrupts').")
829+
f"'reg' property in <Node /sub in {dts_file}:8> has length 4, which is not evenly divisible by 12 (= 4*(<#address-cells> (= 1) + <#size-cells> (= 2))). Note that #*-cells properties come either from the parent node or from the controller (in the case of 'interrupts').")
830830

831831
verify_error("""
832832
/dts-v1/;
@@ -843,7 +843,7 @@ def test_slice_errs(tmp_path):
843843
};
844844
""",
845845
dts_file,
846-
f"'interrupts' property in <Node /sub in '{dts_file}'> has length 4, which is not evenly divisible by 8 (= 4*<#interrupt-cells>). Note that #*-cells properties come either from the parent node or from the controller (in the case of 'interrupts').")
846+
f"'interrupts' property in <Node /sub in {dts_file}:5> has length 4, which is not evenly divisible by 8 (= 4*<#interrupt-cells>). Note that #*-cells properties come either from the parent node or from the controller (in the case of 'interrupts').")
847847

848848
verify_error("""
849849
/dts-v1/;
@@ -863,7 +863,7 @@ def test_slice_errs(tmp_path):
863863
};
864864
""",
865865
dts_file,
866-
f"'ranges' property in <Node /sub-1 in '{dts_file}'> has length 8, which is not evenly divisible by 24 (= 4*(<#address-cells> (= 2) + <#address-cells for parent> (= 1) + <#size-cells> (= 3))). Note that #*-cells properties come either from the parent node or from the controller (in the case of 'interrupts').")
866+
f"'ranges' property in <Node /sub-1 in {dts_file}:7> has length 8, which is not evenly divisible by 24 (= 4*(<#address-cells> (= 2) + <#address-cells for parent> (= 1) + <#size-cells> (= 3))). Note that #*-cells properties come either from the parent node or from the controller (in the case of 'interrupts').")
867867

868868
def test_bad_compatible(tmp_path):
869869
# An invalid compatible should cause an error, even on a node with

0 commit comments

Comments
 (0)