_pytest._py.path: combine PosixPath into LocalPath

This commit is contained in:
Anthony Sottile 2022-10-19 18:44:36 -04:00
parent af078f3a96
commit 6660d45521
1 changed files with 38 additions and 41 deletions

View File

@ -491,7 +491,58 @@ class Stat:
return S_ISLNK(self._osstatresult.st_mode)
class PosixPath(PathBase):
def getuserid(user):
import pwd
if not isinstance(user, int):
user = pwd.getpwnam(user)[2]
return user
def getgroupid(group):
import grp
if not isinstance(group, int):
group = grp.getgrnam(group)[2]
return group
class LocalPath(PathBase):
"""Object oriented interface to os.path and other local filesystem
related information.
"""
class ImportMismatchError(ImportError):
"""raised on pyimport() if there is a mismatch of __file__'s"""
sep = os.sep
def __init__(self, path=None, expanduser=False):
"""Initialize and return a local Path instance.
Path can be relative to the current directory.
If path is None it defaults to the current working directory.
If expanduser is True, tilde-expansion is performed.
Note that Path instances always carry an absolute path.
Note also that passing in a local path object will simply return
the exact same path object. Use new() to get a new copy.
"""
if path is None:
self.strpath = error.checked_call(os.getcwd)
else:
try:
path = os.fspath(path)
except TypeError:
raise ValueError(
"can only pass None, Path instances "
"or non-empty strings to LocalPath"
)
if expanduser:
path = os.path.expanduser(path)
self.strpath = abspath(path)
if sys.platform != "win32":
def chown(self, user, group, rec=0):
"""Change ownership to the given user and group.
user and group may be specified by a number or
@ -527,60 +578,6 @@ class PosixPath(PathBase):
target = self.sep.join(("..",) * n + (relsource,))
error.checked_call(os.symlink, target, self.strpath)
def getuserid(user):
import pwd
if not isinstance(user, int):
user = pwd.getpwnam(user)[2]
return user
def getgroupid(group):
import grp
if not isinstance(group, int):
group = grp.getgrnam(group)[2]
return group
FSBase = not iswin32 and PosixPath or PathBase
class LocalPath(FSBase):
"""Object oriented interface to os.path and other local filesystem
related information.
"""
class ImportMismatchError(ImportError):
"""raised on pyimport() if there is a mismatch of __file__'s"""
sep = os.sep
def __init__(self, path=None, expanduser=False):
"""Initialize and return a local Path instance.
Path can be relative to the current directory.
If path is None it defaults to the current working directory.
If expanduser is True, tilde-expansion is performed.
Note that Path instances always carry an absolute path.
Note also that passing in a local path object will simply return
the exact same path object. Use new() to get a new copy.
"""
if path is None:
self.strpath = error.checked_call(os.getcwd)
else:
try:
path = os.fspath(path)
except TypeError:
raise ValueError(
"can only pass None, Path instances "
"or non-empty strings to LocalPath"
)
if expanduser:
path = os.path.expanduser(path)
self.strpath = abspath(path)
def __hash__(self):
s = self.strpath
if iswin32: