From 45f5e9b3471aa1ff40194212d93ba887617c8cdd Mon Sep 17 00:00:00 2001 From: Adam Forsyth Date: Mon, 14 May 2018 14:10:46 -0500 Subject: [PATCH 1/2] bpo-33440: Defer imports in pathlib to reduce its import time. --- Lib/pathlib.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Lib/pathlib.py b/Lib/pathlib.py index 8431c29c1d6516..49b7846443d082 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -1,16 +1,13 @@ -import fnmatch import functools import io import ntpath import os import posixpath -import re import sys from _collections_abc import Sequence from errno import EINVAL, ENOENT, ENOTDIR from operator import attrgetter from stat import S_ISDIR, S_ISLNK, S_ISREG, S_ISSOCK, S_ISBLK, S_ISCHR, S_ISFIFO -from urllib.parse import quote_from_bytes as urlquote_from_bytes supports_symlinks = True @@ -226,6 +223,7 @@ def is_reserved(self, parts): def make_uri(self, path): # Under Windows, file URIs use the UTF-8 encoding. + from urllib.parse import quote_from_bytes as urlquote_from_bytes drive = path.drive if len(drive) == 2 and drive[1] == ':': # It's a path on a local drive => 'file:///c:/a/b' @@ -347,6 +345,7 @@ def is_reserved(self, parts): def make_uri(self, path): # We represent the path using the local filesystem encoding, # for portability to other applications. + from urllib.parse import quote_from_bytes as urlquote_from_bytes bpath = bytes(path) return 'file://' + urlquote_from_bytes(bpath) @@ -498,6 +497,8 @@ def _select_from(self, parent_path, is_dir, exists, scandir): class _WildcardSelector(_Selector): def __init__(self, pat, child_parts): + import fnmatch + import re self.pat = re.compile(fnmatch.translate(pat)) _Selector.__init__(self, child_parts) @@ -914,6 +915,7 @@ def match(self, path_pattern): """ Return True if this path matches the given pattern. """ + import fnmatch cf = self._flavour.casefold path_pattern = cf(path_pattern) drv, root, pat_parts = self._flavour.parse_parts((path_pattern,)) From 01d7fd4e86a511884dfc93e302ae7ec49d191486 Mon Sep 17 00:00:00 2001 From: Adam Forsyth Date: Mon, 14 May 2018 14:35:00 -0500 Subject: [PATCH 2/2] bpo-33440: Remove unneeded import renames. --- Lib/pathlib.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Lib/pathlib.py b/Lib/pathlib.py index 49b7846443d082..768d4c8e7a6618 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -223,16 +223,16 @@ def is_reserved(self, parts): def make_uri(self, path): # Under Windows, file URIs use the UTF-8 encoding. - from urllib.parse import quote_from_bytes as urlquote_from_bytes + from urllib.parse import quote_from_bytes drive = path.drive if len(drive) == 2 and drive[1] == ':': # It's a path on a local drive => 'file:///c:/a/b' rest = path.as_posix()[2:].lstrip('/') return 'file:///%s/%s' % ( - drive, urlquote_from_bytes(rest.encode('utf-8'))) + drive, quote_from_bytes(rest.encode('utf-8'))) else: # It's a path on a network drive => 'file://host/share/a/b' - return 'file:' + urlquote_from_bytes(path.as_posix().encode('utf-8')) + return 'file:' + quote_from_bytes(path.as_posix().encode('utf-8')) def gethomedir(self, username): if 'HOME' in os.environ: @@ -345,9 +345,9 @@ def is_reserved(self, parts): def make_uri(self, path): # We represent the path using the local filesystem encoding, # for portability to other applications. - from urllib.parse import quote_from_bytes as urlquote_from_bytes + from urllib.parse import quote_from_bytes bpath = bytes(path) - return 'file://' + urlquote_from_bytes(bpath) + return 'file://' + quote_from_bytes(bpath) def gethomedir(self, username): if not username: