Skip to content

Commit

Permalink
Raise an ImportError instead of OSError for unsupported macOS versions
Browse files Browse the repository at this point in the history
  • Loading branch information
sethmlarson committed Apr 5, 2023
1 parent 4fdd327 commit 1c8e93b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
11 changes: 11 additions & 0 deletions docs/source/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/truststore/_macos.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]}"
)

Expand Down
18 changes: 18 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import asyncio
import importlib
import os
import platform
import socket
import ssl
import tempfile
from dataclasses import dataclass
from operator import attrgetter
from unittest import mock

import aiohttp
import aiohttp.client_exceptions
Expand Down Expand Up @@ -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"

0 comments on commit 1c8e93b

Please sign in to comment.