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 <xuxingliang@xiaomi.com>
This commit is contained in:
xuxingliang
2024-11-27 11:30:27 +08:00
committed by Alan C. Assis
parent 27d5a1b4b7
commit 06c7e2a02e

View File

@@ -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: