Red Hat Bugzilla – Bug 1294130
pymongo.cursor.Cursor.__next__ is being replaced with the next builtin instead of def __next__()
Last modified: 2016-01-18 16:03:28 EST
Description of problem:
The following code appears in /usr/lib64/python3.5/site-packages/pymongo/cursor.py:
972 def __next__(self):
973 """Advance the cursor."""
974 if self.__empty:
975 raise StopIteration
976 _db = self.__collection.database
977 if len(self.__data) or self._refresh():
978 if self.__manipulate:
979 return _db._fix_outgoing(self.__data.popleft(),
982 return self.__data.popleft()
984 raise StopIteration
986 __next__ = next
That last line replaces the __next__ function with the Python next() builtin, which causes the Cursor not to be iterable. Strangely, the upstream source code that the current Rawhide release is based on does not contain this error:
This is very severe for Python 3 users, but does not seem to affect Python 2 users.
Version-Release number of selected component (if applicable):
$ rpm -q python3-pymongo
Steps to Reproduce:
1. Download the attached reproducer, which uses python3-mongoengine.
2. $ sudo dnf install python3-mongoengine
3. $ python3 mongoengine_test.py
[rbarlow@boole ~]$ python3 mongoengine_test.py
Traceback (most recent call last):
File "mongoengine_test.py", line 15, in <module>
for u in User.objects:
File "/usr/lib/python3.5/site-packages/mongoengine/queryset/queryset.py", line 80, in _iter_results
File "/usr/lib/python3.5/site-packages/mongoengine/queryset/queryset.py", line 92, in _populate_cache
File "/usr/lib/python3.5/site-packages/mongoengine/queryset/base.py", line 1407, in __next__
raw_doc = next(self._cursor)
TypeError: next expected at least 1 arguments, got 0
It should look like the output given by running the same script with python2:
[rbarlow@boole ~]$ python2 mongoengine_test.py
Created attachment 1109336 [details]
I performed a test, and I believe it is the 2to3 utility that is making this change. The upstream github page states that pymongo supports Python 2.6 and 2.7, and Pythons 3.2-3.5. Their setup.py does not seem to use 2to3. I would interpret that to mean that 2to3 is not needed with current versions of pymongo, but I am not sure.
Alternatively, it may be possible to exclude the fixer that is renaming next() to __next__() so that this problem goes away.
I recommend investigating whether 2to3 is really needed any longer. If it is not, it would be beneficial to drop it from the spec file.
The upstream release notes explicitly state not to use 2to3 with pymongo >= 3:
This is certainly the issue - please fix this as pymongo does not work with Python 3 and Python 3 is the default Python implementation for Fedora >= 23.
This package has changed ownership in the Fedora Package Database. Reassigning to the new owner of this component.
I believe this fix will be straightfoward, as dropping just the 2to3 line makes a package that works in Python 2 and Python 3 with the test I attached earlier.
This is now fixed in Rawhide: