Skip to content

Commit 9c68aa8

Browse files
committed
Adopt black and isort to autoformat the files
This removes any need to figure out how to style the code for h11, instead allowing these two commands to do it instead, black h11/ isort --dont-skip __init__.py --apply --settings-path setup.cfg --recursive h11 In addition a format stage has been added to the tox setup to enforce this styling. This is the same styling as used by the wsproto project.
1 parent dc3e462 commit 9c68aa8

23 files changed

+1019
-705
lines changed

CONTRIBUTING.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ other hand, the following are all very welcome:
4242

4343
* Code should work across all currently supported Python releases.
4444

45-
* Code should follow
46-
[PEP 8](https://www.python.org/dev/peps/pep-0008/) style. We aren't
47-
blind sticklers for this though (in fact,
48-
[not being a blind stickler for PEP 8](https://www.python.org/dev/peps/pep-0008/#a-foolish-consistency-is-the-hobgoblin-of-little-minds)
49-
is one of the requirements in PEP 8!). If you run
50-
[pycodestyle](https://github.com/PyCQA/pycodestyle) over the code
51-
base then it complains about lots of things, but these are generally
52-
places where "fixing the problem" would make the code less
53-
readable. Still, though, when in doubt: PEP 8.
45+
* Code must be formatted using
46+
[black](https://github.com/python/black) and
47+
[isort](https://github.com/timothycrosley/isort) as configured in
48+
the project. With those projects installed the commands,
49+
50+
black h11/
51+
isort --dont-skip __init__.py --apply --settings-path setup.cfg --recursive h11
52+
53+
will format your code for you.
5454

5555
* If you change the code, then you have to also add or fix at least
5656
one test. (See below for how to run the test suite.) This helps us

h11/__init__.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
# semantics to check that what you're asking to write to the wire is sensible,
77
# but at least it gets you out of dealing with the wire itself.
88

9+
from ._connection import *
10+
from ._events import *
11+
from ._state import *
12+
from ._util import LocalProtocolError, ProtocolError, RemoteProtocolError
913
from ._version import __version__
14+
1015
PRODUCT_ID = "python-h11/" + __version__
1116

12-
from ._util import ProtocolError, LocalProtocolError, RemoteProtocolError
13-
from ._events import *
14-
from ._connection import *
15-
from ._state import *
1617

1718
__all__ = ["ProtocolError", "LocalProtocolError", "RemoteProtocolError"]
1819
__all__ += _events.__all__

h11/_abnf.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@
6666
r":"
6767
r"{OWS}"
6868
r"(?P<field_value>{field_value})"
69-
r"{OWS}"
70-
.format(**globals()))
69+
r"{OWS}".format(**globals())
70+
)
7171

7272
# https://svn.tools.ietf.org/svn/wg/httpbis/specs/rfc7230.html#request.line
7373
#
@@ -87,8 +87,8 @@
8787
r" "
8888
r"(?P<target>{request_target})"
8989
r" "
90-
r"{http_version}"
91-
.format(**globals()))
90+
r"{http_version}".format(**globals())
91+
)
9292

9393
# https://svn.tools.ietf.org/svn/wg/httpbis/specs/rfc7230.html#status.line
9494
#
@@ -106,8 +106,8 @@
106106
# https://github.com/scrapy/scrapy/issues/345#issuecomment-281756036
107107
# https://github.com/seanmonstar/httparse/issues/29
108108
# so make it optional. ?: is a non-capturing group.
109-
r"(?: (?P<reason>{reason_phrase}))?"
110-
.format(**globals()))
109+
r"(?: (?P<reason>{reason_phrase}))?".format(**globals())
110+
)
111111

112112
HEXDIG = r"[0-9A-Fa-f]"
113113
# Actually
@@ -125,5 +125,5 @@
125125
chunk_header = (
126126
r"(?P<chunk_size>{chunk_size})"
127127
r"(?P<chunk_ext>{chunk_ext})?"
128-
r"\r\n"
129-
.format(**globals()))
128+
r"\r\n".format(**globals())
129+
)

h11/_connection.py

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
# This contains the main Connection class. Everything in h11 revolves around
22
# this.
33

4-
# Import all event types
5-
from ._events import *
6-
# Import all state sentinels
7-
from ._state import *
8-
# Import the internal things we need
9-
from ._util import LocalProtocolError, RemoteProtocolError, make_sentinel
10-
from ._state import ConnectionState, _SWITCH_UPGRADE, _SWITCH_CONNECT
11-
from ._headers import (
12-
get_comma_header, set_comma_header, has_expect_100_continue,
13-
)
14-
from ._receivebuffer import ReceiveBuffer
4+
from ._events import * # Import all event types
5+
from ._headers import get_comma_header, has_expect_100_continue, set_comma_header
156
from ._readers import READERS
7+
from ._receivebuffer import ReceiveBuffer
8+
from ._state import * # Import all state sentinels
9+
from ._state import _SWITCH_CONNECT, _SWITCH_UPGRADE, ConnectionState
10+
from ._util import ( # Import the internal things we need
11+
LocalProtocolError,
12+
make_sentinel,
13+
RemoteProtocolError,
14+
)
1615
from ._writers import WRITERS
1716

1817
# Everything in __all__ gets re-exported as part of the h11 public API.
@@ -53,6 +52,7 @@ def _keep_alive(event):
5352
return False
5453
return True
5554

