Skip to content

Commit 38264a0

Browse files
authored
pythonGH-127381: pathlib ABCs: remove PathBase.lstat() (python#127382)
Remove the `PathBase.lstat()` method, which is a trivial variation of `stat()`. No user-facing changes because the pathlib ABCs are still private.
1 parent 15d6506 commit 38264a0

File tree

4 files changed

+26
-29
lines changed

4 files changed

+26
-29
lines changed

Lib/pathlib/_abc.py

+2-10
Original file line numberDiff line numberDiff line change
@@ -438,14 +438,6 @@ def stat(self, *, follow_symlinks=True):
438438
"""
439439
raise UnsupportedOperation(self._unsupported_msg('stat()'))
440440

441-
def lstat(self):
442-
"""
443-
Like stat(), except if the path points to a symlink, the symlink's
444-
status information is returned, rather than its target's.
445-
"""
446-
return self.stat(follow_symlinks=False)
447-
448-
449441
# Convenience functions for querying the stat results
450442

451443
def exists(self, *, follow_symlinks=True):
@@ -505,7 +497,7 @@ def is_symlink(self):
505497
Whether this path is a symbolic link.
506498
"""
507499
try:
508-
return S_ISLNK(self.lstat().st_mode)
500+
return S_ISLNK(self.stat(follow_symlinks=False).st_mode)
509501
except (OSError, ValueError):
510502
return False
511503

@@ -789,7 +781,7 @@ def raise_error(*args):
789781
def lstat(path_str):
790782
path = self.with_segments(path_str)
791783
path._resolving = True
792-
return path.lstat()
784+
return path.stat(follow_symlinks=False)
793785

794786
def readlink(path_str):
795787
path = self.with_segments(path_str)

Lib/pathlib/_local.py

+7
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,13 @@ def stat(self, *, follow_symlinks=True):
542542
"""
543543
return os.stat(self, follow_symlinks=follow_symlinks)
544544

545+
def lstat(self):
546+
"""
547+
Like stat(), except if the path points to a symlink, the symlink's
548+
status information is returned, rather than its target's.
549+
"""
550+
return os.lstat(self)
551+
545552
def exists(self, *, follow_symlinks=True):
546553
"""
547554
Whether this path exists.

Lib/test/test_pathlib/test_pathlib.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -546,12 +546,9 @@ def tempdir(self):
546546
self.addCleanup(os_helper.rmtree, d)
547547
return d
548548

549-
def test_matches_pathbase_api(self):
550-
our_names = {name for name in dir(self.cls) if name[0] != '_'}
551-
our_names.remove('is_reserved') # only present in PurePath
549+
def test_matches_pathbase_docstrings(self):
552550
path_names = {name for name in dir(pathlib._abc.PathBase) if name[0] != '_'}
553-
self.assertEqual(our_names, path_names)
554-
for attr_name in our_names:
551+
for attr_name in path_names:
555552
if attr_name == 'parser':
556553
# On Windows, Path.parser is ntpath, but PathBase.parser is
557554
# posixpath, and so their docstrings differ.
@@ -1357,6 +1354,17 @@ def test_symlink_to_unsupported(self):
13571354
with self.assertRaises(pathlib.UnsupportedOperation):
13581355
q.symlink_to(p)
13591356

1357+
@needs_symlinks
1358+
def test_lstat(self):
1359+
p = self.cls(self.base)/ 'linkA'
1360+
st = p.stat()
1361+
self.assertNotEqual(st, p.lstat())
1362+
1363+
def test_lstat_nosymlink(self):
1364+
p = self.cls(self.base) / 'fileA'
1365+
st = p.stat()
1366+
self.assertEqual(st, p.lstat())
1367+
13601368
def test_is_junction(self):
13611369
P = self.cls(self.base)
13621370

Lib/test/test_pathlib/test_pathlib_abc.py

+4-14
Original file line numberDiff line numberDiff line change
@@ -1351,7 +1351,6 @@ def test_unsupported_operation(self):
13511351
p = self.cls('')
13521352
e = UnsupportedOperation
13531353
self.assertRaises(e, p.stat)
1354-
self.assertRaises(e, p.lstat)
13551354
self.assertRaises(e, p.exists)
13561355
self.assertRaises(e, p.samefile, 'foo')
13571356
self.assertRaises(e, p.is_dir)
@@ -2671,17 +2670,6 @@ def test_stat_no_follow_symlinks_nosymlink(self):
26712670
st = p.stat()
26722671
self.assertEqual(st, p.stat(follow_symlinks=False))
26732672

2674-
@needs_symlinks
2675-
def test_lstat(self):
2676-
p = self.cls(self.base)/ 'linkA'
2677-
st = p.stat()
2678-
self.assertNotEqual(st, p.lstat())
2679-
2680-
def test_lstat_nosymlink(self):
2681-
p = self.cls(self.base) / 'fileA'
2682-
st = p.stat()
2683-
self.assertEqual(st, p.lstat())
2684-
26852673
def test_is_dir(self):
26862674
P = self.cls(self.base)
26872675
self.assertTrue((P / 'dirA').is_dir())
@@ -2868,11 +2856,13 @@ def test_delete_dir(self):
28682856
base = self.cls(self.base)
28692857
base.joinpath('dirA')._delete()
28702858
self.assertRaises(FileNotFoundError, base.joinpath('dirA').stat)
2871-
self.assertRaises(FileNotFoundError, base.joinpath('dirA', 'linkC').lstat)
2859+
self.assertRaises(FileNotFoundError, base.joinpath('dirA', 'linkC').stat,
2860+
follow_symlinks=False)
28722861
base.joinpath('dirB')._delete()
28732862
self.assertRaises(FileNotFoundError, base.joinpath('dirB').stat)
28742863
self.assertRaises(FileNotFoundError, base.joinpath('dirB', 'fileB').stat)
2875-
self.assertRaises(FileNotFoundError, base.joinpath('dirB', 'linkD').lstat)
2864+
self.assertRaises(FileNotFoundError, base.joinpath('dirB', 'linkD').stat,
2865+
follow_symlinks=False)
28762866
base.joinpath('dirC')._delete()
28772867
self.assertRaises(FileNotFoundError, base.joinpath('dirC').stat)
28782868
self.assertRaises(FileNotFoundError, base.joinpath('dirC', 'dirD').stat)

0 commit comments

Comments
 (0)