diff --git a/docs/source/index.md b/docs/source/index.md index 819ff0f..98b433d 100644 --- a/docs/source/index.md +++ b/docs/source/index.md @@ -76,6 +76,17 @@ http = urllib3.PoolManager(ssl_context=ctx) resp = http.request("GET", "https://example.com") ``` +If Truststore can't work for a given platform due to APIs not being available then +at import time the exception `ImportError` will be raised with an informative message: + +```python +# On Python 3.9 and earlier: +import truststore # Raises 'ImportError' + +# On macOS 10.7 and earlier: +import truststore # Raises 'ImportError' +``` + ### Using truststore with pip [Pip v22.2](https://discuss.python.org/t/announcement-pip-22-2-release/17543) includes experimental support for verifying certificates with system trust stores using `truststore`. To enable the feature, use the flag `--use-feature=truststore` when installing a package like so: diff --git a/src/truststore/_macos.py b/src/truststore/_macos.py index e78c0af..7dc440b 100644 --- a/src/truststore/_macos.py +++ b/src/truststore/_macos.py @@ -21,7 +21,7 @@ _mac_version = platform.mac_ver()[0] _mac_version_info = tuple(map(int, _mac_version.split("."))) if _mac_version_info < (10, 8): - raise OSError( + raise ImportError( f"Only OS X 10.8 and newer are supported, not {_mac_version_info[0]}.{_mac_version_info[1]}" ) diff --git a/tests/test_api.py b/tests/test_api.py index 588d7bb..c62903d 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -1,4 +1,5 @@ import asyncio +import importlib import os import platform import socket @@ -6,6 +7,7 @@ import tempfile from dataclasses import dataclass from operator import attrgetter +from unittest import mock import aiohttp import aiohttp.client_exceptions @@ -324,3 +326,19 @@ def test_trustme_cert_still_uses_system_certs(trustme_ca): resp = http.request("GET", "https://example.com") assert resp.status == 200 assert len(resp.data) > 0 + + +def test_macos_10_7_import_error(): + with mock.patch("platform.mac_ver") as mac_ver: + # This isn't the full structure, but the version is the first element. + mac_ver.return_value = ("10.7",) + + with pytest.raises(ImportError) as e: + # We want to force a fresh import, so either we get it on the + # first try because the OS isn't macOS or we get it on + # the call to importlib.reload(...). + import truststore._macos + + importlib.reload(truststore._macos) + + assert str(e.value) == "Only OS X 10.8 and newer are supported, not 10.7"