Update brotli support to use the brotlicffi package (#1605)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Update brotli support to use the brotlicffi package
authored by Tom Christie · committed by GitHub · parent 0c2cb24 · Browse files · 7 files·+20 added−23 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+20 added−23 removed
- tests:+7 added−7 removed
- other:+13 added−16 removed
Provenance
- Commit
760af43- Tree
c9c0a78- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
0c2cb24
7 files changed+20 added−23 removed
modified:docs/index.md+1 added-1 removed
113 unmodified linesExpand all11411511611711811811912012112224 unmodified lines113 unmodified linesExpand all* `rfc3986` - URL parsing & normalization. * `idna` - Internationalized domain name support.* `sniffio` - Async library autodetection.* `async_generator` - Backport support for `contextlib.asynccontextmanager`. *(Only required for Python 3.6)** `brotlipy` - Decoding for "brotli" compressed responses. *(Optional)** `brotlicffi` - Decoding for "brotli" compressed responses. *(Optional)*
A huge amount of credit is due to `requests` for the API layout thatmuch of this work follows, as well as to `urllib3` for plenty of designinspiration around the lower-level networking details.24 unmodified linesmodified:httpx/_decoders.py+9 added-9 removed
9 unmodified lines10 11 from ._exceptions import DecodingError12 13 try:14- import brotli14+ import brotlicffi15 except ImportError: # pragma: nocover16- brotli = None16+ brotlicffi = None17 18 19 class ContentDecoder:20 def decode(self, data: bytes) -> bytes:77 unmodified lines98 name. The top branches are for 'brotlipy' and bottom branches for 'Brotli'99 """100 101 def __init__(self) -> None:102- if brotli is None: # pragma: nocover102+ if brotlicffi is None: # pragma: nocover103 raise ImportError(104- "Using 'BrotliDecoder', but the 'brotlipy' or 'brotli' library "104+ "Using 'BrotliDecoder', but the 'brotlicffi' library "105 "is not installed."106 "Make sure to install httpx using `pip install httpx[brotli]`."107 ) from None108 109- self.decompressor = brotli.Decompressor()109+ self.decompressor = brotlicffi.Decompressor()110 self.seen_data = False111 if hasattr(self.decompressor, "decompress"):112 self._decompress = self.decompressor.decompress113 else:3 unmodified lines117 if not data:118 return b""119 self.seen_data = True120 try:121- return self._decompress(data)122- except brotli.error as exc:121+ return self.decompressor.decompress(data)122+ except brotlicffi.Error as exc:123 raise DecodingError(str(exc)) from exc124 125 def flush(self) -> bytes:126 if not self.seen_data:1 unmodified line128 try:129 if hasattr(self.decompressor, "finish"):130 self.decompressor.finish()131 return b""132- except brotli.error as exc: # pragma: nocover132+ except brotlicffi.Error as exc: # pragma: nocover133 raise DecodingError(str(exc)) from exc134 135 136 class MultiDecoder(ContentDecoder):227 unmodified lines364 "br": BrotliDecoder,365 }366 367 368-if brotli is None:368+if brotlicffi is None:369 SUPPORTED_DECODERS.pop("br") # pragma: nocovermodified:tests/models/test_responses.py+3 added-3 removed
1 import json2 import pickle3 from unittest import mock4 5-import brotli5+import brotlicffi6 import pytest7 8 import httpx9 777 unmodified lines787 @pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))788 def test_decode_error_with_request(header_value):789 headers = [(b"Content-Encoding", header_value)]790 body = b"test 123"791- compressed_body = brotli.compress(body)[3:]791+ compressed_body = brotlicffi.compress(body)[3:]792 with pytest.raises(httpx.DecodingError):793 httpx.Response(794 200,795 headers=headers,12 unmodified lines808 @pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))809 def test_value_error_without_request(header_value):810 headers = [(b"Content-Encoding", header_value)]811 body = b"test 123"812- compressed_body = brotli.compress(body)[3:]812+ compressed_body = brotlicffi.compress(body)[3:]813 with pytest.raises(httpx.DecodingError):814 httpx.Response(200, headers=headers, content=compressed_body)815 816 78 unmodified linesmodified:tests/test_decoders.py+4 added-4 removed
1 import zlib2 3-import brotli3+import brotlicffi4 import pytest5 6 import httpx7 from httpx._decoders import (60 unmodified lines68 69 70 def test_brotli():71 body = b"test 123"72- compressed_body = brotli.compress(body)72+ compressed_body = brotlicffi.compress(body)73 74 headers = [(b"Content-Encoding", b"br")]75 response = httpx.Response(76 200,24 unmodified lines101 102 103 def test_multi_with_identity():104 body = b"test 123"105- compressed_body = brotli.compress(body)105+ compressed_body = brotlicffi.compress(body)106 107 headers = [(b"Content-Encoding", b"br, identity")]108 response = httpx.Response(109 200,54 unmodified lines164 @pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))165 def test_decoding_errors(header_value):166 headers = [(b"Content-Encoding", header_value)]167 body = b"test 123"168- compressed_body = brotli.compress(body)[3:]168+ compressed_body = brotlicffi.compress(body)[3:]169 with pytest.raises(httpx.DecodingError):170 request = httpx.Request("GET", "https://example.org")171 httpx.Response(200, headers=headers, content=compressed_body, request=request)172 186 unmodified linesmodified:README.md+1 added-1 removed
121 unmodified lines122 * `rfc3986` - URL parsing & normalization.123 * `idna` - Internationalized domain name support.124 * `sniffio` - Async library autodetection.125 * `async_generator` - Backport support for `contextlib.asynccontextmanager`. *(Only required for Python 3.6)*126-* `brotlipy` - Decoding for "brotli" compressed responses. *(Optional)*126+* `brotlicffi` - Decoding for "brotli" compressed responses. *(Optional)*127 128 A huge amount of credit is due to `requests` for the API layout that129 much of this work follows, as well as to `urllib3` for plenty of design130 inspiration around the lower-level networking details.3 unmodified linesmodified:requirements.txt+1 added-4 removed
1--e .[http2]1+-e .[http2,brotli]2 3-# Optional4-brotlipy==0.7.*5-3 # Documentation4 mkdocs5 mkautodoc6 mkdocs-material24 unmodified linesmodified:setup.py+1 added-1 removed
62 unmodified lines63 "async_generator; python_version < '3.7'"64 ],65 extras_require={66 "http2": "h2==3.*",67- "brotli": "brotlipy==0.7.*",67+ "brotli": "brotlicffi==1.*",68 },69 classifiers=[70 "Development Status :: 4 - Beta",71 "Environment :: Web Environment",14 unmodified lines