Scrapy, a fast high-level web crawling & scraping framework for Python.
Remove unnecessary Failure instances. (#8323)
Andrey Rakhmatullin642b76d182f6
diff --git a/scrapy/core/_http2/agent.py b/scrapy/core/_http2/agent.pyindex e02d783b2..6e65a5fef 100644--- a/scrapy/core/_http2/agent.py+++ b/scrapy/core/_http2/agent.py@@ -5,7 +5,6 @@ from typing import TYPE_CHECKING, TypeAliasfrom twisted.internet import deferfrom twisted.internet.defer import Deferred-from twisted.python.failure import Failurefrom twisted.web.client import URI, BrowserLikePolicyForHTTPS, _StandardEndpointFactoryfrom twisted.web.error import SchemeNotSupported@@ -138,8 +137,8 @@ class H2Agent:uri = URI.fromBytes(bytes(request.url, encoding="utf-8"))try:endpoint = self.get_endpoint(uri)- except SchemeNotSupported:- return defer.fail(Failure())+ except SchemeNotSupported as exc:+ return defer.fail(exc)key = self.get_key(uri)d: Deferred[H2ClientProtocol] = self._pool.get_connection(key, uri, endpoint)diff --git a/scrapy/core/_http2/protocol.py b/scrapy/core/_http2/protocol.pyindex 3b1e8cac0..1a7e85478 100644--- a/scrapy/core/_http2/protocol.py+++ b/scrapy/core/_http2/protocol.py@@ -358,7 +358,8 @@ class H2ClientProtocol(Protocol, TimeoutMixin):# Cancel the timeout if not done yetself.setTimeout(None) # type: ignore[no-untyped-call]- self._conn_lost_errors.append(reason)+ assert reason.value is not None+ self._conn_lost_errors.append(reason.value)# Notify the connection pool instance such that no new requests are# sent over current connectiondiff --git a/scrapy/core/_http2/stream.py b/scrapy/core/_http2/stream.pyindex 637e418d4..6f6df1f81 100644--- a/scrapy/core/_http2/stream.py+++ b/scrapy/core/_http2/stream.py@@ -10,7 +10,6 @@ from h2.errors import ErrorCodesfrom h2.exceptions import H2Error, ProtocolError, StreamClosedErrorfrom twisted.internet.defer import Deferredfrom twisted.internet.error import ConnectionClosed-from twisted.python.failure import Failurefrom twisted.web.client import ResponseFailedfrom scrapy import signals@@ -477,9 +476,8 @@ class Stream:self._deferred_response.errback(ResponseFailed([- Failure(- f"Remote peer {self._protocol.metadata['ip_address']} sent RST_STREAM",- ProtocolError,+ ProtocolError(+ f"Remote peer {self._protocol.metadata['ip_address']} sent RST_STREAM")])diff --git a/scrapy/core/downloader/__init__.py b/scrapy/core/downloader/__init__.pyindex b784b4efe..54f018d13 100644--- a/scrapy/core/downloader/__init__.py+++ b/scrapy/core/downloader/__init__.py@@ -9,7 +9,6 @@ from time import monotonicfrom typing import TYPE_CHECKING, Anyfrom twisted.internet.defer import Deferred, DeferredList, inlineCallbacks-from twisted.python.failure import Failurefrom scrapy import Request, Spider, signalsfrom scrapy.core.downloader.handlers import DownloadHandlers@@ -380,9 +379,9 @@ class Downloader:) -> None:try:response = await self._download(slot, request)- except (Exception, asyncio.CancelledError):+ except (Exception, asyncio.CancelledError) as exc:if not queue_dfd.called:- queue_dfd.errback(Failure())+ queue_dfd.errback(exc)else:queue_dfd.callback(response) # awaited in _enqueue_request()@@ -398,10 +397,8 @@ class Downloader:dropped_count += 1if not queue_dfd.called:queue_dfd.errback(- Failure(- DownloadCancelledError(- "Request dropped due to fast downloader shutdown"- )+ DownloadCancelledError(+ "Request dropped due to fast downloader shutdown"))self.signals.send_catch_log(diff --git a/scrapy/core/downloader/handlers/http11.py b/scrapy/core/downloader/handlers/http11.pyindex bcb32a076..d00836fa7 100644--- a/scrapy/core/downloader/handlers/http11.py+++ b/scrapy/core/downloader/handlers/http11.py@@ -16,7 +16,6 @@ from twisted.internet import sslfrom twisted.internet.defer import Deferred, succeedfrom twisted.internet.endpoints import TCP4ClientEndpointfrom twisted.internet.protocol import Factory, Protocol, connectionDone-from twisted.python.failure import Failurefrom twisted.web._newclient import (HEADER,STATUS,@@ -69,6 +68,7 @@ from ._base_http import BaseHttpDownloadHandlerif TYPE_CHECKING:from twisted.internet.base import ReactorBasefrom twisted.internet.interfaces import IAddress, IConsumer+ from twisted.python.failure import Failurefrom twisted.web._newclient import Request as TxRequestfrom scrapy.crawler import Crawler@@ -790,8 +790,7 @@ class _ResponseReader(Protocol):exc = ResponseDataLossError()exc.__cause__ = reason.value- reason = Failure(exc)- self._finished.errback(reason)+ self._finished.errback(exc)class _LenientHTTPClientParser(HTTPClientParser):diff --git a/scrapy/core/scraper.py b/scrapy/core/scraper.pyindex 6da8ff527..386b29f0b 100644--- a/scrapy/core/scraper.py+++ b/scrapy/core/scraper.py@@ -316,8 +316,8 @@ class Scraper:) -> None:try:await self._scrape(result, request)- except Exception:- queue_dfd.errback(Failure())+ except Exception as exc:+ queue_dfd.errback(exc)else:queue_dfd.callback(None) # awaited in enqueue_scrape()