Skip to content

Commit cc2b016

Browse files
committed
- PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
and .keys(), .items(), .values() return dict views. The dict views aren't fully functional yet; in particular, they can't be compared to sets yet. but they are useful as "iterator wells". There are still 27 failing unit tests; I expect that many of these have fairly trivial fixes, but there are so many, I could use help.
1 parent 4e66dfc commit cc2b016

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+317
-272
lines changed

Include/abstract.h

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,37 +1127,28 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
11271127
11281128
*/
11291129

1130-
/* Implemented as macro:
1130+
PyAPI_FUNC(PyObject *) PyMapping_Keys(PyObject *o);
11311131

1132-
PyObject *PyMapping_Keys(PyObject *o);
1133-
1134-
On success, return a list of the keys in object o. On
1135-
failure, return NULL. This is equivalent to the Python
1136-
expression: o.keys().
1132+
/*
1133+
On success, return a list or tuple of the keys in object o.
1134+
On failure, return NULL.
11371135
*/
1138-
#define PyMapping_Keys(O) PyObject_CallMethod(O,"keys",NULL)
1139-
1140-
/* Implemented as macro:
11411136

1142-
PyObject *PyMapping_Values(PyObject *o);
1137+
PyAPI_FUNC(PyObject *) PyMapping_Values(PyObject *o);
11431138

1144-
On success, return a list of the values in object o. On
1145-
failure, return NULL. This is equivalent to the Python
1146-
expression: o.values().
1139+
/*
1140+
On success, return a list or tuple of the values in object o.
1141+
On failure, return NULL.
11471142
*/
1148-
#define PyMapping_Values(O) PyObject_CallMethod(O,"values",NULL)
1149-
1150-
/* Implemented as macro:
11511143

1152-
PyObject *PyMapping_Items(PyObject *o);
1144+
PyAPI_FUNC(PyObject *) PyMapping_Items(PyObject *o);
11531145

1154-
On success, return a list of the items in object o, where
1155-
each item is a tuple containing a key-value pair. On
1156-
failure, return NULL. This is equivalent to the Python
1157-
expression: o.items().
1146+
/*
1147+
On success, return a list or tuple of the items in object o,
1148+
where each item is a tuple containing a key-value pair.
1149+
On failure, return NULL.
11581150
11591151
*/
1160-
#define PyMapping_Items(O) PyObject_CallMethod(O,"items",NULL)
11611152

11621153
PyAPI_FUNC(PyObject *) PyMapping_GetItemString(PyObject *o, char *key);
11631154

Lib/ConfigParser.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ def defaults(self):
214214
def sections(self):
215215
"""Return a list of section names, excluding [DEFAULT]"""
216216
# self._sections will never have [DEFAULT] in it
217-
return self._sections.keys()
217+
return list(self._sections.keys())
218218

219219
def add_section(self, section):
220220
"""Create a new section in the configuration.
@@ -242,7 +242,7 @@ def options(self, section):
242242
opts.update(self._defaults)
243243
if '__name__' in opts:
244244
del opts['__name__']
245-
return opts.keys()
245+
return list(opts.keys())
246246

247247
def read(self, filenames):
248248
"""Read and parse a filename or a list of filenames.
@@ -547,7 +547,7 @@ def items(self, section, raw=False, vars=None):
547547
if vars:
548548
for key, value in vars.items():
549549
d[self.optionxform(key)] = value
550-
options = d.keys()
550+
options = list(d.keys())
551551
if "__name__" in options:
552552
options.remove("__name__")
553553
if raw:

Lib/Cookie.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -488,8 +488,7 @@ def OutputString(self, attrs=None):
488488
# Now add any defined attributes
489489
if attrs is None:
490490
attrs = self._reserved
491-
items = self.items()
492-
items.sort()
491+
items = sorted(self.items())
493492
for K,V in items:
494493
if V == "": continue
495494
if K not in attrs: continue
@@ -582,8 +581,7 @@ def __setitem__(self, key, value):
582581
def output(self, attrs=None, header="Set-Cookie:", sep="\015\012"):
583582
"""Return a string suitable for HTTP."""
584583
result = []
585-
items = self.items()
586-
items.sort()
584+
items = sorted(self.items())
587585
for K,V in items:
588586
result.append( V.output(attrs, header) )
589587
return sep.join(result)
@@ -593,17 +591,15 @@ def output(self, attrs=None, header="Set-Cookie:", sep="\015\012"):
593591

594592
def __repr__(self):
595593
L = []
596-
items = self.items()
597-
items.sort()
594+
items = sorted(self.items())
598595
for K,V in items:
599596
L.append( '%s=%s' % (K,repr(V.value) ) )
600597
return '<%s: %s>' % (self.__class__.__name__, _spacejoin(L))
601598

602599
def js_output(self, attrs=None):
603600
"""Return a string suitable for JavaScript."""
604601
result = []
605-
items = self.items()
606-
items.sort()
602+
items = sorted(self.items())
607603
for K,V in items:
608604
result.append( V.js_output(attrs) )
609605
return _nulljoin(result)

