Skip to content

[libclang/python/tests] Remove Python <3.6 workarounds #114399

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 1 commit into from
Oct 31, 2024

Conversation

DeinAlptraum
Copy link
Contributor

This removes workarounds for Python versions before 3.6, since our minimum Python version has been bumped to 3.8

@DeinAlptraum DeinAlptraum added the clang:as-a-library libclang and C++ API label Oct 31, 2024
@DeinAlptraum DeinAlptraum requested a review from Endilll October 31, 2024 12:51
@llvmbot llvmbot added the clang Clang issues not falling into any other category label Oct 31, 2024
@llvmbot
Copy link
Member

llvmbot commented Oct 31, 2024

@llvm/pr-subscribers-clang

Author: Jannick Kremer (DeinAlptraum)

Changes

This removes workarounds for Python versions before 3.6, since our minimum Python version has been bumped to 3.8


Full diff: https://github.com/llvm/llvm-project/pull/114399.diff

4 Files Affected:

  • (modified) clang/bindings/python/tests/cindex/test_cdb.py (+3-8)
  • (modified) clang/bindings/python/tests/cindex/test_code_completion.py (+4-6)
  • (modified) clang/bindings/python/tests/cindex/test_translation_unit.py (+10-18)
  • (modified) clang/bindings/python/tests/cindex/util.py (-17)
diff --git a/clang/bindings/python/tests/cindex/test_cdb.py b/clang/bindings/python/tests/cindex/test_cdb.py
index a5cc22796aa2ad..e841220016fbe8 100644
--- a/clang/bindings/python/tests/cindex/test_cdb.py
+++ b/clang/bindings/python/tests/cindex/test_cdb.py
@@ -12,9 +12,7 @@
 import gc
 import unittest
 import sys
-from .util import skip_if_no_fspath
-from .util import str_to_path
-
+from pathlib import Path
 
 kInputsDir = os.path.join(os.path.dirname(__file__), "INPUTS")
 
@@ -48,13 +46,10 @@ def test_lookup_succeed(self):
         cmds = cdb.getCompileCommands("/home/john.doe/MyProject/project.cpp")
         self.assertNotEqual(len(cmds), 0)
 
-    @skip_if_no_fspath
     def test_lookup_succeed_pathlike(self):
         """Same as test_lookup_succeed, but with PathLikes"""
-        cdb = CompilationDatabase.fromDirectory(str_to_path(kInputsDir))
-        cmds = cdb.getCompileCommands(
-            str_to_path("/home/john.doe/MyProject/project.cpp")
-        )
+        cdb = CompilationDatabase.fromDirectory(Path(kInputsDir))
+        cmds = cdb.getCompileCommands(Path("/home/john.doe/MyProject/project.cpp"))
         self.assertNotEqual(len(cmds), 0)
 
     def test_all_compilecommand(self):
diff --git a/clang/bindings/python/tests/cindex/test_code_completion.py b/clang/bindings/python/tests/cindex/test_code_completion.py
index 1d513dbca25364..921a8f1f0aac87 100644
--- a/clang/bindings/python/tests/cindex/test_code_completion.py
+++ b/clang/bindings/python/tests/cindex/test_code_completion.py
@@ -7,8 +7,7 @@
 from clang.cindex import TranslationUnit
 
 import unittest
-from .util import skip_if_no_fspath
-from .util import str_to_path
+from pathlib import Path
 
 
 class TestCodeCompletion(unittest.TestCase):
@@ -57,11 +56,10 @@ def test_code_complete(self):
         ]
         self.check_completion_results(cr, expected)
 
