Skip to content
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

Clean up urllib imports #9371

Merged
merged 1 commit into from
Dec 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
Empty file.
22 changes: 11 additions & 11 deletions src/pip/_internal/index/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import mimetypes
import os
import re
import urllib.parse
import urllib.request
from collections import OrderedDict
from urllib import parse as urllib_parse
from urllib import request as urllib_request

from pip._vendor import html5lib, requests
from pip._vendor.distlib.compat import unescape
Expand Down Expand Up @@ -94,7 +94,7 @@ def _ensure_html_response(url, session):
Raises `_NotHTTP` if the URL is not available for a HEAD request, or
`_NotHTML` if the content type is not text/html.
"""
scheme, netloc, path, query, fragment = urllib_parse.urlsplit(url)
scheme, netloc, path, query, fragment = urllib.parse.urlsplit(url)
if scheme not in {'http', 'https'}:
raise _NotHTTP()

Expand Down Expand Up @@ -192,7 +192,7 @@ def _clean_url_path_part(part):
Clean a "part" of a URL path (i.e. after splitting on "@" characters).
"""
# We unquote prior to quoting to make sure nothing is double quoted.
return urllib_parse.quote(urllib_parse.unquote(part))
return urllib.parse.quote(urllib.parse.unquote(part))


def _clean_file_url_path(part):
Expand All @@ -206,7 +206,7 @@ def _clean_file_url_path(part):
# should not be quoted. On Linux where drive letters do not
# exist, the colon should be quoted. We rely on urllib.request
# to do the right thing here.
return urllib_request.pathname2url(urllib_request.url2pathname(part))
return urllib.request.pathname2url(urllib.request.url2pathname(part))


