[Lldb-commits] [lldb] [lldb][python] Add polymorphic `__getitem__` to `SBModuleSpecList` for Pythonic indexing (PR #189125)
Jonas Devlieghere via lldb-commits
lldb-commits at lists.llvm.org
Mon Mar 30 08:12:45 PDT 2026
================
@@ -11,16 +11,43 @@ STRING_EXTENSION_OUTSIDE(SBModuleSpecList)
'''Iterate over all ModuleSpecs in a lldb.SBModuleSpecList object.'''
return lldb_iter(self, 'GetSize', 'GetSpecAtIndex')
- def __getitem__(self, idx):
- '''Get the ModuleSpec at a given index in an lldb.SBModuleSpecList object.'''
- if not isinstance(idx, int):
- raise TypeError("unsupported index type: %s" % type(idx))
+ def __getitem__(self, key):
+ '''Access module specs by index, full or partial path, or regular expression.
+ specs[0] - access by integer index
+ specs[-1] - access by negative integer index
+ specs['a.out'] - find first spec matching file basename
+ specs['/usr/lib/liba.dylib'] - find first spec matching file fullpath
+ specs[re.compile(r'lib.*')] - find all specs matching regex on fullpath
+ '''
count = len(self)
- if not (-count <= idx < count):
- raise IndexError("list index out of range")
- idx %= count
- return self.GetSpecAtIndex(idx)
+ if type(key) is int:
+ if -count <= key < count:
+ key %= count
+ return self.GetSpecAtIndex(key)
+ else:
+ raise IndexError("list index out of range")
+ elif type(key) is str:
+ if key.find('/') == -1:
----------------
JDevlieghere wrote:
What about Windows paths?
https://github.com/llvm/llvm-project/pull/189125
More information about the lldb-commits
mailing list