Skip to content

Commit de2bd6a

Browse files
DeinAlptraumNoumanAmir657
authored andcommitted
[libclang/python/tests] Remove Python <3.6 workarounds (llvm#114399)
This removes workarounds for Python versions before 3.6, since our minimum Python version has been bumped to 3.8
1 parent adf3aa2 commit de2bd6a

File tree

4 files changed

+17
-49
lines changed

4 files changed

+17
-49
lines changed

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
import gc
1313
import unittest
1414
import sys
15-
from .util import skip_if_no_fspath
16-
from .util import str_to_path
17-
15+
from pathlib import Path
1816

1917
kInputsDir = os.path.join(os.path.dirname(__file__), "INPUTS")
2018

@@ -48,13 +46,10 @@ def test_lookup_succeed(self):
4846
cmds = cdb.getCompileCommands("/home/john.doe/MyProject/project.cpp")
4947
self.assertNotEqual(len(cmds), 0)
5048

51-
@skip_if_no_fspath
5249
def test_lookup_succeed_pathlike(self):
5350
"""Same as test_lookup_succeed, but with PathLikes"""
54-
cdb = CompilationDatabase.fromDirectory(str_to_path(kInputsDir))
55-
cmds = cdb.getCompileCommands(
56-
str_to_path("/home/john.doe/MyProject/project.cpp")
57-
)
51+
cdb = CompilationDatabase.fromDirectory(Path(kInputsDir))
52+
cmds = cdb.getCompileCommands(Path("/home/john.doe/MyProject/project.cpp"))
5853
self.assertNotEqual(len(cmds), 0)
5954

6055
def test_all_compilecommand(self):

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
from clang.cindex import TranslationUnit
88

99
import unittest
10-
from .util import skip_if_no_fspath
11-
from .util import str_to_path
10+
from pathlib import Path
1211

1312

1413
class TestCodeCompletion(unittest.TestCase):
@@ -57,11 +56,10 @@ def test_code_complete(self):
5756
]
5857
self.check_completion_results(cr, expected)
5958

