Skip to content

bpo-33440: Defer imports in pathlib to reduce its import time. #6820

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason of the renaming?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no reason, you are right but this is just a copy. Now he could remove the renaming.

drive = path.drive
if len(drive) == 2 and drive[1] == ':':
# It's a path on a local drive => 'file:///c:/a/b'
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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,))
Expand Down