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 unquote_unreserved by 6% #19

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
19 changes: 13 additions & 6 deletions src/requests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,22 +642,29 @@ def unquote_unreserved(uri):

:rtype: str
"""
# Early exit if there are no percent-escape sequences
if '%' not in uri:
return uri

parts = uri.split("%")
for i in range(1, len(parts)):
h = parts[i][0:2]
unquoted_parts = [parts[0]]

for part in parts[1:]:
h = part[:2]
if len(h) == 2 and h.isalnum():
try:
c = chr(int(h, 16))
except ValueError:
raise InvalidURL(f"Invalid percent-escape sequence: '{h}'")

if c in UNRESERVED_SET:
parts[i] = c + parts[i][2:]
unquoted_parts.append(c + part[2:])
else:
parts[i] = f"%{parts[i]}"
unquoted_parts.append('%' + part)
else:
parts[i] = f"%{parts[i]}"
return "".join(parts)
unquoted_parts.append('%' + part)

return "".join(unquoted_parts)


def requote_uri(uri):
Expand Down