Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor code to use HTTPStatus instead of http.server #6903

Merged
merged 13 commits into from
Nov 28, 2022
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 @@ -352,6 +352,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 @@ -8,6 +8,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 @@ -46,7 +47,6 @@
from .client_proto import ResponseHandler
from .client_reqrep import SSL_ALLOWED_TYPES, ClientRequest, Fingerprint
from .helpers import _SENTINEL, ceil_timeout, is_ip_address, sentinel, set_result
from .http import RESPONSES
from .locks import EventResultOrError
from .resolver import DefaultResolver

Expand Down Expand Up @@ -1227,7 +1227,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
5 changes: 0 additions & 5 deletions aiohttp/http.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import http.server
import sys
from typing import Mapping, Tuple

from . import __version__
from .http_exceptions import HttpProcessingError as HttpProcessingError
Expand Down Expand Up @@ -34,7 +32,6 @@

__all__ = (
"HttpProcessingError",
"RESPONSES",
"SERVER_SOFTWARE",
# .http_writer
"StreamWriter",
Expand Down Expand Up @@ -66,5 +63,3 @@
SERVER_SOFTWARE: str = "Python/{0[0]}.{0[1]} aiohttp/{1}".format(
sys.version_info, __version__
)

RESPONSES: Mapping[int, Tuple[str, str]] = http.server.BaseHTTPRequestHandler.responses
Dreamsorcerer marked this conversation as resolved.
Show resolved Hide resolved
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
from typing import (
TYPE_CHECKING,
Any,
Dict,
Iterator,
Mapping,
MutableMapping,
Optional,
Tuple,
Union,
cast,
)
Expand All @@ -39,7 +38,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 @@ -155,16 +154,15 @@ def set_status(
self,
status: int,
reason: Optional[str] = None,
_RESPONSES: Mapping[int, Tuple[str, str]] = RESPONSES,
Dreamsorcerer marked this conversation as resolved.
Show resolved Hide resolved
) -> 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