From 334c0e966eeb67cc88975cc51c3b7e17f7d1b1ee Mon Sep 17 00:00:00 2001 From: Andrew Lamoureux Date: Sat, 23 Mar 2019 01:15:14 -0400 Subject: kaitai: python3-compatible forgiveness for struct properties hasattr() on python2 has built in exception handler that would return False when computation of a property screwed up - that doesnt exist in python3 --- python/examples/kaitai/__main__.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'python/examples/kaitai/__main__.py') diff --git a/python/examples/kaitai/__main__.py b/python/examples/kaitai/__main__.py index 237fc180..2accd2e5 100755 --- a/python/examples/kaitai/__main__.py +++ b/python/examples/kaitai/__main__.py @@ -42,19 +42,19 @@ def dump(obj, depth=0): indent = ' '*depth if isinstance(obj, kaitaistruct.KaitaiStruct): - for fieldName in dir(obj): - if hasattr(obj, fieldName): - getattr(obj, fieldName) - - for fieldName in dir(obj): - #print('considering field: %s (hasattr returns: %d)' % (fieldName, hasattr(obj, fieldName))) - if fieldName != '_debug' and fieldName.startswith('_'): - continue - if fieldName in dump_exceptions: + fieldNames = [] + for candidate in dir(obj): + if candidate != '_debug' and candidate.startswith('_'): continue - if not hasattr(obj, fieldName): + if candidate in dump_exceptions: continue + try: + if getattr(obj, candidate, False): + fieldNames.append(candidate) + except Exception: + pass + for fieldName in fieldNames: subObj = getattr(obj, fieldName) if type(subObj) == types.MethodType: -- cgit v1.3.1