Lib/UserDict.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ def copy(self):
4242
return c
4343
def keys(self): return self.data.keys()
4444
def items(self): return self.data.items()
45-
def iteritems(self): return self.data.iteritems()
46-
def iterkeys(self): return self.data.iterkeys()
47-
def itervalues(self): return self.data.itervalues()
45+
def iteritems(self): return self.data.items()
46+
def iterkeys(self): return self.data.keys()
47+
def itervalues(self): return self.data.values()
4848
def values(self): return self.data.values()
4949
def update(self, dict=None, **kwargs):
5050
if dict is None:
@@ -111,12 +111,12 @@ def iterkeys(self):
111111

112112
# fourth level uses definitions from lower levels
113113
def itervalues(self):
114-
for _, v in self.iteritems():
114+
for _, v in self.items():
115115
yield v
116116
def values(self):
117-
return [v for _, v in self.iteritems()]
117+
return [v for _, v in self.items()]
118118
def items(self):
119-
return list(self.iteritems())
119+
return list(self.items())
120120
def clear(self):
121121
for key in self.keys():
122122
del self[key]
@@ -140,7 +140,7 @@ def pop(self, key, *args):
140140
return value
141141
def popitem(self):
142142
try:
143-
k, v = self.iteritems().next()
143+
k, v = self.items().next()
144144
except StopIteration:
145145
raise KeyError, 'container is empty'
146146
del self[k]
@@ -152,6 +152,9 @@ def update(self, other=None, **kwargs):
152152
elif hasattr(other, 'iteritems'): # iteritems saves memory and lookups
153153
for k, v in other.iteritems():
154154
self[k] = v
155+
elif hasattr(other, 'items'): # items may also save memory and lookups
156+
for k, v in other.items():
157+
self[k] = v
155158
elif hasattr(other, 'keys'):
156159
for k in other.keys():
157160
self[k] = other[k]
@@ -166,14 +169,14 @@ def get(self, key, default=None):
166169
except KeyError:
167170
return default
168171
def __repr__(self):
169-
return repr(dict(self.iteritems()))
172+
return repr(dict(self.items()))
170173
def __eq__(self, other):
171174
if isinstance(other, DictMixin):
172-
other = dict(other.iteritems())
173-
return dict(self.iteritems()) == other
175+
other = dict(other.items())
176+
return dict(self.items()) == other
174177
def __ne__(self, other):
175178
if isinstance(other, DictMixin):
176-
other = dict(other.iteritems())
177-
return dict(self.iteritems()) != other
179+
other = dict(other.items())
180+
return dict(self.items()) != other
178181
def __len__(self):
179182
return len(self.keys())

Lib/_LWPCookieJar.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ def lwp_cookie_str(cookie):
3737
if cookie.comment: h.append(("comment", cookie.comment))
3838
if cookie.comment_url: h.append(("commenturl", cookie.comment_url))
3939

40-
keys = cookie._rest.keys()
41-
keys.sort()
40+
keys = sorted(cookie._rest.keys())
4241
for k in keys:
4342
h.append((k, str(cookie._rest[k])))
4443

Lib/_strptime.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ def strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
338338
# values
339339
weekday = julian = -1
340340
found_dict = found.groupdict()
341-
for group_key in found_dict.iterkeys():
341+
for group_key in found_dict.keys():
342342
# Directives not explicitly handled below:
343343
# c, x, X
344344
# handled by making out of other directives

Lib/_threading_local.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@
2828
2929
>>> log = []
3030
>>> def f():
31-
... items = mydata.__dict__.items()
32-
... items.sort()
31+
... items = sorted(mydata.__dict__.items())
3332
... log.append(items)
3433
... mydata.number = 11
3534
... log.append(mydata.number)

Lib/base64.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,7 @@ def urlsafe_b64decode(s):
126126
8: 'I', 17: 'R', 26: '2',
127127
}
128128

129-
_b32tab = _b32alphabet.items()
130-
_b32tab.sort()
131-
_b32tab = [v for k, v in _b32tab]
129+
_b32tab = [v for k, v in sorted(_b32alphabet.items())]
132130
_b32rev = dict([(v, int(k)) for k, v in _b32alphabet.items()])
133131

134132

Lib/compiler/ast.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1337,6 +1337,6 @@ def getChildNodes(self):
13371337
def __repr__(self):
13381338
return "Yield(%s)" % (repr(self.value),)
13391339

1340-
for name, obj in globals().items():
1340+
for name, obj in list(globals().items()):
13411341
if isinstance(obj, type) and issubclass(obj, Node):
13421342
nodes[name.lower()] = obj

Lib/compiler/misc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def __contains__(self, elt):
1818
def add(self, elt):
1919
self.elts[elt] = elt
2020
def elements(self):
21-
return self.elts.keys()
21+
return list(self.elts.keys())
2222
def has_elt(self, elt):
2323
return elt in self.elts
2424
def remove(self, elt):

0 commit comments

Comments
 (0)