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
[#8088] Support async/await with Deferreds #453
Conversation
Current coverage is 89.97% (diff: 100%)@@ trunk #453 diff @@
==========================================
Files 837 838 +1
Lines 144857 144783 -74
Methods 0 0
Messages 0 0
Branches 13597 13590 -7
==========================================
- Hits 130729 130272 -457
- Misses 11891 12272 +381
- Partials 2237 2239 +2
|
| raise StopIteration(self.result) | ||
| return self.d | ||
|
|
||
| def __await__(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should also implement __iter__ (also returning self).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. Should this actually need to implement __await__ at all? Since Deferred is the thing with await, I just need to change it to call __iter__ and rename the __await__ on this object...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually it should only implement __iter__, and no __await__:
class Deferred:
def __await__(self):
return _MakeDeferredAwaitable(self)
class _MakeDeferredAwaitable(object):
def __iter__(self):
return self
def __next__(self):
if self.result is not _NO_RESULT:
raise StopIteration(self.result)
return self.dPer PEP 492 -- __await__ should return an iterator (not an awaitable). Also, you might want to implement _MakeDeferredAwaitable.throw if it's possible in Twisted to throw exceptions to the await points.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@1st1 right, hmm, that's fixable.
re throw: it's not, because the Deferred will fire with a Failure; which inlineCallbacks will then throw into it (https://github.com/twisted/twisted/pull/453/files/5ffe7c0aed2c3dcea26ab6c6af8cb1ac7bf5f216#diff-41cf94828242f95ae48c543d7db3309aR1183).
|
|
||
| AwaitTests = _g["AwaitTests"] | ||
| __all__ = ["AwaitTests"] | ||
| except: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
drive-by comment!
This is too broad, in that weird misconfigurations will raise weird errors and these tests silently won't run.
- Move the
compatandFilePathimports, and the_pathconstruction, out of thetryblock. Even the_g = {}. Presumably the only thing that should fail is theexecfile, so that's the only thing that should be in thetry. - Catch only the exception that's the problem (
SyntaxError, I'm guessing?) not bare-word except. - Even the silly obvious can't fail stuff after the
execfileshould be in theelsesuite of atry/except/else; you don't want to swallow any more errors than you have to. - Consider adding an
AwaitTestsclass that just has a single, empty test method and a "skip" attribute, so there's at least visibility that the test was there, and it was intentionally not run. - Maybe don't even have a
try. This is always supposed to work on 3.5, right? So just comparesys.version_infoand do it unconditionally.
This comment brought to you by literally dozens of saturday nights ruined by NameError or AttributeErrorss in some obscure try block that "would only fail in this one case".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess "conditionally", not "unconditionally", but still; what I mean is, "without a try".
| return d | ||
|
|
||
|
|
||
| When writing coroutines, you do not need to use :api:`twisted.internet.defer.ensureDeferred <ensureDeferred>` when you are writing a coroutine which calls other coroutines which await on Deferreds; you can just ``await`` on it directly. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there is repetition of "when writing"
https://twistedmatrix.com/trac/ticket/8088