From 59b5bd0e158b60f95e1b21a5b5b008c18be89ee8 Mon Sep 17 00:00:00 2001 From: Daniel Hollas Date: Fri, 30 Aug 2024 22:01:08 +0100 Subject: [PATCH 1/5] Speedup pathlib import by deffering shutil --- Lib/pathlib/_local.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Lib/pathlib/_local.py b/Lib/pathlib/_local.py index 674c98e1b3050e..c4350485c194df 100644 --- a/Lib/pathlib/_local.py +++ b/Lib/pathlib/_local.py @@ -3,7 +3,6 @@ import operator import os import posixpath -import shutil import sys from glob import _StringGlobber from itertools import chain @@ -824,7 +823,9 @@ def rmdir(self): """ os.rmdir(self) - _rmtree = shutil.rmtree + def _rmtree(self): + import shutil + shutil.rmtree(path=self) def rename(self, target): """ From 3957097be1e2cff9c1db5d60b4e307800983dd18 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sat, 31 Aug 2024 11:12:51 +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 --- .../next/Library/2024-08-31-11-12-49.gh-issue-118761.Ai_Ma1.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2024-08-31-11-12-49.gh-issue-118761.Ai_Ma1.rst diff --git a/Misc/NEWS.d/next/Library/2024-08-31-11-12-49.gh-issue-118761.Ai_Ma1.rst b/Misc/NEWS.d/next/Library/2024-08-31-11-12-49.gh-issue-118761.Ai_Ma1.rst new file mode 100644 index 00000000000000..285481d88e5f39 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-08-31-11-12-49.gh-issue-118761.Ai_Ma1.rst @@ -0,0 +1 @@ +Speed up pathlib import time by 15% by deferring shutil import. From 496e0ac5fb528f9387e37b1ee00f7d86f58e9e1c Mon Sep 17 00:00:00 2001 From: Daniel Hollas Date: Sat, 31 Aug 2024 19:34:10 +0100 Subject: [PATCH 3/5] Update Lib/pathlib/_local.py Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> --- Lib/pathlib/_local.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/pathlib/_local.py b/Lib/pathlib/_local.py index c4350485c194df..ff3d338ac22df8 100644 --- a/Lib/pathlib/_local.py +++ b/Lib/pathlib/_local.py @@ -824,6 +824,7 @@ def rmdir(self): os.rmdir(self) def _rmtree(self): + # Lazy import to improve module import time import shutil shutil.rmtree(path=self) From 889836d6a4cdf128adfe4fd6c5606b66ed828e5d Mon Sep 17 00:00:00 2001 From: Daniel Hollas Date: Sat, 31 Aug 2024 19:38:07 +0100 Subject: [PATCH 4/5] Delete News entry --- .../next/Library/2024-08-31-11-12-49.gh-issue-118761.Ai_Ma1.rst | 1 - 1 file changed, 1 deletion(-) delete mode 100644 Misc/NEWS.d/next/Library/2024-08-31-11-12-49.gh-issue-118761.Ai_Ma1.rst diff --git a/Misc/NEWS.d/next/Library/2024-08-31-11-12-49.gh-issue-118761.Ai_Ma1.rst b/Misc/NEWS.d/next/Library/2024-08-31-11-12-49.gh-issue-118761.Ai_Ma1.rst deleted file mode 100644 index 285481d88e5f39..00000000000000 --- a/Misc/NEWS.d/next/Library/2024-08-31-11-12-49.gh-issue-118761.Ai_Ma1.rst +++ /dev/null @@ -1 +0,0 @@ -Speed up pathlib import time by 15% by deferring shutil import. From 0067d84b95d907dc4a4a857bc427ff5f1872d10e Mon Sep 17 00:00:00 2001 From: Daniel Hollas Date: Sat, 31 Aug 2024 19:41:56 +0100 Subject: [PATCH 5/5] remove path keyword --- Lib/pathlib/_local.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/pathlib/_local.py b/Lib/pathlib/_local.py index ff3d338ac22df8..51abe58410bc7c 100644 --- a/Lib/pathlib/_local.py +++ b/Lib/pathlib/_local.py @@ -826,7 +826,7 @@ def rmdir(self): def _rmtree(self): # Lazy import to improve module import time import shutil - shutil.rmtree(path=self) + shutil.rmtree(self) def rename(self, target): """