Ho'jat Kaveh
Ho'jat Kaveh

Reputation: 123

mongoengine - RuntimeError: generator raised StopIteration

I have flask-mongoengine application and I have a seriuos problem when I want to iterate over a mongoengine queryset object. here is the code for my mongoengine object:

mongo_models:

class Candid(Document):
   candid_intent_id = StringField()
   id_list = ListField(StringField())
   custom_code = StringField()
   is_approved = BooleanField()

   def to_json(self, *args, **kwargs):
       return {'candid_intent_id': self.candid_intent_id,
               'id_list': self.id_list,
               'custom_code': self.custom_code,
               'is_approved': self.is_approved}

and I want to iterate over them like this:

candid_list:

custom_code = 'Bob'
query_set = Candid.objects(is_denied=False, custom_code=current_request.custom_code)
try:
    for candid in query_set:
        suggested_intent_list.append(candid.to_json())
        if not candid.is_approved:
            suggested_intent_count += 1
except StopIteration:
    return 'StopIteration error'

now here is the catch: when run my code (with python 3.5.2) using a local mongo server it works fine (whether Candid collection is empty or not), but I deploy the code on a dockerized server (with python 3.7.0) I get the following Runtime Error:

File "/controller.py", line 25, in <candid_list>
   for candid in query_set:
RuntimeError: generator raised StopIteration

btw, mongoengine version is the same for local and docker server run: mongoengine==0.15.0.

please tell me if I have to provide more information, and any help would be greatly appriciated.

Upvotes: 2

Views: 2126

Answers (3)

Prasad
Prasad

Reputation: 1245

Upgrading mongoengine solved my problem. The old version (or the version which is causing this issue) need to be updated where this bug has been fixed.

pip install mongoengine --upgrade

Upvotes: 4

Ho&#39;jat Kaveh
Ho&#39;jat Kaveh

Reputation: 123

as @FHTMitchell pointed out the problem was with defferance between python version on my local machine (3.5.2) and my docker server (3.7.0). I changed the python for the docker image to python:3.5-slim and it solved the problem. hope it helps someone. But most importantly the lesson i got from this is this: make sure your development and deployment environment match!

Upvotes: 0

FHTMitchell
FHTMitchell

Reputation: 12156

The problem is that raising StopIteration inside generators became deprecated in python 3.5, raised a warning in python 3.6 and now raises an error in python 3.7. It sounds like this package is not ready for python 3.7 yet.

https://www.python.org/dev/peps/pep-0479/#transition-plan

Upvotes: 2

Related Questions