Skip to content

Commit

Permalink
Restore backwards-compatibility for partial handlers.
Browse files Browse the repository at this point in the history
Fix #1095.
  • Loading branch information
aaugustin committed Feb 20, 2022
1 parent 695d43a commit f605e86
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
3 changes: 3 additions & 0 deletions docs/project/changelog.rst
Expand Up @@ -38,6 +38,9 @@ Improvements
Bug fixes
.........

* Fixed backwards-incompatibility in 10.1 for connection handlers created with
:func:`functools.partial`.

* Avoided leaking open sockets when :func:`~client.connect` is canceled.

10.1
Expand Down
28 changes: 19 additions & 9 deletions src/websockets/legacy/server.py
Expand Up @@ -1126,17 +1126,27 @@ def remove_path_argument(
Callable[[WebSocketServerProtocol, str], Awaitable[Any]],
]
) -> Callable[[WebSocketServerProtocol], Awaitable[Any]]:
if len(inspect.signature(ws_handler).parameters) == 2:
# Enable deprecation warning and announce deprecation in 11.0.
# warnings.warn("remove second argument of ws_handler", DeprecationWarning)
try:
inspect.signature(ws_handler).bind(None)
except TypeError:
try:
inspect.signature(ws_handler).bind(None, "")
except TypeError: # pragma: no cover
# ws_handler accepts neither one nor two arguments; leave it alone.
pass
else:
# ws_handler accepts two arguments; activate backwards compatibility.

# Enable deprecation warning and announce deprecation in 11.0.
# warnings.warn("remove second argument of ws_handler", DeprecationWarning)

async def _ws_handler(websocket: WebSocketServerProtocol) -> Any:
return await cast(
Callable[[WebSocketServerProtocol, str], Awaitable[Any]],
ws_handler,
)(websocket, websocket.path)
async def _ws_handler(websocket: WebSocketServerProtocol) -> Any:
return await cast(
Callable[[WebSocketServerProtocol, str], Awaitable[Any]],
ws_handler,
)(websocket, websocket.path)

return _ws_handler
return _ws_handler

return cast(
Callable[[WebSocketServerProtocol], Awaitable[Any]],
Expand Down
17 changes: 17 additions & 0 deletions tests/legacy/test_client_server.py
Expand Up @@ -497,6 +497,23 @@ async def handler_with_path(ws, path):
"/path",
)

def test_ws_handler_argument_backwards_compatibility_partial(self):
async def handler_with_path(ws, path, extra):
await ws.send(path)

bound_handler_with_path = functools.partial(handler_with_path, extra=None)

with self.temp_server(
handler=bound_handler_with_path,
# Enable deprecation warning and announce deprecation in 11.0.
# deprecation_warnings=["remove second argument of ws_handler"],
):
with self.temp_client("/path"):
self.assertEqual(
self.loop.run_until_complete(self.client.recv()),
"/path",
)

async def process_request_OK(path, request_headers):
return http.HTTPStatus.OK, [], b"OK\n"

Expand Down

0 comments on commit f605e86

Please sign in to comment.