55+
5656
def _body_framing(request_method, event):
5757
# Called when we enter SEND_BODY to figure out framing information for
5858
# this body.
@@ -73,10 +73,11 @@ def _body_framing(request_method, event):
7373
# Step 1: some responses always have an empty body, regardless of what the
7474
# headers say.
7575
if type(event) is Response:
76-
if (event.status_code in (204, 304)
76+
if (
77+
event.status_code in (204, 304)
7778
or request_method == b"HEAD"
78-
or (request_method == b"CONNECT"
79-
and 200 <= event.status_code < 300)):
79+
or (request_method == b"CONNECT" and 200 <= event.status_code < 300)
80+
):
8081
return ("content-length", (0,))
8182
# Section 3.3.3 also lists another case -- responses with status_code
8283
# < 200. For us these are InformationalResponses, not Responses, so
@@ -100,12 +101,14 @@ def _body_framing(request_method, event):
100101
else:
101102
return ("http/1.0", ())
102103

104+
103105
################################################################
104106
#
105107
# The main Connection class
106108
#
107109
################################################################
108110

111+
109112
class Connection(object):
110113
"""An object encapsulating the state of an HTTP connection.
111114
@@ -121,14 +124,14 @@ class Connection(object):
121124
:exc:`RemoteProtocolError`.
122125
123126
"""
124-
def __init__(self,
125-
our_role,
126-
max_incomplete_event_size=DEFAULT_MAX_INCOMPLETE_EVENT_SIZE):
127+
128+
def __init__(
129+
self, our_role, max_incomplete_event_size=DEFAULT_MAX_INCOMPLETE_EVENT_SIZE
130+
):
127131
self._max_incomplete_event_size = max_incomplete_event_size
128132
# State and role tracking
129133
if our_role not in (CLIENT, SERVER):
130-
raise ValueError(
131-
"expected CLIENT or SERVER, not {!r}".format(our_role))
134+
raise ValueError("expected CLIENT or SERVER, not {!r}".format(our_role))
132135
self.our_role = our_role
133136
if our_role is CLIENT:
134137
self.their_role = SERVER
@@ -185,8 +188,7 @@ def their_state(self):
185188

186189
@property
187190
def they_are_waiting_for_100_continue(self):
188-
return (self.their_role is CLIENT
189-
and self.client_is_waiting_for_100_continue)
191+
return self.their_role is CLIENT and self.client_is_waiting_for_100_continue
190192

191193
def start_next_cycle(self):
192194
"""Attempt to reset our connection state for a new request/response
@@ -217,8 +219,10 @@ def _server_switch_event(self, event):
217219
if type(event) is InformationalResponse and event.status_code == 101:
218220
return _SWITCH_UPGRADE
219221
if type(event) is Response:
220-
if (_SWITCH_CONNECT in self._cstate.pending_switch_proposals
221-
and 200 <= event.status_code < 300):
222+
if (
223+
_SWITCH_CONNECT in self._cstate.pending_switch_proposals
224+
and 200 <= event.status_code < 300
225+
):
222226
return _SWITCH_CONNECT
223227
return None
224228

@@ -244,8 +248,11 @@ def _process_event(self, role, event):
244248
self._request_method = event.method
245249

246250
# self.their_http_version
247-
if (role is self.their_role
248-
and type(event) in (Request, Response, InformationalResponse)):
251+
if role is self.their_role and type(event) in (
252+
Request,
253+
Response,
254+
InformationalResponse,
255+
):
249256
self.their_http_version = event.http_version
250257

251258
# Keep alive handling
@@ -341,8 +348,7 @@ def receive_data(self, data):
341348
"""
342349
if data:
343350
if self._receive_buffer_closed:
344-
raise RuntimeError(
345-
"received close, then received more data?")
351+
raise RuntimeError("received close, then received more data?")
346352
self._receive_buffer += data
347353
else:
348354
self._receive_buffer_closed = True
@@ -414,8 +420,7 @@ def next_event(self):
414420
"""
415421

416422
if self.their_state is ERROR:
417-
raise RemoteProtocolError(
418-
"Can't receive data when peer state is ERROR")
423+
raise RemoteProtocolError("Can't receive data when peer state is ERROR")
419424
try:
420425
event = self._extract_next_receive_event()
421426
if event not in [NEED_DATA, PAUSED]:
@@ -425,13 +430,13 @@ def next_event(self):
425430
if len(self._receive_buffer) > self._max_incomplete_event_size:
426431
# 431 is "Request header fields too large" which is pretty
427432
# much the only situation where we can get here
428-
raise RemoteProtocolError("Receive buffer too long",
429-
error_status_hint=431)
433+
raise RemoteProtocolError(
434+
"Receive buffer too long", error_status_hint=431
435+
)
430436
if self._receive_buffer_closed:
431437
# We're still trying to complete some event, but that's
432438
# never going to happen because no more data is coming
433-
raise RemoteProtocolError(
434-
"peer unexpectedly closed connection")
439+
raise RemoteProtocolError("peer unexpectedly closed connection")
435440
return event
436441
except BaseException as exc:
437442
self._process_error(self.their_role)
@@ -476,8 +481,7 @@ def send_with_data_passthrough(self, event):
476481
477482
"""
478483
if self.our_state is ERROR:
479-
raise LocalProtocolError(
480-
"Can't send data when our state is ERROR")
484+
raise LocalProtocolError("Can't send data when our state is ERROR")
481485
try:
482486
if type(event) is Response:
483487
self._clean_up_response_headers_for_sending(event)
@@ -557,8 +561,7 @@ def _clean_up_response_headers_for_sending(self, response):
557561
# to fix it instead of erroring out, so we'll accord the user the
558562
# same respect).
559563
set_comma_header(headers, b"content-length", [])
560-
if (self.their_http_version is None
561-
or self.their_http_version < b"1.1"):
564+
if self.their_http_version is None or self.their_http_version < b"1.1":
562565
# Either we never got a valid request and are sending back an
563566
# error (their_http_version is None), so we assume the worst;
564567
# or else we did get a valid HTTP/1.0 request, so we know that

0 commit comments

Comments
 (0)