From d623b82c459265f1ccb6cd9d0df079ab206feadc Mon Sep 17 00:00:00 2001 From: Nineteendo Date: Sat, 30 Mar 2024 20:32:20 +0100 Subject: [PATCH 1/5] Reduce syscalls for `posixpath.ismount` --- Lib/posixpath.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/Lib/posixpath.py b/Lib/posixpath.py index 4fc02be69bd6e1..98a9abaeaa42b2 100644 --- a/Lib/posixpath.py +++ b/Lib/posixpath.py @@ -202,12 +202,7 @@ 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) except (OSError, ValueError): From 07691c4d14784e4f1addce553c9d61555512582b Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sat, 30 Mar 2024 19:34:52 +0000 Subject: [PATCH 2/5] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2024-03-30-19-34-51.gh-issue-117394.qCepkD.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2024-03-30-19-34-51.gh-issue-117394.qCepkD.rst 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..2710aed6b49b36 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2024-03-30-19-34-51.gh-issue-117394.qCepkD.rst @@ -0,0 +1 @@ +Reduce syscalls for :func:`posixpath.ismount`. From cb532fc67beeb49f4f440272ffee08a29919a375 Mon Sep 17 00:00:00 2001 From: Nice Zombies Date: Sat, 30 Mar 2024 20:41:41 +0100 Subject: [PATCH 3/5] Update Misc/NEWS.d/next/Core and Builtins/2024-03-30-19-34-51.gh-issue-117394.qCepkD.rst Co-authored-by: Alex Waygood --- .../2024-03-30-19-34-51.gh-issue-117394.qCepkD.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 index 2710aed6b49b36..7e0ce9ae37ac26 100644 --- 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 @@ -1 +1,2 @@ -Reduce syscalls for :func:`posixpath.ismount`. +Speedup :func:`os.path.ismount` on Unix systems by reducing the number of +syscalls. From d3a0a92e65c0ddd3d418c0cc5345563363f899ce Mon Sep 17 00:00:00 2001 From: Nineteendo Date: Mon, 1 Apr 2024 07:05:28 +0200 Subject: [PATCH 4/5] Fix `ismount('/mnt/storage/link/storage')` --- Lib/posixpath.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/posixpath.py b/Lib/posixpath.py index 98a9abaeaa42b2..33d9cfea4dddb0 100644 --- a/Lib/posixpath.py +++ b/Lib/posixpath.py @@ -204,7 +204,7 @@ def ismount(path): parent = dirname(abspath(path)) try: - s2 = os.lstat(parent) + s2 = os.stat(parent) except (OSError, ValueError): return False From 8fde308876d70abe63d05efbd26cc99444a1b1e6 Mon Sep 17 00:00:00 2001 From: Nineteendo Date: Tue, 2 Apr 2024 12:53:25 +0200 Subject: [PATCH 5/5] monkey patch `os.stat` --- Lib/test/test_posixpath.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) 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):