Skip to content

Commit

Permalink
Verify input types for PrettyPrinter with a generator
Browse files Browse the repository at this point in the history
  • Loading branch information
sara hartse committed Mar 3, 2020
1 parent d4b879d commit 57bfdb6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
22 changes: 15 additions & 7 deletions sdb/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,24 +634,32 @@ def pretty_print(self, objs: Iterable[drgn.Object]) -> None:
# pylint: disable=missing-docstring
raise NotImplementedError

def _call( # type: ignore[return]
def check_input_type(
self,
objs: Iterable[drgn.Object]) -> Optional[Iterable[drgn.Object]]:
objs: Iterable[drgn.Object]) -> Iterable[drgn.Object]:
"""
This function will call pretty_print() on each input object,
verifying the types as we go.
This function acts as a generator, checking that each passed object
matches the input type for the command
"""

assert self.input_type is not None
type_name = type_canonicalize_name(self.input_type)
for obj in objs:
if type_canonical_name(obj.type_) != type_name:
raise CommandError(
self.name,
f'exepected input of type {self.input_type}, but received '
f'expected input of type {self.input_type}, but received '
f'type {obj.type_}')
yield obj

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


class Locator(Command):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sdb: range_tree: expected input of type range_tree_t *, but received type spa_t *
3 changes: 3 additions & 0 deletions tests/integration/test_core_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@

# ptype - bogus type
"ptype bogus_t",

# pretty printer passed incorrect type
"spa | range_tree"
]

CMD_TABLE = POS_CMDS + NEG_CMDS
Expand Down

0 comments on commit 57bfdb6

Please sign in to comment.