Mercurial > public > mercurial-scm > hg
comparison mercurial/pycompat.py @ 29799:45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
These functions will be imported automagically by our code transformer.
getattr() and setattr() are widely used in our code. We wouldn't probably
want to rewrite every single call of getattr/setattr. delattr() and hasattr()
aren't that important, but they are functions of the same kind.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Sun, 14 Aug 2016 12:51:21 +0900 |
parents | 31d588fcd2b9 |
children | 178c89e8519a |
comparison
equal
deleted
inserted
replaced
29798:31d588fcd2b9 | 29799:45fa8de47a0f |
---|---|
29 import urllib.parse as urlparse | 29 import urllib.parse as urlparse |
30 import xmlrpc.client as xmlrpclib | 30 import xmlrpc.client as xmlrpclib |
31 | 31 |
32 if sys.version_info[0] >= 3: | 32 if sys.version_info[0] >= 3: |
33 import builtins | 33 import builtins |
34 import functools | |
34 builtins.xrange = range | 35 builtins.xrange = range |
36 | |
37 def _wrapattrfunc(f): | |
38 @functools.wraps(f) | |
39 def w(object, name, *args): | |
40 if isinstance(name, bytes): | |
41 name = name.decode(u'utf-8') | |
42 return f(object, name, *args) | |
43 return w | |
44 | |
45 delattr = _wrapattrfunc(builtins.delattr) | |
46 getattr = _wrapattrfunc(builtins.getattr) | |
47 hasattr = _wrapattrfunc(builtins.hasattr) | |
48 setattr = _wrapattrfunc(builtins.setattr) | |
35 | 49 |
36 stringio = io.StringIO | 50 stringio = io.StringIO |
37 empty = _queue.Empty | 51 empty = _queue.Empty |
38 queue = _queue.Queue | 52 queue = _queue.Queue |
39 | 53 |