Python 3.7 - RuntimeError: generator raised StopIteration

1,876 views
Skip to first unread message

gbr

unread,
Jun 28, 2019, 4:00:52 PM6/28/19
to sqlalchemy-alembic
I've upgraded my application to Python 3.7 and to the latest version of Alembic which triggers an exception when `context.get_current_revision()` is called.

```
  File "app.py", line 395, in check_database_version
    current_rev = context.get_current_revision()
  File "~/env-py3.7/lib/python3.7/site-packages/alembic/runtime/migration.py", line 239, in get_current_revision
    heads = self.get_current_heads()
  File "~/env-py3.7/lib/python3.7/site-packages/alembic/runtime/migration.py", line 289, in get_current_heads
    row[0] for row in self.connection.execute(self._version.select())
  File "~/env-py3.7/lib/python3.7/site-packages/alembic/runtime/migration.py", line 289, in <genexpr>
    row[0] for row in self.connection.execute(self._version.select())
RuntimeError: generator raised StopIteration
```

I think this is due to PEP 479.
PEP 479 is enabled for all code in Python 3.7, meaning that StopIteration exceptions raised directly or indirectly in coroutines and generators are transformed into RuntimeError exceptions. (Contributed by Yury Selivanov in bpo-32670.)

I changed `ResultProxy.__iter__()` to:

```
class ResultProxy
    ...
    def __iter__(self):
        while True:
            row = self.fetchone()
            if row is None:
                # raise StopIteration
                return
            else:
                yield row
```

which seems to resolve the problem. Is this a problem with Alembic and an incompatibility with Python 3.7, and is this expected to be fixed soon?

Mike Bayer

unread,
Jun 28, 2019, 4:30:49 PM6/28/19
to sqlalchem...@googlegroups.com
The source code for ResultProxy is in SQLAlchemy, not Alembic.  The "StopIteration" you seem to be referring to was removed four years ago in https://github.com/sqlalchemy/sqlalchemy/commit/6ab120558078bdcbfbe06d2ca55bd7a0d417bbb4 , so I would suggest upgrading to a modern version of SQLAlchemy.


```

which seems to resolve the problem. Is this a problem with Alembic and an incompatibility with Python 3.7, and is this expected to be fixed soon?


--
You received this message because you are subscribed to the Google Groups "sqlalchemy-alembic" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy-alem...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

gbr

unread,
Jun 28, 2019, 4:55:42 PM6/28/19
to sqlalchemy-alembic
Indeed, I was using version 1.0.9 and didn't realise that this wasn't an issue with alembic. Thanks for the quick response.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy-alembic+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages