Skip to content

Commit

Permalink
Support brotlicffi alternatively to brotli
Browse files Browse the repository at this point in the history
Support the brotlicffi implementation as an alternative to brotli.
This is necessary since the current version of brotli (as of 1.1.0)
does not work on PyPy.  This uses the approach of preferring brotlicffi
over brotli in imports, and listing brotli or brotlicffi depending
on the Python implementation in dependencies, as recommended
in brotlicffi documentation:
https://pypi.org/project/brotlicffi/1.1.0.0/#using-brotlicffi-in-projects
  • Loading branch information
mgorny committed Sep 16, 2023
1 parent b6afdc3 commit 3d05a35
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ ignore_missing_imports = True
[mypy-brotli]
ignore_missing_imports = True

[mypy-brotlicffi]
ignore_missing_imports = True

[mypy-gunicorn.*]
ignore_missing_imports = True

Expand Down
1 change: 1 addition & 0 deletions CHANGES/7611.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support using ``brotlicffi`` Python package alternatively to ``brotli``, as the latter does not work on PyPy.
5 changes: 4 additions & 1 deletion aiohttp/compression_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
from typing import Optional, cast

try:
import brotli
try:
import brotlicffi as brotli
except ImportError:
import brotli

HAS_BROTLI = True
except ImportError: # pragma: no cover
Expand Down
3 changes: 2 additions & 1 deletion docs/client_quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ The ``gzip`` and ``deflate`` transfer-encodings are automatically
decoded for you.

You can enable ``brotli`` transfer-encodings support,
just install `Brotli <https://pypi.org/project/Brotli>`_.
just install `Brotli <https://pypi.org/project/Brotli/>`_
or `brotlicffi <https://pypi.org/project/brotlicffi/>`_.

JSON Request
============
Expand Down
3 changes: 2 additions & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ Dependencies
$ pip install aiodns
- *Optional* :term:`Brotli` for brotli (:rfc:`7932`) client compression support.
- *Optional* :term:`Brotli` or :term:`brotlicffi` for brotli (:rfc:`7932`)
client compression support.

.. code-block:: bash
Expand Down
1 change: 1 addition & 0 deletions docs/spelling_wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ boolean
botocore
brotli
Brotli
brotlicffi
brotlipy
bugfix
Bugfixes
Expand Down
3 changes: 2 additions & 1 deletion requirements/runtime-deps.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ yarl >= 1.0, < 2.0
frozenlist >= 1.1.1
aiosignal >= 1.1.2
aiodns >= 1.1; sys_platform=="linux" or sys_platform=="darwin"
Brotli
Brotli; platform_python_implementation == 'CPython'
brotlicffi; platform_python_implementation != 'CPython'
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ install_requires =
speedups =
# required c-ares (aiodns' backend) will not build on windows
aiodns >= 1.1; sys_platform=="linux" or sys_platform=="darwin"
Brotli
Brotli; platform_python_implementation == 'CPython'
brotlicffi; platform_python_implementation != 'CPython'

[options.packages.find]
exclude =
Expand Down
5 changes: 4 additions & 1 deletion tests/test_http_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
)

try:
import brotli
try:
import brotlicffi as brotli
except ImportError:
import brotli
except ImportError:
brotli = None

Expand Down
6 changes: 5 additions & 1 deletion tests/test_web_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from typing import Any, Optional
from unittest import mock

import brotli
import pytest
from multidict import CIMultiDictProxy, MultiDict
from yarl import URL
Expand All @@ -19,6 +18,11 @@
from aiohttp.test_utils import make_mocked_coro
from aiohttp.typedefs import Handler

try:
import brotlicffi as brotli
except ImportError:
import brotli

try:
import ssl
except ImportError:
Expand Down

0 comments on commit 3d05a35

Please sign in to comment.