Skip to content

Commit 419f7b6

Browse files
committed
Move asyncio compatibility to a new package.
1 parent 8c6f726 commit 419f7b6

File tree

7 files changed

+12
-11
lines changed

7 files changed

+12
-11
lines changed

src/websockets/asyncio/__init__.py

Whitespace-only changes.
File renamed without changes.

src/websockets/legacy/client.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
cast,
2121
)
2222

23+
from ..asyncio.compatibility import asyncio_timeout
2324
from ..datastructures import Headers, HeadersLike
2425
from ..exceptions import (
2526
InvalidHandshake,
@@ -44,7 +45,6 @@
4445
from ..http import USER_AGENT
4546
from ..typing import ExtensionHeader, LoggerLike, Origin, Subprotocol
4647
from ..uri import WebSocketURI, parse_uri
47-
from .compatibility import asyncio_timeout
4848
from .handshake import build_request, check_response
4949
from .http import read_response
5050
from .protocol import WebSocketCommonProtocol

src/websockets/legacy/protocol.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
cast,
2929
)
3030

31+
from ..asyncio.compatibility import asyncio_timeout
3132
from ..datastructures import Headers
3233
from ..exceptions import (
3334
ConnectionClosed,
@@ -53,7 +54,6 @@
5354
)
5455
from ..protocol import State
5556
from ..typing import Data, LoggerLike, Subprotocol
56-
from .compatibility import asyncio_timeout
5757
from .framing import Frame
5858

5959

src/websockets/legacy/server.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
cast,
2626
)
2727

28+
from ..asyncio.compatibility import asyncio_timeout
2829
from ..datastructures import Headers, HeadersLike, MultipleValuesError
2930
from ..exceptions import (
3031
AbortHandshake,
@@ -46,7 +47,6 @@
4647
from ..http import USER_AGENT
4748
from ..protocol import State
4849
from ..typing import ExtensionHeader, LoggerLike, Origin, Subprotocol
49-
from .compatibility import asyncio_timeout
5050
from .handshake import build_response, check_request
5151
from .http import read_request
5252
from .protocol import WebSocketCommonProtocol

src/websockets/sync/messages.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,10 @@ def get(self, timeout: Optional[float] = None) -> Data:
7272
7373
Raises:
7474
EOFError: If the stream of frames has ended.
75-
RuntimeError: If two threads run :meth:`get` or :meth:``get_iter`
75+
RuntimeError: If two threads run :meth:`get` or :meth:`get_iter`
7676
concurrently.
77+
TimeoutError: If a timeout is provided and elapses before a
78+
complete message is received.
7779
7880
"""
7981
with self.mutex:
@@ -131,7 +133,7 @@ def get_iter(self) -> Iterator[Data]:
131133
132134
Raises:
133135
EOFError: If the stream of frames has ended.
134-
RuntimeError: If two threads run :meth:`get` or :meth:``get_iter`
136+
RuntimeError: If two threads run :meth:`get` or :meth:`get_iter`
135137
concurrently.
136138
137139
"""
@@ -159,11 +161,9 @@ def get_iter(self) -> Iterator[Data]:
159161
self.get_in_progress = True
160162

161163
# Locking with get_in_progress ensures only one thread can get here.
162-
yield from chunks
163-
while True:
164-
chunk = self.chunks_queue.get()
165-
if chunk is None:
166-
break
164+
for chunk in chunks:
165+
yield chunk
166+
while (chunk := self.chunks_queue.get()) is not None:
167167
yield chunk
168168

169169
with self.mutex:
@@ -242,6 +242,7 @@ def put(self, frame: Frame) -> None:
242242
self.put_in_progress = True
243243

244244
# Release the lock to allow get() to run and eventually set the event.
245+
# Locking with get_in_progress ensures only one coroutine can get here.
245246
self.message_fetched.wait()
246247

247248
with self.mutex:

tests/legacy/test_client_server.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import urllib.request
1515
import warnings
1616

17+
from websockets.asyncio.compatibility import asyncio_timeout
1718
from websockets.datastructures import Headers
1819
from websockets.exceptions import (
1920
ConnectionClosed,
@@ -29,7 +30,6 @@
2930
)
3031
from websockets.http import USER_AGENT
3132
from websockets.legacy.client import *
32-
from websockets.legacy.compatibility import asyncio_timeout
3333
from websockets.legacy.handshake import build_response
3434
from websockets.legacy.http import read_response
3535
from websockets.legacy.server import *

0 commit comments

Comments
 (0)