summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGES3
-rw-r--r--documentation/index.rst20
-rw-r--r--six.py21
-rw-r--r--test_six.py8
4 files changed, 52 insertions, 0 deletions
diff --git a/CHANGES b/CHANGES
index d54d913..d5df485 100644
--- a/CHANGES
+++ b/CHANGES
@@ -6,6 +6,9 @@ This file lists the changes in each six version.
1.1
---
+- Add compatibility mappings for iterators over the keys, values, and items of a
+ dictionary.
+
- Fix six.MAXSIZE on platforms where sizeof(long) != sizeof(Py_ssize_t).
- Issue #3: Add six.moves mappings for filter, map, and zip.
diff --git a/documentation/index.rst b/documentation/index.rst
index 9a42881..a948e67 100644
--- a/documentation/index.rst
+++ b/documentation/index.rst
@@ -149,6 +149,26 @@ functions and methods is the stdlib :mod:`py3:inspect` module.
so using six's version is only necessary when supporting Python 3.0 or 3.1.
+.. function:: iterkeys(dictionary)
+
+ Returns an iterator over *dictionary*\'s keys. This replaces
+ ``dictionary.iterkeys()`` on Python 2 and ``dictionary.keys()`` on Python 3.
+
+
+.. function:: itervalues(dictionary)
+
+ Returns an iterator over *dictionary*\'s values. This replaces
+ ``dictionary.itervalues()`` on Python 2 and ``dictionary.values()`` on
+ Python 3.
+
+
+.. function:: iteritems(dictionary)
+
+ Returns an iterator over *dictionary*\'s items. This replaces
+ ``dictionary.iteritems()`` on Python 2 and ``dictionary.items()`` on
+ Python 3.
+
+
Syntax compatibility
>>>>>>>>>>>>>>>>>>>>
diff --git a/six.py b/six.py
index 26d066d..2607931 100644
--- a/six.py
+++ b/six.py
@@ -184,6 +184,10 @@ if PY3:
_func_code = "__code__"
_func_defaults = "__defaults__"
+
+ _iterkeys = "keys"
+ _itervalues = "values"
+ _iteritems = "items"
else:
_meth_func = "im_func"
_meth_self = "im_self"
@@ -191,6 +195,10 @@ else:
_func_code = "func_code"
_func_defaults = "func_defaults"
+ _iterkeys = "iterkeys"
+ _itervalues = "itervalues"
+ _iteritems = "iteritems"
+
if PY3:
def get_unbound_function(unbound):
@@ -220,6 +228,19 @@ get_function_code = operator.attrgetter(_func_code)
get_function_defaults = operator.attrgetter(_func_defaults)
+def iterkeys(d):
+ """Return an iterator over the keys of a dictionary."""
+ return getattr(d, _iterkeys)()
+
+def itervalues(d):
+ """Return an iterator over the values of a dictionary."""
+ return getattr(d, _itervalues)()
+
+def iteritems(d):
+ """Return an iterator over the (key, value) pairs of a dictionary."""
+ return getattr(d, _iteritems)()
+
+
if PY3:
def b(s):
return s.encode("latin-1")
diff --git a/test_six.py b/test_six.py
index 38da028..cbd6e91 100644
--- a/test_six.py
+++ b/test_six.py
@@ -217,6 +217,14 @@ def test_get_function_defaults():
assert six.get_function_defaults(f) == (3, 4)
+def test_dictionary_iterators():
+ d = dict(zip(range(10), reversed(range(10))))
+ for name in "keys", "values", "items":
+ it = getattr(six, "iter" + name)(d)
+ assert not isinstance(it, list)
+ assert list(it) == list(getattr(d, name)())
+
+
def test_advance_iterator():
l = [1, 2]
it = iter(l)