[Core] Fix Twisted fromCoroutine AttrError

Users with older versions of Twisted <= 21.2 were encoutering the
following error:

    File "/home/calum/projects/deluge/deluge/decorators.py", line 191, in activate
      d = defer.Deferred.fromCoroutine(self.coro)

    builtins.AttributeError: type object 'Deferred' has no attribute 'fromCoroutine'

Fixed by falling back to ensureDeferred since fromCoroutine was
introduced in Twisted 21.2 as a saner name for handling of coroutines.

Ref: https://twistedmatrix.com/trac/ticket/9825
This commit is contained in:
Calum Lind 2022-02-16 16:19:34 +00:00
parent 03938839e0
commit 055a84bb15
No known key found for this signature in database
GPG Key ID: 90597A687B836BA3
1 changed files with 6 additions and 3 deletions

View File

@ -188,7 +188,11 @@ class CoroutineDeferred(defer.Deferred):
"""If the result wasn't awaited before the next context switch, we turn it into a deferred."""
if self.awaited is None:
self.awaited = False
d = defer.Deferred.fromCoroutine(self.coro)
try:
d = defer.Deferred.fromCoroutine(self.coro)
except AttributeError:
# Fallback for Twisted <= 21.2 without fromCoroutine
d = defer.ensureDeferred(self.coro)
d.chainDeferred(self)
def addCallbacks(self, *args, **kwargs): # noqa: N802
@ -208,8 +212,7 @@ def maybe_coroutine(
@wraps(f)
def wrapper(*args, **kwargs):
# Uncomment for quick testing to make sure CoroutineDeferred magic isn't at fault
# from twisted.internet.defer import ensureDeferred
# return ensureDeferred(f(*args, **kwargs))
# return defer.ensureDeferred(f(*args, **kwargs))
return CoroutineDeferred(f(*args, **kwargs))
return wrapper