comparison mercurial/pycompat.py @ 31566:c6df6a23dfe5

pycompat: alias urlreq.unquote to unquote_to_bytes Previously, urlreq.unquote aliased to urllib.parse.unquote, which returned a str/unicode. We like bytes, so switch urlreq.unquote to dispatch to urllib.parse.unquote_to_bytes. This required a minor helper function to register an alias under a different name from which it points. If this turns into a common pattern, we could likely teach _registeralias to accept tuple values defining the mapping. Until then, I didn't feel like adding complexity to _registeralias.
author Gregory Szorc <gregory.szorc@gmail.com>
date Tue, 21 Mar 2017 22:20:11 -0700
parents a1e40ceee640
children 6c9772867344
comparison
equal deleted inserted replaced
31565:553ad16b274f 31566:c6df6a23dfe5
265 """Add items that will be populated at the first access""" 265 """Add items that will be populated at the first access"""
266 items = map(sysstr, items) 266 items = map(sysstr, items)
267 self._aliases.update( 267 self._aliases.update(
268 (item.replace(sysstr('_'), sysstr('')).lower(), (origin, item)) 268 (item.replace(sysstr('_'), sysstr('')).lower(), (origin, item))
269 for item in items) 269 for item in items)
270
271 def _registeralias(self, origin, attr, name):
272 """Alias ``origin``.``attr`` as ``name``"""
273 self._aliases[sysstr(name)] = (origin, sysstr(attr))
270 274
271 def __getattr__(self, name): 275 def __getattr__(self, name):
272 try: 276 try:
273 origin, item = self._aliases[name] 277 origin, item = self._aliases[name]
274 except KeyError: 278 except KeyError:
335 urlreq._registeraliases(urllib.parse, ( 339 urlreq._registeraliases(urllib.parse, (
336 "splitattr", 340 "splitattr",
337 "splitpasswd", 341 "splitpasswd",
338 "splitport", 342 "splitport",
339 "splituser", 343 "splituser",
340 "unquote", 344 ))
341 )) 345 urlreq._registeralias(urllib.parse, "unquote_to_bytes", "unquote")
342 import urllib.request 346 import urllib.request
343 urlreq._registeraliases(urllib.request, ( 347 urlreq._registeraliases(urllib.request, (
344 "AbstractHTTPHandler", 348 "AbstractHTTPHandler",
345 "BaseHandler", 349 "BaseHandler",
346 "build_opener", 350 "build_opener",