Files
llvm-project/lldb/test/API/python_api/sbmodule/TestSBModule.py
Piyush Jaiswal e8566f83d2 [lldb][python] Add polymorphic __getitem__ to SBModuleSpecList for Pythonic indexing (#189125)
### Summary

`SBModuleSpecList` already supports `len()` and iteration via `__len__`
and `__iter__`, but is not subscriptable — `specs[0]` raises
`TypeError`.

This adds a `__getitem__` method that supports integer indexing (with
negative index support) and string lookup using `endswith()` matching,
which works for both Unix and Windows paths.

### 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
basename or partial/full path via `endswith()`. Returns first match or
`None` |

### Error handling

- **`IndexError`** for out-of-bounds integer indices
- **`TypeError`** for unsupported key types (e.g., `float`)
- **`None`** for string lookups 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[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 basename and full path (endswith() matching)
- Missing key returns None

```
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)
```

Co-authored-by: Piyush Jaiswal <piyushjais@meta.com>
2026-04-06 14:48:18 -07:00

173 lines
6.4 KiB
Python

"""Test the SBDModule APIs."""
import lldb
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbutil
import os, signal, subprocess
class SBModuleAPICase(TestBase):
def setUp(self):
TestBase.setUp(self)
self.background_pid = None
def tearDown(self):
TestBase.tearDown(self)
if self.background_pid:
os.kill(self.background_pid, signal.SIGKILL)
@skipIfRemote
def test_GetObjectName(self):
"""Test the SBModule::GetObjectName() method"""
self.build()
exe = self.getBuildArtifact("a.out")
libfoo_path = self.getBuildArtifact("libfoo.a")
target_exe = self.dbg.CreateTarget(exe)
self.assertTrue(target_exe.IsValid(), "Target for a.out is valid")
# Test that the executable module has no object name (usually the first module in the target)
exe_module = target_exe.GetModuleAtIndex(0)
self.assertTrue(exe_module.IsValid(), "Executable module is valid")
self.assertIsNone(
exe_module.GetObjectName(), "a.out should have no object name"
)
# check archive member names
module_specs = lldb.SBModuleSpecList.GetModuleSpecifications(libfoo_path)
self.assertGreater(
module_specs.GetSize(), 0, "Archive should have at least one module spec"
)
self.assertEqual(
module_specs[0].GetObjectName(),
module_specs.GetSpecAtIndex(0).GetObjectName(),
"subscript [0] matches GetSpecAtIndex(0)",
)
self.assertEqual(
module_specs[-1].GetObjectName(),
module_specs.GetSpecAtIndex(module_specs.GetSize() - 1).GetObjectName(),
"subscript [-1] matches last item",
)
found = set()
expected = {"a.o", "b.o"}
for i in range(module_specs.GetSize()):
spec = module_specs.GetSpecAtIndex(i)
obj_name = spec.GetObjectName()
self.assertIsInstance(obj_name, str)
self.assertIn(obj_name, expected, f"Unexpected object name: {obj_name}")
# create a module from the arhive using the sepc
module = lldb.SBModule(spec)
self.assertTrue(module.IsValid(), "Module is valid")
self.assertTrue(module.IsValid(), f"Module for {obj_name} is valid")
self.assertEqual(
module.GetObjectName(), obj_name, f"Object name for {obj_name} matches"
)
found.add(obj_name)
self.assertEqual(found, expected, "Did not find all expected archive members")
@skipUnlessDarwin
@skipIfRemote
def test_module_is_file_backed(self):
"""Test the SBModule::IsFileBacked() method"""
self.build()
target, _, _, _ = lldbutil.run_to_source_breakpoint(
self, "// break here", lldb.SBFileSpec("main.c")
)
self.assertGreater(target.GetNumModules(), 0)
main_module = target.GetModuleAtIndex(0)
self.assertEqual(main_module.GetFileSpec().GetFilename(), "a.out")
self.assertTrue(
main_module.IsFileBacked(), "The module should be backed by a file on disk"
)
self.dbg.DeleteTarget(target)
self.assertEqual(self.dbg.GetNumTargets(), 0)
exe = self.getBuildArtifact("a.out")
background_process = subprocess.Popen([exe])
self.assertTrue(background_process, "process is not valid")
self.background_pid = background_process.pid
os.unlink(exe)
target = self.dbg.CreateTarget("")
self.assertEqual(self.dbg.GetNumTargets(), 1)
error = lldb.SBError()
process = target.AttachToProcessWithID(
self.dbg.GetListener(), self.background_pid, error
)
self.assertTrue(error.Success() and process, PROCESS_IS_VALID)
main_module = target.FindModule(lldb.SBFileSpec("a.out"))
self.assertIsNotNone(main_module)
self.assertFalse(
main_module.IsFileBacked(),
"The module should not be backed by a file on disk.",
)
error = process.Destroy()
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: lookup by partial path (endswith matching)
fullpath = str(spec0.GetFileSpec())
if fullpath:
found = specs[fullpath]
self.assertIsNotNone(found, "Should find spec by full path '%s'" % fullpath)
# String indexing: missing basename returns None
self.assertIsNone(
specs["nonexistent_file.xyz"],
"Lookup of nonexistent basename should return None",
)