Skip to content

Commit 70209e7

Browse files
author
sara hartse
committed
Refactor PrettyPrinter type-checking and extend it to all commands
1 parent bf5de5c commit 70209e7

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

sdb/command.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,28 @@ def _call(self,
240240
"""
241241
raise NotImplementedError()
242242

243+
def __input_type_check(
244+
self, objs: Iterable[drgn.Object]) -> Iterable[drgn.Object]:
245+
valid_types = [type_canonicalize_name(self.input_type)]
246+
247+
#
248+
# Some commands support multiple input types. Check for InputHandler
249+
# implementations to expand the set of valid input types.
250+
#
251+
for (_, method) in inspect.getmembers(self, inspect.ismethod):
252+
if not hasattr(method, "input_typename_handled"):
253+
continue
254+
valid_types.append(
255+
type_canonicalize_name(method.input_typename_handled))
256+
257+
for obj in objs:
258+
if type_canonical_name(obj.type_) not in valid_types:
259+
raise CommandError(
260+
self.name,
261+
f'exepected input of type {self.input_type}, but received '
262+
f'type {obj.type_}')
263+
yield obj
264+
243265
def __invalid_memory_objects_check(self, objs: Iterable[drgn.Object],
244266
fatal: bool) -> Iterable[drgn.Object]:
245267
"""
@@ -277,7 +299,11 @@ def call(self, objs: Iterable[drgn.Object]) -> Iterable[drgn.Object]:
277299
# the command is running.
278300
#
279301
try:
280-
result = self._call(objs)
302+
if self.input_type and objs:
303+
result = self._call(self.__input_type_check(objs))
304+
else:
305+
result = self._call(objs)
306+
281307
if result is not None:
282308
#
283309
# The whole point of the SingleInputCommands are that
@@ -637,17 +663,7 @@ def _call( # type: ignore[return]
637663
This function will call pretty_print() on each input object,
638664
verifying the types as we go.
639665
"""
640-
641-
assert self.input_type is not None
642-
type_name = type_canonicalize_name(self.input_type)
643-
for obj in objs:
644-
if type_canonical_name(obj.type_) != type_name:
645-
raise CommandError(
646-
self.name,
647-
f'exepected input of type {self.input_type}, but received '
648-
f'type {obj.type_}')
649-
650-
self.pretty_print([obj])
666+
self.pretty_print(objs)
651667

652668

653669
class Locator(Command):
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
sdb: cpu_counter_sum: input is not a percpu_counter
1+
sdb: cpu_counter_sum: exepected input of type struct percpu_counter *, but received type avl_tree_t *

0 commit comments

Comments
 (0)