Skip to content

Commit 8a20661

Browse files
RedBeard0531DeinAlptraum
authored andcommitted
[libclang/python] Add equality comparison operators for File
1 parent e22579a commit 8a20661

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

clang/bindings/python/clang/cindex.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3470,6 +3470,12 @@ def __str__(self):
34703470
def __repr__(self):
34713471
return "<File: %s>" % (self.name)
34723472

3473+
def __eq__(self, other) -> bool:
3474+
return isinstance(other, File) and bool(conf.lib.clang_File_isEqual(self, other))
3475+
3476+
def __ne__(self, other) -> bool:
3477+
return not self.__eq__(other)
3478+
34733479
@staticmethod
34743480
def from_result(res, arg):
34753481
assert isinstance(res, c_object_p)
@@ -3956,6 +3962,7 @@ def set_property(self, property, value):
39563962
("clang_getFile", [TranslationUnit, c_interop_string], c_object_p),
39573963
("clang_getFileName", [File], _CXString),
39583964
("clang_getFileTime", [File], c_uint),
3965+
("clang_File_isEqual", [File, File], bool),
39593966
("clang_getIBOutletCollectionType", [Cursor], Type),
39603967
("clang_getIncludedFile", [Cursor], c_object_p),
39613968
(

clang/bindings/python/tests/cindex/test_file.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,18 @@ def test_file(self):
1616
self.assertEqual(str(file), "t.c")
1717
self.assertEqual(file.name, "t.c")
1818
self.assertEqual(repr(file), "<File: t.c>")
19+
20+
def test_file_eq(self):
21+
index = Index.create()
22+
tu = index.parse(
23+
"t.c",
24+
unsaved_files=[
25+
("t.c", "int a = 729;"),
26+
("s.c", "int a = 729;"),
27+
],
28+
)
29+
file1 = File.from_name(tu, "t.c")
30+
file2 = File.from_name(tu, "s.c")
31+
self.assertEqual(file1, file1)
32+
self.assertNotEqual(file1, file2)
33+
self.assertNotEqual(file1, "t.c")

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ Python Binding Changes
444444
----------------------
445445
- Added ``Type.get_methods``, a binding for ``clang_visitCXXMethods``, which
446446
allows visiting the methods of a class.
447+
- Add equality comparison operators for ``File`` type
447448

448449
OpenMP Support
449450
--------------

0 commit comments

Comments
 (0)