Skip to content

Commit 78a7e92

Browse files
committed
Fix Python 3.11 support in paths.py
1 parent 5c4792d commit 78a7e92

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

domdf_python_tools/paths.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,9 @@ class PathPlus(pathlib.Path):
387387
"""
388388

389389
__slots__ = ()
390-
_accessor = pathlib._normal_accessor # type: ignore
390+
391+
if sys.version_info < (3, 11):
392+
_accessor = pathlib._normal_accessor # type: ignore
391393
_closed = False
392394

393395
def _init(self, *args, **kwargs):
@@ -757,7 +759,7 @@ def rename(self: _P, target: Union[str, pathlib.PurePath]) -> _P: # type: ignor
757759
:returns: The new Path instance pointing to the target path.
758760
"""
759761

760-
self._accessor.rename(self, target) # type: ignore
762+
os.rename(self, target) # type: ignore
761763
return self.__class__(target)
762764

763765
def replace(self: _P, target: Union[str, pathlib.PurePath]) -> _P: # type: ignore
@@ -778,7 +780,7 @@ def replace(self: _P, target: Union[str, pathlib.PurePath]) -> _P: # type: igno
778780
:returns: The new Path instance pointing to the target path.
779781
"""
780782

781-
self._accessor.replace(self, target) # type: ignore
783+
os.replace(self, target) # type: ignore
782784
return self.__class__(target)
783785

784786
def unlink(self, missing_ok: bool = False) -> None:
@@ -792,7 +794,7 @@ def unlink(self, missing_ok: bool = False) -> None:
792794
"""
793795

794796
try:
795-
self._accessor.unlink(self)
797+
os.unlink(self)
796798
except FileNotFoundError:
797799
if not missing_ok:
798800
raise

tests/test_paths_stdlib.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,8 @@ def test_mkdir_concurrent_parent_creation(BASE):
431431
p = PathPlus(BASE, "dirCPC%d" % pattern_num)
432432
assert not (p.exists())
433433

434+
real_mkdir = os.mkdir
435+
434436
def my_mkdir(path, mode=0o777):
435437
path = str(path)
436438
# Emulate another process that would create the directory
@@ -439,15 +441,19 @@ def my_mkdir(path, mode=0o777):
439441
# function is called at most 5 times (dirCPC/dir1/dir2,
440442
# dirCPC/dir1, dirCPC, dirCPC/dir1, dirCPC/dir1/dir2).
441443
if pattern.pop():
442-
os.mkdir(path, mode) # From another process.
444+
real_mkdir(path, mode) # From another process.
443445
concurrently_created.add(path)
444-
os.mkdir(path, mode) # Our real call.
446+
real_mkdir(path, mode) # Our real call.
445447

446448
pattern = [bool(pattern_num & (1 << n)) for n in range(5)]
447449
concurrently_created: Set = set()
448450
p12 = p / "dir1" / "dir2"
449451
try:
450-
with mock.patch("pathlib._normal_accessor.mkdir", my_mkdir):
452+
if sys.version_info > (3, 11):
453+
cm = mock.patch("os.mkdir", my_mkdir)
454+
else:
455+
cm = mock.patch("pathlib._normal_accessor.mkdir", my_mkdir)
456+
with cm:
451457
p12.mkdir(parents=True, exist_ok=False)
452458
except FileExistsError:
453459
assert (str(p12) in concurrently_created)

0 commit comments

Comments
 (0)