Mercurial > hg
comparison mercurial/urllibcompat.py @ 40159:5774fc623a18
py3: coerce bytestr to bytes to appease urllib.parse.quote_from_bytes()
Differential Revision: https://phab.mercurial-scm.org/D4969
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Thu, 11 Oct 2018 22:26:12 +0200 |
parents | 5bc7ff103081 |
children | 2372284d9457 |
comparison
equal
deleted
inserted
replaced
40158:9310037f0636 | 40159:5774fc623a18 |
---|---|
90 | 90 |
91 # urllib.parse.quote() accepts both str and bytes, decodes bytes | 91 # urllib.parse.quote() accepts both str and bytes, decodes bytes |
92 # (if necessary), and returns str. This is wonky. We provide a custom | 92 # (if necessary), and returns str. This is wonky. We provide a custom |
93 # implementation that only accepts bytes and emits bytes. | 93 # implementation that only accepts bytes and emits bytes. |
94 def quote(s, safe=r'/'): | 94 def quote(s, safe=r'/'): |
95 # bytestr has an __iter__ that emits characters. quote_from_bytes() | |
96 # does an iteration and expects ints. We coerce to bytes to appease it. | |
97 if isinstance(s, pycompat.bytestr): | |
98 s = bytes(s) | |
95 s = urllib.parse.quote_from_bytes(s, safe=safe) | 99 s = urllib.parse.quote_from_bytes(s, safe=safe) |
96 return s.encode('ascii', 'strict') | 100 return s.encode('ascii', 'strict') |
97 | 101 |
98 # urllib.parse.urlencode() returns str. We use this function to make | 102 # urllib.parse.urlencode() returns str. We use this function to make |
99 # sure we return bytes. | 103 # sure we return bytes. |