Skip to content

Commit 3ff6367

Browse files
committed
[lldb] Accept negative indexes in __getitem__
To the Python bindings, add support for Python-like negative indexes. While was using `script`, I tried to access a thread's bottom frame with `thread.frame[-1]`, but that failed. This change updates the `__getitem__` implementations to support negative indexes as one would expect in Python. Differential Revision: https://reviews.llvm.org/D143282
1 parent 5d07e04 commit 3ff6367

File tree

13 files changed

+74
-18
lines changed

13 files changed

+74
-18
lines changed

lldb/bindings/interface/SBBreakpoint.i

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,11 @@ public:
273273
return 0
274274

275275
def __getitem__(self, key):
276-
if type(key) is int and key < len(self):
277-
return self.sbbreakpoint.GetLocationAtIndex(key)
276+
if isinstance(key, int):
277+
count = len(self)
278+
if -count <= key < count:
279+
key %= count
280+
return self.sbbreakpoint.GetLocationAtIndex(key)
278281
return None
279282

280283
def get_locations_access_object(self):

lldb/bindings/interface/SBInstructionList.i

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ public:
8383
'''Access instructions by integer index for array access or by lldb.SBAddress to find an instruction that matches a section offset address object.'''
8484
if type(key) is int:
8585
# Find an instruction by index
86-
if key < len(self):
86+
count = len(self)
87+
if -count <= key < count:
88+
key %= count
8789
return self.GetInstructionAtIndex(key)
8890
elif type(key) is SBAddress:
8991
# Find an instruction using a lldb.SBAddress object

lldb/bindings/interface/SBModule.i

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,8 @@ public:
415415
def __getitem__(self, key):
416416
count = len(self)
417417
if type(key) is int:
418-
if key < count:
418+
if -count <= key < count:
419+
key %= count
419420
return self.sbmodule.GetSymbolAtIndex(key)
420421
elif type(key) is str:
421422
matches = []
@@ -476,7 +477,8 @@ public:
476477
def __getitem__(self, key):
477478
count = len(self)
478479
if type(key) is int:
479-
if key < count:
480+
if -count <= key < count:
481+
key %= count
480482
return self.sbmodule.GetSectionAtIndex(key)
481483
elif type(key) is str:
482484
for idx in range(count):
@@ -511,7 +513,8 @@ public:
511513
def __getitem__(self, key):
512514
count = len(self)
513515
if type(key) is int:
514-
if key < count:
516+
if -count <= key < count:
517+
key %= count
515518
return self.sbmodule.GetCompileUnitAtIndex(key)
516519
elif type(key) is str:
517520
is_full_path = key[0] == '/'

lldb/bindings/interface/SBProcess.i

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,8 +487,11 @@ public:
487487
return 0
488488

489489
def __getitem__(self, key):
490-
if type(key) is int and key < len(self):
491-
return self.sbprocess.GetThreadAtIndex(key)
490+
if isinstance(key, int):
491+
count = len(self)
492+
if -count <= key < count:
493+
key %= count
494+
return self.sbprocess.GetThreadAtIndex(key)
492495
return None
493496

494497
def get_threads_access_object(self):

lldb/bindings/interface/SBSymbolContextList.i

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ public:
7474

7575
def __getitem__(self, key):
7676
count = len(self)
77-
if type(key) is int:
78-
if key < count:
77+
if isinstance(key, int):
78+
if -count <= key < count:
79+
key %= count
7980
return self.GetContextAtIndex(key)
8081
else:
8182
raise IndexError

lldb/bindings/interface/SBTarget.i

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,8 @@ public:
10011001
def __getitem__(self, key):
10021002
num_modules = self.sbtarget.GetNumModules()
10031003
if type(key) is int:
1004-
if key < num_modules:
1004+
if -num_modules <= key < num_modules:
1005+
key %= num_modules
10051006
return self.sbtarget.GetModuleAtIndex(key)
10061007
elif type(key) is str:
10071008
if key.find('/') == -1:

lldb/bindings/interface/SBThread.i

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,11 @@ public:
434434
return 0
435435

436436
def __getitem__(self, key):
437-
if type(key) is int and key < self.sbthread.GetNumFrames():
438-
return self.sbthread.GetFrameAtIndex(key)
437+
if isinstance(key, int):
438+
count = len(self)
439+
if -count <= key < count:
440+
key %= count
441+
return self.sbthread.GetFrameAtIndex(key)
439442
return None
440443

441444
def get_frames_access_object(self):

lldb/bindings/interface/SBTypeCategory.i

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ namespace lldb {
147147
def __getitem__(self, key):
148148
num_items = len(self)
149149
if type(key) is int:
150-
if key < num_items:
150+
if -num_items <= key < num_items:
151+
key %= num_items
151152
return self.get_at_index_function(self.sbcategory,key)
152153
elif type(key) is str:
153154
return self.get_by_name_function(self.sbcategory,SBTypeNameSpecifier(key))

lldb/bindings/interface/SBTypeEnumMember.i

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ public:
121121
def __getitem__(self, key):
122122
num_elements = self.GetSize()
123123
if type(key) is int:
124-
if key < num_elements:
124+
if -num_elements <= key < num_elements:
125+
key %= num_elements
125126
return self.GetTypeEnumMemberAtIndex(key)
126127
elif type(key) is str:
127128
for idx in range(num_elements):

lldb/bindings/interface/SBValue.i

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,8 +459,11 @@ public:
459459
return 0
460460

461461
def __getitem__(self, key):
462-
if type(key) is int and key < len(self):
463-
return self.sbvalue.GetChildAtIndex(key)
462+
if isinstance(key, int):
463+
count = len(self)
464+
if -count <= key < count:
465+
key %= count
466+
return self.sbvalue.GetChildAtIndex(key)
464467
return None
465468

466469
def get_child_access_object(self):

0 commit comments

Comments
 (0)