[Lldb-commits] [lldb] cf784ac - [lldb] Support comparing FileSpec against Python strings (#190690)
via lldb-commits
lldb-commits at lists.llvm.org
Tue Apr 7 09:28:17 PDT 2026
Author: Jonas Devlieghere
Date: 2026-04-07T17:28:11+01:00
New Revision: cf784ac314b5eefab3ada8ae125a2de56568ef7d
URL: https://github.com/llvm/llvm-project/commit/cf784ac314b5eefab3ada8ae125a2de56568ef7d
DIFF: https://github.com/llvm/llvm-project/commit/cf784ac314b5eefab3ada8ae125a2de56568ef7d.diff
LOG: [lldb] Support comparing FileSpec against Python strings (#190690)
We got a bug report where someone was iterating over the modules and
wanted to verify that the module name was empty and noticed it didn't
trigger.
```
for module in target.module_iter():
if module.file is None or module.file == "":
# Do something
```
My initial hypothesis was that we were somehow skipping modules, but
upon further investigation, it was the string comparison that was the
culprit. The reporter (reasonably) expected the `file` property to
return a string, but in reality it returns a SBFileSpec.
This could be avoided by explicitly comparing with an empty FileSpec,
but that seems needlessly tedious.
```
for module in target.module_iter():
if module.file is None or module.file == lldb.SBFileSpec(""):
# Do something
```
Instead, add support for comparing a SBFileSpec against a string.
rdar://174166420
Added:
lldb/test/API/python_api/filespec/TestFileSpecAPI.py
Modified:
lldb/bindings/interface/SBFileSpecExtensions.i
Removed:
################################################################################
diff --git a/lldb/bindings/interface/SBFileSpecExtensions.i b/lldb/bindings/interface/SBFileSpecExtensions.i
index e2c81e6b0f8b0..d4a0839b8f485 100644
--- a/lldb/bindings/interface/SBFileSpecExtensions.i
+++ b/lldb/bindings/interface/SBFileSpecExtensions.i
@@ -6,8 +6,19 @@ STRING_EXTENSION_OUTSIDE(SBFileSpec)
# operator== is a free function, which swig does not handle, so we inject
# our own equality operator here
def __eq__(self, other):
+ if isinstance(other, str):
+ other = SBFileSpec(other)
+ if not isinstance(other, SBFileSpec):
+ return NotImplemented
return not self.__ne__(other)
+ def __ne__(self, other):
+ if isinstance(other, str):
+ other = SBFileSpec(other)
+ if not isinstance(other, SBFileSpec):
+ return NotImplemented
+ return _lldb.SBFileSpec___ne__(self, other)
+
fullpath = property(str, None, doc='''A read only property that returns the fullpath as a python string.''')
basename = property(GetFilename, None, doc='''A read only property that returns the path basename as a python string.''')
dirname = property(GetDirectory, None, doc='''A read only property that returns the path directory name as a python string.''')
diff --git a/lldb/test/API/python_api/filespec/TestFileSpecAPI.py b/lldb/test/API/python_api/filespec/TestFileSpecAPI.py
new file mode 100644
index 0000000000000..48ceb9a981967
--- /dev/null
+++ b/lldb/test/API/python_api/filespec/TestFileSpecAPI.py
@@ -0,0 +1,35 @@
+"""
+Test SBFileSpec APIs, with emphasis on equality comparisons against strings.
+"""
+
+import lldb
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+
+
+class FileSpecAPITestCase(TestBase):
+ NO_DEBUG_INFO_TESTCASE = True
+
+ def test_filespec_eq(self):
+ """Test SBFileSpec equality comparisons."""
+ empty = lldb.SBFileSpec()
+ self.assertTrue(empty == lldb.SBFileSpec())
+ self.assertTrue(empty == "")
+ self.assertTrue(empty == lldb.SBFileSpec(""))
+ self.assertFalse(empty != "")
+ self.assertTrue(not empty)
+ self.assertFalse(empty is None)
+
+ def test_filespec_eq_path(self):
+ """Test SBFileSpec equality with non-empty path strings."""
+ spec = lldb.SBFileSpec("/a/b")
+ self.assertTrue(spec == "/a/b")
+ self.assertFalse(spec == "/a/c")
+ self.assertFalse(spec != "/a/b")
+ self.assertTrue(spec != "/a/c")
+
+ def test_filespec_eq_other_type(self):
+ """Test SBFileSpec equality with unsupported types returns False."""
+ spec = lldb.SBFileSpec()
+ self.assertFalse(spec == 42)
+ self.assertFalse(spec == [])
More information about the lldb-commits
mailing list