Index: src/org/python/core/PyStringMap.java =================================================================== --- src/org/python/core/PyStringMap.java (revision 3019) +++ src/org/python/core/PyStringMap.java (working copy) @@ -543,17 +543,48 @@ } return l; } + /** + * return an iterator over (key, value) pairs + */ + public synchronized PyObject iteritems() { + return new PyStringMapIter(keys, values, PyStringMapIter.ITEMS); + } + + /** + * return an iterator over the keys + */ + public synchronized PyObject iterkeys() { + return new PyStringMapIter(keys, values, PyStringMapIter.KEYS); + } + + /** + * return an iterator over the values + */ + public synchronized PyObject itervalues() { + return new PyStringMapIter(keys, values, PyStringMapIter.VALUES); + } } +/* extended, based on PyDictionaryIter */ class PyStringMapIter extends PyIterator { String[] keyTable; PyObject[] valTable; private int idx; + private int type; + public static final int KEYS = 0; + public static final int VALUES = 1; + public static final int ITEMS = 2; + public PyStringMapIter(String[] keys, PyObject[] values) { + this(keys, values, KEYS); + } + + public PyStringMapIter(String[] keys, PyObject[] values, int type) { this.keyTable = keys; this.valTable = values; this.idx = 0; + this.type=type; } public PyObject __iternext__() { @@ -561,14 +592,23 @@ for (; idx < n; idx++) { String key = keyTable[idx]; - if (key == null || key == "" || valTable[idx] == null) + PyObject val = valTable[idx]; + if (key == null || key == "" || val == null) continue; idx++; - return Py.newString(key); + + switch(type) { + case VALUES: + return val; + case ITEMS: + return new PyTuple(new PyObject[] { Py.newString(key), + val }); + default: // KEYS + return Py.newString(key); + } } return null; } - }