Skip to content

Commit 84211d7

Browse files
authored
Add subshell docstrings (#1405)
1 parent 501a1ad commit 84211d7

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

ipykernel/kernelbase.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ def __init__(self, **kwargs):
300300
)
301301

302302
async def dispatch_control(self, msg):
303-
# Ensure only one control message is processed at a time
303+
"""Dispatch a control request, ensuring only one message is processed at a time."""
304304
async with asyncio.Lock():
305305
await self.process_control(msg)
306306

@@ -1225,6 +1225,10 @@ async def do_debug_request(self, msg):
12251225
raise NotImplementedError
12261226

12271227
async def create_subshell_request(self, socket, ident, parent) -> None:
1228+
"""Handle a create subshell request.
1229+
1230+
.. versionadded:: 7
1231+
"""
12281232
if not self.session:
12291233
return
12301234
if not self._supports_kernel_subshells:
@@ -1241,6 +1245,10 @@ async def create_subshell_request(self, socket, ident, parent) -> None:
12411245
self.session.send(socket, "create_subshell_reply", reply, parent, ident)
12421246

12431247
async def delete_subshell_request(self, socket, ident, parent) -> None:
1248+
"""Handle a delete subshell request.
1249+
1250+
.. versionadded:: 7
1251+
"""
12441252
if not self.session:
12451253
return
12461254
if not self._supports_kernel_subshells:
@@ -1265,6 +1273,10 @@ async def delete_subshell_request(self, socket, ident, parent) -> None:
12651273
self.session.send(socket, "delete_subshell_reply", reply, parent, ident)
12661274

12671275
async def list_subshell_request(self, socket, ident, parent) -> None:
1276+
"""Handle a list subshell request.
1277+
1278+
.. versionadded:: 7
1279+
"""
12681280
if not self.session:
12691281
return
12701282
if not self._supports_kernel_subshells:

ipykernel/socket_pair.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Pair of ZMQ inproc sockets used for communication between threads."""
2+
13
from __future__ import annotations
24

35
from typing import Any
@@ -12,6 +14,8 @@ class SocketPair:
1214
1315
One of the threads is always the shell_channel_thread, the other may be the control
1416
thread, main thread or a subshell thread.
17+
18+
.. versionadded:: 7
1519
"""
1620

1721
from_socket: zmq.Socket[Any]
@@ -21,20 +25,23 @@ class SocketPair:
2125
on_recv_copy: bool
2226

2327
def __init__(self, context: zmq.Context[Any], name: str):
28+
"""Initialize the inproc socker pair."""
2429
self.from_socket = context.socket(zmq.PAIR)
2530
self.to_socket = context.socket(zmq.PAIR)
2631
address = self._address(name)
2732
self.from_socket.bind(address)
2833
self.to_socket.connect(address) # Or do I need to do this in another thread?
2934

3035
def close(self):
36+
"""Close the inproc socker pair."""
3137
self.from_socket.close()
3238

3339
if self.to_stream is not None:
3440
self.to_stream.close()
3541
self.to_socket.close()
3642

3743
def on_recv(self, io_loop: IOLoop, on_recv_callback, copy: bool = False):
44+
"""Set the callback used when a message is received on the to stream."""
3845
# io_loop is that of the 'to' thread.
3946
self.on_recv_callback = on_recv_callback
4047
self.on_recv_copy = copy
@@ -43,12 +50,15 @@ def on_recv(self, io_loop: IOLoop, on_recv_callback, copy: bool = False):
4350
self.resume_on_recv()
4451

4552
def pause_on_recv(self):
53+
"""Pause receiving on the to stream."""
4654
if self.to_stream is not None:
4755
self.to_stream.stop_on_recv()
4856

4957
def resume_on_recv(self):
58+
"""Resume receiving on the to stream."""
5059
if self.to_stream is not None and not self.to_stream.closed():
5160
self.to_stream.on_recv(self.on_recv_callback, copy=self.on_recv_copy)
5261

5362
def _address(self, name) -> str:
63+
"""Return the address used for this inproc socket pair."""
5464
return f"inproc://subshell{name}"

ipykernel/subshell.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99

1010

1111
class SubshellThread(BaseThread):
12-
"""A thread for a subshell."""
12+
"""A thread for a subshell.
13+
14+
.. versionadded:: 7
15+
"""
1316

1417
def __init__(
1518
self,
@@ -27,6 +30,7 @@ def __init__(
2730
self.aborting = False
2831

2932
def run(self) -> None:
33+
"""Run the thread."""
3034
try:
3135
super().run()
3236
finally:

ipykernel/subshell_manager.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class SubshellManager:
2929
3030
Sending reply messages via the shell_socket is wrapped by another lock to protect
3131
against multiple subshells attempting to send at the same time.
32+
33+
.. versionadded:: 7
3234
"""
3335

3436
def __init__(
@@ -37,6 +39,7 @@ def __init__(
3739
shell_channel_io_loop: IOLoop,
3840
shell_socket: zmq.Socket[t.Any],
3941
):
42+
"""Initialize the subshell manager."""
4043
assert current_thread() == main_thread()
4144

4245
self._context: zmq.Context[t.Any] = context
@@ -80,22 +83,30 @@ def close(self) -> None:
8083
self._shell_channel_to_main.close()
8184

8285
def get_shell_channel_to_subshell_pair(self, subshell_id: str | None) -> SocketPair:
86+
"""Return the inproc socket pair used to send messages from the shell channel
87+
to a particular subshell or main shell."""
8388
if subshell_id is None:
8489
return self._shell_channel_to_main
8590
with self._lock_cache:
8691
return self._cache[subshell_id].shell_channel_to_subshell
8792

8893
def get_subshell_to_shell_channel_socket(self, subshell_id: str | None) -> zmq.Socket[t.Any]:
94+
"""Return the socket used by a particular subshell or main shell to send
95+
messages to the shell channel.
96+
"""
8997
if subshell_id is None:
9098
return self._main_to_shell_channel.from_socket
9199
with self._lock_cache:
92100
return self._cache[subshell_id].subshell_to_shell_channel.from_socket
93101

94102
def get_shell_channel_to_subshell_socket(self, subshell_id: str | None) -> zmq.Socket[t.Any]:
103+
"""Return the socket used by the shell channel to send messages to a particular
104+
subshell or main shell.
105+
"""
95106
return self.get_shell_channel_to_subshell_pair(subshell_id).from_socket
96107

97108
def get_subshell_aborting(self, subshell_id: str) -> bool:
98-
"""Get the aborting flag of the specified subshell."""
109+
"""Get the boolean aborting flag of the specified subshell."""
99110
return self._cache[subshell_id].aborting
100111

101112
def list_subshell(self) -> list[str]:
@@ -107,6 +118,9 @@ def list_subshell(self) -> list[str]:
107118
return list(self._cache)
108119

109120
def set_on_recv_callback(self, on_recv_callback):
121+
"""Set the callback used by the main shell and all subshells to receive
122+
messages sent from the shell channel thread.
123+
"""
110124
assert current_thread() == main_thread()
111125
self._on_recv_callback = on_recv_callback
112126
self._shell_channel_to_main.on_recv(IOLoop.current(), partial(self._on_recv_callback, None))

0 commit comments

Comments
 (0)