From 14bcf7004a143f7a6fb2df9a48196a113d531f50 Mon Sep 17 00:00:00 2001 From: Seth Michael Larson Date: Tue, 1 Dec 2020 20:09:32 -0600 Subject: [PATCH 1/2] Switch to Brotli C bindings for CPython, brotlicffi for non-CPython --- .github/workflows/ci.yml | 4 ---- docs/advanced-usage.rst | 3 ++- noxfile.py | 9 --------- setup.cfg | 3 ++- setup.py | 5 ++++- src/urllib3/response.py | 5 ++++- src/urllib3/util/request.py | 5 ++++- 7 files changed, 16 insertions(+), 18 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c0defd8eb4..d6555c368e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -51,10 +51,6 @@ jobs: os: ubuntu-latest experimental: false nox-session: test-pypy - - python-version: 3.7 - os: ubuntu-latest - experimental: false - nox-session: google_brotli - python-version: 2.7 os: ubuntu-latest experimental: false diff --git a/docs/advanced-usage.rst b/docs/advanced-usage.rst index 540fbe5996..acbc294ac2 100644 --- a/docs/advanced-usage.rst +++ b/docs/advanced-usage.rst @@ -334,7 +334,8 @@ Brotli Encoding Brotli is a compression algorithm created by Google with better compression than gzip and deflate and is supported by urllib3 if the -`brotlipy `_ package is installed. +`Brotli `_ package or +`brotlicffi `_ package is installed. You may also request the package be installed via the ``urllib3[brotli]`` extra: .. code-block:: bash diff --git a/noxfile.py b/noxfile.py index ee2f623292..ba0ec52ccf 100644 --- a/noxfile.py +++ b/noxfile.py @@ -83,15 +83,6 @@ def unsupported_python2(session): assert "Unsupported Python version" in process.stderr -@nox.session(python=["3"]) -def google_brotli(session): - # https://pypi.org/project/Brotli/ is the Google version of brotli, so - # install it separately and don't install our brotli extra (which installs - # brotlipy). - session.install("brotli") - tests_impl(session, extras="socks,secure") - - @nox.session() def format(session): """Run code formatters.""" diff --git a/setup.cfg b/setup.cfg index 90b79d8c5d..31a465b580 100644 --- a/setup.cfg +++ b/setup.cfg @@ -15,7 +15,8 @@ requires-dist = idna>=2.0.0; extra == 'secure' certifi; extra == 'secure' PySocks>=1.5.6,<2.0,!=1.5.7; extra == 'socks' - brotlipy>=0.6.0; extra == 'brotli' + brotli>=1.0.9; platform_python_implementation == 'CPython' and extra == 'brotli' + brotlicffi>=0.8.0; platform_python_implementation != 'CPython' and extra == 'brotli' [tool:pytest] xfail_strict = true diff --git a/setup.py b/setup.py index 2d449b0950..02071aa586 100755 --- a/setup.py +++ b/setup.py @@ -109,7 +109,10 @@ requires=[], python_requires=">=3.6, <4", extras_require={ - "brotli": ["brotlipy>=0.6.0"], + "brotli": [ + "brotli>=1.0.9; platform_python_implementation == 'CPython'", + "brotlicffi>=0.8.0; platform_python_implementation != 'CPython'", + ], "secure": [ "pyOpenSSL>=0.14", "cryptography>=1.3.4", diff --git a/src/urllib3/response.py b/src/urllib3/response.py index 77b6ffc16d..d277120ee6 100644 --- a/src/urllib3/response.py +++ b/src/urllib3/response.py @@ -7,7 +7,10 @@ from socket import timeout as SocketTimeout try: - import brotli + try: + import brotlicffi as brotli + except ImportError: + import brotli except ImportError: brotli = None diff --git a/src/urllib3/util/request.py b/src/urllib3/util/request.py index 6cbdad9b4c..64d40641df 100644 --- a/src/urllib3/util/request.py +++ b/src/urllib3/util/request.py @@ -11,7 +11,10 @@ ACCEPT_ENCODING = "gzip,deflate" try: - import brotli as _unused_module_brotli # noqa: F401 + try: + import brotlicffi as _unused_module_brotli # noqa: F401 + except ImportError: + import brotli as _unused_module_brotli # noqa: F401 except ImportError: pass else: From 0ec82d02f117bf054b3361c15cbff526390d6b86 Mon Sep 17 00:00:00 2001 From: Seth Michael Larson Date: Thu, 3 Dec 2020 21:48:04 -0600 Subject: [PATCH 2/2] Fix brotli make_headers() test on PyPy --- test/__init__.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/__init__.py b/test/__init__.py index 0512aac0fa..a75cbdc9f8 100644 --- a/test/__init__.py +++ b/test/__init__.py @@ -9,7 +9,10 @@ import pytest try: - import brotli + try: + import brotlicffi as brotli + except ImportError: + import brotli except ImportError: brotli = None