quamash/__init__.py | 14 ++++--- quamash/_unix.py | 2 +- tests/test_qeventloop.py | 96 +++++++++++++++++++++--------------------------- 3 files changed, 52 insertions(+), 60 deletions(-) diff --git a/quamash/__init__.py b/quamash/__init__.py index 1c16118..97a4c84 100644 --- a/quamash/__init__.py +++ b/quamash/__init__.py @@ -180,7 +180,11 @@ class _SimpleTimer(QtCore.QObject): self._stopped = False def add_callback(self, handle, delay=0): - timerid = self.startTimer(delay * 1000) + """ + delay should be in seconds(float or int), while + startTimer accepts milliseconds(int) + """ + timerid = self.startTimer(round(delay * 1000)) self._logger.debug("Registering timer id {0}".format(timerid)) assert timerid not in self.__callbacks self.__callbacks[timerid] = handle @@ -225,14 +229,14 @@ class _QEventLoop: >>> >>> app = getfixture('application') >>> - >>> @asyncio.coroutine - ... def xplusy(x, y): - ... yield from asyncio.sleep(.1) + ... async def xplusy(x, y): + ... await asyncio.sleep(.1) ... assert x + y == 4 - ... yield from asyncio.sleep(.1) + ... await asyncio.sleep(.1) >>> >>> loop = QEventLoop(app) >>> asyncio.set_event_loop(loop) + >>> asyncio.events._set_running_loop(loop) >>> with loop: ... loop.run_until_complete(xplusy(2, 2)) """ diff --git a/quamash/_unix.py b/quamash/_unix.py index 0e7137e..f39c655 100644 --- a/quamash/_unix.py +++ b/quamash/_unix.py @@ -41,7 +41,7 @@ def _fileobj_to_fd(fileobj): return fd -class _SelectorMapping(collections.Mapping): +class _SelectorMapping(collections.abc.Mapping): """Mapping of file objects to selector keys.""" diff --git a/tests/test_qeventloop.py b/tests/test_qeventloop.py index ba6f674..0cccbb4 100644 --- a/tests/test_qeventloop.py +++ b/tests/test_qeventloop.py @@ -20,6 +20,7 @@ import pytest def loop(request, application): lp = quamash.QEventLoop(application) asyncio.set_event_loop(lp) + asyncio.events._set_running_loop(lp) additional_exceptions = [] @@ -116,33 +117,30 @@ class TestCanRunTasksInExecutor: was_invoked.value = 1 logging.debug('end blocking_func()') - @asyncio.coroutine - def blocking_task(self, loop, executor, was_invoked): + async def blocking_task(self, loop, executor, was_invoked): logging.debug('start blocking task()') fut = loop.run_in_executor(executor, self.blocking_func, was_invoked) - yield from asyncio.wait_for(fut, timeout=5.0) + await asyncio.wait_for(fut, timeout=5.0) logging.debug('start blocking task()') def test_can_execute_subprocess(loop): """Verify that a subprocess can be executed.""" - @asyncio.coroutine - def mycoro(): - process = yield from asyncio.create_subprocess_exec( + async def mycoro(): + process = await asyncio.create_subprocess_exec( sys.executable or 'python', '-c', 'import sys; sys.exit(5)') - yield from process.wait() + await process.wait() assert process.returncode == 5 loop.run_until_complete(asyncio.wait_for(mycoro(), timeout=3)) def test_can_read_subprocess(loop): """Verify that a subprocess's data can be read from stdout.""" - @asyncio.coroutine - def mycoro(): - process = yield from asyncio.create_subprocess_exec( + async def mycoro(): + process = await asyncio.create_subprocess_exec( sys.executable or 'python', '-c', 'print("Hello async world!")', stdout=subprocess.PIPE) - received_stdout = yield from process.stdout.readexactly(len(b'Hello async world!\n')) - yield from process.wait() + received_stdout = await process.stdout.readexactly(len(b'Hello async world!\n')) + await process.wait() assert process.returncode == 0 assert received_stdout.strip() == b'Hello async world!' @@ -151,12 +149,11 @@ def test_can_read_subprocess(loop): def test_can_communicate_subprocess(loop): """Verify that a subprocess's data can be passed in/out via stdin/stdout.""" - @asyncio.coroutine - def mycoro(): - process = yield from asyncio.create_subprocess_exec( + async def mycoro(): + process = await asyncio.create_subprocess_exec( sys.executable or 'python', '-c', 'print(input())', stdout=subprocess.PIPE, stdin=subprocess.PIPE) - received_stdout, received_stderr = yield from process.communicate(b'Hello async world!\n') - yield from process.wait() + received_stdout, received_stderr = await process.communicate(b'Hello async world!\n') + await process.wait() assert process.returncode == 0 assert received_stdout.strip() == b'Hello async world!' loop.run_until_complete(asyncio.wait_for(mycoro(), timeout=3)) @@ -165,12 +162,11 @@ def test_can_communicate_subprocess(loop): def test_can_terminate_subprocess(loop): """Verify that a subprocess can be terminated.""" # Start a never-ending process - @asyncio.coroutine - def mycoro(): - process = yield from asyncio.create_subprocess_exec( + async def mycoro(): + process = await asyncio.create_subprocess_exec( sys.executable or 'python', '-c', 'import time\nwhile True: time.sleep(1)') process.terminate() - yield from process.wait() + await process.wait() assert process.returncode != 0 loop.run_until_complete(mycoro()) @@ -186,8 +182,7 @@ def test_loop_callback_exceptions_bubble_up(loop): def test_loop_running(loop): """Verify that loop.is_running returns True when running.""" - @asyncio.coroutine - def is_running(): + async def is_running(): nonlocal loop assert loop.is_running() @@ -217,7 +212,8 @@ def test_future_not_done_on_loop_shutdown(loop): def test_call_later_must_not_coroutine(loop): """Verify TypeError occurs call_later is given a coroutine.""" - mycoro = asyncio.coroutine(lambda: None) + async def mycoro(): + return None with pytest.raises(TypeError): loop.call_soon(mycoro) @@ -479,15 +475,14 @@ def test_reader_writer_echo(loop, sock_pair): """Verify readers and writers can send data to each other.""" c_sock, s_sock = sock_pair - @asyncio.coroutine - def mycoro(): - c_reader, c_writer = yield from asyncio.open_connection(sock=c_sock) - s_reader, s_writer = yield from asyncio.open_connection(sock=s_sock) + async def mycoro(): + c_reader, c_writer = await asyncio.open_connection(sock=c_sock) + s_reader, s_writer = await asyncio.open_connection(sock=s_sock) data = b'Echo... Echo... Echo...' s_writer.write(data) - yield from s_writer.drain() - read_data = yield from c_reader.readexactly(len(data)) + await s_writer.drain() + read_data = await c_reader.readexactly(len(data)) assert data == read_data s_writer.close() @@ -499,22 +494,20 @@ def test_regression_bug13(loop, sock_pair): c_sock, s_sock = sock_pair client_done, server_done = asyncio.Future(), asyncio.Future() - @asyncio.coroutine - def server_coro(): - s_reader, s_writer = yield from asyncio.open_connection(sock=s_sock) + async def server_coro(): + s_reader, s_writer = await asyncio.open_connection(sock=s_sock) s_writer.write(b'1') - yield from s_writer.drain() - assert (yield from s_reader.readexactly(1)) == b'2' + await s_writer.drain() + assert (await s_reader.readexactly(1)) == b'2' s_writer.write(b'3') - yield from s_writer.drain() + await s_writer.drain() server_done.set_result(True) result1 = None result3 = None - @asyncio.coroutine - def client_coro(): + async def client_coro(): def cb1(): nonlocal result1 assert result1 is None @@ -570,20 +563,18 @@ def test_add_reader_replace(loop, sock_pair): called2 = True any_callback() - @asyncio.coroutine - def server_coro(): - s_reader, s_writer = yield from asyncio.open_connection( + async def server_coro(): + s_reader, s_writer = await asyncio.open_connection( sock=s_sock) s_writer.write(b"foo") - yield from s_writer.drain() + await s_writer.drain() - @asyncio.coroutine - def client_coro(): + async def client_coro(): loop.add_reader(c_sock.fileno(), callback1) loop.add_reader(c_sock.fileno(), callback2) - yield from callback_invoked + await callback_invoked loop.remove_reader(c_sock.fileno()) - assert (yield from loop.sock_recv(c_sock, 3)) == b"foo" + assert (await loop.sock_recv(c_sock, 3)) == b"foo" client_done = asyncio.ensure_future(client_coro()) server_done = asyncio.ensure_future(server_coro()) @@ -620,11 +611,10 @@ def test_add_writer_replace(loop, sock_pair): called2 = True any_callback() - @asyncio.coroutine - def client_coro(): + async def client_coro(): loop.add_writer(c_sock.fileno(), callback1) loop.add_writer(c_sock.fileno(), callback2) - yield from callback_invoked + await callback_invoked loop.remove_writer(c_sock.fileno()) loop.run_until_complete(asyncio.wait_for(client_coro(), timeout=0.1)) @@ -693,8 +683,7 @@ def test_exception_handler(loop): coro_run = False loop.set_debug(True) - @asyncio.coroutine - def future_except(): + async def future_except(): nonlocal coro_run coro_run = True loop.stop() @@ -730,10 +719,9 @@ def test_exception_handler_simple(loop): def test_not_running_immediately_after_stopped(loop): - @asyncio.coroutine - def mycoro(): + async def mycoro(): assert loop.is_running() - yield from asyncio.sleep(0) + await asyncio.sleep(0) loop.stop() assert not loop.is_running() assert not loop.is_running()