Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify FileList._safe_path for PathLike support #4251

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/4251.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Refactored egg_info.FileList._safe_path to support ``os.PathLike`` inputs.
1 change: 1 addition & 0 deletions newsfragments/4251.removal.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Bytes inputs to .files are no longer supported. If this removal affects your project or environment, please report the details in the linked PR.
41 changes: 17 additions & 24 deletions setuptools/command/egg_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from .._importlib import metadata
from .. import _entry_points, _normalization
from .._path import StrPath
from . import _requirestxt

from setuptools import Command
Expand Down Expand Up @@ -486,8 +487,7 @@ def append(self, item):
item = item[:-1]
path = convert_path(item)

if self._safe_path(path):
self.files.append(path)
self.extend([path])

def extend(self, paths):
self.files.extend(filter(self._safe_path, paths))
Expand All @@ -502,33 +502,26 @@ def _repair(self):
"""
self.files = list(filter(self._safe_path, self.files))

def _safe_path(self, path):
enc_warn = "'%s' not %s encodable -- skipping"

# To avoid accidental trans-codings errors, first to unicode
u_path = unicode_utils.filesys_decode(path)
if u_path is None:
log.warn("'%s' in unexpected encoding -- skipping" % path)
return False

# Must ensure utf-8 encodability
utf8_path = unicode_utils.try_encode(u_path, "utf-8")
if utf8_path is None:
log.warn(enc_warn, path, 'utf-8')
return False
def _safe_path(self, path: StrPath):
return (
self._encodeable(path)
and not self._ignored_egg_info(path)
and os.path.exists(path)
)

@staticmethod
def _encodeable(path):
if isinstance(path, bytes):
raise ValueError(f"Paths as bytes are no longer supported (got {path})")
try:
# ignore egg-info paths
is_egg_info = ".egg-info" in u_path or b".egg-info" in utf8_path
if self.ignore_egg_info_dir and is_egg_info:
return False
# accept is either way checks out
if os.path.exists(u_path) or os.path.exists(utf8_path):
return True
# this will catch any encode errors decoding u_path
return str(path).encode(sys.getfilesystemencoding())
except UnicodeEncodeError:
enc_warn = "'%s' not %s encodable -- skipping"
log.warn(enc_warn, path, sys.getfilesystemencoding())

def _ignored_egg_info(self, path):
return self.ignore_egg_info_dir and '.egg-info' in str(path)


class manifest_maker(sdist):
template = "MANIFEST.in"
Expand Down
Loading