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

@ -8,12 +8,18 @@ about zip file members in tests.
import errno
import io
import pathlib.types
import posixpath
import stat
import zipfile
from stat import S_IFMT, S_ISDIR, S_ISREG, S_ISLNK
from . import is_pypi
if is_pypi:
from pathlib_abc import PathInfo, _ReadablePath, _WritablePath
else:
from pathlib.types import PathInfo, _ReadablePath, _WritablePath
class ZipPathGround:
can_symlink = True
@ -31,7 +37,10 @@ class ZipPathGround:
path.zip_file.writestr(str(path), data)
def create_dir(self, path):
path.zip_file.mkdir(str(path))
zip_info = zipfile.ZipInfo(str(path) + '/')
zip_info.external_attr |= stat.S_IFDIR << 16
zip_info.external_attr |= stat.FILE_ATTRIBUTE_DIRECTORY
path.zip_file.writestr(zip_info, '')
def create_symlink(self, path, target):
zip_info = zipfile.ZipInfo(str(path))
@ -80,7 +89,7 @@ class ZipPathGround:
return stat.S_ISLNK(info.external_attr >> 16)
class MissingZipPathInfo:
class MissingZipPathInfo(PathInfo):
"""
PathInfo implementation that is used when a zip file member is missing.
"""
@ -105,7 +114,7 @@ class MissingZipPathInfo:
missing_zip_path_info = MissingZipPathInfo()
class ZipPathInfo:
class ZipPathInfo(PathInfo):
"""
PathInfo implementation for an existing zip file member.
"""
@ -216,7 +225,7 @@ class ZipFileList:
self.tree.resolve(item.filename, create=True).zip_info = item
class ReadableZipPath(pathlib.types._ReadablePath):
class ReadableZipPath(_ReadablePath):
"""
Simple implementation of a ReadablePath class for .zip files.
"""
@ -279,7 +288,7 @@ class ReadableZipPath(pathlib.types._ReadablePath):
return self.with_segments(self.zip_file.read(info.zip_info).decode())
class WritableZipPath(pathlib.types._WritablePath):
class WritableZipPath(_WritablePath):
"""
Simple implementation of a WritablePath class for .zip files.
"""
@ -314,10 +323,13 @@ class WritableZipPath(pathlib.types._WritablePath):
return self.zip_file.open(str(self), 'w')
def mkdir(self, mode=0o777):
self.zip_file.mkdir(str(self), mode)
zinfo = zipfile.ZipInfo(str(self) + '/')
zinfo.external_attr |= stat.S_IFDIR << 16
zinfo.external_attr |= stat.FILE_ATTRIBUTE_DIRECTORY
self.zip_file.writestr(zinfo, '')
def symlink_to(self, target, target_is_directory=False):
zinfo = zipfile.ZipInfo(str(self))._for_archive(self.zip_file)
zinfo = zipfile.ZipInfo(str(self))
zinfo.external_attr = stat.S_IFLNK << 16
if target_is_directory:
zinfo.external_attr |= 0x10