From 5f1c841b7516e65b29eef1b58c9ad15e087fdb19 Mon Sep 17 00:00:00 2001 From: Clement Rouault Date: Fri, 18 Mar 2016 18:03:45 +0100 Subject: [PATCH] Add ctypes struct pprinter --- windows/utils/pythonutils.py | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/windows/utils/pythonutils.py b/windows/utils/pythonutils.py index 47f2014..96923b5 100644 --- a/windows/utils/pythonutils.py +++ b/windows/utils/pythonutils.py @@ -1,5 +1,6 @@ """utils fonctions non windows-related""" import ctypes +import _ctypes def fixedpropety(f): @@ -22,4 +23,32 @@ def swallow_ctypes_copy(ctypes_object): # type replacement based on name def transform_ctypes_fields(struct, replacement): - return [(name, replacement.get(name, type)) for name, type in struct._fields_] \ No newline at end of file + return [(name, replacement.get(name, type)) for name, type in struct._fields_] + + +def print_ctypes_struct(struct, name="", ident=0, hexa=False): + if isinstance(struct, _ctypes._Pointer): + if ctypes.cast(struct, ctypes.c_void_p).value is None: + print("{0} -> NULL".format(name)) + return + return print_ctypes_struct(struct[0], name + "", hexa=hexa) + + if not hasattr(struct, "_fields_"): + value = struct + if hasattr(struct, "value"): + value = struct.value + + if isinstance(value, basestring): + value = repr(value) + if hexa: + try: + print("{0} -> {1}".format(name, hex(value))) + return + except TypeError: + pass + print("{0} -> {1}".format(name, value)) + return + + for fname, ftype in struct._fields_: + value = getattr(struct, fname) + print_ctypes_struct(value, "{0}.{1}".format(name, fname), hexa=hexa) \ No newline at end of file