Skip to content

Commit d69c00b

Browse files
authored
Stop using twisted.internet.defer.returnValue (#18020)
`defer.returnValue` was only needed in Python 2; in Python 3, a simple `return` is fine. `twisted.internet.defer.returnValue` is deprecated as of Twisted 24.7.0. Most uses of `returnValue` in synapse were removed a while back; this cleans up some remaining bits.
1 parent 2d23250 commit d69c00b

File tree

5 files changed

+11
-27
lines changed

5 files changed

+11
-27
lines changed

changelog.d/18020.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Remove some remaining uses of `twisted.internet.defer.returnValue`. Contributed by Colin Watson.

contrib/cmdclient/console.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ def _check_can_login(self):
245245

246246
if "flows" not in json_res:
247247
print("Failed to find any login flows.")
248-
defer.returnValue(False)
248+
return False
249249

250250
flow = json_res["flows"][0] # assume first is the one we want.
251251
if "type" not in flow or "m.login.password" != flow["type"] or "stages" in flow:
@@ -254,8 +254,8 @@ def _check_can_login(self):
254254
"Unable to login via the command line client. Please visit "
255255
"%s to login." % fallback_url
256256
)
257-
defer.returnValue(False)
258-
defer.returnValue(True)
257+
return False
258+
return True
259259

260260
def do_emailrequest(self, line):
261261
"""Requests the association of a third party identifier

contrib/cmdclient/http.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def put_json(self, url, data):
7878
url, data, headers_dict={"Content-Type": ["application/json"]}
7979
)
8080
body = yield readBody(response)
81-
defer.returnValue((response.code, body))
81+
return response.code, body
8282

8383
@defer.inlineCallbacks
8484
def get_json(self, url, args=None):
@@ -88,7 +88,7 @@ def get_json(self, url, args=None):
8888
url = "%s?%s" % (url, qs)
8989
response = yield self._create_get_request(url)
9090
body = yield readBody(response)
91-
defer.returnValue(json.loads(body))
91+
return json.loads(body)
9292

9393
def _create_put_request(self, url, json_data, headers_dict: Optional[dict] = None):
9494
"""Wrapper of _create_request to issue a PUT request"""
@@ -134,7 +134,7 @@ def do_request(
134134
response = yield self._create_request(method, url)
135135

136136
body = yield readBody(response)
137-
defer.returnValue(json.loads(body))
137+
return json.loads(body)
138138

139139
@defer.inlineCallbacks
140140
def _create_request(
@@ -173,7 +173,7 @@ def _create_request(
173173
if self.verbose:
174174
print("Status %s %s" % (response.code, response.phrase))
175175
print(pformat(list(response.headers.getAllRawHeaders())))
176-
defer.returnValue(response)
176+
return response
177177

178178
def sleep(self, seconds):
179179
d = defer.Deferred()

synapse/logging/scopecontextmanager.py

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,10 @@
2020
#
2121

2222
import logging
23-
from types import TracebackType
24-
from typing import Optional, Type
23+
from typing import Optional
2524

2625
from opentracing import Scope, ScopeManager, Span
2726

28-
import twisted
29-
3027
from synapse.logging.context import (
3128
LoggingContext,
3229
current_context,
@@ -112,9 +109,6 @@ class _LogContextScope(Scope):
112109
"""
113110
A custom opentracing scope, associated with a LogContext
114111
115-
* filters out _DefGen_Return exceptions which arise from calling
116-
`defer.returnValue` in Twisted code
117-
118112
* When the scope is closed, the logcontext's active scope is reset to None.
119113
and - if enter_logcontext was set - the logcontext is finished too.
120114
"""
@@ -146,17 +140,6 @@ def __init__(
146140
self._finish_on_close = finish_on_close
147141
self._enter_logcontext = enter_logcontext
148142

149-
def __exit__(
150-
self,
151-
exc_type: Optional[Type[BaseException]],
152-
value: Optional[BaseException],
153-
traceback: Optional[TracebackType],
154-
) -> None:
155-
if exc_type == twisted.internet.defer._DefGen_Return:
156-
# filter out defer.returnValue() calls
157-
exc_type = value = traceback = None
158-
super().__exit__(exc_type, value, traceback)
159-
160143
def __str__(self) -> str:
161144
return f"Scope<{self.span}>"
162145

synapse/util/patch_inline_callbacks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def check_yield_points_inner(
162162
d = result.throwExceptionIntoGenerator(gen)
163163
else:
164164
d = gen.send(result)
165-
except (StopIteration, defer._DefGen_Return) as e:
165+
except StopIteration as e:
166166
if current_context() != expected_context:
167167
# This happens when the context is lost sometime *after* the
168168
# final yield and returning. E.g. we forgot to yield on a
@@ -183,7 +183,7 @@ def check_yield_points_inner(
183183
)
184184
)
185185
changes.append(err)
186-
# The `StopIteration` or `_DefGen_Return` contains the return value from the
186+
# The `StopIteration` contains the return value from the
187187
# generator.
188188
return cast(T, e.value)
189189

0 commit comments

Comments
 (0)