sovnarkom/mongo-python3-driver — pull request #1 by appcove (2012-02-05 19:36:55+00:00)
Title: Fixed incorrect handling of responseFlags attribute of the OP_REPLY struct

Hello,

I was attempting to get Mongo 1.8 working with Python 3.1.4. Upon making any connection to the server, I was greeted with this stack trace:

>>> x = pymongo.Connection('localhost', 27017)
Traceback (most recent call last):
File "", line 1, in
File "pymongo/connection.py", line 170, in __init__
self.__find_master()
File "pymongo/connection.py", line 342, in __find_master
master = self.__master(sock)
File "pymongo/connection.py", line 227, in __master
result = self["admin"].command({"ismaster": 1}, _sock=sock)
File "pymongo/database.py", line 215, in command
_is_command=True)
File "pymongo/collection.py", line 347, in find_one
_is_command=_is_command):
File "pymongo/cursor.py", line 482, in __next__
if len(self.__data) or self._refresh():
File "pymongo/cursor.py", line 458, in _refresh
self.__query_spec(), self.__fields))
File "pymongo/cursor.py", line 426, in __send_message
response = helpers._unpack_response(response, self.__id)
File "pymongo/helpers.py", line 98, in _unpack_response
assert response_flag == 0
AssertionError

The problem was caused by `response_flag` bit #3 being set as of Mongo 1.6. This tripped the `AssertionError`.

In pymongo.helpers._unpack_response, there was a problem in how the
responseFlags were being parsed. Rather than checking to see if each
bit was set with a bitwise AND, the == operator was being used. This
meant that if more than one flag was ever set, then the == would return
False, and the logic would proceed incorrectly.

This has been changed to `response_flag & 2 == 2` to check for bit #1.
The value `2` is derived from boolean 0b0010, meaning bit #1.

The actual problem appears to have been introduced in Mongod 1.6 when
bit #3 is always set:

AwaitCapable: Set when the server supports the AwaitData Query
option. If it doesn't, a client should sleep a little between
getMore's of a Tailable cursor. Mongod version 1.6 supports AwaitData
and thus always sets AwaitCapable.

Thanks!
Jason Garber