diff --git a/mongo.py b/mongo.py index 22a9b22f1..569544900 100644 --- a/mongo.py +++ b/mongo.py @@ -18,6 +18,10 @@ class DatabaseException(Exception): """Raised when a database operation fails. """ +class InvalidOperation(Exception): + """Raised when a client attempts to perform an invalid operation. + """ + class ConnectionException(IOError): """Raised when a connection to the database cannot be made or is lost. """ @@ -446,6 +450,28 @@ class Cursor(object): self.__collection.database()._kill_cursor(self.__id) self.__killed = True + def __check_okay_to_chain(self): + """Check if it is okay to chain more options onto this cursor. + """ + if self.__retrieved or self.__id is not None: + raise InvalidOperation("cannot set options after executing query") + + def limit(self, limit): + """Limits the number of results to be returned by this cursor. + + Raises TypeError if limit is not an instance of int. Raises + InvalidOperation if this cursor has already been used. + + Arguments: + - `limit`: the number of results to return + """ + if not isinstance(limit, types.IntType): + raise TypeError("limit must be an int") + self.__check_okay_to_chain() + + self.__limit = limit + return self + def _refresh(self): """Refreshes the cursor with more data from Mongo. @@ -488,6 +514,7 @@ class Cursor(object): limit = self.__limit - self.__retrieved else: self._die() + return 0 message = struct.pack("