1
0
mirror of https://github.com/scrapy/scrapy.git synced 2025-02-06 13:30:05 +00:00

Deprecate overriding ScrapyClientContextFactory.getContext().

This commit is contained in:
Andrey Rakhmatullin 2025-01-28 01:54:43 +05:00
parent 16b998f9ca
commit bc1aeeefc9
2 changed files with 26 additions and 1 deletions

View File

@ -22,6 +22,7 @@ from scrapy.core.downloader.tls import (
openssl_methods,
)
from scrapy.exceptions import ScrapyDeprecationWarning
from scrapy.utils.deprecate import method_is_overridden
from scrapy.utils.misc import build_from_crawler, load_object
if 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,
)
@classmethod
def 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:
# FIXME
ctx: SSL.Context = self.getCertificateOptions().getContext()
ctx.set_options(0x4) # OP_LEGACY_SERVER_CONNECT
return ctx

View File

@ -1,8 +1,10 @@
from __future__ import annotations
import shutil
import warnings
from pathlib import Path
from tempfile import mkdtemp
from typing import Any
import OpenSSL.SSL
import 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(