-    @skip_if_no_fspath
     def test_code_complete_pathlike(self):
         files = [
             (
-                str_to_path("fake.c"),
+                Path("fake.c"),
                 """
 /// Aaa.
 int test1;
@@ -77,14 +75,14 @@ def test_code_complete_pathlike(self):
         ]
 
         tu = TranslationUnit.from_source(
-            str_to_path("fake.c"),
+            Path("fake.c"),
             ["-std=c99"],
             unsaved_files=files,
             options=TranslationUnit.PARSE_INCLUDE_BRIEF_COMMENTS_IN_CODE_COMPLETION,
         )
 
         cr = tu.codeComplete(
-            str_to_path("fake.c"),
+            Path("fake.c"),
             9,
             1,
             unsaved_files=files,
diff --git a/clang/bindings/python/tests/cindex/test_translation_unit.py b/clang/bindings/python/tests/cindex/test_translation_unit.py
index ff7213c69dd0f1..6181463b0804f3 100644
--- a/clang/bindings/python/tests/cindex/test_translation_unit.py
+++ b/clang/bindings/python/tests/cindex/test_translation_unit.py
@@ -7,9 +7,9 @@
 from contextlib import contextmanager
 import gc
 import os
-import sys
 import tempfile
 import unittest
+from pathlib import Path
 
 from clang.cindex import CursorKind
 from clang.cindex import Cursor
@@ -22,8 +22,6 @@
 from clang.cindex import TranslationUnit
 from .util import get_cursor
 from .util import get_tu
-from .util import skip_if_no_fspath
-from .util import str_to_path
 
 
 kInputsDir = os.path.join(os.path.dirname(__file__), "INPUTS")
@@ -47,7 +45,7 @@ def save_tu_pathlike(tu):
     Returns the filename it was saved to.
     """
     with tempfile.NamedTemporaryFile() as t:
-        tu.save(str_to_path(t.name))
+        tu.save(Path(t.name))
         yield t.name
 
 
@@ -105,24 +103,21 @@ def test_unsaved_files(self):
         self.assertEqual(spellings[-1], "y")
 
     def test_unsaved_files_2(self):
-        if sys.version_info.major >= 3:
-            from io import StringIO
-        else:
-            from io import BytesIO as StringIO
+        from io import StringIO
+
         tu = TranslationUnit.from_source(
             "fake.c", unsaved_files=[("fake.c", StringIO("int x;"))]
         )
         spellings = [c.spelling for c in tu.cursor.get_children()]
         self.assertEqual(spellings[-1], "x")
 
-    @skip_if_no_fspath
     def test_from_source_accepts_pathlike(self):
         tu = TranslationUnit.from_source(
-            str_to_path("fake.c"),
+            Path("fake.c"),
             ["-Iincludes"],
             unsaved_files=[
                 (
-                    str_to_path("fake.c"),
+                    Path("fake.c"),
                     """
 #include "fake.h"
     int x;
@@ -130,7 +125,7 @@ def test_from_source_accepts_pathlike(self):
     """,
                 ),
                 (
-                    str_to_path("includes/fake.h"),
+                    Path("includes/fake.h"),
                     """
 #define SOME_DEFINE y
     """,
@@ -192,7 +187,6 @@ def test_save(self):
             self.assertTrue(os.path.exists(path))
             self.assertGreater(os.path.getsize(path), 0)
 
-    @skip_if_no_fspath
     def test_save_pathlike(self):
         """Ensure TranslationUnit.save() works with PathLike filename."""
 
@@ -234,14 +228,13 @@ def test_load(self):
             # Just in case there is an open file descriptor somewhere.
             del tu2
 
-    @skip_if_no_fspath
     def test_load_pathlike(self):
         """Ensure TranslationUnits can be constructed from saved files -
         PathLike variant."""
         tu = get_tu("int foo();")
         self.assertEqual(len(tu.diagnostics), 0)
         with save_tu(tu) as path:
-            tu2 = TranslationUnit.from_ast_file(filename=str_to_path(path))
+            tu2 = TranslationUnit.from_ast_file(filename=Path(path))
             self.assertEqual(len(tu2.diagnostics), 0)
 
             foo = get_cursor(tu2, "foo")
@@ -268,18 +261,17 @@ def test_get_file(self):
         with self.assertRaises(Exception):
             f = tu.get_file("foobar.cpp")
 
-    @skip_if_no_fspath
     def test_get_file_pathlike(self):
         """Ensure tu.get_file() works appropriately with PathLike filenames."""
 
         tu = get_tu("int foo();")
 
-        f = tu.get_file(str_to_path("t.c"))
+        f = tu.get_file(Path("t.c"))
         self.assertIsInstance(f, File)
         self.assertEqual(f.name, "t.c")
 
         with self.assertRaises(Exception):
-            f = tu.get_file(str_to_path("foobar.cpp"))
+            f = tu.get_file(Path("foobar.cpp"))
 
     def test_get_source_location(self):
         """Ensure tu.get_source_location() works."""
diff --git a/clang/bindings/python/tests/cindex/util.py b/clang/bindings/python/tests/cindex/util.py
index 8ba3114b35d1e1..e2d150a7b6b431 100644
--- a/clang/bindings/python/tests/cindex/util.py
+++ b/clang/bindings/python/tests/cindex/util.py
@@ -1,16 +1,5 @@
 # This file provides common utility functions for the test suite.
 
-import os
-
-HAS_FSPATH = hasattr(os, "fspath")
-
-if HAS_FSPATH:
-    from pathlib import Path as str_to_path
-else:
-    str_to_path = None
-
-import unittest
-
 from clang.cindex import Cursor
 from clang.cindex import TranslationUnit
 
@@ -81,14 +70,8 @@ def get_cursors(source, spelling):
     return cursors
 
 
-skip_if_no_fspath = unittest.skipUnless(
-    HAS_FSPATH, "Requires file system path protocol / Python 3.6+"
-)
-
 __all__ = [
     "get_cursor",
     "get_cursors",
     "get_tu",
-    "skip_if_no_fspath",
-    "str_to_path",
 ]

@DeinAlptraum DeinAlptraum merged commit 31faa39 into llvm:main Oct 31, 2024
13 checks passed
@DeinAlptraum DeinAlptraum deleted the compat branch October 31, 2024 14:21
smallp-o-p pushed a commit to smallp-o-p/llvm-project that referenced this pull request Nov 3, 2024
This removes workarounds for Python versions before 3.6, since our
minimum Python version has been bumped to 3.8
NoumanAmir657 pushed a commit to NoumanAmir657/llvm-project that referenced this pull request Nov 4, 2024
This removes workarounds for Python versions before 3.6, since our
minimum Python version has been bumped to 3.8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:as-a-library libclang and C++ API clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants