Merged in bmispelon/six (pull request #11)
Add itertools.zip_longest to six.moves.
diff --git a/CHANGES b/CHANGES
index 2613a3e..8842c6f 100644
--- a/CHANGES
+++ b/CHANGES
@@ -6,6 +6,13 @@
Development version
-------------------
+- Issue #25: Add the unichr function, which returns a string for a Unicode
+ codepoint.
+
+- Issue #26: Add byte2int function, which complements int2byte.
+
+- Add a PY2 constant with obvious semantics.
+
- Add helpers for indexing and iterating over bytes: iterbytes and indexbytes.
- Add create_bound_method() wrapper.
diff --git a/README b/README
index a131ac6..99bd3a6 100644
--- a/README
+++ b/README
@@ -3,7 +3,9 @@
writing Python code that is compatible on both Python versions. See the
documentation for more information on what is provided.
-Six supports Python 2.4+.
+Six supports every Python version since 2.4. It is contained in only one Python
+file, so it can be easily copied into your project. (The copyright and license
+notice must be retained.)
Online documentation is at http://pythonhosted.org/six/.
diff --git a/documentation/index.rst b/documentation/index.rst
index 7049fd7..1f45d50 100644
--- a/documentation/index.rst
+++ b/documentation/index.rst
@@ -31,6 +31,10 @@
Package contents
----------------
+.. data:: PY2
+
+ A boolean indicating if the code is running on Python 2.
+
.. data:: PY3
A boolean indicating if the code is running on Python 3.
@@ -76,11 +80,12 @@
.. data:: MAXSIZE
- The maximum size of a container. This is equivalent to
- :data:`py3:sys.maxsize` in Python 2.6 and later (including 3.x). Note, this
- is temptingly similar to, but not the same as :data:`py2:sys.maxint` in
- Python 2. There is no direct equivalent to :data:`py2:sys.maxint` in Python
- 3 because its integer type has no limits aside from memory.
+ The maximum size of a container like :func:`py3:list` or :func:`py3:dict`.
+ This is equivalent to :data:`py3:sys.maxsize` in Python 2.6 and later
+ (including 3.x). Note, this is temptingly similar to, but not the same as
+ :data:`py2:sys.maxint` in Python 2. There is no direct equivalent to
+ :data:`py2:sys.maxint` in Python 3 because its integer type has no limits
+ aside from memory.
Here's example usage of the module::
@@ -291,7 +296,7 @@
with the latin-1 encoding to bytes.
-.. note::
+ .. note::
Since all Python versions 2.6 and after support the ``b`` prefix,
:func:`b`, code without 2.5 support doesn't need :func:`b`.
@@ -319,12 +324,24 @@
ASCII data.
+.. function:: unichr(c)
+
+ Return the (Unicode) string representing the codepoint *c*. This is
+ equivalent to :func:`py2:unichr` on Python 2 and :func:`py3:chr` on Python 3.
+
+
.. function:: int2byte(i)
Converts *i* to a byte. *i* must be in ``range(0, 256)``. This is
equivalent to :func:`py2:chr` in Python 2 and ``bytes((i,))`` in Python 3.
+.. function:: byte2int(bs)
+
+ Converts the first byte of *bs* to an integer. This is equivalent to
+ ``ord(bs[0])`` on Python 2 and ``bs[0]`` on Python 3.
+
+
.. function:: indexbytes(buf, i)
Return the byte at index *i* of *buf* as an integer. This is equivalent to
diff --git a/six.py b/six.py
index dc5aa64..a2c690f 100644
--- a/six.py
+++ b/six.py
@@ -28,7 +28,8 @@
__version__ = "1.3.0"
-# True if we are running on Python 3.
+# Useful for very coarse version differentiation.
+PY2 = sys.version_info[0] == 2
PY3 = sys.version_info[0] == 3
if PY3:
@@ -62,7 +63,7 @@
else:
# 64-bit
MAXSIZE = int((1 << 63) - 1)
- del X
+ del X
def _add_doc(func, doc):
@@ -305,12 +306,14 @@
return s.encode("latin-1")
def u(s):
return s
+ unichr = chr
if sys.version_info[1] <= 1:
def int2byte(i):
return bytes((i,))
else:
# This is about 2x faster than the implementation above on 3.2+
int2byte = operator.methodcaller("to_bytes", 1, "big")
+ byte2int = operator.itemgetter(0)
indexbytes = operator.getitem
iterbytes = iter
import io
@@ -321,7 +324,10 @@
return s
def u(s):
return unicode(s, "unicode_escape")
+ unichr = unichr
int2byte = chr
+ def byte2int(bs):
+ return ord(bs[0])
def indexbytes(buf, i):
return ord(buf[i])
def iterbytes(buf):
diff --git a/test_six.py b/test_six.py
index 207b0cf..e2131e7 100644
--- a/test_six.py
+++ b/test_six.py
@@ -359,11 +359,22 @@
assert len(s) == 1
+def test_unichr():
+ assert six.u("\u1234") == six.unichr(0x1234)
+ assert type(six.u("\u1234")) is type(six.unichr(0x1234))
+
+
def test_int2byte():
assert six.int2byte(3) == six.b("\x03")
py.test.raises((OverflowError, ValueError), six.int2byte, 256)
+def test_byte2int():
+ assert six.byte2int(six.b("\x03")) == 3
+ assert six.byte2int(six.b("\x03\x04")) == 3
+ py.test.raises(IndexError, six.byte2int, six.b(""))
+
+
def test_bytesindex():
assert six.indexbytes(six.b("hello"), 3) == ord("l")