diff --git a/six.py b/six.py index d0aece89f..7e1e64240 100644 --- a/six.py +++ b/six.py @@ -597,6 +597,8 @@ def iterlists(d, **kw): viewitems = operator.methodcaller("items") else: + import collections as _collections + def iterkeys(d, **kw): return d.iterkeys(**kw) @@ -609,11 +611,27 @@ def iteritems(d, **kw): def iterlists(d, **kw): return d.iterlists(**kw) - viewkeys = operator.methodcaller("viewkeys") - - viewvalues = operator.methodcaller("viewvalues") + def viewkeys(d): + return ( + _collections.KeysView(d) + if isinstance(d, types.DictProxyType) + else d.viewkeys() + ) + + def viewvalues(d): + return ( + _collections.ValuesView(d) + if isinstance(d, types.DictProxyType) + else d.viewvalues() + ) + + def viewitems(d): + return ( + _collections.ItemsView(d) + if isinstance(d, types.DictProxyType) + else d.viewitems() + ) - viewitems = operator.methodcaller("viewitems") _add_doc(iterkeys, "Return an iterator over the keys of a dictionary.") _add_doc(itervalues, "Return an iterator over the values of a dictionary.")