Skip to content

Commit caddc5b

Browse files
committed
Added itervalues and iterkeys to map proxy. Fixes #2443
1 parent 5599eae commit caddc5b

3 files changed

Lines changed: 39 additions & 0 deletions

File tree

Lib/test/test_dict_jy.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ def test_hashmap_builtin_pymethods(self):
137137
x['a'] = 1
138138
x[(1, 2)] = 'xyz'
139139
self.assertEqual({tup for tup in x.iteritems()}, {('a', 1), ((1, 2), 'xyz')})
140+
self.assertEqual({tup for tup in x.itervalues()}, {1, 'xyz'})
141+
self.assertEqual({tup for tup in x.iterkeys()}, {'a', (1, 2)})
140142
self.assertEqual(str(x), repr(x))
141143
self.assertEqual(type(str(x)), type(repr(x)))
142144

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Jython 2.7.1rc
1818
- [ 2112 ] time.strptime() has different default year in Jython and CPython
1919
- [ 1767 ] Rich comparisons
2020
- [ 2314 ] Failures in test_shutil on Windows
21+
- [ 2443 ] java.util.Map derived classes lack iterkeys, itervalues methods
2122

2223
New Features
2324
- Added uname function to posix module. The mostly Java-based implementation even

src/org/python/core/JavaProxyMap.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,40 @@ public PyObject __iternext__() {
223223
};
224224
}
225225
};
226+
private static final PyBuiltinMethodNarrow mapIterKeysProxy = new MapMethod("iterkeys", 0) {
227+
@Override
228+
public PyObject __call__() {
229+
final Iterator<Object> keyIterator = asMap().keySet().iterator();
230+
return new PyIterator() {
231+
@Override
232+
public PyObject __iternext__() {
233+
if (keyIterator.hasNext()) {
234+
Object nextKey = keyIterator.next();
235+
// yield a Python key
236+
return Py.java2py(nextKey);
237+
}
238+
return null;
239+
}
240+
};
241+
}
242+
};
243+
private static final PyBuiltinMethodNarrow mapIterValuesProxy = new MapMethod("itervalues", 0) {
244+
@Override
245+
public PyObject __call__() {
246+
final Iterator<Object> valueIterator = asMap().values().iterator();
247+
return new PyIterator() {
248+
@Override
249+
public PyObject __iternext__() {
250+
if (valueIterator.hasNext()) {
251+
Object nextValue = valueIterator.next();
252+
// yield a Python value
253+
return Py.java2py(nextValue);
254+
}
255+
return null;
256+
}
257+
};
258+
}
259+
};
226260
private static final PyBuiltinMethodNarrow mapHasKeyProxy = new MapMethod("has_key", 1) {
227261
@Override
228262
public PyObject __call__(PyObject key) {
@@ -456,6 +490,8 @@ static PyBuiltinMethod[] getProxyMethods() {
456490
mapPutProxy,
457491
mapRemoveProxy,
458492
mapIterItemsProxy,
493+
mapIterKeysProxy,
494+
mapIterValuesProxy,
459495
mapHasKeyProxy,
460496
mapKeysProxy,
461497
mapSetDefaultProxy,

0 commit comments

Comments
 (0)