aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/test/test_pathlib/support/local_path.py
diff options
context:
space:
mode:
authorBarney Gale <barney.gale@gmail.com>2025-03-21 22:18:20 +0000
committerGitHub <noreply@github.com>2025-03-21 22:18:20 +0000
commitcf9d1a4b6b28a76a49edba4028d5533195172287 (patch)
treec20da37c6f435fe18a84b59ca83cbb3fae81c91f /Lib/test/test_pathlib/support/local_path.py
parent56d0f9af147b2280ea0af7af5e57df1a01271991 (diff)
downloadcpython-cf9d1a4b6b28a76a49edba4028d5533195172287.tar.gz
cpython-cf9d1a4b6b28a76a49edba4028d5533195172287.zip
GH-128520: pathlib ABCs: allow tests to be run externally (#131315)
Adjust the tests for the `pathlib.types` module so that they can be run against the `pathlib-abc` PyPI package, which is a backport of the module for older Python versions. Specifically, we add a `.support.is_pypi` switch that is false in the stdlib and true in the pathlib-abc package. This controls which package we import, and whether or not we run tests against `PurePath` and `Path`. For compatibility with older Python versions, we stop using `zipfile.ZipFile.mkdir()` and `zipfile.ZipInfo._for_archive()`.
Diffstat (limited to 'Lib/test/test_pathlib/support/local_path.py')
-rw-r--r--Lib/test/test_pathlib/support/local_path.py29
1 files changed, 20 insertions, 9 deletions
diff --git a/Lib/test/test_pathlib/support/local_path.py b/Lib/test/test_pathlib/support/local_path.py
index 1cf64316b40..4f027754f6a 100644
--- a/Lib/test/test_pathlib/support/local_path.py
+++ b/Lib/test/test_pathlib/support/local_path.py
@@ -7,25 +7,36 @@ about local paths in tests.
"""
import os
-import pathlib.types
-from test.support import os_helper
-from test.test_pathlib.support.lexical_path import LexicalPath
+from . import is_pypi
+from .lexical_path import LexicalPath
+
+if is_pypi:
+ from shutil import rmtree
+ from pathlib_abc import PathInfo, _ReadablePath, _WritablePath
+ can_symlink = True
+ testfn = "TESTFN"
+else:
+ from pathlib.types import PathInfo, _ReadablePath, _WritablePath
+ from test.support import os_helper
+ can_symlink = os_helper.can_symlink()
+ testfn = os_helper.TESTFN
+ rmtree = os_helper.rmtree
class LocalPathGround:
- can_symlink = os_helper.can_symlink()
+ can_symlink = can_symlink
def __init__(self, path_cls):
self.path_cls = path_cls
def setup(self, local_suffix=""):
- root = self.path_cls(os_helper.TESTFN + local_suffix)
+ root = self.path_cls(testfn + local_suffix)
os.mkdir(root)
return root
def teardown(self, root):
- os_helper.rmtree(root)
+ rmtree(root)
def create_file(self, p, data=b''):
with open(p, 'wb') as f:
@@ -79,7 +90,7 @@ class LocalPathGround:
return f.read()
-class LocalPathInfo(pathlib.types.PathInfo):
+class LocalPathInfo(PathInfo):
"""
Simple implementation of PathInfo for a local path
"""
@@ -123,7 +134,7 @@ class LocalPathInfo(pathlib.types.PathInfo):
return self._is_symlink
-class ReadableLocalPath(pathlib.types._ReadablePath, LexicalPath):
+class ReadableLocalPath(_ReadablePath, LexicalPath):
"""
Simple implementation of a ReadablePath class for local filesystem paths.
"""
@@ -146,7 +157,7 @@ class ReadableLocalPath(pathlib.types._ReadablePath, LexicalPath):
return self.with_segments(os.readlink(self))
-class WritableLocalPath(pathlib.types._WritablePath, LexicalPath):
+class WritableLocalPath(_WritablePath, LexicalPath):
"""
Simple implementation of a WritablePath class for local filesystem paths.
"""