Skip to content

Commit b03d29b

Browse files
author
Jon Betts
committed
Avoid the use of the word "async" for Python 3.7+ compatibility
Also some white space changes, might be line endings?
1 parent 961c07c commit b03d29b

4 files changed

Lines changed: 34 additions & 23 deletions

File tree

test/autobahn_test_servers.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def run_python3_asyncio(host="127.0.0.1", port=9009):
8585
wsaccel.patch_ws4py()
8686
from ws4py.async_websocket import EchoWebSocket
8787
from ws4py.server.tulipserver import WebSocketProtocol
88-
88+
8989
loop = asyncio.get_event_loop()
9090

9191
def start_server():
@@ -121,14 +121,14 @@ def run_autobahn_server(host="127.0.0.1", port=9003):
121121
from twisted.internet import reactor
122122
from autobahn.twisted.websocket import WebSocketServerProtocol, \
123123
WebSocketServerFactory
124-
124+
125125
class MyServerProtocol(WebSocketServerProtocol):
126126
def onMessage(self, payload, isBinary):
127127
self.sendMessage(payload, isBinary)
128128

129129
logger = logging.getLogger('autobahn_testsuite')
130130
logger.warning("Serving Autobahn server on %s:%s" % (host, port))
131-
131+
132132
factory = WebSocketServerFactory("ws://%s:%d" % (host, port))
133133
factory.protocol = MyServerProtocol
134134

@@ -142,7 +142,7 @@ def run_python_wsgi(host="127.0.0.1", port=9002):
142142
"""
143143
run_python_wsgi_async(host, port, False)
144144

145-
def run_python_wsgi_async(host="127.0.0.1", port=9010, async=True):
145+
def run_python_wsgi_async(host="127.0.0.1", port=9010, async_=True):
146146
"""
147147
Runs wsgi server on python 2.x with async middleware"
148148
"""
@@ -153,7 +153,7 @@ def run_python_wsgi_async(host="127.0.0.1", port=9010, async=True):
153153
from ws4py.server.wsgiutils import WebSocketWSGIApplication
154154

155155
app = WebSocketWSGIApplication(handler_cls=EchoWebSocket)
156-
if async:
156+
if async_:
157157
def middleware(app):
158158
def later(environ, start_response):
159159
for part in app(environ, start_response):

ws4py/_asyncio_compat.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"""Provide compatibility over different versions of asyncio."""
2+
3+
import asyncio
4+
5+
if hasattr(asyncio, "async"):
6+
# Compatibility for Python 3.3 and older
7+
ensure_future = getattr(asyncio, "async")
8+
else:
9+
ensure_future = asyncio.ensure_future

ws4py/async_websocket.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import types
2020

2121
from ws4py.websocket import WebSocket as _WebSocket
22+
from ws4py import _asyncio_compat
2223
from ws4py.messaging import Message
2324

2425
__all__ = ['WebSocket', 'EchoWebSocket']
@@ -84,7 +85,7 @@ def close_connection(self):
8485
def closeit():
8586
yield from self.proto.writer.drain()
8687
self.proto.writer.close()
87-
asyncio.async(closeit())
88+
_asyncio_compat.ensure_future(closeit())
8889

8990
def _write(self, data):
9091
"""
@@ -94,7 +95,7 @@ def _write(self, data):
9495
def sendit(data):
9596
self.proto.writer.write(data)
9697
yield from self.proto.writer.drain()
97-
asyncio.async(sendit(data))
98+
_asyncio_compat.ensure_future(sendit(data))
9899

99100
@asyncio.coroutine
100101
def run(self):

ws4py/server/tulipserver.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from ws4py import WS_KEY, WS_VERSION
1010
from ws4py.exc import HandshakeError
1111
from ws4py.websocket import WebSocket
12+
from ws4py import _asyncio_compat
1213

1314
LF = b'\n'
1415
CRLF = b'\r\n'
@@ -25,7 +26,7 @@ def __init__(self, handler_cls):
2526

2627
def _pseudo_connected(self, reader, writer):
2728
pass
28-
29+
2930
def connection_made(self, transport):
3031
"""
3132
A peer is now connected and we receive an instance
@@ -40,17 +41,17 @@ def connection_made(self, transport):
4041
#self.stream.set_transport(transport)
4142
asyncio.StreamReaderProtocol.connection_made(self, transport)
4243
# Let make it concurrent for others to tag along
43-
f = asyncio.async(self.handle_initial_handshake())
44+
f = _asyncio_compat.ensure_future(self.handle_initial_handshake())
4445
f.add_done_callback(self.terminated)
4546

4647
@property
4748
def writer(self):
4849
return self._stream_writer
49-
50+
5051
@property
5152
def reader(self):
5253
return self._stream_reader
53-
54+
5455
def terminated(self, f):
5556
if f.done() and not f.cancelled():
5657
ex = f.exception()
@@ -70,12 +71,12 @@ def close(self):
7071
transport.
7172
"""
7273
self.ws.close()
73-
74+
7475
def timeout(self):
7576
self.ws.close_connection()
7677
if self.ws.started:
7778
self.ws.closed(1002, "Peer connection timed-out")
78-
79+
7980
def connection_lost(self, exc):
8081
"""
8182
The peer connection is now, the closing
@@ -88,7 +89,7 @@ def connection_lost(self, exc):
8889
self.ws.close_connection()
8990
if self.ws.started:
9091
self.ws.closed(1002, "Peer connection was lost")
91-
92+
9293
@asyncio.coroutine
9394
def handle_initial_handshake(self):
9495
"""
@@ -100,15 +101,15 @@ def handle_initial_handshake(self):
100101
"""
101102
request_line = yield from self.next_line()
102103
method, uri, req_protocol = request_line.strip().split(SPACE, 2)
103-
104+
104105
# GET required
105106
if method.upper() != b'GET':
106107
raise HandshakeError('HTTP method must be a GET')
107-
108+
108109
headers = yield from self.read_headers()
109110
if req_protocol == b'HTTP/1.1' and 'Host' not in headers:
110111
raise ValueError("Missing host header")
111-
112+
112113
for key, expected_value in [('Upgrade', 'websocket'),
113114
('Connection', 'upgrade')]:
114115
actual_value = headers.get(key, '').lower()
@@ -160,7 +161,7 @@ def handle_initial_handshake(self):
160161
self.ws.protocols = ws_protocols
161162
self.ws.extensions = ws_extensions
162163
self.ws.headers = headers
163-
164+
164165
response = [req_protocol + b' 101 Switching Protocols']
165166
response.append(b'Upgrade: websocket')
166167
response.append(b'Content-Type: text/plain')
@@ -184,7 +185,7 @@ def handle_websocket(self):
184185
exchange is completed and terminated.
185186
"""
186187
yield from self.ws.run()
187-
188+
188189
@asyncio.coroutine
189190
def read_headers(self):
190191
"""
@@ -198,21 +199,21 @@ def read_headers(self):
198199
if line == CRLF:
199200
break
200201
return BytesHeaderParser().parsebytes(headers)
201-
202+
202203
@asyncio.coroutine
203204
def next_line(self):
204205
"""
205206
Reads data until \r\n is met and then return all read
206-
bytes.
207+
bytes.
207208
"""
208209
line = yield from self.reader.readline()
209210
if not line.endswith(CRLF):
210211
raise ValueError("Missing mandatory trailing CRLF")
211212
return line
212-
213+
213214
if __name__ == '__main__':
214215
from ws4py.async_websocket import EchoWebSocket
215-
216+
216217
loop = asyncio.get_event_loop()
217218

218219
def start_server():

0 commit comments

Comments
 (0)