diff --git a/CHANGES.txt b/CHANGES.txt index 91e52cc8496..dd249acc4e8 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -47,6 +47,8 @@ CHANGES - Introduce aiohttp.WSMessage officially +- Rename Message -> WSMessage + 0.22.3 (07-26-2016) ------------------- diff --git a/aiohttp/__init__.py b/aiohttp/__init__.py index 80e1d0ff44e..84d2de01935 100644 --- a/aiohttp/__init__.py +++ b/aiohttp/__init__.py @@ -17,7 +17,7 @@ from .streams import * # noqa from .multipart import * # noqa from .client_ws import ClientWebSocketResponse # noqa -from ._ws_impl import WSMsgType, WSCloseCode, Message, WebSocketError # noqa +from ._ws_impl import WSMsgType, WSCloseCode, WSMessage, WebSocketError # noqa from .file_sender import FileSender # noqa @@ -35,5 +35,5 @@ multidict.__all__ + # noqa multipart.__all__ + # noqa ('hdrs', 'FileSender', 'WSMsgType', 'MsgType', 'WSCloseCode', - 'WebSocketError', 'Message', + 'WebSocketError', 'WSMessage', 'ClientWebSocketResponse')) diff --git a/aiohttp/_ws_impl.py b/aiohttp/_ws_impl.py index 1f6b92b0ef2..940244939ca 100644 --- a/aiohttp/_ws_impl.py +++ b/aiohttp/_ws_impl.py @@ -17,7 +17,7 @@ __all__ = ('WebSocketParser', 'WebSocketWriter', 'do_handshake', - 'Message', 'WebSocketError', 'WSMsgType', 'WSCloseCode') + 'WSMessage', 'WebSocketError', 'WSMsgType', 'WSCloseCode') class WSCloseCode(IntEnum): @@ -61,10 +61,11 @@ class WSMsgType(IntEnum): MSG_SIZE = 2 ** 14 -_MessageBase = collections.namedtuple('Message', ['tp', 'data', 'extra']) +_WSMessageBase = collections.namedtuple('_WSMessageBase', + ['tp', 'data', 'extra']) -class Message(_MessageBase): +class WSMessage(_WSMessageBase): def json(self, *, loads=json.loads): """Return parsed JSON data. @@ -73,7 +74,7 @@ def json(self, *, loads=json.loads): return loads(self.data) -CLOSED_MESSAGE = Message(WSMsgType.closed, None, None) +CLOSED_MESSAGE = WSMessage(WSMsgType.closed, None, None) class WebSocketError(Exception): @@ -101,22 +102,22 @@ def WebSocketParser(out, buf): raise WebSocketError( WSCloseCode.invalid_text, 'Invalid UTF-8 text message') from exc - msg = Message(WSMsgType.close, close_code, close_message) + msg = WSMessage(WSMsgType.close, close_code, close_message) elif payload: raise WebSocketError( WSCloseCode.protocol_error, 'Invalid close frame: {} {} {!r}'.format( fin, opcode, payload)) else: - msg = Message(WSMsgType.close, 0, '') + msg = WSMessage(WSMsgType.close, 0, '') out.feed_data(msg, 0) elif opcode == WSMsgType.ping: - out.feed_data(Message(WSMsgType.ping, payload, ''), len(payload)) + out.feed_data(WSMessage(WSMsgType.ping, payload, ''), len(payload)) elif opcode == WSMsgType.pong: - out.feed_data(Message(WSMsgType.pong, payload, ''), len(payload)) + out.feed_data(WSMessage(WSMsgType.pong, payload, ''), len(payload)) elif opcode not in (WSMsgType.text, WSMsgType.binary): raise WebSocketError( @@ -133,7 +134,7 @@ def WebSocketParser(out, buf): # text message, Case 5.* if _opcode == WSMsgType.ping: out.feed_data( - Message(WSMsgType.ping, payload, ''), len(payload)) + WSMessage(WSMsgType.ping, payload, ''), len(payload)) fin, _opcode, payload = yield from parse_frame(buf, True) elif _opcode == WSMsgType.close: if len(payload) >= 2: @@ -149,15 +150,15 @@ def WebSocketParser(out, buf): raise WebSocketError( WSCloseCode.invalid_text, 'Invalid UTF-8 text message') from exc - msg = Message(WSMsgType.close, close_code, - close_message) + msg = WSMessage(WSMsgType.close, close_code, + close_message) elif payload: raise WebSocketError( WSCloseCode.protocol_error, 'Invalid close frame: {} {} {!r}'.format( fin, opcode, payload)) else: - msg = Message(WSMsgType.close, 0, '') + msg = WSMessage(WSMsgType.close, 0, '') out.feed_data(msg, 0) fin, _opcode, payload = yield from parse_frame(buf, True) @@ -173,9 +174,8 @@ def WebSocketParser(out, buf): if opcode == WSMsgType.text: try: text = b''.join(data).decode('utf-8') - out.feed_data( - Message( - WSMsgType.text, text, ''), len(text)) + out.feed_data(WSMessage(WSMsgType.text, text, ''), + len(text)) except UnicodeDecodeError as exc: raise WebSocketError( WSCloseCode.invalid_text, @@ -183,7 +183,7 @@ def WebSocketParser(out, buf): else: data = b''.join(data) out.feed_data( - Message(WSMsgType.binary, data, ''), len(data)) + WSMessage(WSMsgType.binary, data, ''), len(data)) native_byteorder = sys.byteorder diff --git a/aiohttp/client_ws.py b/aiohttp/client_ws.py index 0133efe0b5c..fb0034070d7 100644 --- a/aiohttp/client_ws.py +++ b/aiohttp/client_ws.py @@ -5,7 +5,7 @@ import sys import json -from ._ws_impl import Message, WebSocketError, WSMsgType, CLOSED_MESSAGE +from ._ws_impl import WSMessage, WebSocketError, WSMsgType, CLOSED_MESSAGE PY_35 = sys.version_info >= (3, 5) @@ -132,13 +132,13 @@ def receive(self): except WebSocketError as exc: self._close_code = exc.code yield from self.close(code=exc.code) - return Message(WSMsgType.error, exc, None) + return WSMessage(WSMsgType.error, exc, None) except Exception as exc: self._exception = exc self._closing = True self._close_code = 1006 yield from self.close() - return Message(WSMsgType.error, exc, None) + return WSMessage(WSMsgType.error, exc, None) if msg.tp == WSMsgType.close: self._closing = True diff --git a/aiohttp/web_ws.py b/aiohttp/web_ws.py index 6b1766f5bec..a18c4eb46e1 100644 --- a/aiohttp/web_ws.py +++ b/aiohttp/web_ws.py @@ -5,7 +5,7 @@ from . import hdrs, Timeout from .errors import HttpProcessingError, ClientDisconnectedError -from ._ws_impl import (do_handshake, Message, WebSocketError, +from ._ws_impl import (do_handshake, WSMessage, WebSocketError, WSMsgType, CLOSED_MESSAGE) from .web_exceptions import ( HTTPBadRequest, HTTPMethodNotAllowed, HTTPInternalServerError) @@ -229,17 +229,17 @@ def receive(self): except WebSocketError as exc: self._close_code = exc.code yield from self.close(code=exc.code) - return Message(WSMsgType.error, exc, None) + return WSMessage(WSMsgType.error, exc, None) except ClientDisconnectedError: self._closed = True self._close_code = 1006 - return Message(WSMsgType.close, None, None) + return WSMessage(WSMsgType.close, None, None) except Exception as exc: self._exception = exc self._closing = True self._close_code = 1006 yield from self.close() - return Message(WSMsgType.error, exc, None) + return WSMessage(WSMsgType.error, exc, None) if msg.tp == WSMsgType.close: self._closing = True diff --git a/tests/test_client_ws.py b/tests/test_client_ws.py index cd903d3a9dd..f28ccddf23a 100644 --- a/tests/test_client_ws.py +++ b/tests/test_client_ws.py @@ -231,7 +231,7 @@ def test_close(self, m_req, m_os, WebSocketWriter): aiohttp.ws_connect('http://test.org', loop=self.loop)) self.assertFalse(resp.closed) - msg = aiohttp.Message(aiohttp.MsgType.close, b'', b'') + msg = aiohttp.WSMessage(aiohttp.MsgType.close, b'', b'') reader.read.return_value = helpers.create_future(self.loop) reader.read.return_value.set_result(msg)