Commit Graph

3 Commits

Author SHA1 Message Date
Mads Kiilerich
2f4504e446 fix trivial spelling errors 2012-08-15 22:38:42 +02:00
Renato Cunha
2000c93772 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
2010-08-07 16:38:38 -03:00
Renato Cunha
48aa19eddb py3kcompat: added a "compatibility layer" for py3k
This patch adds some ugly constructs. The first of them is bytesformatter, a
function that formats strings like when '%' is called. The main motivation for
this function is py3k's strange behavior:

>>> 'foo %s' % b'bar'
"foo b'bar'"
>>> b'foo %s' % b'bar'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for %: 'bytes' and 'bytes'
>>> b'foo %s' % 'bar'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for %: 'bytes' and 'str'

In other words, if we can't format bytes with bytes, and recall that all
mercurial strings will be converted by a fixer, then things will break badly if
we don't take a similar approach.

The other addition with this patch is that the os.environ dictionary is
monkeypatched to have bytes items. Hopefully this won't be needed in the
future, as python 3.2 might get a os.environb dictionary that holds bytes
items.
2010-08-03 13:52:48 -03:00