Skip to content

Commit

Permalink
gh-101524: Split Up the _xxsubinterpreters Module (gh-101526)
Browse files Browse the repository at this point in the history
This is step 1 in potentially dropping all the "channel"-related code. Channels have already been removed from PEP 554.

#101524
  • Loading branch information
ericsnowcurrently authored Feb 4, 2023
1 parent d4c410f commit c67b005
Show file tree
Hide file tree
Showing 15 changed files with 4,058 additions and 3,748 deletions.
23 changes: 12 additions & 11 deletions Lib/test/support/interpreters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import time
import _xxsubinterpreters as _interpreters
import _xxinterpchannels as _channels

# aliases:
from _xxsubinterpreters import (
from _xxsubinterpreters import is_shareable
from _xxinterpchannels import (
ChannelError, ChannelNotFoundError, ChannelEmptyError,
is_shareable,
)


Expand Down Expand Up @@ -102,22 +103,22 @@ def create_channel():
The channel may be used to pass data safely between interpreters.
"""
cid = _interpreters.channel_create()
cid = _channels.create()
recv, send = RecvChannel(cid), SendChannel(cid)
return recv, send


def list_all_channels():
"""Return a list of (recv, send) for all open channels."""
return [(RecvChannel(cid), SendChannel(cid))
for cid in _interpreters.channel_list_all()]
for cid in _channels.list_all()]


class _ChannelEnd:
"""The base class for RecvChannel and SendChannel."""

def __init__(self, id):
if not isinstance(id, (int, _interpreters.ChannelID)):
if not isinstance(id, (int, _channels.ChannelID)):
raise TypeError(f'id must be an int, got {id!r}')
self._id = id

Expand Down Expand Up @@ -152,10 +153,10 @@ def recv(self, *, _sentinel=object(), _delay=10 / 1000): # 10 milliseconds
This blocks until an object has been sent, if none have been
sent already.
"""
obj = _interpreters.channel_recv(self._id, _sentinel)
obj = _channels.recv(self._id, _sentinel)
while obj is _sentinel:
time.sleep(_delay)
obj = _interpreters.channel_recv(self._id, _sentinel)
obj = _channels.recv(self._id, _sentinel)
return obj

def recv_nowait(self, default=_NOT_SET):
Expand All @@ -166,9 +167,9 @@ def recv_nowait(self, default=_NOT_SET):
is the same as recv().
"""
if default is _NOT_SET:
return _interpreters.channel_recv(self._id)
return _channels.recv(self._id)
else:
return _interpreters.channel_recv(self._id, default)
return _channels.recv(self._id, default)


class SendChannel(_ChannelEnd):
Expand All @@ -179,7 +180,7 @@ def send(self, obj):
This blocks until the object is received.
"""
_interpreters.channel_send(self._id, obj)
_channels.send(self._id, obj)
# XXX We are missing a low-level channel_send_wait().
# See bpo-32604 and gh-19829.
# Until that shows up we fake it:
Expand All @@ -194,4 +195,4 @@ def send_nowait(self, obj):
# XXX Note that at the moment channel_send() only ever returns
# None. This should be fixed when channel_send_wait() is added.
# See bpo-32604 and gh-19829.
return _interpreters.channel_send(self._id, obj)
return _channels.send(self._id, obj)
Loading

0 comments on commit c67b005

Please sign in to comment.