Skip to content

Commit 741dc48

Browse files
njsmithpgjones
authored andcommitted
Force status codes to be integers, not IntEnums
IntEnum objects, like http.HTTPStatus objects, are subclasses of int, but don't behave the same in all circumstances, e.g. string formatting. Force our status codes to be actual int objects, so that they'll behave the way we expect. Fixes gh-72.
1 parent 836d95d commit 741dc48

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

h11/_events.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ def __init__(self, **kwargs):
5757
if "status_code" in self.__dict__:
5858
if not isinstance(self.status_code, int):
5959
raise LocalProtocolError("status code must be integer")
60+
# Because IntEnum objects are instances of int, but aren't
61+
# duck-compatible (sigh), see gh-72.
62+
self.status_code = int(self.status_code)
63+
6064

6165
self._validate()
6266

h11/tests/test_events.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,16 @@ def test_events():
134134

135135
cc = ConnectionClosed()
136136
assert repr(cc) == "ConnectionClosed()"
137+
138+
139+
def test_intenum_status_code():
140+
# https://github.com/python-hyper/h11/issues/72
141+
try:
142+
from http import HTTPStatus
143+
except ImportError:
144+
pytest.skip("Only affects Python 3")
145+
146+
r = Response(status_code=HTTPStatus.OK, headers=[], http_version="1.0")
147+
assert r.status_code == HTTPStatus.OK
148+
assert type(r.status_code) is not type(HTTPStatus.OK)
149+
assert type(r.status_code) is int

0 commit comments

Comments
 (0)