Skip to content

Commit 57bfdb6

Browse files
author
sara hartse
committed
Verify input types for PrettyPrinter with a generator
1 parent d4b879d commit 57bfdb6

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

sdb/command.py

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

637-
def _call( # type: ignore[return]
637+
def check_input_type(
638638
self,
639-
objs: Iterable[drgn.Object]) -> Optional[Iterable[drgn.Object]]:
639+
objs: Iterable[drgn.Object]) -> Iterable[drgn.Object]:
640640
"""
641-
This function will call pretty_print() on each input object,
642-
verifying the types as we go.
641+
This function acts as a generator, checking that each passed object
642+
matches the input type for the command
643643
"""
644-
645644
assert self.input_type is not None
646645
type_name = type_canonicalize_name(self.input_type)
647646
for obj in objs:
648647
if type_canonical_name(obj.type_) != type_name:
649648
raise CommandError(
650649
self.name,
651-
f'exepected input of type {self.input_type}, but received '
650+
f'expected input of type {self.input_type}, but received '
652651
f'type {obj.type_}')
652+
yield obj
653653

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

656664

657665
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)