diff --git a/lldb/bindings/interface/SBModuleSpecListExtensions.i b/lldb/bindings/interface/SBModuleSpecListExtensions.i index 073e9d06738f..2dab521bbd62 100644 --- a/lldb/bindings/interface/SBModuleSpecListExtensions.i +++ b/lldb/bindings/interface/SBModuleSpecListExtensions.i @@ -11,16 +11,28 @@ 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 or by file path. + 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 full or partial path + ''' 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: + for idx in range(count): + spec = self.GetSpecAtIndex(idx) + if str(spec.GetFileSpec()).endswith(key): + return spec + return None + 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 1e6cdefc8d8f..f6edef22aef5 100644 --- a/lldb/test/API/python_api/sbmodule/TestSBModule.py +++ b/lldb/test/API/python_api/sbmodule/TestSBModule.py @@ -110,3 +110,63 @@ class SBModuleAPICase(TestBase): 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", + )