summaryrefslogtreecommitdiff
path: root/python/examples/kaitai/__main__.py
diff options
context:
space:
mode:
authorAndrew Lamoureux <andrew@vector35.com>2019-03-23 01:15:14 -0400
committerAndrew Lamoureux <andrew@vector35.com>2019-03-23 01:15:14 -0400
commit334c0e966eeb67cc88975cc51c3b7e17f7d1b1ee (patch)
treefbee805aa70f1b85f4513f488df5e0683458da98 /python/examples/kaitai/__main__.py
parentaf001b025b5aad5b205cb97977f3951fe6ef2a71 (diff)
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
Diffstat (limited to 'python/examples/kaitai/__main__.py')
-rwxr-xr-xpython/examples/kaitai/__main__.py20
1 files changed, 10 insertions, 10 deletions
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: