Skip to content

Commit

Permalink
Fix compatiblity with websockets 3+, close housleyjk#15
Browse files Browse the repository at this point in the history
  • Loading branch information
housleyjk committed Feb 4, 2016
1 parent 76aadfb commit 239a191
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ Changes

.. :changelog:
0.3.4 (2016-02-03)
------------------
- Fix compatiblity with websockets 3+

0.3.3 (2015-11-21)
------------------
- Merge fix for `ignore_websocket_closed` to allow chained exceptions
Expand Down
23 changes: 23 additions & 0 deletions aiopyramid/websocket/config/gunicorn.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,27 @@
from aiopyramid.config import AsyncioMapperBase


def _connection_closed_to_none(func):
"""
A backwards compatibility shim for websockets 3+. We need to
still return `None` rather than throwing an exception in order
to unite the interface with uWSGI even though the exception is
more Pythonic.
"""

@asyncio.coroutine
@functools.wraps(func)
def _connection_closed_to_none_inner(*args, **kwargs):
try:
msg = yield from func(*args, **kwargs)
except websockets.exceptions.ConnectionClosed:
msg = None

return msg

return _connection_closed_to_none_inner


def _use_bytes(func):
"""
Encodes strings received from websockets to bytes to
Expand Down Expand Up @@ -89,6 +110,8 @@ def _ensure_ws_close(ws):
if WebsocketMapper.use_bytes:
ws.recv = _use_bytes(ws.recv)

ws.recv = _connection_closed_to_none(ws.recv)

yield from view_callable(ws)
yield from ws.close()

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

setup(
name='aiopyramid',
version='0.3.3',
version='0.3.4',
description='Tools for running pyramid using asyncio.',
long_description=README + '\n\n\n\n' + CHANGES,
classifiers=[
Expand Down

0 comments on commit 239a191

Please sign in to comment.