Mercurial > public > mercurial-scm > hg
comparison mercurial/py3kcompat.py @ 11878:8bb1481cf08f
py3kcompat: added fake ord implementation for py3k
In py3k, a bytes object __getitem__ will return an int instead of a
one-character bytes object. This has negative consequences when we want to
ord(), like in the following example:
>>> b'foo'[0]
102
>>> ord(b'foo'[0])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: ord() expected string of length 1, but int found
This patch overrides the default ord() implementation to just return an int
that's what is passed as an argument for ord(). Making the above call succeed:
>>> ord(b'foo'[0])
102
author | Renato Cunha <renatoc@gmail.com> |
---|---|
date | Sat, 07 Aug 2010 16:38:38 -0300 |
parents | 37a70a784397 |
children | e7cfe3587ea4 |
comparison
equal
deleted
inserted
replaced
11876:1fe94103c6ee | 11878:8bb1481cf08f |
---|---|
57 # UTF-8 is fine for us | 57 # UTF-8 is fine for us |
58 bkey = key.encode('utf-8', 'surrogateescape') | 58 bkey = key.encode('utf-8', 'surrogateescape') |
59 bvalue = os.environ[key].encode('utf-8', 'surrogateescape') | 59 bvalue = os.environ[key].encode('utf-8', 'surrogateescape') |
60 os.environ[bkey] = bvalue | 60 os.environ[bkey] = bvalue |
61 | 61 |
62 origord = builtins.ord | |
63 def fakeord(char): | |
64 if isinstance(char, int): | |
65 return char | |
66 return origord(char) | |
67 builtins.ord = fakeord | |
68 | |
62 if __name__ == '__main__': | 69 if __name__ == '__main__': |
63 import doctest | 70 import doctest |
64 doctest.testmod() | 71 doctest.testmod() |
65 | 72 |