From 76755639281295f7e061eff615c0573b3171b9ae Mon Sep 17 00:00:00 2001 From: "Olivier Wilkinson (reivilibre)" Date: Fri, 13 Aug 2021 20:05:06 +0100 Subject: [PATCH 1/3] Implementation --- synapse/util/manhole.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/synapse/util/manhole.py b/synapse/util/manhole.py index da24ba0470b6..522daa323d00 100644 --- a/synapse/util/manhole.py +++ b/synapse/util/manhole.py @@ -12,6 +12,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +import inspect import sys import traceback @@ -20,6 +21,7 @@ from twisted.conch.manhole import ColoredManhole, ManholeInterpreter from twisted.conch.ssh.keys import Key from twisted.cred import checkers, portal +from twisted.internet import defer PUBLIC_KEY = ( "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDHhGATaW4KhE23+7nrH4jFx3yLq9OjaEs5" @@ -141,3 +143,15 @@ def showtraceback(self): self.write("".join(lines)) finally: last_tb = ei = None + + def displayhook(self, obj): + """ + We override the displayhook so that we automatically convert coroutines + into Deferreds. (Our superclass' displayhook will take care of the rest, + by displaying the Deferred if it's ready, or registering a callback + if it's not). + """ + if inspect.iscoroutine(obj): + super().displayhook(defer.ensureDeferred(obj)) + else: + super().displayhook(obj) From 3a37692e08136c1965641eae49d39edaf5ee145e Mon Sep 17 00:00:00 2001 From: "Olivier Wilkinson (reivilibre)" Date: Fri, 13 Aug 2021 20:05:22 +0100 Subject: [PATCH 2/3] This part of the doc is obsolete but still worthwhile for old versions --- docs/manhole.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/manhole.md b/docs/manhole.md index 37d1d7823c00..db92df88dcc9 100644 --- a/docs/manhole.md +++ b/docs/manhole.md @@ -67,7 +67,7 @@ This gives a Python REPL in which `hs` gives access to the `synapse.server.HomeServer` object - which in turn gives access to many other parts of the process. -Note that any call which returns a coroutine will need to be wrapped in `ensureDeferred`. +Note that, prior to Synapse 1.41, any call which returns a coroutine will need to be wrapped in `ensureDeferred`. As a simple example, retrieving an event from the database: From 2ffb7b4410cea935b0cbaa29d485cf507b1d76a1 Mon Sep 17 00:00:00 2001 From: "Olivier Wilkinson (reivilibre)" Date: Fri, 13 Aug 2021 20:05:27 +0100 Subject: [PATCH 3/3] Newsfile --- changelog.d/10602.feature | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/10602.feature diff --git a/changelog.d/10602.feature b/changelog.d/10602.feature new file mode 100644 index 000000000000..ab18291a20bb --- /dev/null +++ b/changelog.d/10602.feature @@ -0,0 +1 @@ +The Synapse manhole no longer needs coroutines to be wrapped in `defer.ensureDeferred`.