Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prove tasklets can be Python 2.7 and 3.7 compatible. #174

Merged
merged 2 commits into from
Aug 26, 2019

Conversation

chrisrossi
Copy link
Contributor

If our codebase is to be compatible with Python 2.7, we have to change
all instances where we return from a tasklet to using raise ndb.Return(), because Python 2 doesn't allow returning a value from a
generator.

Python 3.7 also doesn't allow raising StopIteration from inside of a
generator, which, on first blush, seemed to preclude having a common
codebase that was both Python 2.7 and 3.7 compatible. As it turns out,
though, we can just make tasklets.Return inherit from
Exception instead of from StopIteration.

Crisis averted.

Chris Rossi added 2 commits August 26, 2019 14:21
If our codebase is to be compatible with Python 2.7, we have to change
all instances where we ``return`` from a tasklet to using ``raise
ndb.Return()``, because Python 2 doesn't allow returning a value from a
generator.

Python 3.7 also doesn't allow raising StopIteration from inside of a
generator, which, on first blush, seemed to preclude having a common
codebase that was both Python 2.7 and 3.7 compatible. As it turns out,
though, we can just make ``tasklets.Return`` inherit from
``Exception`` instead of from ``StopIteration``.

Crisis averted.
@chrisrossi chrisrossi requested a review from cguardia August 26, 2019 18:33
@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Aug 26, 2019
Copy link
Contributor

@cguardia cguardia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What a scare! Good thing it was fixable.

@chrisrossi
Copy link
Contributor Author

And just to be clear: from now until we decide we can drop Python 2 support, we need to always raise Return. When we have a test suite running under 2.7, that will be enforced.

Users, however, can do whatever they want. If they're using Python 2 they'll need to raise Return, as well, but Python 3 users should feel free to just use a simple return. That will still work just fine. We just can't do it ourselves, because of backwards compatibility.

@chrisrossi chrisrossi merged commit 78ad37e into googleapis:master Aug 26, 2019
@chrisrossi chrisrossi deleted the Return branch August 26, 2019 19:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants