Skip to content

[libclang/python] Add equality comparison operators for File #130383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions clang/bindings/python/clang/cindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -3499,6 +3499,14 @@ def __str__(self):
def __repr__(self):
return "<File: %s>" % (self.name)

def __eq__(self, other) -> bool:
return isinstance(other, File) and bool(
conf.lib.clang_File_isEqual(self, other)
)

def __ne__(self, other) -> bool:
return not self.__eq__(other)

@staticmethod
def from_result(res, arg):
assert isinstance(res, c_object_p)
Expand Down Expand Up @@ -3986,6 +3994,7 @@ def set_property(self, property, value):
("clang_getFile", [TranslationUnit, c_interop_string], c_object_p),
("clang_getFileName", [File], _CXString),
("clang_getFileTime", [File], c_uint),
("clang_File_isEqual", [File, File], bool),
("clang_getIBOutletCollectionType", [Cursor], Type),
("clang_getIncludedFile", [Cursor], c_object_p),
(
Expand Down
1 change: 1 addition & 0 deletions clang/bindings/python/tests/cindex/INPUTS/a.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1, 2, 3
1 change: 1 addition & 0 deletions clang/bindings/python/tests/cindex/INPUTS/b.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1, 2, 3
6 changes: 6 additions & 0 deletions clang/bindings/python/tests/cindex/INPUTS/testfile.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
int a[] = {
#include "a.inc"
};
int b[] = {
#include "b.inc"
};
54 changes: 53 additions & 1 deletion clang/bindings/python/tests/cindex/test_file.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import os

from clang.cindex import Config, File, Index
from clang.cindex import Config, File, Index, TranslationUnit

if "CLANG_LIBRARY_PATH" in os.environ:
Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])

import unittest

inputs_dir = os.path.join(os.path.dirname(__file__), "INPUTS")

class TestFile(unittest.TestCase):
def test_file(self):
Expand All @@ -16,3 +17,54 @@ def test_file(self):
self.assertEqual(str(file), "t.c")
self.assertEqual(file.name, "t.c")
self.assertEqual(repr(file), "<File: t.c>")

def test_file_eq(self):
path = os.path.join(inputs_dir, "testfile.c")
path_a = os.path.join(inputs_dir, "a.inc")
path_b = os.path.join(inputs_dir, "b.inc")
tu = TranslationUnit.from_source(path)
main_file = File.from_name(tu, path)
a_file = File.from_name(tu, path_a)
a_file2 = File.from_name(tu, path_a)
b_file = File.from_name(tu, path_b)

self.assertEqual(a_file, a_file2)
self.assertNotEqual(a_file, b_file)
self.assertNotEqual(main_file, a_file)
self.assertNotEqual(main_file, b_file)
self.assertNotEqual(main_file, "t.c")

def test_file_eq_in_memory(self):
tu = TranslationUnit.from_source(
"testfile.c",
unsaved_files=[
(
"testfile.c",
"""
int a[] = {
#include "a.inc"
};
int b[] = {
#include "b.inc"
};
""",
),
("a.inc", "1,2,3"),
("b.inc", "1,2,3"),
],
)

path = os.path.join(inputs_dir, "testfile.c")
path_a = os.path.join(inputs_dir, "a.inc")
path_b = os.path.join(inputs_dir, "b.inc")
tu = TranslationUnit.from_source(path)
main_file = File.from_name(tu, path)
a_file = File.from_name(tu, path_a)
a_file2 = File.from_name(tu, path_a)
b_file = File.from_name(tu, path_b)

self.assertEqual(a_file, a_file2)
self.assertNotEqual(a_file, b_file)
self.assertNotEqual(main_file, a_file)
self.assertNotEqual(main_file, b_file)
self.assertNotEqual(main_file, "a.inc")
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,7 @@ Python Binding Changes
allows visiting the methods of a class.
- Added ``Type.get_fully_qualified_name``, which provides fully qualified type names as
instructed by a PrintingPolicy.
- Add equality comparison operators for ``File`` type

OpenMP Support
--------------
Expand Down
Loading