diff --git a/Lib/posixpath.py b/Lib/posixpath.py index 4fc02be69bd6e1..33d9cfea4dddb0 100644 --- a/Lib/posixpath.py +++ b/Lib/posixpath.py @@ -202,14 +202,9 @@ def ismount(path): if stat.S_ISLNK(s1.st_mode): return False - path = os.fspath(path) - if isinstance(path, bytes): - parent = join(path, b'..') - else: - parent = join(path, '..') - parent = realpath(parent) + parent = dirname(abspath(path)) try: - s2 = os.lstat(parent) + s2 = os.stat(parent) except (OSError, ValueError): return False diff --git a/Lib/test/test_posixpath.py b/Lib/test/test_posixpath.py index cbb7c4c52d9697..3abef388d1fc55 100644 --- a/Lib/test/test_posixpath.py +++ b/Lib/test/test_posixpath.py @@ -242,6 +242,7 @@ def test_ismount_symlinks(self): def test_ismount_different_device(self): # Simulate the path being on a different device from its parent by # mocking out st_dev. + save_stat = os.stat save_lstat = os.lstat def fake_lstat(path): st_ino = 0 @@ -251,15 +252,17 @@ def fake_lstat(path): st_ino = 1 return posix.stat_result((0, st_ino, st_dev, 0, 0, 0, 0, 0, 0, 0)) try: - os.lstat = fake_lstat + os.stat = os.lstat = fake_lstat self.assertIs(posixpath.ismount(ABSTFN), True) finally: + os.stat = save_stat os.lstat = save_lstat @unittest.skipIf(posix is None, "Test requires posix module") def test_ismount_directory_not_readable(self): # issue #2466: Simulate ismount run on a directory that is not # readable, which used to return False. + save_stat = os.stat save_lstat = os.lstat def fake_lstat(path): st_ino = 0 @@ -273,9 +276,10 @@ def fake_lstat(path): st_ino = 1 return posix.stat_result((0, st_ino, st_dev, 0, 0, 0, 0, 0, 0, 0)) try: - os.lstat = fake_lstat + os.stat = os.lstat = fake_lstat self.assertIs(posixpath.ismount(ABSTFN), True) finally: + os.stat = save_stat os.lstat = save_lstat def test_isjunction(self): diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-03-30-19-34-51.gh-issue-117394.qCepkD.rst b/Misc/NEWS.d/next/Core and Builtins/2024-03-30-19-34-51.gh-issue-117394.qCepkD.rst new file mode 100644 index 00000000000000..7e0ce9ae37ac26 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2024-03-30-19-34-51.gh-issue-117394.qCepkD.rst @@ -0,0 +1,2 @@ +Speedup :func:`os.path.ismount` on Unix systems by reducing the number of +syscalls.