refactor: use __qualname__, () for large strings

This commit is contained in:
Aditya 2020-07-01 20:15:33 +05:30
parent c361fe0d3b
commit 4acdc2e5d6
3 changed files with 18 additions and 17 deletions

View File

@ -121,7 +121,7 @@ class H2ClientProtocol(Protocol):
def request(self, request: Request):
if not isinstance(request, Request):
raise TypeError(f'Expected scrapy.http.Request, received {request.__class__.__name__}')
raise TypeError(f'Expected scrapy.http.Request, received {request.__class__.__qualname__}')
stream = self._new_stream(request)
d = stream.get_response()

View File

@ -146,7 +146,7 @@ class Stream:
self._deferred_response = Deferred(_cancel)
def __str__(self):
return f'Stream(id={self.stream_id})'
return f'Stream(id={self.stream_id!r})'
__repr__ = __str__
@ -190,11 +190,11 @@ class Stream:
def _get_request_headers(self):
url = urlparse(self._request.url)
# Make sure pseudo-headers comes before all the other headers
path = url.path
if url.query:
path += '?' + url.query
# Make sure pseudo-headers comes before all the other headers
headers = [
(':method', self._request.method),
(':authority', url.netloc),
@ -284,8 +284,10 @@ class Stream:
if self._log_warnsize:
self._reached_warnsize = True
warning_msg = f"Received more ({self._response['flow_controlled_size']}) bytes than download " \
+ f'warn size ({self._download_warnsize}) in request {self._request}'
warning_msg = (
f'Received more ({self._response["flow_controlled_size"]}) bytes than download '
f'warn size ({self._download_warnsize}) in request {self._request}'
)
logger.warning(warning_msg)
# Acknowledge the data received
@ -306,8 +308,10 @@ class Stream:
if self._log_warnsize:
self._reached_warnsize = True
warning_msg = f'Expected response size ({expected_size}) larger than ' \
+ f'download warn size ({self._download_warnsize}) in request {self._request}'
warning_msg = (
f'Expected response size ({expected_size}) larger than '
f'download warn size ({self._download_warnsize}) in request {self._request}'
)
logger.warning(warning_msg)
def reset_stream(self, reason=StreamCloseReason.RESET):
@ -340,7 +344,7 @@ class Stream:
raise StreamClosedError(self.stream_id)
if not isinstance(reason, StreamCloseReason):
raise TypeError(f'Expected StreamCloseReason, received {reason.__class__.__name__}')
raise TypeError(f'Expected StreamCloseReason, received {reason.__class__.__qualname__}')
self._cb_close(self.stream_id)
self.stream_closed_server = True
@ -356,8 +360,10 @@ class Stream:
# having Content-Length)
if reason is StreamCloseReason.MAXSIZE_EXCEEDED:
expected_size = int(self._response['headers'].get(b'Content-Length', -1))
error_msg = f'Cancelling download of {self._request.url}: expected response ' \
f'size ({expected_size}) larger than download max size ({self._download_maxsize}).'
error_msg = (
f'Cancelling download of {self._request.url}: expected response '
f'size ({expected_size}) larger than download max size ({self._download_maxsize}).'
)
logger.error(error_msg)
self._deferred_response.errback(CancelledError(error_msg))

View File

@ -560,13 +560,8 @@ class Https2ClientProtocolTestCase(TestCase):
d_list = [
self.test_invalid_hostname(),
self.test_invalid_host_port(),
self._check_GET(Request(self.get_url('/get-data-html-small')), Data.HTML_SMALL, 200),
self._check_POST_json(
JsonRequest(url=self.get_url('/post-data-json-small'), method='POST', data=Data.JSON_SMALL),
Data.JSON_SMALL,
Data.EXTRA_SMALL,
200
)
self.test_GET_small_body(),
self.test_POST_small_json()
]
return DeferredList(d_list, fireOnOneErrback=True)