Skip to content

Commit 94343f6

Browse files
committed
Move InvalidStatusCode to the legacy package.
It is only used by the legacy implementation.
1 parent 57d66f0 commit 94343f6

File tree

6 files changed

+29
-25
lines changed

6 files changed

+29
-25
lines changed

src/websockets/__init__.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
"InvalidParameterValue",
2929
"InvalidState",
3030
"InvalidStatus",
31-
"InvalidStatusCode",
3231
"InvalidUpgrade",
3332
"InvalidURI",
3433
"NegotiationError",
@@ -47,6 +46,7 @@
4746
"unix_connect",
4847
# .legacy.exceptions
4948
"InvalidMessage",
49+
"InvalidStatusCode",
5050
# .legacy.protocol
5151
"WebSocketCommonProtocol",
5252
# .legacy.server
@@ -86,7 +86,6 @@
8686
InvalidParameterValue,
8787
InvalidState,
8888
InvalidStatus,
89-
InvalidStatusCode,
9089
InvalidUpgrade,
9190
InvalidURI,
9291
NegotiationError,
@@ -102,7 +101,7 @@
102101
basic_auth_protocol_factory,
103102
)
104103
from .legacy.client import WebSocketClientProtocol, connect, unix_connect
105-
from .legacy.exceptions import InvalidMessage
104+
from .legacy.exceptions import InvalidMessage, InvalidStatusCode
106105
from .legacy.protocol import WebSocketCommonProtocol
107106
from .legacy.server import (
108107
WebSocketServer,
@@ -146,7 +145,6 @@
146145
"InvalidParameterValue": ".exceptions",
147146
"InvalidState": ".exceptions",
148147
"InvalidStatus": ".exceptions",
149-
"InvalidStatusCode": ".exceptions",
150148
"InvalidUpgrade": ".exceptions",
151149
"InvalidURI": ".exceptions",
152150
"NegotiationError": ".exceptions",
@@ -165,6 +163,7 @@
165163
"unix_connect": ".legacy.client",
166164
# .legacy.exceptions
167165
"InvalidMessage": ".legacy.exceptions",
166+
"InvalidStatusCode": ".legacy.exceptions",
168167
# .legacy.protocol
169168
"WebSocketCommonProtocol": ".legacy.protocol",
170169
# .legacy.server

src/websockets/exceptions.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -250,20 +250,6 @@ def __str__(self) -> str:
250250
)
251251

252252

253-
class InvalidStatusCode(InvalidHandshake):
254-
"""
255-
Raised when a handshake response status code is invalid.
256-
257-
"""
258-
259-
def __init__(self, status_code: int, headers: datastructures.Headers) -> None:
260-
self.status_code = status_code
261-
self.headers = headers
262-
263-
def __str__(self) -> str:
264-
return f"server rejected WebSocket connection: HTTP {self.status_code}"
265-
266-
267253
class NegotiationError(InvalidHandshake):
268254
"""
269255
Raised when negotiating an extension fails.
@@ -406,14 +392,18 @@ class ProtocolError(WebSocketException):
406392

407393
# When type checking, import non-deprecated aliases eagerly. Else, import on demand.
408394
if typing.TYPE_CHECKING:
409-
from .legacy.exceptions import InvalidMessage
395+
from .legacy.exceptions import (
396+
InvalidMessage,
397+
InvalidStatusCode,
398+
)
410399

411400
WebSocketProtocolError = ProtocolError
412401
else:
413402
lazy_import(
414403
globals(),
415404
aliases={
416405
"InvalidMessage": ".legacy.exceptions",
406+
"InvalidStatusCode": ".legacy.exceptions",
417407
"WebSocketProtocolError": ".legacy.exceptions",
418408
},
419409
)

src/websockets/legacy/client.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
from ..exceptions import (
2222
InvalidHandshake,
2323
InvalidHeader,
24-
InvalidStatusCode,
2524
NegotiationError,
2625
RedirectHandshake,
2726
SecurityError,
@@ -40,7 +39,7 @@
4039
from ..http11 import USER_AGENT
4140
from ..typing import ExtensionHeader, LoggerLike, Origin, Subprotocol
4241
from ..uri import WebSocketURI, parse_uri
43-
from .exceptions import InvalidMessage
42+
from .exceptions import InvalidMessage, InvalidStatusCode
4443
from .handshake import build_request, check_response
4544
from .http import read_response
4645
from .protocol import WebSocketCommonProtocol

src/websockets/legacy/exceptions.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from .. import datastructures
12
from ..exceptions import (
23
InvalidHandshake,
34
ProtocolError as WebSocketProtocolError, # noqa: F401
@@ -9,3 +10,17 @@ class InvalidMessage(InvalidHandshake):
910
Raised when a handshake request or response is malformed.
1011
1112
"""
13+
14+
15+
class InvalidStatusCode(InvalidHandshake):
16+
"""
17+
Raised when a handshake response status code is invalid.
18+
19+
"""
20+
21+
def __init__(self, status_code: int, headers: datastructures.Headers) -> None:
22+
self.status_code = status_code
23+
self.headers = headers
24+
25+
def __str__(self) -> str:
26+
return f"server rejected WebSocket connection: HTTP {self.status_code}"

tests/legacy/test_exceptions.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import unittest
22

3+
from websockets.datastructures import Headers
34
from websockets.legacy.exceptions import *
45

56

@@ -10,6 +11,10 @@ def test_str(self):
1011
InvalidMessage("malformed HTTP message"),
1112
"malformed HTTP message",
1213
),
14+
(
15+
InvalidStatusCode(403, Headers()),
16+
"server rejected WebSocket connection: HTTP 403",
17+
),
1318
]:
1419
with self.subTest(exception=exception):
1520
self.assertEqual(str(exception), exception_str)

tests/test_exceptions.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,6 @@ def test_str(self):
127127
InvalidStatus(Response(401, "Unauthorized", Headers())),
128128
"server rejected WebSocket connection: HTTP 401",
129129
),
130-
(
131-
InvalidStatusCode(403, Headers()),
132-
"server rejected WebSocket connection: HTTP 403",
133-
),
134130
(
135131
NegotiationError("unsupported subprotocol: spam"),
136132
"unsupported subprotocol: spam",

0 commit comments

Comments
 (0)