|
|
32e5f0 |
From 9b8f36d08a5bdffa83019f679a9c9d2ef5ca4302 Mon Sep 17 00:00:00 2001
|
|
|
32e5f0 |
From: Aymeric Augustin <aymeric.augustin@m4x.org>
|
|
|
32e5f0 |
Date: Sun, 15 Jul 2018 11:07:47 +0200
|
|
|
32e5f0 |
Subject: [PATCH 3/3] Support yield from connect/serve on Python 3.7.
|
|
|
32e5f0 |
|
|
|
32e5f0 |
Fix #435.
|
|
|
32e5f0 |
|
|
|
32e5f0 |
(cherry picked from commit 91a376685b1ab7103d3d861ff8b02a1c00f142b1)
|
|
|
32e5f0 |
---
|
|
|
32e5f0 |
websockets/client.py | 1 +
|
|
|
32e5f0 |
websockets/py35/_test_client_server.py | 3 ++
|
|
|
32e5f0 |
websockets/server.py | 1 +
|
|
|
32e5f0 |
websockets/test_client_server.py | 41 ++++++++++++++++++++++++++
|
|
|
32e5f0 |
4 files changed, 46 insertions(+)
|
|
|
32e5f0 |
|
|
|
32e5f0 |
diff --git a/websockets/client.py b/websockets/client.py
|
|
|
32e5f0 |
index a86b90f..bb3009b 100644
|
|
|
32e5f0 |
--- a/websockets/client.py
|
|
|
32e5f0 |
+++ b/websockets/client.py
|
|
|
32e5f0 |
@@ -385,6 +385,7 @@ class Connect:
|
|
|
32e5f0 |
self._creating_connection = loop.create_connection(
|
|
|
32e5f0 |
factory, host, port, **kwds)
|
|
|
32e5f0 |
|
|
|
32e5f0 |
+ @asyncio.coroutine
|
|
|
32e5f0 |
def __iter__(self): # pragma: no cover
|
|
|
32e5f0 |
transport, protocol = yield from self._creating_connection
|
|
|
32e5f0 |
|
|
|
32e5f0 |
diff --git a/websockets/py35/_test_client_server.py b/websockets/py35/_test_client_server.py
|
|
|
32e5f0 |
index 5360d8d..c656dd3 100644
|
|
|
32e5f0 |
--- a/websockets/py35/_test_client_server.py
|
|
|
32e5f0 |
+++ b/websockets/py35/_test_client_server.py
|
|
|
32e5f0 |
@@ -39,6 +39,7 @@ class AsyncAwaitTests(unittest.TestCase):
|
|
|
32e5f0 |
self.loop.run_until_complete(server.wait_closed())
|
|
|
32e5f0 |
|
|
|
32e5f0 |
def test_server(self):
|
|
|
32e5f0 |
+
|
|
|
32e5f0 |
async def run_server():
|
|
|
32e5f0 |
# Await serve.
|
|
|
32e5f0 |
server = await serve(handler, 'localhost', 0)
|
|
|
32e5f0 |
@@ -83,6 +84,7 @@ class ContextManagerTests(unittest.TestCase):
|
|
|
32e5f0 |
@unittest.skipIf(
|
|
|
32e5f0 |
sys.version_info[:3] <= (3, 5, 0), 'this test requires Python 3.5.1+')
|
|
|
32e5f0 |
def test_server(self):
|
|
|
32e5f0 |
+
|
|
|
32e5f0 |
async def run_server():
|
|
|
32e5f0 |
# Use serve as an asynchronous context manager.
|
|
|
32e5f0 |
async with serve(handler, 'localhost', 0) as server:
|
|
|
32e5f0 |
@@ -99,6 +101,7 @@ class ContextManagerTests(unittest.TestCase):
|
|
|
32e5f0 |
@unittest.skipUnless(
|
|
|
32e5f0 |
hasattr(socket, 'AF_UNIX'), 'this test requires Unix sockets')
|
|
|
32e5f0 |
def test_unix_server(self):
|
|
|
32e5f0 |
+
|
|
|
32e5f0 |
async def run_server(path):
|
|
|
32e5f0 |
async with unix_serve(handler, path) as server:
|
|
|
32e5f0 |
self.assertTrue(server.sockets)
|
|
|
32e5f0 |
diff --git a/websockets/server.py b/websockets/server.py
|
|
|
32e5f0 |
index 46c80dc..86fa700 100644
|
|
|
32e5f0 |
--- a/websockets/server.py
|
|
|
32e5f0 |
+++ b/websockets/server.py
|
|
|
32e5f0 |
@@ -729,6 +729,7 @@ class Serve:
|
|
|
32e5f0 |
self._creating_server = creating_server
|
|
|
32e5f0 |
self.ws_server = ws_server
|
|
|
32e5f0 |
|
|
|
32e5f0 |
+ @asyncio.coroutine
|
|
|
32e5f0 |
def __iter__(self): # pragma: no cover
|
|
|
32e5f0 |
server = yield from self._creating_server
|
|
|
32e5f0 |
self.ws_server.wrap(server)
|
|
|
32e5f0 |
diff --git a/websockets/test_client_server.py b/websockets/test_client_server.py
|
|
|
32e5f0 |
index a3e1e92..6c25784 100644
|
|
|
32e5f0 |
--- a/websockets/test_client_server.py
|
|
|
32e5f0 |
+++ b/websockets/test_client_server.py
|
|
|
32e5f0 |
@@ -24,6 +24,7 @@ from .extensions.permessage_deflate import (
|
|
|
32e5f0 |
)
|
|
|
32e5f0 |
from .handshake import build_response
|
|
|
32e5f0 |
from .http import USER_AGENT, read_response
|
|
|
32e5f0 |
+from .protocol import State
|
|
|
32e5f0 |
from .server import *
|
|
|
32e5f0 |
from .test_protocol import MS
|
|
|
32e5f0 |
|
|
|
32e5f0 |
@@ -1056,6 +1057,46 @@ class ClientServerOriginTests(unittest.TestCase):
|
|
|
32e5f0 |
self.loop.run_until_complete(server.wait_closed())
|
|
|
32e5f0 |
|
|
|
32e5f0 |
|
|
|
32e5f0 |
+class YieldFromTests(unittest.TestCase):
|
|
|
32e5f0 |
+
|
|
|
32e5f0 |
+ def setUp(self):
|
|
|
32e5f0 |
+ self.loop = asyncio.new_event_loop()
|
|
|
32e5f0 |
+ asyncio.set_event_loop(self.loop)
|
|
|
32e5f0 |
+
|
|
|
32e5f0 |
+ def tearDown(self):
|
|
|
32e5f0 |
+ self.loop.close()
|
|
|
32e5f0 |
+
|
|
|
32e5f0 |
+ def test_client(self):
|
|
|
32e5f0 |
+ start_server = serve(handler, 'localhost', 0)
|
|
|
32e5f0 |
+ server = self.loop.run_until_complete(start_server)
|
|
|
32e5f0 |
+
|
|
|
32e5f0 |
+ @asyncio.coroutine
|
|
|
32e5f0 |
+ def run_client():
|
|
|
32e5f0 |
+ # Yield from connect.
|
|
|
32e5f0 |
+ client = yield from connect(get_server_uri(server))
|
|
|
32e5f0 |
+ self.assertEqual(client.state, State.OPEN)
|
|
|
32e5f0 |
+ yield from client.close()
|
|
|
32e5f0 |
+ self.assertEqual(client.state, State.CLOSED)
|
|
|
32e5f0 |
+
|
|
|
32e5f0 |
+ self.loop.run_until_complete(run_client())
|
|
|
32e5f0 |
+
|
|
|
32e5f0 |
+ server.close()
|
|
|
32e5f0 |
+ self.loop.run_until_complete(server.wait_closed())
|
|
|
32e5f0 |
+
|
|
|
32e5f0 |
+ def test_server(self):
|
|
|
32e5f0 |
+
|
|
|
32e5f0 |
+ @asyncio.coroutine
|
|
|
32e5f0 |
+ def run_server():
|
|
|
32e5f0 |
+ # Yield from serve.
|
|
|
32e5f0 |
+ server = yield from serve(handler, 'localhost', 0)
|
|
|
32e5f0 |
+ self.assertTrue(server.sockets)
|
|
|
32e5f0 |
+ server.close()
|
|
|
32e5f0 |
+ yield from server.wait_closed()
|
|
|
32e5f0 |
+ self.assertFalse(server.sockets)
|
|
|
32e5f0 |
+
|
|
|
32e5f0 |
+ self.loop.run_until_complete(run_server())
|
|
|
32e5f0 |
+
|
|
|
32e5f0 |
+
|
|
|
32e5f0 |
if sys.version_info[:2] >= (3, 5): # pragma: no cover
|
|
|
32e5f0 |
from .py35._test_client_server import AsyncAwaitTests # noqa
|
|
|
32e5f0 |
from .py35._test_client_server import ContextManagerTests # noqa
|
|
|
32e5f0 |
--
|
|
|
32e5f0 |
2.18.0
|
|
|
32e5f0 |
|