Restore brotlicffi support (#7160)

* restore brotlicffi dependency

* pre-commit
This commit is contained in:
Rui Xi 2025-11-25 15:06:55 +08:00 committed by GitHub
parent 4cb0144b39
commit 813fd9f1ac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 32 additions and 14 deletions

View File

@ -31,7 +31,10 @@ logger = getLogger(__name__)
ACCEPTED_ENCODINGS: list[bytes] = [b"gzip", b"deflate"]
try:
import brotli
try:
import brotli
except ImportError:
import brotlicffi as brotli
except ImportError:
pass
else:
@ -40,8 +43,8 @@ else:
except AttributeError: # pragma: no cover
warnings.warn(
"You have brotli installed. But 'br' encoding support now requires "
"brotli version >= 1.2.0. Please upgrade brotli version to make Scrapy "
"decode 'br' encoded responses.",
"brotli's or brotlicffi's version >= 1.2.0. Please upgrade "
"brotli/brotlicffi to make Scrapy decode 'br' encoded responses.",
)
else:
ACCEPTED_ENCODINGS.append(b"br")
@ -208,7 +211,7 @@ class HttpCompressionMiddleware:
f"from unsupported encoding(s) '{encodings_str}'."
)
if b"br" in encodings:
msg += " You need to install brotli >= 1.2.0 to decode 'br'."
msg += " You need to install brotli or brotlicffi >= 1.2.0 to decode 'br'."
if b"zstd" in encodings:
msg += " You need to install zstandard to decode 'zstd'."
logger.warning(msg)

View File

@ -3,7 +3,10 @@ import zlib
from io import BytesIO
with contextlib.suppress(ImportError):
import brotli
try:
import brotli
except ImportError:
import brotlicffi as brotli
with contextlib.suppress(ImportError):
import zstandard

View File

@ -52,9 +52,14 @@ FORMAT = {
def _skip_if_no_br() -> None:
try:
import brotli # noqa: PLC0415
try:
import brotli # noqa: PLC0415
brotli.Decompressor.can_accept_more_data
brotli.Decompressor.can_accept_more_data
except (ImportError, AttributeError):
import brotlicffi # noqa: PLC0415
brotlicffi.Decompressor.can_accept_more_data
except (ImportError, AttributeError):
pytest.skip("no brotli support")
@ -152,9 +157,14 @@ class TestHttpCompression:
def test_process_response_br_unsupported(self):
try:
import brotli # noqa: F401,PLC0415
try:
import brotli # noqa: F401,PLC0415
pytest.skip("Requires not having brotli support")
pytest.skip("Requires not having brotli support")
except ImportError:
import brotlicffi # noqa: F401,PLC0415
pytest.skip("Requires not having brotli support")
except ImportError:
pass
response = self._getresponse("br")
@ -171,9 +181,9 @@ class TestHttpCompression:
"scrapy.downloadermiddlewares.httpcompression",
"WARNING",
(
"HttpCompressionMiddleware cannot decode the response for"
" http://scrapytest.org/ from unsupported encoding(s) 'br'."
" You need to install brotli >= 1.2.0 to decode 'br'."
"HttpCompressionMiddleware cannot decode the response for "
"http://scrapytest.org/ from unsupported encoding(s) 'br'. "
"You need to install brotli or brotlicffi >= 1.2.0 to decode 'br'."
),
),
)

View File

@ -131,7 +131,8 @@ deps =
Twisted[http2]
boto3
bpython # optional for shell wrapper tests
brotli >= 1.2.0 # optional for HTTP compress downloader middleware tests
brotli >= 1.2.0; implementation_name != "pypy" # optional for HTTP compress downloader middleware tests
brotlicffi >= 1.2.0.0; implementation_name == "pypy" # optional for HTTP compress downloader middleware tests
google-cloud-storage
ipython
robotexclusionrulesparser
@ -145,7 +146,8 @@ deps =
Pillow==8.3.2
boto3==1.20.0
bpython==0.7.1
brotli==1.2.0
brotli==1.2.0; implementation_name != "pypy"
brotlicffi==1.2.0.0; implementation_name == "pypy"
google-cloud-storage==1.29.0
ipython==7.1.0
robotexclusionrulesparser==1.6.2