Skip to content

Commit

Permalink
Merge branch 'master' into fix_exceptions_ws_receive_content
Browse files Browse the repository at this point in the history
  • Loading branch information
ara-25 authored Oct 20, 2024
2 parents 16823c3 + ced4fd3 commit 1b78404
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 19 deletions.
19 changes: 11 additions & 8 deletions aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,14 +304,17 @@ def connection_key(self) -> ConnectionKey:
else:
h = None
url = self.url
return ConnectionKey(
url.raw_host or "",
url.port,
url.scheme in _SSL_SCHEMES,
self._ssl,
self.proxy,
self.proxy_auth,
h,
return tuple.__new__(
ConnectionKey,
(
url.raw_host or "",
url.port,
url.scheme in _SSL_SCHEMES,
self._ssl,
self.proxy,
self.proxy_auth,
h,
),
)

@property
Expand Down
28 changes: 20 additions & 8 deletions aiohttp/http_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,12 @@ def json(self, *, loads: Callable[[Any], Any] = json.loads) -> Any:
return loads(self.data)


WS_CLOSED_MESSAGE = WSMessage(WSMsgType.CLOSED, None, None)
WS_CLOSING_MESSAGE = WSMessage(WSMsgType.CLOSING, None, None)
# Constructing the tuple directly to avoid the overhead of
# the lambda and arg processing since NamedTuples are constructed
# with a run time built lambda
# https://github.com/python/cpython/blob/d83fcf8371f2f33c7797bc8f5423a8bca8c46e5c/Lib/collections/__init__.py#L441
WS_CLOSED_MESSAGE = tuple.__new__(WSMessage, (WSMsgType.CLOSED, None, None))
WS_CLOSING_MESSAGE = tuple.__new__(WSMessage, (WSMsgType.CLOSING, None, None))


class WebSocketError(Exception):
Expand Down Expand Up @@ -402,10 +406,14 @@ def _feed_data(self, data: bytes) -> None:
WSCloseCode.INVALID_TEXT, "Invalid UTF-8 text message"
) from exc

self.queue.feed_data(WSMessage(WSMsgType.TEXT, text, ""))
# tuple.__new__ is used to avoid the overhead of the lambda
msg = tuple.__new__(WSMessage, (WSMsgType.TEXT, text, ""))
self.queue.feed_data(msg)
continue

self.queue.feed_data(WSMessage(WSMsgType.BINARY, payload_merged, ""))
# tuple.__new__ is used to avoid the overhead of the lambda
msg = tuple.__new__(WSMessage, (WSMsgType.BINARY, payload_merged, ""))
self.queue.feed_data(msg)
elif opcode == WSMsgType.CLOSE:
if len(payload) >= 2:
close_code = UNPACK_CLOSE_CODE(payload[:2])[0]
Expand All @@ -420,22 +428,26 @@ def _feed_data(self, data: bytes) -> None:
raise WebSocketError(
WSCloseCode.INVALID_TEXT, "Invalid UTF-8 text message"
) from exc
msg = WSMessage(WSMsgType.CLOSE, close_code, close_message)
msg = tuple.__new__(
WSMessage, (WSMsgType.CLOSE, close_code, close_message)
)
elif payload:
raise WebSocketError(
WSCloseCode.PROTOCOL_ERROR,
f"Invalid close frame: {fin} {opcode} {payload!r}",
)
else:
msg = WSMessage(WSMsgType.CLOSE, 0, "")
msg = tuple.__new__(WSMessage, (WSMsgType.CLOSE, 0, ""))

self.queue.feed_data(msg)

elif opcode == WSMsgType.PING:
self.queue.feed_data(WSMessage(WSMsgType.PING, payload, ""))
msg = tuple.__new__(WSMessage, (WSMsgType.PING, payload, ""))
self.queue.feed_data(msg)

elif opcode == WSMsgType.PONG:
self.queue.feed_data(WSMessage(WSMsgType.PONG, payload, ""))
msg = tuple.__new__(WSMessage, (WSMsgType.PONG, payload, ""))
self.queue.feed_data(msg)

else:
raise WebSocketError(
Expand Down
2 changes: 1 addition & 1 deletion requirements/constraints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ uvloop==0.21.0 ; platform_system != "Windows"
# -r requirements/lint.in
valkey==6.0.2
# via -r requirements/lint.in
virtualenv==20.26.6
virtualenv==20.27.0
# via pre-commit
wait-for-it==2.2.2
# via -r requirements/test.in
Expand Down
2 changes: 1 addition & 1 deletion requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ uvloop==0.21.0 ; platform_system != "Windows" and implementation_name == "cpytho
# -r requirements/lint.in
valkey==6.0.2
# via -r requirements/lint.in
virtualenv==20.26.6
virtualenv==20.27.0
# via pre-commit
wait-for-it==2.2.2
# via -r requirements/test.in
Expand Down
2 changes: 1 addition & 1 deletion requirements/lint.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,5 @@ uvloop==0.21.0 ; platform_system != "Windows"
# via -r requirements/lint.in
valkey==6.0.2
# via -r requirements/lint.in
virtualenv==20.26.6
virtualenv==20.27.0
# via pre-commit

0 comments on commit 1b78404

Please sign in to comment.