[libclang/python] Add equality comparison operators for File (#130383)
This covers the `File` interface changes added by #120590 --------- Co-authored-by: Mathias Stearn <redbeard0531@gmail.com> Co-authored-by: Vlad Serebrennikov <serebrennikov.vladislav@gmail.com>
This commit is contained in:
@@ -3499,6 +3499,14 @@ class File(ClangObject):
|
||||
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)
|
||||
@@ -3986,6 +3994,7 @@ FUNCTION_LIST: list[LibFunc] = [
|
||||
("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),
|
||||
(
|
||||
|
||||
1
clang/bindings/python/tests/cindex/INPUTS/a.inc
Normal file
1
clang/bindings/python/tests/cindex/INPUTS/a.inc
Normal file
@@ -0,0 +1 @@
|
||||
1, 2, 3
|
||||
1
clang/bindings/python/tests/cindex/INPUTS/b.inc
Normal file
1
clang/bindings/python/tests/cindex/INPUTS/b.inc
Normal file
@@ -0,0 +1 @@
|
||||
1, 2, 3
|
||||
6
clang/bindings/python/tests/cindex/INPUTS/testfile.c
Normal file
6
clang/bindings/python/tests/cindex/INPUTS/testfile.c
Normal file
@@ -0,0 +1,6 @@
|
||||
int a[] = {
|
||||
#include "a.inc"
|
||||
};
|
||||
int b[] = {
|
||||
#include "b.inc"
|
||||
};
|
||||
@@ -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):
|
||||
@@ -16,3 +17,54 @@ class TestFile(unittest.TestCase):
|
||||
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")
|
||||
|
||||
@@ -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
|
||||
--------------
|
||||
|
||||
Reference in New Issue
Block a user