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

⚡️ Speed up function _basic_auth_str by 6% #12

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
8 changes: 2 additions & 6 deletions src/requests/_internal_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]*$")
Expand All @@ -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):
Expand Down
24 changes: 9 additions & 15 deletions src/requests/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 "
Expand All @@ -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(
Expand All @@ -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()
Expand Down
25 changes: 5 additions & 20 deletions src/requests/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down