summaryrefslogtreecommitdiff
path: root/lang
diff options
context:
space:
mode:
authorDon Anderson <dda@ddanderson.com>2011-09-06 06:44:21 -0400
committerDon Anderson <dda@ddanderson.com>2011-09-06 06:44:21 -0400
commite7bdd917b0548825176d01412c1fb814e81637ce (patch)
tree882828df20795420a4a637ce604e17cef796e7e0 /lang
parent9a0feab38fdb59c73ce494f8ffa4d34f58aeb491 (diff)
downloadmongo-e7bdd917b0548825176d01412c1fb814e81637ce.tar.gz
Use an adaptor class for iterable, so we don't have to rename 'wt_cursor.next'.
refs #31 --HG-- extra : rebase_source : 0dcc43912afc6a15566f18551faf64c6369fabb9
Diffstat (limited to 'lang')
-rw-r--r--lang/python/wiredtiger.i31
1 files changed, 18 insertions, 13 deletions
diff --git a/lang/python/wiredtiger.i b/lang/python/wiredtiger.i
index b59937996cf..992c0c9b790 100644
--- a/lang/python/wiredtiger.i
+++ b/lang/python/wiredtiger.i
@@ -102,6 +102,20 @@ static PyObject *wtError;
%pythoncode %{
WiredTigerError = _wiredtiger.WiredTigerError
+
+# Implements the iterable contract
+class IterableCursor:
+ def __init__(self, cursor):
+ self.cursor = cursor
+
+ def __iter__(self):
+ return self
+
+ def next(self):
+ if self.cursor.next() == WT_NOTFOUND:
+ raise StopIteration
+ return self.cursor.get_keys() + self.cursor.get_values()
+
%}
%typemap(out) int {
@@ -227,27 +241,18 @@ SELFHELPER(struct wt_cursor)
self._value = pack(self.value_format, *args)
self._set_value(self._value)
- # Implement the iterable contract for wt_cursor
def __iter__(self):
- return self
-
- def next(self):
- if self._next() == WT_NOTFOUND:
- raise StopIteration
- return self.get_keys() + self.get_values()
+ if not hasattr(self, '_iterable'):
+ self._iterable = IterableCursor(self)
+ return self._iterable
# De-position the cursor so the next iteration starts from the beginning
def reset(self):
self.last()
- self._next()
+ self.next()
%}
};
-/*
- * We want our own 'next' function for wt_cursor to implement iterable.
- */
-%rename(_next) next(WT_CURSOR *);
-
/* Remove / rename parts of the C API that we don't want in Python. */
%immutable wt_cursor::session;
%immutable wt_cursor::key_format;