Skip to content

Commit

Permalink
Fix #162: Make pure Python quoter consistent with Cython version
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov committed Jan 21, 2018
1 parent 829f656 commit f487f00
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
5 changes: 5 additions & 0 deletions tests/test_quoting.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,8 @@ def test_quote_very_large_string(quoter):
# more than 8 KiB
s = 'abcфух%30%0a' * 1024
assert quoter()(s) == 'abc%D1%84%D1%83%D1%850%0A' * 1024


def test_space(quoter):
s = '% A'
assert quoter()(s) == '%25%20A'
12 changes: 11 additions & 1 deletion yarl/quoting.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
from string import ascii_letters, ascii_lowercase, digits

BASCII_LOWERCASE = ascii_lowercase.encode('ascii')
Expand All @@ -10,6 +11,9 @@
ALLOWED = UNRESERVED + SUB_DELIMS_WITHOUT_QS


_IS_HEX = re.compile(b'[A-Z0-9][A-Z0-9]')


class _PyQuoter:
def __init__(self, *, safe='', protected='', qs=False):
self._safe = safe
Expand Down Expand Up @@ -39,10 +43,16 @@ def __call__(self, val):

if pct:
if ch in BASCII_LOWERCASE:
ch = ch - 32
ch = ch - 32 # convert to uppercase
pct.append(ch)
if len(pct) == 3: # pragma: no branch # peephole optimizer
pct = bytes(pct)
buf = pct[1:]
if not _IS_HEX.match(buf):
ret.extend(b'%25')
pct = b''
idx -= 2
continue
try:
unquoted = chr(int(pct[1:].decode('ascii'), base=16))
except ValueError:
Expand Down

0 comments on commit f487f00

Please sign in to comment.