Skip to content

Commit

Permalink
Review fix for issue #216.
Browse files Browse the repository at this point in the history
* Clarify documentation wording a bit.
* Make the backwards-compatibility logic more explicit (and removable).
* Move klass with legacy_recv, the other backwards-compatibility shim.
  • Loading branch information
aaugustin committed Jul 29, 2017
1 parent 725675e commit 39cec14
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
12 changes: 6 additions & 6 deletions docs/cheatsheet.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ Server
execute the application logic, and finally closes the connection after
the handler exits normally or with an exception.

* You may subclass :class:`~websockets.server.WebSocketServerProtocol` and
pass it or a factory function as the ``create_protocol`` argument for
advanced customization.
* For advanced customization, you may subclass
:class:`~websockets.server.WebSocketServerProtocol` and pass either this
subclass or a factory function as the ``create_protocol`` argument.

Client
------
Expand All @@ -34,9 +34,9 @@ Client

* On Python ≥ 3.5, you can also use it as an asynchronous context manager.

* You may subclass :class:`~websockets.server.WebSocketClientProtocol` and
pass it or a factory function as the ``create_protocol`` argument for
advanced customization.
* For advanced customization, you may subclass
:class:`~websockets.server.WebSocketClientProtocol` and pass either this
subclass or a factory function as the ``create_protocol`` argument.

* Call :meth:`~websockets.protocol.WebSocketCommonProtocol.recv` and
:meth:`~websockets.protocol.WebSocketCommonProtocol.send` to receive and
Expand Down
20 changes: 13 additions & 7 deletions websockets/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,11 @@ def handshake(self, wsuri,


@asyncio.coroutine
def connect(uri, *, create_protocol=None, klass=None,
def connect(uri, *,
create_protocol=None,
timeout=10, max_size=2 ** 20, max_queue=2 ** 5,
read_limit=2 ** 16, write_limit=2 ** 16,
loop=None, legacy_recv=False,
loop=None, legacy_recv=False, klass=None,
origin=None, subprotocols=None, extra_headers=None,
**kwds):
"""
Expand All @@ -155,10 +156,9 @@ def connect(uri, *, create_protocol=None, klass=None,
``read_limit``, and ``write_limit`` optional arguments is described in the
documentation of :class:`~websockets.protocol.WebSocketCommonProtocol`.
The ``create_protocol`` parameter allows customizing the
:class:`WebSocketClientProtocol` class used. The argument should be a
callable or class accepting the same arguments as
:class:`WebSocketClientProtocol` and that returns a
The ``create_protocol`` parameter allows customizing the asyncio protocol
that manages the connection. It should be a callable or class accepting
the same arguments as :class:`WebSocketClientProtocol` and returning a
:class:`WebSocketClientProtocol` instance. It defaults to
:class:`WebSocketClientProtocol`.
Expand All @@ -181,7 +181,13 @@ def connect(uri, *, create_protocol=None, klass=None,
if loop is None:
loop = asyncio.get_event_loop()

create_protocol = create_protocol or klass or WebSocketClientProtocol
# Backwards-compatibility: create_protocol used to be called klass.
# In the unlikely event that both are specified, klass is ignored.
if create_protocol is None:
create_protocol = klass

if create_protocol is None:
create_protocol = WebSocketClientProtocol

wsuri = parse_uri(uri)
if wsuri.secure:
Expand Down
19 changes: 12 additions & 7 deletions websockets/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,10 +407,10 @@ def wait_closed(self):

@asyncio.coroutine
def serve(ws_handler, host=None, port=None, *,
create_protocol=None, klass=None,
create_protocol=None,
timeout=10, max_size=2 ** 20, max_queue=2 ** 5,
read_limit=2 ** 16, write_limit=2 ** 16,
loop=None, legacy_recv=False,
loop=None, legacy_recv=False, klass=None,
origins=None, subprotocols=None, extra_headers=None,
**kwds):
"""
Expand Down Expand Up @@ -440,10 +440,9 @@ def serve(ws_handler, host=None, port=None, *,
set the ``ssl`` keyword argument to a :class:`~ssl.SSLContext` to enable
TLS.
The ``create_protocol`` parameter allows customizing the
:class:`WebSocketServerProtocol` class used. The argument should be a
callable or class accepting the same arguments as
:class:`WebSocketServerProtocol` and that returns a
The ``create_protocol`` parameter allows customizing the asyncio protocol
that manages the connection. It should be a callable or class accepting
the same arguments as :class:`WebSocketServerProtocol` and returning a
:class:`WebSocketServerProtocol` instance. It defaults to
:class:`WebSocketServerProtocol`.
Expand Down Expand Up @@ -479,7 +478,13 @@ def serve(ws_handler, host=None, port=None, *,
if loop is None:
loop = asyncio.get_event_loop()

create_protocol = create_protocol or klass or WebSocketServerProtocol
# Backwards-compatibility: create_protocol used to be called klass.
# In the unlikely event that both are specified, klass is ignored.
if create_protocol is None:
create_protocol = klass

if create_protocol is None:
create_protocol = WebSocketServerProtocol

ws_server = WebSocketServer(loop)

Expand Down

0 comments on commit 39cec14

Please sign in to comment.