60-
@skip_if_no_fspath
6159
def test_code_complete_pathlike(self):
6260
files = [
6361
(
64-
str_to_path("fake.c"),
62+
Path("fake.c"),
6563
"""
6664
/// Aaa.
6765
int test1;
@@ -77,14 +75,14 @@ def test_code_complete_pathlike(self):
7775
]
7876

7977
tu = TranslationUnit.from_source(
80-
str_to_path("fake.c"),
78+
Path("fake.c"),
8179
["-std=c99"],
8280
unsaved_files=files,
8381
options=TranslationUnit.PARSE_INCLUDE_BRIEF_COMMENTS_IN_CODE_COMPLETION,
8482
)
8583

8684
cr = tu.codeComplete(
87-
str_to_path("fake.c"),
85+
Path("fake.c"),
8886
9,
8987
1,
9088
unsaved_files=files,

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

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
from contextlib import contextmanager
88
import gc
99
import os
10-
import sys
1110
import tempfile
1211
import unittest
12+
from pathlib import Path
1313

1414
from clang.cindex import CursorKind
1515
from clang.cindex import Cursor
@@ -22,8 +22,6 @@
2222
from clang.cindex import TranslationUnit
2323
from .util import get_cursor
2424
from .util import get_tu
25-
from .util import skip_if_no_fspath
26-
from .util import str_to_path
2725

2826

2927
kInputsDir = os.path.join(os.path.dirname(__file__), "INPUTS")
@@ -47,7 +45,7 @@ def save_tu_pathlike(tu):
4745
Returns the filename it was saved to.
4846
"""
4947
with tempfile.NamedTemporaryFile() as t:
50-
tu.save(str_to_path(t.name))
48+
tu.save(Path(t.name))
5149
yield t.name
5250

5351

@@ -105,32 +103,29 @@ def test_unsaved_files(self):
105103
self.assertEqual(spellings[-1], "y")
106104

107105
def test_unsaved_files_2(self):
108-
if sys.version_info.major >= 3:
109-
from io import StringIO
110-
else:
111-
from io import BytesIO as StringIO
106+
from io import StringIO
107+
112108
tu = TranslationUnit.from_source(
113109
"fake.c", unsaved_files=[("fake.c", StringIO("int x;"))]
114110
)
115111
spellings = [c.spelling for c in tu.cursor.get_children()]
116112
self.assertEqual(spellings[-1], "x")
117113

118-
@skip_if_no_fspath
119114
def test_from_source_accepts_pathlike(self):
120115
tu = TranslationUnit.from_source(
121-
str_to_path("fake.c"),
116+
Path("fake.c"),
122117
["-Iincludes"],
123118
unsaved_files=[
124119
(
125-
str_to_path("fake.c"),
120+
Path("fake.c"),
126121
"""
127122
#include "fake.h"
128123
int x;
129124
int SOME_DEFINE;
130125
""",
131126
),
132127
(
133-
str_to_path("includes/fake.h"),
128+
Path("includes/fake.h"),
134129
"""
135130
#define SOME_DEFINE y
136131
""",
@@ -192,7 +187,6 @@ def test_save(self):
192187
self.assertTrue(os.path.exists(path))
193188
self.assertGreater(os.path.getsize(path), 0)
194189

195-
@skip_if_no_fspath
196190
def test_save_pathlike(self):
197191
"""Ensure TranslationUnit.save() works with PathLike filename."""
198192

@@ -234,14 +228,13 @@ def test_load(self):
234228
# Just in case there is an open file descriptor somewhere.
235229
del tu2
236230

237-
@skip_if_no_fspath
238231
def test_load_pathlike(self):
239232
"""Ensure TranslationUnits can be constructed from saved files -
240233
PathLike variant."""
241234
tu = get_tu("int foo();")
242235
self.assertEqual(len(tu.diagnostics), 0)
243236
with save_tu(tu) as path:
244-
tu2 = TranslationUnit.from_ast_file(filename=str_to_path(path))
237+
tu2 = TranslationUnit.from_ast_file(filename=Path(path))
245238
self.assertEqual(len(tu2.diagnostics), 0)
246239

247240
foo = get_cursor(tu2, "foo")
@@ -268,18 +261,17 @@ def test_get_file(self):
268261
with self.assertRaises(Exception):
269262
f = tu.get_file("foobar.cpp")
270263

271-
@skip_if_no_fspath
272264
def test_get_file_pathlike(self):
273265
"""Ensure tu.get_file() works appropriately with PathLike filenames."""
274266

275267
tu = get_tu("int foo();")
276268

277-
f = tu.get_file(str_to_path("t.c"))
269+
f = tu.get_file(Path("t.c"))
278270
self.assertIsInstance(f, File)
279271
self.assertEqual(f.name, "t.c")
280272

281273
with self.assertRaises(Exception):
282-
f = tu.get_file(str_to_path("foobar.cpp"))
274+
f = tu.get_file(Path("foobar.cpp"))
283275

284276
def test_get_source_location(self):
285277
"""Ensure tu.get_source_location() works."""

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

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
# This file provides common utility functions for the test suite.
22

3-
import os
4-
5-
HAS_FSPATH = hasattr(os, "fspath")
6-
7-
if HAS_FSPATH:
8-
from pathlib import Path as str_to_path
9-
else:
10-
str_to_path = None
11-
12-
import unittest
13-
143
from clang.cindex import Cursor
154
from clang.cindex import TranslationUnit
165

@@ -81,14 +70,8 @@ def get_cursors(source, spelling):
8170
return cursors
8271

8372

84-
skip_if_no_fspath = unittest.skipUnless(
85-
HAS_FSPATH, "Requires file system path protocol / Python 3.6+"
86-
)
87-
8873
__all__ = [
8974
"get_cursor",
9075
"get_cursors",
9176
"get_tu",
92-
"skip_if_no_fspath",
93-
"str_to_path",
9477
]

0 commit comments

Comments
 (0)