From 055a84bb15b03f066681a17f0274bcc0fbf82d04 Mon Sep 17 00:00:00 2001 From: Calum Lind Date: Wed, 16 Feb 2022 16:19:34 +0000 Subject: [PATCH] [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 --- deluge/decorators.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/deluge/decorators.py b/deluge/decorators.py index 33b8f1984..2f9fcd7d3 100644 --- a/deluge/decorators.py +++ b/deluge/decorators.py @@ -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