mirror of
https://github.com/python/cpython.git
synced 2025-09-27 10:50:04 +00:00
GH-127381: pathlib ABCs: remove PathBase.lstat()
(#127382)
Remove the `PathBase.lstat()` method, which is a trivial variation of `stat()`. No user-facing changes because the pathlib ABCs are still private.
This commit is contained in:
parent
15d6506d17
commit
38264a060a
4 changed files with 26 additions and 29 deletions
|
@ -438,14 +438,6 @@ class PathBase(PurePathBase):
|
||||||
"""
|
"""
|
||||||
raise UnsupportedOperation(self._unsupported_msg('stat()'))
|
raise UnsupportedOperation(self._unsupported_msg('stat()'))
|
||||||
|
|
||||||
def lstat(self):
|
|
||||||
"""
|
|
||||||
Like stat(), except if the path points to a symlink, the symlink's
|
|
||||||
status information is returned, rather than its target's.
|
|
||||||
"""
|
|
||||||
return self.stat(follow_symlinks=False)
|
|
||||||
|
|
||||||
|
|
||||||
# Convenience functions for querying the stat results
|
# Convenience functions for querying the stat results
|
||||||
|
|
||||||
def exists(self, *, follow_symlinks=True):
|
def exists(self, *, follow_symlinks=True):
|
||||||
|
@ -505,7 +497,7 @@ class PathBase(PurePathBase):
|
||||||
Whether this path is a symbolic link.
|
Whether this path is a symbolic link.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
return S_ISLNK(self.lstat().st_mode)
|
return S_ISLNK(self.stat(follow_symlinks=False).st_mode)
|
||||||
except (OSError, ValueError):
|
except (OSError, ValueError):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@ -789,7 +781,7 @@ class PathBase(PurePathBase):
|
||||||
def lstat(path_str):
|
def lstat(path_str):
|
||||||
path = self.with_segments(path_str)
|
path = self.with_segments(path_str)
|
||||||
path._resolving = True
|
path._resolving = True
|
||||||
return path.lstat()
|
return path.stat(follow_symlinks=False)
|
||||||
|
|
||||||
def readlink(path_str):
|
def readlink(path_str):
|
||||||
path = self.with_segments(path_str)
|
path = self.with_segments(path_str)
|
||||||
|
|
|
@ -542,6 +542,13 @@ class Path(PathBase, PurePath):
|
||||||
"""
|
"""
|
||||||
return os.stat(self, follow_symlinks=follow_symlinks)
|
return os.stat(self, follow_symlinks=follow_symlinks)
|
||||||
|
|
||||||
|
def lstat(self):
|
||||||
|
"""
|
||||||
|
Like stat(), except if the path points to a symlink, the symlink's
|
||||||
|
status information is returned, rather than its target's.
|
||||||
|
"""
|
||||||
|
return os.lstat(self)
|
||||||
|
|
||||||
def exists(self, *, follow_symlinks=True):
|
def exists(self, *, follow_symlinks=True):
|
||||||
"""
|
"""
|
||||||
Whether this path exists.
|
Whether this path exists.
|
||||||
|
|
|
@ -546,12 +546,9 @@ class PathTest(test_pathlib_abc.DummyPathTest, PurePathTest):
|
||||||
self.addCleanup(os_helper.rmtree, d)
|
self.addCleanup(os_helper.rmtree, d)
|
||||||
return d
|
return d
|
||||||
|
|
||||||
def test_matches_pathbase_api(self):
|
def test_matches_pathbase_docstrings(self):
|
||||||
our_names = {name for name in dir(self.cls) if name[0] != '_'}
|
|
||||||
our_names.remove('is_reserved') # only present in PurePath
|
|
||||||
path_names = {name for name in dir(pathlib._abc.PathBase) if name[0] != '_'}
|
path_names = {name for name in dir(pathlib._abc.PathBase) if name[0] != '_'}
|
||||||
self.assertEqual(our_names, path_names)
|
for attr_name in path_names:
|
||||||
for attr_name in our_names:
|
|
||||||
if attr_name == 'parser':
|
if attr_name == 'parser':
|
||||||
# On Windows, Path.parser is ntpath, but PathBase.parser is
|
# On Windows, Path.parser is ntpath, but PathBase.parser is
|
||||||
# posixpath, and so their docstrings differ.
|
# posixpath, and so their docstrings differ.
|
||||||
|
@ -1357,6 +1354,17 @@ class PathTest(test_pathlib_abc.DummyPathTest, PurePathTest):
|
||||||
with self.assertRaises(pathlib.UnsupportedOperation):
|
with self.assertRaises(pathlib.UnsupportedOperation):
|
||||||
q.symlink_to(p)
|
q.symlink_to(p)
|
||||||
|
|
||||||
|
@needs_symlinks
|
||||||
|
def test_lstat(self):
|
||||||
|
p = self.cls(self.base)/ 'linkA'
|
||||||
|
st = p.stat()
|
||||||
|
self.assertNotEqual(st, p.lstat())
|
||||||
|
|
||||||
|
def test_lstat_nosymlink(self):
|
||||||
|
p = self.cls(self.base) / 'fileA'
|
||||||
|
st = p.stat()
|
||||||
|
self.assertEqual(st, p.lstat())
|
||||||
|
|
||||||
def test_is_junction(self):
|
def test_is_junction(self):
|
||||||
P = self.cls(self.base)
|
P = self.cls(self.base)
|
||||||
|
|
||||||
|
|
|
@ -1351,7 +1351,6 @@ class PathBaseTest(PurePathBaseTest):
|
||||||
p = self.cls('')
|
p = self.cls('')
|
||||||
e = UnsupportedOperation
|
e = UnsupportedOperation
|
||||||
self.assertRaises(e, p.stat)
|
self.assertRaises(e, p.stat)
|
||||||
self.assertRaises(e, p.lstat)
|
|
||||||
self.assertRaises(e, p.exists)
|
self.assertRaises(e, p.exists)
|
||||||
self.assertRaises(e, p.samefile, 'foo')
|
self.assertRaises(e, p.samefile, 'foo')
|
||||||
self.assertRaises(e, p.is_dir)
|
self.assertRaises(e, p.is_dir)
|
||||||
|
@ -2671,17 +2670,6 @@ class DummyPathTest(DummyPurePathTest):
|
||||||
st = p.stat()
|
st = p.stat()
|
||||||
self.assertEqual(st, p.stat(follow_symlinks=False))
|
self.assertEqual(st, p.stat(follow_symlinks=False))
|
||||||
|
|
||||||
@needs_symlinks
|
|
||||||
def test_lstat(self):
|
|
||||||
p = self.cls(self.base)/ 'linkA'
|
|
||||||
st = p.stat()
|
|
||||||
self.assertNotEqual(st, p.lstat())
|
|
||||||
|
|
||||||
def test_lstat_nosymlink(self):
|
|
||||||
p = self.cls(self.base) / 'fileA'
|
|
||||||
st = p.stat()
|
|
||||||
self.assertEqual(st, p.lstat())
|
|
||||||
|
|
||||||
def test_is_dir(self):
|
def test_is_dir(self):
|
||||||
P = self.cls(self.base)
|
P = self.cls(self.base)
|
||||||
self.assertTrue((P / 'dirA').is_dir())
|
self.assertTrue((P / 'dirA').is_dir())
|
||||||
|
@ -2868,11 +2856,13 @@ class DummyPathTest(DummyPurePathTest):
|
||||||
base = self.cls(self.base)
|
base = self.cls(self.base)
|
||||||
base.joinpath('dirA')._delete()
|
base.joinpath('dirA')._delete()
|
||||||
self.assertRaises(FileNotFoundError, base.joinpath('dirA').stat)
|
self.assertRaises(FileNotFoundError, base.joinpath('dirA').stat)
|
||||||
self.assertRaises(FileNotFoundError, base.joinpath('dirA', 'linkC').lstat)
|
self.assertRaises(FileNotFoundError, base.joinpath('dirA', 'linkC').stat,
|
||||||
|
follow_symlinks=False)
|
||||||
base.joinpath('dirB')._delete()
|
base.joinpath('dirB')._delete()
|
||||||
self.assertRaises(FileNotFoundError, base.joinpath('dirB').stat)
|
self.assertRaises(FileNotFoundError, base.joinpath('dirB').stat)
|
||||||
self.assertRaises(FileNotFoundError, base.joinpath('dirB', 'fileB').stat)
|
self.assertRaises(FileNotFoundError, base.joinpath('dirB', 'fileB').stat)
|
||||||
self.assertRaises(FileNotFoundError, base.joinpath('dirB', 'linkD').lstat)
|
self.assertRaises(FileNotFoundError, base.joinpath('dirB', 'linkD').stat,
|
||||||
|
follow_symlinks=False)
|
||||||
base.joinpath('dirC')._delete()
|
base.joinpath('dirC')._delete()
|
||||||
self.assertRaises(FileNotFoundError, base.joinpath('dirC').stat)
|
self.assertRaises(FileNotFoundError, base.joinpath('dirC').stat)
|
||||||
self.assertRaises(FileNotFoundError, base.joinpath('dirC', 'dirD').stat)
|
self.assertRaises(FileNotFoundError, base.joinpath('dirC', 'dirD').stat)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue