]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-137855: Improve import time of `pathlib` (GH-146327)
authorHugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Tue, 5 May 2026 12:38:03 +0000 (15:38 +0300)
committerGitHub <noreply@github.com>
Tue, 5 May 2026 12:38:03 +0000 (14:38 +0200)
Improve import time of pathlib

Lib/pathlib/__init__.py
Lib/test/test_pathlib/test_pathlib.py

index 44f967eb12dd4fdf1257e50bf8360e958e1211e4..a32e4b5320ff6dd61d5f2ef05862a64286067224 100644 (file)
@@ -12,12 +12,13 @@ import os
 import posixpath
 import sys
 from errno import *
-from glob import _StringGlobber, _no_recurse_symlinks
 from itertools import chain
 from stat import (
     S_IMODE, S_ISDIR, S_ISREG, S_ISLNK, S_ISSOCK, S_ISBLK, S_ISCHR, S_ISFIFO,
 )
 from _collections_abc import Sequence
+lazy import shutil
+lazy from glob import _StringGlobber, _no_recurse_symlinks
 
 try:
     import pwd
@@ -1255,8 +1256,6 @@ class Path(PurePath):
         if self.is_symlink() or self.is_junction():
             self.unlink()
         elif self.is_dir():
-            # Lazy import to improve module import time
-            import shutil
             shutil.rmtree(self)
         else:
             self.unlink()
index 19f4506c109c147f84fe4fb81c567c419d0871fa..09d1b5d725e5ba72f27804205111e917323c1206 100644 (file)
@@ -81,7 +81,7 @@ class UnsupportedOperationTest(unittest.TestCase):
 class LazyImportTest(unittest.TestCase):
     @cpython_only
     def test_lazy_import(self):
-        import_helper.ensure_lazy_imports("pathlib", {"shutil"})
+        import_helper.ensure_lazy_imports("pathlib", {"glob", "shutil"})
 
 
 #