Skip to content

Commit

Permalink
Stop using twisted.internet.defer.returnValue (#18020)
Browse files Browse the repository at this point in the history
`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.
  • Loading branch information
cjwatson authored Dec 20, 2024
1 parent 2d23250 commit d69c00b
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 27 deletions.
1 change: 1 addition & 0 deletions changelog.d/18020.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove some remaining uses of `twisted.internet.defer.returnValue`. Contributed by Colin Watson.
6 changes: 3 additions & 3 deletions contrib/cmdclient/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def _check_can_login(self):

if "flows" not in json_res:
print("Failed to find any login flows.")
defer.returnValue(False)
return False

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

def do_emailrequest(self, line):
"""Requests the association of a third party identifier
Expand Down
8 changes: 4 additions & 4 deletions contrib/cmdclient/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def put_json(self, url, data):
url, data, headers_dict={"Content-Type": ["application/json"]}
)
body = yield readBody(response)
defer.returnValue((response.code, body))
return response.code, body

@defer.inlineCallbacks
def get_json(self, url, args=None):
Expand All @@ -88,7 +88,7 @@ def get_json(self, url, args=None):
url = "%s?%s" % (url, qs)
response = yield self._create_get_request(url)
body = yield readBody(response)
defer.returnValue(json.loads(body))
return json.loads(body)

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

body = yield readBody(response)
defer.returnValue(json.loads(body))
return json.loads(body)

@defer.inlineCallbacks
def _create_request(
Expand Down Expand Up @@ -173,7 +173,7 @@ def _create_request(
if self.verbose:
print("Status %s %s" % (response.code, response.phrase))
print(pformat(list(response.headers.getAllRawHeaders())))
defer.returnValue(response)
return response

def sleep(self, seconds):
d = defer.Deferred()
Expand Down
19 changes: 1 addition & 18 deletions synapse/logging/scopecontextmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,10 @@
#

import logging
from types import TracebackType
from typing import Optional, Type
from typing import Optional

from opentracing import Scope, ScopeManager, Span

import twisted

from synapse.logging.context import (
LoggingContext,
current_context,
Expand Down Expand Up @@ -112,9 +109,6 @@ class _LogContextScope(Scope):
"""
A custom opentracing scope, associated with a LogContext
* filters out _DefGen_Return exceptions which arise from calling
`defer.returnValue` in Twisted code
* When the scope is closed, the logcontext's active scope is reset to None.
and - if enter_logcontext was set - the logcontext is finished too.
"""
Expand Down Expand Up @@ -146,17 +140,6 @@ def __init__(
self._finish_on_close = finish_on_close
self._enter_logcontext = enter_logcontext

def __exit__(
self,
exc_type: Optional[Type[BaseException]],
value: Optional[BaseException],
traceback: Optional[TracebackType],
) -> None:
if exc_type == twisted.internet.defer._DefGen_Return:
# filter out defer.returnValue() calls
exc_type = value = traceback = None
super().__exit__(exc_type, value, traceback)

def __str__(self) -> str:
return f"Scope<{self.span}>"

Expand Down
4 changes: 2 additions & 2 deletions synapse/util/patch_inline_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def check_yield_points_inner(
d = result.throwExceptionIntoGenerator(gen)
else:
d = gen.send(result)
except (StopIteration, defer._DefGen_Return) as e:
except StopIteration as e:
if current_context() != expected_context:
# This happens when the context is lost sometime *after* the
# final yield and returning. E.g. we forgot to yield on a
Expand All @@ -183,7 +183,7 @@ def check_yield_points_inner(
)
)
changes.append(err)
# The `StopIteration` or `_DefGen_Return` contains the return value from the
# The `StopIteration` contains the return value from the
# generator.
return cast(T, e.value)

Expand Down

0 comments on commit d69c00b

Please sign in to comment.