diff options
author | Mark W. Eichin <eichin@thok.org> | 2009-04-07 07:09:12 +0000 |
---|---|---|
committer | Mark W. Eichin <eichin@thok.org> | 2009-04-07 07:09:12 +0000 |
commit | 721679c85dadd2187f1af9296cb32867922d1c9a (patch) | |
tree | c5fa2c19534e229e820f97a1b71da6189dc59eff /lib | |
parent | 0e4482af0042da208008eb570cd1881a100efae3 (diff) |
ctypes docs say "enums are easy" but don't give any further guidance.
Turns out they are easy :-) Also added "fake" enums to render address
family tags correctly; cheat a little and lift the values from the
python socket module directly, to avoid duplication.
Diffstat (limited to 'lib')
-rwxr-xr-x | lib/zephyr_tests.py | 38 |
1 files changed, 34 insertions, 4 deletions
diff --git a/lib/zephyr_tests.py b/lib/zephyr_tests.py index 8727642..8f9ca50 100755 --- a/lib/zephyr_tests.py +++ b/lib/zephyr_tests.py @@ -36,6 +36,21 @@ def ctypes_pprint(cstruct, indent=""): else: print field_value +class Enum(c_int): + def pprint(self): + try: + return "%s(%d)" % (self._values_[self.value], self.value) + except IndexError: + 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): + try: + return "%s(%d)" % (self._values_[self.value], self.value) + except IndexError: + return "unknown enum value(%d)" % (self.value) + # TODO: pick some real framework later, we're just poking around for now class TestSuite(object): @@ -67,15 +82,19 @@ class in6_addr(Structure): ("in6_u", _U_in6_u), ] +class AF_(Enum_u16): + _socket_af = dict([(v,n) for n,v in socket.__dict__.items() if n.startswith("AF_")]) + _values_ = [_socket_af.get(k, "unknown address family") for k in range(min(_socket_af), max(_socket_af)+1)] + class sockaddr(Structure): _fields_ = [ - ("sa_family", c_uint16), + ("sa_family", AF_), ("sa_data", c_char * 14), ] class sockaddr_in(Structure): _fields_ = [ - ("sin_family", c_uint16), + ("sin_family", AF_), ("sin_port", c_uint16), ("sin_addr", in_addr), # hack from linux - do we actually need it? @@ -85,7 +104,7 @@ class sockaddr_in(Structure): # RFC2553... class sockaddr_in6(Structure): _fields_ = [ - ("sin6_family", c_uint16), + ("sin6_family", AF_), ("sin6_port", c_uint16), ("sin6_flowinfo", c_uint32), ("sin6_addr", in6_addr), @@ -100,6 +119,16 @@ Z_MAXOTHERFIELDS = 10 #define ZNOAUTH ((Z_AuthProc)0) ZNOAUTH = 0 +# typedef enum { +# UNSAFE, UNACKED, ACKED, HMACK, HMCTL, SERVACK, SERVNAK, CLIENTACK, STAT +# } ZNotice_Kind_t; +# extern const char *ZNoticeKinds[9]; + +class ZNotice_Kind_t(Enum): + _values_ = [ + "UNSAFE", "UNACKED", "ACKED", "HMACK", "HMCTL", "SERVACK", "SERVNAK", "CLIENTACK", "STAT", + ] + # struct _ZTimeval { class _ZTimeval(Structure): _fields_ = [ @@ -149,7 +178,7 @@ class ZNotice_t(Structure): # char *z_version; ("z_version", c_char_p), # ZNotice_Kind_t z_kind; - ("z_kind", c_int), # no enums yet + ("z_kind", ZNotice_Kind_t), # ZUnique_Id_t z_uid; ("z_uid", ZUnique_Id_t), # union { @@ -171,6 +200,7 @@ class ZNotice_t(Structure): # int z_auth; ("z_auth", c_int), # int z_checked_auth; + # TODO: fake enum, for display ("z_checked_auth", c_int), # int z_authent_len; ("z_authent_len", c_int), |