Skip to content

Commit 174b562

Browse files
committed
Introduce member property which looks up members by name
1 parent 7e2c554 commit 174b562

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

lldb/bindings/interface/SBValueExtensions.i

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ STRING_EXTENSION_OUTSIDE(SBValue)
77
return self.GetDynamicValue (eDynamicCanRunTarget)
88

99
class children_access(object):
10-
'''A helper object that will lazily hand out child values when supplied an index or name.'''
10+
'''A helper object that will lazily hand out child values when supplied an index.'''
1111

1212
def __init__(self, sbvalue):
1313
self.sbvalue = sbvalue
@@ -23,19 +23,25 @@ STRING_EXTENSION_OUTSIDE(SBValue)
2323
if -count <= key < count:
2424
key %= count
2525
return self.sbvalue.GetChildAtIndex(key)
26-
elif isinstance(key, str):
27-
if child := self.sbvalue.GetChildMemberWithName(key):
28-
return child
29-
# Support base classes, which are children but not members.
30-
for child in self.sbvalue:
31-
if child.name == key:
32-
return child
3326
return None
3427

3528
def get_child_access_object(self):
3629
'''An accessor function that returns a children_access() object which allows lazy member variable access from a lldb.SBValue object.'''
3730
return self.children_access (self)
3831

32+
def get_member_access_object(self):
33+
'''An accessor function that returns an interface which provides subscript based lookup of child members.'''
34+
class member_access:
35+
def __init__(self, valobj):
36+
self.valobj = valobj
37+
38+
def __getitem__(self, key):
39+
if isinstance(key, str):
40+
return self.valobj.GetChildMemberWithName(key)
41+
raise TypeError("invalid subscript key")
42+
43+
return member_access(self)
44+
3945
def get_value_child_list(self):
4046
'''An accessor function that returns a list() that contains all children in a lldb.SBValue object.'''
4147
children = []
@@ -56,7 +62,8 @@ STRING_EXTENSION_OUTSIDE(SBValue)
5662
return self.GetNumChildren()
5763

5864
children = property(get_value_child_list, None, doc='''A read only property that returns a list() of lldb.SBValue objects for the children of the value.''')
59-
child = property(get_child_access_object, None, doc='''A read only property that returns an object that can access children of a variable by index or by name.''')
65+
child = property(get_child_access_object, None, doc='''A read only property that returns an object that can access children of a variable by index (child_value = value.children[12]).''')
66+
member = property(get_member_access_object, None, doc='''A read only property that returns an object that can access child members by name.''')
6067
name = property(GetName, None, doc='''A read only property that returns the name of this value as a string.''')
6168
type = property(GetType, None, doc='''A read only property that returns a lldb.SBType object that represents the type for this value.''')
6269
size = property(GetByteSize, None, doc='''A read only property that returns the size in bytes of this value.''')

lldb/test/API/python_api/value/TestValueAPI.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ def test(self):
140140
val_i = target.EvaluateExpression("i")
141141
val_s = target.EvaluateExpression("s")
142142
val_a = target.EvaluateExpression("a")
143-
self.assertTrue(val_s.child["a"].GetAddress().IsValid(), VALID_VARIABLE)
144-
self.assertTrue(val_s.child["a"].AddressOf(), VALID_VARIABLE)
143+
self.assertTrue(val_s.member["a"].GetAddress().IsValid(), VALID_VARIABLE)
144+
self.assertTrue(val_s.member["a"].AddressOf(), VALID_VARIABLE)
145145
self.assertTrue(val_a.Cast(val_i.GetType()).AddressOf(), VALID_VARIABLE)
146146

147147
# Test some other cases of the Cast API. We allow casts from one struct type
@@ -208,7 +208,7 @@ def test(self):
208208
weird_cast = f_var.Cast(val_s.GetType())
209209
self.assertSuccess(weird_cast.GetError(), "Can cast from a larger to a smaller")
210210
self.assertEqual(
211-
weird_cast.child["a"].GetValueAsSigned(0),
211+
weird_cast.member["a"].GetValueAsSigned(0),
212212
33,
213213
"Got the right value",
214214
)

0 commit comments

Comments
 (0)