Skip to content

Commit

Permalink
Support brotlicffi alternatively to brotli (#7611)
Browse files Browse the repository at this point in the history
## What do these changes do?

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 18, 2023
1 parent b51d5d7 commit b8a3b0b
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .mypy.ini
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
@@ -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
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
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
7 changes: 7 additions & 0 deletions docs/glossary.rst
Expand Up @@ -40,6 +40,13 @@

https://pypi.org/project/Brotli/

brotlicffi

An alternative implementation of :term:`Brotli` built using the CFFI
library. This implementation supports PyPy correctly.

https://pypi.org/project/brotlicffi/

callable

Any object that can be called. Use :func:`callable` to check
Expand Down
3 changes: 2 additions & 1 deletion docs/index.rst
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
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
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
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
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
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 b8a3b0b

Please sign in to comment.