Skip to content

Commit

Permalink
Refactor _safe_path into three functions. Remove support for bytes. A…
Browse files Browse the repository at this point in the history
…llow for path to be `PathLike`.
  • Loading branch information
jaraco committed Mar 3, 2024
1 parent 83a8e46 commit 7918b35
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
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.
38 changes: 16 additions & 22 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 @@ -501,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

0 comments on commit 7918b35

Please sign in to comment.