Skip to content
Myr

Move from urlparse to parse_url for prepending schemes

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Nate Prewitt · committed by Nate Prewitt · parent 39d0fdd · Browse files · 2 files+15 added−5 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+15 added−5 removed

  • tests:+1 added−0 removed
  • other:+14 added−5 removed

2 files+15 added−5 removed

modified:requests/utils.py+14 added-5 removed
19 unmodified lines
20import warnings21import zipfile22from collections import OrderedDict23from urllib3.util import make_headers24from urllib3.util import parse_url2526from .__version__ import __version__27from . import certs28# to_native_string is unused here, but imported here for backwards compatibility
934 unmodified lines
963 Does not replace a present scheme with the one provided as an argument.964965 :rtype: str966 """966 scheme, netloc, path, params, query, fragment = urlparse(url, new_scheme)967 parsed = parse_url(url)968 scheme, auth, host, port, path, query, fragment = parsed969968 # urlparse is a finicky beast, and sometimes decides that there isn't a969 # netloc present. Assume that it's being over-cautious, and switch netloc970 # and path if urlparse decided there was no netloc.970 # A defect in urlparse determines that there isn't a netloc present in some971 # urls. We previously assumed parsing was overly cautious, and swapped the972 # netloc and path. Due to a lack of tests on the original defect, this is973 # maintained with parse_url for backwards compatibility.974 netloc = parsed.netloc975 if not netloc:976 netloc, path = path, netloc977974 return urlunparse((scheme, netloc, path, params, query, fragment))978 if scheme is None:979 scheme = new_scheme980 if path is None:981 path = ''982983 return urlunparse((scheme, netloc, path, '', query, fragment))984985986def get_auth_from_url(url):987 """Given a url with authentication components, extract them into a tuple of988 username,password.989
68 unmodified lines
modified:tests/test_utils.py+1 added
599 unmodified lines
600@pytest.mark.parametrize(601 'value, expected', (602 ('example.com/path', 'http://example.com/path'),603 ('//example.com/path', 'http://example.com/path'),604 ('example.com:80', 'http://example.com:80'),605 ))606def test_prepend_scheme_if_needed(value, expected):607 assert prepend_scheme_if_needed(value, 'http') == expected608
193 unmodified lines