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 e3a5de9
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 6 deletions.
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 `brotlicffi <https://pypi.org/project/brotlicffi/>`_
or `Brotli <https://pypi.org/project/Brotli/>`_.

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:`brotlicffi` or :term:`Brotli` 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 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 e3a5de9

Please sign in to comment.