Skip to content

Commit

Permalink
Backport #6903 (#7104)
Browse files Browse the repository at this point in the history
Co-authored-by: Zainab Lawal <zainablaw1012@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed Nov 28, 2022
1 parent 3e7448a commit f81cd7f
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGES/6903.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Refactor code to use HTTPStatus instead of http.server to reduce import time
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ Yury Pliner
Yury Selivanov
Yusuke Tsutsumi
Yuval Ofir
Zainab Lawal
Zeal Wierslee
Zlatan Sičanica
Марк Коренберг
Expand Down
4 changes: 2 additions & 2 deletions aiohttp/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import warnings
from collections import defaultdict, deque
from contextlib import suppress
from http import HTTPStatus
from http.cookies import SimpleCookie
from itertools import cycle, islice
from time import monotonic
Expand Down Expand Up @@ -53,7 +54,6 @@
noop,
sentinel,
)
from .http import RESPONSES
from .locks import EventResultOrError
from .resolver import DefaultResolver

Expand Down Expand Up @@ -1309,7 +1309,7 @@ async def _create_proxy_connection(
if resp.status != 200:
message = resp.reason
if message is None:
message = RESPONSES[resp.status][0]
message = HTTPStatus(resp.status).phrase
raise ClientHttpProxyError(
proxy_resp.request_info,
resp.history,
Expand Down
6 changes: 4 additions & 2 deletions aiohttp/http.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import http.server
import sys
from http import HTTPStatus
from typing import Mapping, Tuple

from . import __version__
Expand Down Expand Up @@ -67,4 +67,6 @@
sys.version_info, __version__
)

RESPONSES: Mapping[int, Tuple[str, str]] = http.server.BaseHTTPRequestHandler.responses
RESPONSES: Mapping[int, Tuple[str, str]] = {
v: (v.phrase, v.description) for v in HTTPStatus.__members__.values()
}
10 changes: 4 additions & 6 deletions aiohttp/web_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@
import warnings
import zlib
from concurrent.futures import Executor
from http import HTTPStatus
from http.cookies import Morsel, SimpleCookie
from typing import (
TYPE_CHECKING,
Any,
Dict,
Iterator,
Mapping,
MutableMapping,
Optional,
Tuple,
Union,
cast,
)
Expand All @@ -37,7 +36,7 @@
sentinel,
validate_etag_value,
)
from .http import RESPONSES, SERVER_SOFTWARE, HttpVersion10, HttpVersion11
from .http import SERVER_SOFTWARE, HttpVersion10, HttpVersion11
from .payload import Payload
from .typedefs import JSONEncoder, LooseHeaders

Expand Down Expand Up @@ -135,16 +134,15 @@ def set_status(
self,
status: int,
reason: Optional[str] = None,
_RESPONSES: Mapping[int, Tuple[str, str]] = RESPONSES,
) -> None:
assert not self.prepared, (
"Cannot change the response status code after " "the headers have been sent"
)
self._status = int(status)
if reason is None:
try:
reason = _RESPONSES[self._status][0]
except Exception:
reason = HTTPStatus(self._status).phrase
except ValueError:
reason = ""
self._reason = reason

Expand Down
2 changes: 1 addition & 1 deletion tests/test_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ def test_import_time(pytester: pytest.Pytester) -> None:
else:
os.environ["PYTHONPATH"] = old_path

assert best_time_ms < 250
assert best_time_ms < 200

0 comments on commit f81cd7f

Please sign in to comment.