Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade from deprecated trio.Queue; fix tests; fix docs #49

Merged
merged 3 commits into from
Dec 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/source/rationale.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ domains rigidly separate.

The core of the "normal" asyncio main loop is the repeated execution of
synchronous code that's submitted to
:meth:`asyncio.AbstractEventLoop.call_soon` or
:meth:`asyncio.AbstractEventLoop.call_later`, or as the callbacks for
:meth:`asyncio.AbstractEventLoop.add_reader` /
:meth:`asyncio.AbstractEventLoop.add_writer`.
:meth:`python:asyncio.loop.call_soon` or
:meth:`python:asyncio.loop.call_later`, or as the callbacks for
:meth:`python:asyncio.loop.add_reader` /
:meth:`python:asyncio.loop.add_writer`.

Everything else within the ``asyncio`` core, esp. Futures, Coroutines, and
``await``'ing them, is just syntactic sugar. There is no concept of a
Expand Down
10 changes: 5 additions & 5 deletions docs/source/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -522,13 +522,13 @@ Cancellations are also propagated whenever possible. This means
Deferred Calls
----------------

:meth:`asyncio.AbstractEventLoop.call_soon` and friends work as usual.
:meth:`python:asyncio.loop.call_soon` and friends work as usual.

----------------
Worker Threads
----------------

:meth:`asyncio.AbstractEventLoop.run_in_executor` works as usual.
:meth:`python:asyncio.loop.run_in_executor` works as usual.

There is one caveat: the executor must be either ``None`` or an instance of
:class:`trio_asyncio.TrioExecutor`. The constructor of this class accepts one
Expand All @@ -540,8 +540,8 @@ argument: the number of workers.
File descriptors
------------------

:meth:`asyncio.AbstractEventLoop.add_reader` and
:meth:`asyncio.AbstractEventLoop.add_writer` work as usual, if you really
:meth:`python:asyncio.loop.add_reader` and
:meth:`python:asyncio.loop.add_writer` work as usual, if you really
need them. Behind the scenes, these calls create a Trio task which runs the
callback.

Expand All @@ -551,7 +551,7 @@ You might consider converting code using these calls to native Trio tasks.
Signals
---------

:meth:`asyncio.AbstractEventLoop.add_signal_handler` works as usual.
:meth:`python:asyncio.loop.add_signal_handler` works as usual.

------------
Subprocesses
Expand Down
1 change: 1 addition & 0 deletions newsfragments/49.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Replace deprecated ``trio.Queue`` with new channels, requiring Trio 0.9 or later.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"""

install_requires = [
"trio >= 0.5.0",
"trio >= 0.9.0",
"async_generator >= 1.6",
"outcome",
]
Expand Down
14 changes: 12 additions & 2 deletions tests/interop/test_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@
import sniffio
from tests import aiotest
import sys
import warnings
from async_generator import asynccontextmanager
from .. import utils as test_utils


def de_deprecate_converter(func):
def wrapper(proc):
with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning)
return func(proc)

return wrapper


class SomeThing:
flag = 0

Expand All @@ -31,15 +41,15 @@ async def dly_trio_adapted(self):
self.flag |= 2
return 8

@aio2trio
@de_deprecate_converter(aio2trio)
async def dly_trio_depr(self):
if sys.version_info >= (3, 7):
assert sniffio.current_async_library() == "trio"
await trio.sleep(0.01)
self.flag |= 2
return 8

@trio2aio
@de_deprecate_converter(trio2aio)
async def dly_asyncio_depr(self):
if sys.version_info >= (3, 7):
assert sniffio.current_async_library() == "asyncio"
Expand Down
4 changes: 2 additions & 2 deletions tests/python/test_selector_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
MOCK_ANY = mock.ANY


class TestBaseSelectorEventLoop(BaseSelectorEventLoop):
class BaseSelectorEventLoopForTest(BaseSelectorEventLoop):
def _make_self_pipe(self):
self._ssock = mock.Mock()
self._csock = mock.Mock()
Expand Down Expand Up @@ -58,7 +58,7 @@ def setUp(self):
super().setUp()
self.selector = mock.Mock()
self.selector.select.return_value = []
self.loop = TestBaseSelectorEventLoop(self.selector)
self.loop = BaseSelectorEventLoopForTest(self.selector)
self.set_event_loop(self.loop)

def test_make_socket_transport(self):
Expand Down
4 changes: 2 additions & 2 deletions tests/python/test_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
]


class TestSubprocessTransport(base_subprocess.BaseSubprocessTransport):
class SubprocessTransportForTest(base_subprocess.BaseSubprocessTransport):
def _start(self, *args, **kwargs):
self._proc = mock.Mock()
self._proc.stdin = None
Expand All @@ -44,7 +44,7 @@ def create_transport(self, waiter=None):
protocol = mock.Mock()
protocol.connection_made._is_coroutine = False
protocol.process_exited._is_coroutine = False
transport = TestSubprocessTransport(
transport = SubprocessTransportForTest(
self.loop, protocol, ['test'], False, None, None, None, 0, waiter=waiter
)
return (transport, protocol)
Expand Down
2 changes: 1 addition & 1 deletion trio_asyncio/async_.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class TrioEventLoop(BaseTrioEventLoop):

def _queue_handle(self, handle):
self._check_closed()
self._q.put_nowait(handle)
self._q_send.send_nowait(handle)
return handle

def default_exception_handler(self, context):
Expand Down
10 changes: 5 additions & 5 deletions trio_asyncio/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def __init__(self, queue_len=None):
queue_len = 10000

# Processing queue
self._q = trio.Queue(queue_len)
self._q_send, self._q_recv = trio.open_memory_channel(queue_len)

# which files to close?
self._close_files = set()
Expand Down Expand Up @@ -406,7 +406,7 @@ def call_soon_threadsafe(self, callback, *args, context=None):
self._check_callback(callback, 'call_soon_threadsafe')
self._check_closed()
h = Handle(callback, args, self, context=context, is_sync=True)
self._token.run_sync_soon(self._q.put_nowait, h)
self._token.run_sync_soon(self._q_send.send_nowait, h)

# drop all timers

Expand Down Expand Up @@ -512,7 +512,7 @@ async def synchronize(self):
def _handle_sig(self, sig, _):
"""Helper to safely enqueue a signal handler."""
h = self._signal_handlers[sig]
self._token.run_sync_soon(self._q.put_nowait, h)
self._token.run_sync_soon(self._q_send.send_nowait, h)

def add_signal_handler(self, sig, callback, *args):
"""asyncio's method to add a signal handler.
Expand Down Expand Up @@ -741,10 +741,10 @@ async def _main_loop_one(self, no_wait=False):

if obj is None:
if no_wait:
obj = self._q.get_nowait()
obj = self._q_recv.receive_nowait()
else:
with trio.move_on_after(timeout):
obj = await self._q.get()
obj = await self._q_recv.receive()
if obj is None:
# Timeout reached. Presumably now a timer is ready,
# so restart from the beginning.
Expand Down
4 changes: 2 additions & 2 deletions trio_asyncio/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def _queue_handle(self, handle):

def put(self, handle):
self._some_deferred -= 1
self._q.put_nowait(handle)
self._q_send.send_nowait(handle)

# If we don't have a token, the main loop is not yet running
# thus we can't have a race condition.
Expand All @@ -81,7 +81,7 @@ def put(self, handle):
self._some_deferred += 1
self._token.run_sync_soon(put, self, handle)
else:
self._q.put_nowait(handle)
self._q_send.send_nowait(handle)
return handle

def run_forever(self):
Expand Down