[Lldb-commits] [lldb] [lldb][python] Add polymorphic `__getitem__` to `SBModuleSpecList` for Pythonic indexing (PR #189125)
via lldb-commits
lldb-commits at lists.llvm.org
Fri Mar 27 15:50:33 PDT 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-lldb
Author: Piyush Jaiswal (piyushjaiswal98)
<details>
<summary>Changes</summary>
### Summary
`SBModuleSpecList` already supports `len()` and iteration via `__len__` and `__iter__`, but is not subscriptable — `specs[0]` raises `TypeError`.
This replaces the existing integer-only `__getitem__` with a polymorphic version following the same lookup pattern used by `SBTarget.modules_access` in `SBTargetExtensions.i`.
### Supported key types
| Key type | Example | Behavior |
|---|---|---|
| `int` | `specs[0]`, `specs[-1]` | Direct index with negative index support |
| `str` | `specs['a.out']`, `specs['/usr/lib/liba.dylib']` | Lookup by file basename (no `/`) or full path (contains `/`). Returns first match or `None` |
| `re.compile(...)` | `specs[re.compile(r'lib.*')]` | Regex match on file path. Returns a `list` of all matching specs |
### Error handling
- **`IndexError`** for out-of-bounds integer indices
- **`TypeError`** for unsupported key types (e.g., `float`)
- **`None`** for string lookups with no match
- **`[]`** (empty list) for regex patterns with no match
### Before
```python
>>> specs = lldb.SBModuleSpecList.GetModuleSpecifications('/bin/ls')
>>> specs[0]
Traceback (most recent call last):
File "<console>", line 1, in <module>
TypeError: 'SBModuleSpecList' object is not subscriptable
```
### After
```python
>>> import lldb, re
>>> specs = lldb.SBModuleSpecList.GetModuleSpecifications('/bin/ls')
>>> specs[0]
file = '/bin/ls', arch = x86_64-*-linux, uuid = 3CCC0D8A-..., object size = 140928
>>> specs[-1]
file = '/bin/ls', arch = x86_64-*-linux, uuid = 3CCC0D8A-..., object size = 140928
>>> specs['ls']
file = '/bin/ls', arch = x86_64-*-linux, uuid = 3CCC0D8A-..., object size = 140928
>>> specs[re.compile(r'.*')]
[file = '/bin/ls', arch = x86_64-*-linux, uuid = 3CCC0D8A-..., object size = 140928]
>>> specs[999]
IndexError: list index out of range
>>> specs[1.5]
TypeError: unsupported index type: <class 'float'>
```
### Test plan
Added test_module_spec_list_indexing to TestSBModule.py covering:
- Positive and negative integer indexing
- Out-of-bounds raises IndexError
- Unsupported key type raises TypeError
- String lookup by file basename (and missing key returns None)
- Regex matching all specs and regex with no matches
```
bin/llvm-lit -sv lldb/test/API/python_api/sbmodule/TestSBModule.py
```
```
PASS: LLDB :: test_GetObjectName_dwarf (TestSBModule.SBModuleAPICase)
PASS: LLDB :: test_GetObjectName_dwo (TestSBModule.SBModuleAPICase)
PASS: LLDB :: test_module_spec_list_indexing_dwarf (TestSBModule.SBModuleAPICase)
PASS: LLDB :: test_module_spec_list_indexing_dwo (TestSBModule.SBModuleAPICase)
----------------------------------------------------------------------
Ran 12 tests in 1.854s
OK (skipped=6)
```
---
Full diff: https://github.com/llvm/llvm-project/pull/189125.diff
2 Files Affected:
- (modified) lldb/bindings/interface/SBModuleSpecListExtensions.i (+36-9)
- (modified) lldb/test/API/python_api/sbmodule/TestSBModule.py (+72)
``````````diff
diff --git a/lldb/bindings/interface/SBModuleSpecListExtensions.i b/lldb/bindings/interface/SBModuleSpecListExtensions.i
index 073e9d06738f5..a4bfe474b430b 100644
--- a/lldb/bindings/interface/SBModuleSpecListExtensions.i
+++ b/lldb/bindings/interface/SBModuleSpecListExtensions.i
@@ -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:
+ for idx in range(count):
+ spec = self.GetSpecAtIndex(idx)
+ if spec.GetFileSpec().GetFilename() == key:
+ return spec
+ else:
+ for idx in range(count):
+ spec = self.GetSpecAtIndex(idx)
+ if str(spec.GetFileSpec()) == key:
+ return spec
+ return None
+ elif isinstance(key, type(re.compile(''))):
+ matching_specs = []
+ for idx in range(count):
+ spec = self.GetSpecAtIndex(idx)
+ re_match = key.search(str(spec.GetFileSpec()))
+ if re_match:
+ matching_specs.append(spec)
+ return matching_specs
+ else:
+ raise TypeError("unsupported index type: %s" % type(key))
%}
#endif
}
-
diff --git a/lldb/test/API/python_api/sbmodule/TestSBModule.py b/lldb/test/API/python_api/sbmodule/TestSBModule.py
index 1e6cdefc8d8f0..7cc68876ee4c6 100644
--- a/lldb/test/API/python_api/sbmodule/TestSBModule.py
+++ b/lldb/test/API/python_api/sbmodule/TestSBModule.py
@@ -1,5 +1,7 @@
"""Test the SBDModule APIs."""
+import re
+
import lldb
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
@@ -110,3 +112,73 @@ def test_module_is_file_backed(self):
self.assertSuccess(
error, "couldn't destroy process %s" % background_process.pid
)
+
+ @skipIfRemote
+ def test_module_spec_list_indexing(self):
+ """Test that SBModuleSpecList supports Pythonic indexing."""
+ self.build()
+ libfoo_path = self.getBuildArtifact("libfoo.a")
+ specs = lldb.SBModuleSpecList.GetModuleSpecifications(libfoo_path)
+ count = specs.GetSize()
+ self.assertGreater(count, 0, "Archive should have at least one module spec")
+
+ # Integer indexing: positive indices
+ for i in range(count):
+ self.assertEqual(
+ str(specs[i]),
+ str(specs.GetSpecAtIndex(i)),
+ "specs[%d] should match GetSpecAtIndex(%d)" % (i, i),
+ )
+
+ # Integer indexing: negative indices
+ self.assertEqual(
+ str(specs[-1]),
+ str(specs.GetSpecAtIndex(count - 1)),
+ "specs[-1] should match last element",
+ )
+ self.assertEqual(
+ str(specs[-count]),
+ str(specs.GetSpecAtIndex(0)),
+ "specs[-count] should match first element",
+ )
+
+ # Integer indexing: out of bounds raises IndexError
+ self.assertRaises(IndexError, lambda: specs[count])
+ self.assertRaises(IndexError, lambda: specs[-count - 1])
+
+ # Unsupported key type raises TypeError
+ self.assertRaises(TypeError, lambda: specs[1.5])
+
+ # String indexing: lookup by file basename
+ spec0 = specs.GetSpecAtIndex(0)
+ basename = spec0.GetFileSpec().GetFilename()
+ if basename:
+ found = specs[basename]
+ self.assertIsNotNone(found, "Should find spec by basename '%s'" % basename)
+ self.assertEqual(
+ found.GetFileSpec().GetFilename(),
+ basename,
+ "Found spec basename should match",
+ )
+
+ # String indexing: missing basename returns None
+ self.assertIsNone(
+ specs["nonexistent_file.xyz"],
+ "Lookup of nonexistent basename should return None",
+ )
+
+ # Regex indexing: match all specs
+ all_matches = specs[re.compile(r".*")]
+ self.assertEqual(
+ len(all_matches),
+ count,
+ "Regex '.*' should match all specs",
+ )
+
+ # Regex indexing: no matches returns empty list
+ no_matches = specs[re.compile(r"^ZZZZZ_no_match$")]
+ self.assertEqual(
+ len(no_matches),
+ 0,
+ "Regex with no matches should return empty list",
+ )
``````````
</details>
https://github.com/llvm/llvm-project/pull/189125
More information about the lldb-commits
mailing list