Skip to content

Commit 0ea73b6

Browse files
sara hartseshartse
authored andcommitted
Verify input types for PrettyPrinter with a generator
1 parent d4b879d commit 0ea73b6

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

sdb/command.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -634,24 +634,31 @@ def pretty_print(self, objs: Iterable[drgn.Object]) -> None:
634634
# pylint: disable=missing-docstring
635635
raise NotImplementedError
636636

637-
def _call( # type: ignore[return]
638-
self,
639-
objs: Iterable[drgn.Object]) -> Optional[Iterable[drgn.Object]]:
637+
def check_input_type(self,
638+
objs: Iterable[drgn.Object]) -> Iterable[drgn.Object]:
640639
"""
641-
This function will call pretty_print() on each input object,
642-
verifying the types as we go.
640+
This function acts as a generator, checking that each passed object
641+
matches the input type for the command
643642
"""
644-
645643
assert self.input_type is not None
646644
type_name = type_canonicalize_name(self.input_type)
647645
for obj in objs:
648646
if type_canonical_name(obj.type_) != type_name:
649647
raise CommandError(
650648
self.name,
651-
f'exepected input of type {self.input_type}, but received '
649+
f'expected input of type {self.input_type}, but received '
652650
f'type {obj.type_}')
651+
yield obj
653652

654-
self.pretty_print([obj])
653+
def _call( # type: ignore[return]
654+
self,
655+
objs: Iterable[drgn.Object]) -> Optional[Iterable[drgn.Object]]:
656+
"""
657+
This function will call pretty_print() on each input object,
658+
verifying the types as we go.
659+
"""
660+
assert self.input_type is not None
661+
self.pretty_print(self.check_input_type(objs))
655662

656663

657664
class Locator(Command):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sdb: range_tree: expected input of type range_tree_t *, but received type spa_t *

tests/integration/test_core_generic.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@
168168

169169
# ptype - bogus type
170170
"ptype bogus_t",
171+
172+
# pretty printer passed incorrect type
173+
"spa | range_tree"
171174
]
172175

173176
CMD_TABLE = POS_CMDS + NEG_CMDS

0 commit comments

Comments
 (0)