diff --git a/src/requests/_internal_utils.py b/src/requests/_internal_utils.py index f2cf635e29..74684b1937 100644 --- a/src/requests/_internal_utils.py +++ b/src/requests/_internal_utils.py @@ -8,6 +8,7 @@ import re from .compat import builtin_str +from .compat import str as builtin_str _VALID_HEADER_NAME_RE_BYTE = re.compile(rb"^[^:\s][^:\r\n]*$") _VALID_HEADER_NAME_RE_STR = re.compile(r"^[^:\s][^:\r\n]*$") @@ -27,12 +28,7 @@ def to_native_string(string, encoding="ascii"): that string in the native string type, encoding and decoding where necessary. This assumes ASCII unless told otherwise. """ - if isinstance(string, builtin_str): - out = string - else: - out = string.decode(encoding) - - return out + return string if isinstance(string, builtin_str) else string.decode(encoding) def unicode_is_ascii(u_string): diff --git a/src/requests/auth.py b/src/requests/auth.py index 4a7ce6dc14..3fda2c5306 100644 --- a/src/requests/auth.py +++ b/src/requests/auth.py @@ -14,7 +14,10 @@ from base64 import b64encode from ._internal_utils import to_native_string -from .compat import basestring, str, urlparse +from .compat import basestring +from .compat import str +from .compat import str as builtin_str +from .compat import urlparse from .cookies import extract_cookies_to_jar from .utils import parse_dict_header @@ -25,13 +28,7 @@ def _basic_auth_str(username, password): """Returns a Basic Auth string.""" - # "I want us to put a big-ol' comment on top of it that - # says that this behaviour is dumb but we need to preserve - # it because people are relying on it." - # - Lukasa - # - # These are here solely to maintain backwards compatibility - # for things like ints. This will be removed in 3.0.0. + # Maintain backwards compatibility if not isinstance(username, basestring): warnings.warn( "Non-string usernames will no longer be supported in Requests " @@ -40,7 +37,7 @@ def _basic_auth_str(username, password): "problems.".format(username), category=DeprecationWarning, ) - username = str(username) + username = builtin_str(username) if not isinstance(password, basestring): warnings.warn( @@ -50,14 +47,11 @@ def _basic_auth_str(username, password): "problems.".format(type(password)), category=DeprecationWarning, ) - password = str(password) + password = builtin_str(password) # -- End Removal -- - if isinstance(username, str): - username = username.encode("latin1") - - if isinstance(password, str): - password = password.encode("latin1") + username = username.encode("latin1") if isinstance(username, str) else username + password = password.encode("latin1") if isinstance(password, str) else password authstr = "Basic " + to_native_string( b64encode(b":".join((username, password))).strip() diff --git a/src/requests/compat.py b/src/requests/compat.py index 7f9d754350..2fbd02bec3 100644 --- a/src/requests/compat.py +++ b/src/requests/compat.py @@ -74,29 +74,14 @@ def _resolve_char_detection(): from http import cookiejar as cookielib from http.cookies import Morsel from io import StringIO - # -------------- # Legacy Imports # -------------- -from urllib.parse import ( - quote, - quote_plus, - unquote, - unquote_plus, - urldefrag, - urlencode, - urljoin, - urlparse, - urlsplit, - urlunparse, -) -from urllib.request import ( - getproxies, - getproxies_environment, - parse_http_list, - proxy_bypass, - proxy_bypass_environment, -) +from urllib.parse import (quote, quote_plus, unquote, unquote_plus, urldefrag, + urlencode, urljoin, urlparse, urlsplit, urlunparse) +from urllib.request import (getproxies, getproxies_environment, + parse_http_list, proxy_bypass, + proxy_bypass_environment) builtin_str = str str = str