Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check for correct h2 version range #3402

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog/3290.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Added version checking for ``h2`` (https://pypi.org/project/h2/) usage.

Now only accepting supported h2 major version 4.x.x.
15 changes: 15 additions & 0 deletions src/urllib3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@
"See: https://github.com/urllib3/urllib3/issues/2168"
)

# Ensure that Python is compiled with h2>=4.0.0 and <5.0.0
# If the 'h2' module isn't installed at all that's
# fine, we only care if the module is installed.
try:
import h2 # type: ignore[import-untyped]
except ImportError:
pass
else:
if not (h2.__version__ >= "4.0.0" and h2.__version__ < "5.0.0"):
raise ImportError(
"urllib3 v2 supports h2 version 4.x.x, currently "
f"the 'h2' module is compiled with {h2.__version__!r}. "
"See: https://github.com/urllib3/urllib3/issues/3290"
)
Comment on lines +48 to +61
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the HTTP/2 support is rudimentary and it has to be enabled explicitly by calling urllib3.http2.inject_into_urllib3, I believe we shouldn't raise the error at the top level. This can be breaking needlessly for environments that have both urllib3 and h2 installed but never call inject_into_urllib3.


__author__ = "Andrey Petrov (andrey.petrov@shazow.net)"
__license__ = "MIT"
__version__ = __version__
Expand Down
29 changes: 29 additions & 0 deletions test/test_compatibility.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
from __future__ import annotations

import http.cookiejar
import importlib
import sys
import urllib
from unittest import mock

import pytest

from urllib3.response import HTTPResponse

Expand All @@ -20,3 +25,27 @@ def test_extract(self) -> None:
response.headers.add("set-cookie", c)
cookiejar.extract_cookies(response, request) # type: ignore[arg-type]
assert len(cookiejar) == len(cookies)


class TestInitialization:
def test_h2_version_check(self) -> None:
with mock.patch.dict("sys.modules", {}):
importlib.reload(sys.modules["urllib3"])

mock_h2 = mock.Mock()

mock_h2.__version__ = "4.1.0"
with mock.patch.dict("sys.modules", {"h2": mock_h2}):
importlib.reload(sys.modules["urllib3"])

mock_h2.__version__ = "3.9.9"
with mock.patch.dict("sys.modules", {"h2": mock_h2}):
with pytest.raises(ImportError) as excinfo:
importlib.reload(sys.modules["urllib3"])
assert "h2 version 4.x.x" in str(excinfo.value)

mock_h2.__version__ = "5.0.0"
with mock.patch.dict("sys.modules", {"h2": mock_h2}):
with pytest.raises(ImportError) as excinfo:
importlib.reload(sys.modules["urllib3"])
assert "h2 version 4.x.x" in str(excinfo.value)
Loading