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

Incorrect iteration of MotorCursor causes infinite loop

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor - P4
    • Resolution: Fixed
    • Affects Version/s: 0.4
    • Fix Version/s: 0.5
    • Component/s: None
    • Labels:
      None

      Description

      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.

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                Created:
                Updated:
                Resolved: