From 98145b0787b37d6cffacc423b97ac14542afb295 Mon Sep 17 00:00:00 2001 From: "Mark W. Eichin" Date: Tue, 7 Apr 2009 07:48:07 +0000 Subject: reworked with a pprint/pformat distinction (mostly to stop propating indent) added comments z_sender_sockaddr now displays only the branch implied by sa_family --- lib/zephyr_tests.py | 78 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 53 insertions(+), 25 deletions(-) (limited to 'lib/zephyr_tests.py') diff --git a/lib/zephyr_tests.py b/lib/zephyr_tests.py index 06817ca..fc06fd3 100755 --- a/lib/zephyr_tests.py +++ b/lib/zephyr_tests.py @@ -23,36 +23,58 @@ from ctypes import Structure, Union, sizeof __revision__ = "$Id$" __version__ = "%s/%s" % (__revision__.split()[3], __revision__.split()[2]) +def print_line_or_lines(results, indent): + """short values on same line, multi-line on later ones...""" + if len(results) == 1: + print results[0] + else: + print + for result in results: + print indent + result + def ctypes_pprint(cstruct, indent=""): """pretty print a ctypes Structure or Union""" + for field_name, field_ctype in cstruct._fields_: field_value = getattr(cstruct, field_name) print indent + field_name, + next_indent = indent + " " pprint_name = "pprint_%s" % field_name + pformat_name = "pformat_%s" % field_name if hasattr(cstruct, pprint_name): - print getattr(cstruct, pprint_name)(indent + " ") + # no longer used + getattr(cstruct, pprint_name)(next_indent) + elif hasattr(cstruct, pformat_name): + # counted-array and other common cases + print_line_or_lines(getattr(cstruct, pformat_name)(), next_indent) + elif hasattr(field_value, "pformat"): + # common for small compound types + print_line_or_lines(field_value.pformat(), next_indent) elif hasattr(field_value, "pprint"): - print field_value.pprint() + # useful for Union selectors + field_value.pprint(next_indent) elif hasattr(field_value, "_fields_"): + # generic recursion print - ctypes_pprint(field_value, indent + " ") + ctypes_pprint(field_value, next_indent) else: + # generic simple (or unknown/uninteresting) value print field_value class Enum(c_int): - def pprint(self): + def pformat(self): try: - return "%s(%d)" % (self._values_[self.value], self.value) + return ["%s(%d)" % (self._values_[self.value], self.value)] except IndexError: - return "unknown enum value(%d)" % (self.value) + return ["unknown enum value(%d)" % (self.value)] # not really an enum, but we get a richer effect by treating it as one class Enum_u16(c_uint16): - def pprint(self): + def pformat(self): try: - return "%s(%d)" % (self._values_[self.value], self.value) + return ["%s(%d)" % (self._values_[self.value], self.value)] except IndexError: - return "unknown enum value(%d)" % (self.value) + return ["unknown enum value(%d)" % (self.value)] # TODO: pick some real framework later, we're just poking around for now @@ -70,8 +92,8 @@ class in_addr(Structure): _fields_ = [ ("s_addr", c_uint32), ] - def pprint(self): - return socket.inet_ntoa(struct.pack("= 1000000: # invalid usec, still treat as numbers - return "%dsec, %dusec (%s)" % (self.tv_sec, self.tv_usec, timestr) - return "%d.%06dsec (%s)" % (self.tv_sec, self.tv_usec, timestr) + return ["%dsec, %dusec (%s)" % (self.tv_sec, self.tv_usec, timestr)] + return ["%d.%06dsec (%s)" % (self.tv_sec, self.tv_usec, timestr)] # typedef struct _ZUnique_Id_t { class ZUnique_Id_t(Structure): @@ -172,6 +196,14 @@ class _U_z_sender_sockaddr(Union): ("ip6", sockaddr_in6), # } z_sender_sockaddr; ] + def pprint(self, indent): + print + if self.sa.sa_family.value == socket.AF_INET: + ctypes_pprint(self.ip4, indent + ".ip4:") + elif self.sa.sa_family.value == socket.AF_INET6: + ctypes_pprint(self.ip6, indent + ".ip6:") + else: + ctypes_pprint(self.sa, indent + ".sa:") # typedef struct _ZNotice_t { class ZNotice_t(Structure): @@ -243,19 +275,15 @@ class ZNotice_t(Structure): ("z_hdr_fields", POINTER(c_char_p)), # } ZNotice_t; ] - def pprint_z_other_fields(self, indent): - fields = ["%s%d: %s" % (indent, n, self.z_other_fields[n]) - for n in range(Z_MAXOTHERFIELDS)] - return "\n" + "\n".join(fields) - def pprint_z_hdr_fields(self, indent): + def pformat_z_other_fields(self): + return ["%d: %s" % (n, self.z_other_fields[n]) + for n in range(Z_MAXOTHERFIELDS)] + def pformat_z_hdr_fields(self): if not self.z_hdr_fields: - return "NULL" - fields = ["%s%d: %s" % (indent, n, self.z_hdr_fields[n]) - for n in range(self.z_num_hdr_fields)] - return "\n" + "\n".join(fields) + return ["NULL"] + return ["%d: %s" % (n, self.z_hdr_fields[n]) + for n in range(self.z_num_hdr_fields)] - - class libZephyr(object): """wrappers for functions in libZephyr""" testable_funcs = [ -- cgit v1.2.3