comparison mercurial/pycompat.py @ 31409:fb1f70331ee6

pycompat: custom implementation of urllib.parse.quote() urllib.parse.quote() accepts either str or bytes and returns str. There exists a urllib.parse.quote_from_bytes() which only accepts bytes. We should probably use that to retain strong typing and avoid surprises. In addition, since nearly all strings in Mercurial are bytes, we probably don't want quote() returning unicode. So, this patch implements a custom quote() that only accepts bytes and returns bytes. The quoted URL should only contain URL safe characters which is a strict subset of ASCII. So `.encode('ascii', 'strict')` should be safe.
author Gregory Szorc <gregory.szorc@gmail.com>
date Mon, 13 Mar 2017 12:16:47 -0700
parents 1ed169c5e235
children 4acc49335a6e
comparison
equal deleted inserted replaced
31408:1ed169c5e235 31409:fb1f70331ee6
267 )) 267 ))
268 268
269 else: 269 else:
270 import urllib.parse 270 import urllib.parse
271 urlreq._registeraliases(urllib.parse, ( 271 urlreq._registeraliases(urllib.parse, (
272 "quote",
273 "splitattr", 272 "splitattr",
274 "splitpasswd", 273 "splitpasswd",
275 "splitport", 274 "splitport",
276 "splituser", 275 "splituser",
277 "unquote", 276 "unquote",
311 "HTTPServer", 310 "HTTPServer",
312 "BaseHTTPRequestHandler", 311 "BaseHTTPRequestHandler",
313 "SimpleHTTPRequestHandler", 312 "SimpleHTTPRequestHandler",
314 "CGIHTTPRequestHandler", 313 "CGIHTTPRequestHandler",
315 )) 314 ))
315
316 # urllib.parse.quote() accepts both str and bytes, decodes bytes
317 # (if necessary), and returns str. This is wonky. We provide a custom
318 # implementation that only accepts bytes and emits bytes.
319 def quote(s, safe=r'/'):
320 s = urllib.parse.quote_from_bytes(s, safe=safe)
321 return s.encode('ascii', 'strict')
322
323 urlreq.quote = quote