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()`.
This commit is contained in:
Barney Gale 2025-03-21 22:18:20 +00:00 committed by GitHub
parent 56d0f9af14
commit cf9d1a4b6b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 127 additions and 72 deletions

View file

@ -7,25 +7,36 @@
"""
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 @@ def readbytes(self, p):
return f.read()
class LocalPathInfo(pathlib.types.PathInfo):
class LocalPathInfo(PathInfo):
"""
Simple implementation of PathInfo for a local path
"""
@ -123,7 +134,7 @@ def is_symlink(self):
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 @@ def readlink(self):
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.
"""