# percent-encoded: /
Expand Down Expand Up @@ -245,11 +245,11 @@ def _clean_link(url):
"""
# Split the URL into parts according to the general structure
# `scheme://netloc/path;parameters?query#fragment`.
result = urllib_parse.urlparse(url)
result = urllib.parse.urlparse(url)
# If the netloc is empty, then the URL refers to a local filesystem path.
is_local_path = not result.netloc
path = _clean_url_path(result.path, is_local_path=is_local_path)
return urllib_parse.urlunparse(result._replace(path=path))
return urllib.parse.urlunparse(result._replace(path=path))


def _create_link_from_element(
Expand All @@ -265,7 +265,7 @@ def _create_link_from_element(
if not href:
return None

url = _clean_link(urllib_parse.urljoin(base_url, href))
url = _clean_link(urllib.parse.urljoin(base_url, href))
pyrequire = anchor.get('data-requires-python')
pyrequire = unescape(pyrequire) if pyrequire else None

Expand Down Expand Up @@ -416,13 +416,13 @@ def _get_html_page(link, session=None):
return None

# Tack index.html onto file:// URLs that point to directories
scheme, _, path, _, _, _ = urllib_parse.urlparse(url)
if (scheme == 'file' and os.path.isdir(urllib_request.url2pathname(path))):
scheme, _, path, _, _, _ = urllib.parse.urlparse(url)
if (scheme == 'file' and os.path.isdir(urllib.request.url2pathname(path))):
# add trailing slash if not present so urljoin doesn't trim
# final segment
if not url.endswith('/'):
url += '/'
url = urllib_parse.urljoin(url, 'index.html')
url = urllib.parse.urljoin(url, 'index.html')
logger.debug(' file: URL is directory, getting %s', url)

try:
Expand Down
6 changes: 3 additions & 3 deletions src/pip/_internal/models/direct_url.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
""" PEP 610 """
import json
import re
from urllib import parse as urllib_parse
import urllib.parse

from pip._internal.utils.typing import MYPY_CHECK_RUNNING

Expand Down Expand Up @@ -194,9 +194,9 @@ def redacted_url(self):
environment variables as specified in PEP 610, or it is ``git``
in the case of a git URL.
"""
purl = urllib_parse.urlsplit(self.url)
purl = urllib.parse.urlsplit(self.url)
netloc = self._remove_auth_from_netloc(purl.netloc)
surl = urllib_parse.urlunsplit(
surl = urllib.parse.urlunsplit(
(purl.scheme, netloc, purl.path, purl.query, purl.fragment)
)
return surl
Expand Down
6 changes: 3 additions & 3 deletions src/pip/_internal/models/index.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from urllib import parse as urllib_parse
import urllib.parse


class PackageIndex:
Expand All @@ -12,7 +12,7 @@ def __init__(self, url, file_storage_domain):
# type: (str, str) -> None
super().__init__()
self.url = url
self.netloc = urllib_parse.urlsplit(url).netloc
self.netloc = urllib.parse.urlsplit(url).netloc
self.simple_url = self._url_for_path('simple')
self.pypi_url = self._url_for_path('pypi')

Expand All @@ -23,7 +23,7 @@ def __init__(self, url, file_storage_domain):

def _url_for_path(self, path):
# type: (str) -> str
return urllib_parse.urljoin(self.url, path)
return urllib.parse.urljoin(self.url, path)


PyPI = PackageIndex(
Expand Down
10 changes: 5 additions & 5 deletions src/pip/_internal/models/link.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import posixpath
import re
from urllib import parse as urllib_parse
import urllib.parse

from pip._internal.utils.filetypes import WHEEL_EXTENSION
from pip._internal.utils.misc import (
Expand Down Expand Up @@ -67,7 +67,7 @@ def __init__(
if url.startswith('\\\\'):
url = path_to_url(url)

self._parsed_url = urllib_parse.urlsplit(url)
self._parsed_url = urllib.parse.urlsplit(url)
# Store the url as a private attribute to prevent accidentally
# trying to set a new value.
self._url = url
Expand Down Expand Up @@ -112,7 +112,7 @@ def filename(self):
netloc, user_pass = split_auth_from_netloc(self.netloc)
return netloc

name = urllib_parse.unquote(name)
name = urllib.parse.unquote(name)
assert name, (
'URL {self._url!r} produced no filename'.format(**locals()))
return name
Expand All @@ -138,7 +138,7 @@ def netloc(self):
@property
def path(self):
# type: () -> str
return urllib_parse.unquote(self._parsed_url.path)
return urllib.parse.unquote(self._parsed_url.path)

def splitext(self):
# type: () -> Tuple[str, str]
Expand All @@ -153,7 +153,7 @@ def ext(self):
def url_without_fragment(self):
# type: () -> str
scheme, netloc, path, query, fragment = self._parsed_url
return urllib_parse.urlunsplit((scheme, netloc, path, query, None))
return urllib.parse.urlunsplit((scheme, netloc, path, query, None))

_egg_fragment_re = re.compile(r'[#&]egg=([^&]*)')

Expand Down
8 changes: 4 additions & 4 deletions src/pip/_internal/models/search_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
import os
import posixpath
from urllib import parse as urllib_parse
import urllib.parse

from pip._vendor.packaging.utils import canonicalize_name

Expand Down Expand Up @@ -53,7 +53,7 @@ def create(
# relies on TLS.
if not has_tls():
for link in itertools.chain(index_urls, built_find_links):
parsed = urllib_parse.urlparse(link)
parsed = urllib.parse.urlparse(link)
if parsed.scheme == 'https':
logger.warning(
'pip is configured with locations that require '
Expand Down Expand Up @@ -86,7 +86,7 @@ def get_formatted_locations(self):
redacted_index_url = redact_auth_from_url(url)

# Parse the URL
purl = urllib_parse.urlsplit(redacted_index_url)
purl = urllib.parse.urlsplit(redacted_index_url)

# URL is generally invalid if scheme and netloc is missing
# there are issues with Python and URL parsing, so this test
Expand Down Expand Up @@ -122,7 +122,7 @@ def mkurl_pypi_url(url):
# type: (str) -> str
loc = posixpath.join(
url,
urllib_parse.quote(canonicalize_name(project_name)))
urllib.parse.quote(canonicalize_name(project_name)))
# For maximum compatibility with easy_install, ensure the path
# ends in a trailing slash. Although this isn't in the spec
# (and PyPI can handle it without the slash) some other index
Expand Down
4 changes: 2 additions & 2 deletions src/pip/_internal/network/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""

import logging
from urllib import parse as urllib_parse
import urllib.parse

from pip._vendor.requests.auth import AuthBase, HTTPBasicAuth
from pip._vendor.requests.utils import get_netrc_auth
Expand Down Expand Up @@ -250,7 +250,7 @@ def handle_401(self, resp, **kwargs):
if not self.prompting:
return resp

parsed = urllib_parse.urlparse(resp.url)
parsed = urllib.parse.urlparse(resp.url)

# Prompt the user for a new username and password
username, password, save = self._prompt_for_password(parsed.netloc)
Expand Down
4 changes: 2 additions & 2 deletions src/pip/_internal/network/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import os
import platform
import sys
import urllib.parse
import warnings
from urllib import parse as urllib_parse

from pip._vendor import requests, six, urllib3
from pip._vendor.cachecontrol import CacheControlAdapter
Expand Down Expand Up @@ -347,7 +347,7 @@ def iter_secure_origins(self):
def is_secure_origin(self, location):
# type: (Link) -> bool
# Determine if this url used a secure transport mechanism
parsed = urllib_parse.urlparse(str(location))
parsed = urllib.parse.urlparse(str(location))
origin_protocol, origin_host, origin_port = (
parsed.scheme, parsed.hostname, parsed.port,
)
Expand Down
6 changes: 3 additions & 3 deletions src/pip/_internal/network/xmlrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"""

import logging
from urllib import parse as urllib_parse
import urllib.parse

# NOTE: XMLRPC Client is not annotated in typeshed as on 2017-07-17, which is
# why we ignore the type on this import
Expand All @@ -29,14 +29,14 @@ class PipXmlrpcTransport(xmlrpc_client.Transport):
def __init__(self, index_url, session, use_datetime=False):
# type: (str, PipSession, bool) -> None
super().__init__(use_datetime)
index_parts = urllib_parse.urlparse(index_url)
index_parts = urllib.parse.urlparse(index_url)
self._scheme = index_parts.scheme
self._session = session

def request(self, host, handler, request_body, verbose=False):
# type: (str, str, Dict[str, str], bool) -> None
parts = (self._scheme, host, handler, None, None, None)
url = urllib_parse.urlunparse(parts)
url = urllib.parse.urlunparse(parts)
try:
headers = {'Content-Type': 'text/xml'}
response = self._session.post(url, data=request_body,
Expand Down
4 changes: 2 additions & 2 deletions src/pip/_internal/req/req_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import os
import re
import shlex
from urllib import parse as urllib_parse
import urllib.parse

from pip._internal.cli import cmdoptions
from pip._internal.exceptions import InstallationError, RequirementsFileParseError
Expand Down Expand Up @@ -354,7 +354,7 @@ def _parse_and_recurse(self, filename, constraint):
# original file is over http
if SCHEME_RE.search(filename):
# do a url join so relative paths work
req_path = urllib_parse.urljoin(filename, req_path)
req_path = urllib.parse.urljoin(filename, req_path)
# original file and nested file are paths
elif not SCHEME_RE.search(req_path):
# do a join so relative paths work
Expand Down
13 changes: 6 additions & 7 deletions src/pip/_internal/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
import shutil
import stat
import sys
import urllib.parse
from collections import deque
from io import StringIO
from itertools import filterfalse, tee, zip_longest
from urllib import parse as urllib_parse
from urllib.parse import unquote as urllib_unquote

from pip._vendor import pkg_resources
from pip._vendor.packaging.utils import canonicalize_name
Expand Down Expand Up @@ -697,7 +696,7 @@ def parse_netloc(netloc):
Return the host-port pair from a netloc.
"""
url = build_url_from_netloc(netloc)
parsed = urllib_parse.urlparse(url)
parsed = urllib.parse.urlparse(url)
return parsed.hostname, parsed.port


Expand All @@ -723,7 +722,7 @@ def split_auth_from_netloc(netloc):
user_pass = auth, None

user_pass = tuple(
None if x is None else urllib_unquote(x) for x in user_pass
None if x is None else urllib.parse.unquote(x) for x in user_pass
)

return netloc, user_pass
Expand All @@ -745,7 +744,7 @@ def redact_netloc(netloc):
user = '****'
password = ''
else:
user = urllib_parse.quote(user)
user = urllib.parse.quote(user)
password = ':****'
return '{user}{password}@{netloc}'.format(user=user,
password=password,
Expand All @@ -762,13 +761,13 @@ def _transform_url(url, transform_netloc):
Returns a tuple containing the transformed url as item 0 and the
original tuple returned by transform_netloc as item 1.
"""
purl = urllib_parse.urlsplit(url)
purl = urllib.parse.urlsplit(url)
netloc_tuple = transform_netloc(purl.netloc)
# stripped url
url_pieces = (
purl.scheme, netloc_tuple[0], purl.path, purl.query, purl.fragment
)
surl = urllib_parse.urlunsplit(url_pieces)
surl = urllib.parse.urlunsplit(url_pieces)
return surl, netloc_tuple


Expand Down
10 changes: 5 additions & 5 deletions src/pip/_internal/utils/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import sys
from urllib import parse as urllib_parse
from urllib import request as urllib_request
import urllib.parse
import urllib.request

from pip._internal.utils.typing import MYPY_CHECK_RUNNING

Expand All @@ -23,7 +23,7 @@ def path_to_url(path):
quoted path parts.
"""
path = os.path.normpath(os.path.abspath(path))
url = urllib_parse.urljoin('file:', urllib_request.pathname2url(path))
url = urllib.parse.urljoin('file:', urllib.request.pathname2url(path))
return url


Expand All @@ -36,7 +36,7 @@ def url_to_path(url):
"You can only turn file: urls into filenames (not {url!r})"
.format(**locals()))

_, netloc, path, _, _ = urllib_parse.urlsplit(url)
_, netloc, path, _, _ = urllib.parse.urlsplit(url)

if not netloc or netloc == 'localhost':
# According to RFC 8089, same as empty authority.
Expand All @@ -50,5 +50,5 @@ def url_to_path(url):
.format(**locals())
)

path = urllib_request.url2pathname(netloc + path)
path = urllib.request.url2pathname(netloc + path)
return path
Loading