llvm-project
e8566f83 - [lldb][python] Add polymorphic `__getitem__` to `SBModuleSpecList` for Pythonic indexing (#189125)

Commit
23 days ago
[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>
Parents
Loading