Skip to content

Commit

Permalink
MAINT: py3k: remove os.fspath and os.PathLike backports (#17473)
Browse files Browse the repository at this point in the history
Since we no longer support Python 3.5 and below, this code is unreachable.

As with the rest of `py3k.py`, we leave behind the aliases to prevent breaking downstream code.
  • Loading branch information
zer0link committed Oct 10, 2020
1 parent aeb2374 commit 4afd82d
Showing 1 changed file with 2 additions and 51 deletions.
53 changes: 2 additions & 51 deletions numpy/compat/py3k.py
Expand Up @@ -133,54 +133,5 @@ def npy_load_module(name, fn, info=None):


# Backport os.fs_path, os.PathLike, and PurePath.__fspath__
if sys.version_info[:2] >= (3, 6):
os_fspath = os.fspath
os_PathLike = os.PathLike
else:
def _PurePath__fspath__(self):
return str(self)

class os_PathLike(abc_ABC):
"""Abstract base class for implementing the file system path protocol."""

@abc.abstractmethod
def __fspath__(self):
"""Return the file system path representation of the object."""
raise NotImplementedError

@classmethod
def __subclasshook__(cls, subclass):
if PurePath is not None and issubclass(subclass, PurePath):
return True
return hasattr(subclass, '__fspath__')


def os_fspath(path):
"""Return the path representation of a path-like object.
If str or bytes is passed in, it is returned unchanged. Otherwise the
os.PathLike interface is used to get the path representation. If the
path representation is not str or bytes, TypeError is raised. If the
provided path is not str, bytes, or os.PathLike, TypeError is raised.
"""
if isinstance(path, (str, bytes)):
return path

# Work from the object's type to match method resolution of other magic
# methods.
path_type = type(path)
try:
path_repr = path_type.__fspath__(path)
except AttributeError:
if hasattr(path_type, '__fspath__'):
raise
elif PurePath is not None and issubclass(path_type, PurePath):
return _PurePath__fspath__(path)
else:
raise TypeError("expected str, bytes or os.PathLike object, "
"not " + path_type.__name__)
if isinstance(path_repr, (str, bytes)):
return path_repr
else:
raise TypeError("expected {}.__fspath__() to return str or bytes, "
"not {}".format(path_type.__name__,
type(path_repr).__name__))
os_fspath = os.fspath
os_PathLike = os.PathLike

0 comments on commit 4afd82d

Please sign in to comment.