Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 19e51b1

Browse files
authored
Manhole: wrap coroutines in defer.ensureDeferred automatically (#10602)
1 parent 0db8cab commit 19e51b1

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

changelog.d/10602.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The Synapse manhole no longer needs coroutines to be wrapped in `defer.ensureDeferred`.

docs/manhole.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ This gives a Python REPL in which `hs` gives access to the
6767
`synapse.server.HomeServer` object - which in turn gives access to many other
6868
parts of the process.
6969

70-
Note that any call which returns a coroutine will need to be wrapped in `ensureDeferred`.
70+
Note that, prior to Synapse 1.41, any call which returns a coroutine will need to be wrapped in `ensureDeferred`.
7171

7272
As a simple example, retrieving an event from the database:
7373

synapse/util/manhole.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
15+
import inspect
1516
import sys
1617
import traceback
1718

@@ -20,6 +21,7 @@
2021
from twisted.conch.manhole import ColoredManhole, ManholeInterpreter
2122
from twisted.conch.ssh.keys import Key
2223
from twisted.cred import checkers, portal
24+
from twisted.internet import defer
2325

2426
PUBLIC_KEY = (
2527
"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDHhGATaW4KhE23+7nrH4jFx3yLq9OjaEs5"
@@ -141,3 +143,15 @@ def showtraceback(self):
141143
self.write("".join(lines))
142144
finally:
143145
last_tb = ei = None
146+
147+
def displayhook(self, obj):
148+
"""
149+
We override the displayhook so that we automatically convert coroutines
150+
into Deferreds. (Our superclass' displayhook will take care of the rest,
151+
by displaying the Deferred if it's ready, or registering a callback
152+
if it's not).
153+
"""
154+
if inspect.iscoroutine(obj):
155+
super().displayhook(defer.ensureDeferred(obj))
156+
else:
157+
super().displayhook(obj)

0 commit comments

Comments
 (0)