From 06c7e2a02e9bc28454fd370c8c987364cb6f8205 Mon Sep 17 00:00:00 2001 From: xuxingliang Date: Wed, 27 Nov 2024 11:30:27 +0800 Subject: [PATCH] nxgdb/utils: add has_field and fix ArrayIterator 1. Add method to check if an object has specified field. 2. Fix Array iterator that walrus expression should store result of the function, instead of the compare result. Note that walrus operation has lowest priority except ','. Signed-off-by: xuxingliang --- tools/pynuttx/nxgdb/utils.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tools/pynuttx/nxgdb/utils.py b/tools/pynuttx/nxgdb/utils.py index e33530f977..8e9be82c5b 100644 --- a/tools/pynuttx/nxgdb/utils.py +++ b/tools/pynuttx/nxgdb/utils.py @@ -441,6 +441,20 @@ def get_field(val, key, default=None): return default +def has_field(obj: Union[gdb.Type, gdb.Value], key): + if isinstance(obj, gdb.Type): + t = obj + elif isinstance(obj, gdb.Value): + t = obj.type + else: + raise gdb.GdbError(f"Unsupported type {type(obj)}") + + while t.code in (gdb.TYPE_CODE_PTR, gdb.TYPE_CODE_ARRAY, gdb.TYPE_CODE_TYPEDEF): + t = t.target() + + return any(f.name == key for f in t.fields()) + + def get_bytes(val, size): """Convert a gdb value to a bytes object""" try: @@ -958,7 +972,7 @@ class ArrayIterator: raise gdb.error(f"Not an array: {array}, type: {array.type}") if type_code == gdb.TYPE_CODE_ARRAY: - if n := nitems(array) > 0: + if (n := nitems(array)) > 0: maxlen = min(n, maxlen) if maxlen is not None else n if maxlen is None: