Uploaded image for project: 'Motor'
  1. Motor
  2. MOTOR-64

Incorrect iteration of MotorCursor causes infinite loop

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 0.5
    • Affects Version/s: 0.4
    • Component/s: None
    • Labels:
      None

      The following is not the right way to iterate a MotorCursor:

      for doc in cursor:
          pass
      

      Motor should throw TypeError: MotorCursor object is not iterable. Instead, "doc" is bound to the cursor itself and the loop runs forever.

      This is because, to my surprise, a Python object without an __iter__ is still iterable if it implements __getitem__. The for-loop first calls:

      MotorCursor.__getitem__(0)
      

      ...which updates the delegate Cursor's skip and limit and returns the original MotorCursor. Hence the infinite loop.

      It's probably best to delete __getitem__ and require you to use skip() and limit() instead; implementing __getitem__ falsely implies that a MotorCursor is iterable by conventional methods.

            Assignee:
            jesse@mongodb.com A. Jesse Jiryu Davis
            Reporter:
            jesse@mongodb.com A. Jesse Jiryu Davis
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: