From a2d1b9034f5f0170286c2762cd62eb8293f936fe Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Tue, 2 Apr 2024 11:17:50 +0300 Subject: [PATCH 1/2] gh-117394: Speed up os.path.ismount() on Posix It is now 2-3 times faster if the user has permissions. --- Lib/posixpath.py | 9 ++++++--- .../2024-04-02-11-17-44.gh-issue-117394.2aoSlb.rst | 1 + 2 files changed, 7 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2024-04-02-11-17-44.gh-issue-117394.2aoSlb.rst diff --git a/Lib/posixpath.py b/Lib/posixpath.py index 4fc02be69bd6e1..36b6cd2edd2812 100644 --- a/Lib/posixpath.py +++ b/Lib/posixpath.py @@ -207,11 +207,14 @@ def ismount(path): parent = join(path, b'..') else: parent = join(path, '..') - parent = realpath(parent) try: s2 = os.lstat(parent) - except (OSError, ValueError): - return False + except OSError: + parent = realpath(parent) + try: + s2 = os.lstat(parent) + except (OSError, ValueError): + return False dev1 = s1.st_dev dev2 = s2.st_dev diff --git a/Misc/NEWS.d/next/Library/2024-04-02-11-17-44.gh-issue-117394.2aoSlb.rst b/Misc/NEWS.d/next/Library/2024-04-02-11-17-44.gh-issue-117394.2aoSlb.rst new file mode 100644 index 00000000000000..7b695be4b35d0c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-04-02-11-17-44.gh-issue-117394.2aoSlb.rst @@ -0,0 +1 @@ +:func:`os.path.ismount` is now 2-3 times faster if the user has permissions. From 65a897fdbd0d65143bc87f2de8a671408bee861d Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Wed, 3 Apr 2024 00:19:08 +0300 Subject: [PATCH 2/2] Update Lib/posixpath.py --- Lib/posixpath.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/posixpath.py b/Lib/posixpath.py index 36b6cd2edd2812..c10c80fac34026 100644 --- a/Lib/posixpath.py +++ b/Lib/posixpath.py @@ -213,7 +213,7 @@ def ismount(path): parent = realpath(parent) try: s2 = os.lstat(parent) - except (OSError, ValueError): + except OSError: return False dev1 = s1.st_dev