Skip to content

Commit 4bb7ac5

Browse files
Merge pull request #814 from davidbrochart/async_stdin_hook
Make _stdin_hook_default async
2 parents 5b2aa2b + 238761a commit 4bb7ac5

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

jupyter_client/client.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Copyright (c) Jupyter Development Team.
33
# Distributed under the terms of the Modified BSD License.
44
import asyncio
5+
import inspect
56
import sys
67
import time
78
import typing as t
@@ -232,7 +233,7 @@ async def _async_recv_reply(
232233
continue
233234
return reply
234235

235-
def _stdin_hook_default(self, msg: t.Dict[str, t.Any]) -> None:
236+
async def _stdin_hook_default(self, msg: t.Dict[str, t.Any]) -> None:
236237
"""Handle an input request"""
237238
content = msg["content"]
238239
if content.get("password", False):
@@ -251,7 +252,7 @@ def _stdin_hook_default(self, msg: t.Dict[str, t.Any]) -> None:
251252

252253
# only send stdin reply if there *was not* another request
253254
# or execution finished while we were reading.
254-
if not (self.stdin_channel.msg_ready() or self.shell_channel.msg_ready()):
255+
if not (await self.stdin_channel.msg_ready() or await self.shell_channel.msg_ready()):
255256
self.input(raw_data)
256257

257258
def _output_hook_default(self, msg: t.Dict[str, t.Any]) -> None:
@@ -469,7 +470,7 @@ async def _async_execute_interactive(
469470
If not specified, output will be redisplayed.
470471
471472
stdin_hook: callable(msg)
472-
Function to be called with stdin_request messages.
473+
Function or awaitable to be called with stdin_request messages.
473474
If not specified, input/getpass will be called.
474475
475476
Returns
@@ -536,7 +537,9 @@ async def _async_execute_interactive(
536537
raise TimeoutError("Timeout waiting for output")
537538
if stdin_socket in events:
538539
req = await self.stdin_channel.get_msg(timeout=0)
539-
stdin_hook(req)
540+
res = stdin_hook(req)
541+
if inspect.isawaitable(res):
542+
await res
540543
continue
541544
if iopub_socket not in events:
542545
continue

0 commit comments

Comments
 (0)