Scrapy, a fast high-level web crawling & scraping framework for Python.
Deprecate overriding ScrapyClientContextFactory.getContext().
Andrey Rakhmatullinbc1aeeefc970
diff --git a/scrapy/core/downloader/contextfactory.py b/scrapy/core/downloader/contextfactory.pyindex b01ee97f3..d1ba6208a 100644--- a/scrapy/core/downloader/contextfactory.py+++ b/scrapy/core/downloader/contextfactory.py@@ -22,6 +22,7 @@ from scrapy.core.downloader.tls import (openssl_methods,)from scrapy.exceptions import ScrapyDeprecationWarning+from scrapy.utils.deprecate import method_is_overriddenfrom scrapy.utils.misc import build_from_crawler, load_objectif TYPE_CHECKING:@@ -62,6 +63,13 @@ class ScrapyClientContextFactory(BrowserLikePolicyForHTTPS):self.tls_ciphers = AcceptableCiphers.fromOpenSSLCipherString(tls_ciphers)else:self.tls_ciphers = DEFAULT_CIPHERS+ if method_is_overridden(type(self), ScrapyClientContextFactory, "getContext"):+ warnings.warn(+ "Overriding ScrapyClientContextFactory.getContext() is deprecated and that method"+ " will be removed in a future Scrapy version. Override creatorForNetloc() instead.",+ category=ScrapyDeprecationWarning,+ stacklevel=2,+ )@classmethoddef from_settings(@@ -121,7 +129,6 @@ class ScrapyClientContextFactory(BrowserLikePolicyForHTTPS):# kept for old-style HTTP/1.0 downloader context twisted calls,# e.g. connectSSL()def getContext(self, hostname: Any = None, port: Any = None) -> SSL.Context:- # FIXMEctx: SSL.Context = self.getCertificateOptions().getContext()ctx.set_options(0x4) # OP_LEGACY_SERVER_CONNECTreturn ctxdiff --git a/tests/test_core_downloader.py b/tests/test_core_downloader.pyindex 0a0c0a4f0..e67337fc7 100644--- a/tests/test_core_downloader.py+++ b/tests/test_core_downloader.py@@ -1,8 +1,10 @@from __future__ import annotationsimport shutil+import warningsfrom pathlib import Pathfrom tempfile import mkdtemp+from typing import Anyimport OpenSSL.SSLimport pytest@@ -92,6 +94,22 @@ class ContextFactoryTestCase(ContextFactoryBaseTestCase):)self.assertEqual(body, to_bytes(s))+ def test_override_getContext(self):+ class MyFactory(ScrapyClientContextFactory):+ def getContext(+ self, hostname: Any = None, port: Any = None+ ) -> OpenSSL.SSL.Context:+ ctx: OpenSSL.SSL.Context = super().getContext(hostname, port)+ return ctx++ with warnings.catch_warnings(record=True) as w:+ MyFactory()+ self.assertEqual(len(w), 1)+ self.assertIn(+ "Overriding ScrapyClientContextFactory.getContext() is deprecated",+ str(w[0].message),+ )+class ContextFactoryTLSMethodTestCase(ContextFactoryBaseTestCase):async def _assert_factory_works(