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

Raise an ImportError instead of OSError for unsupported macOS versions #100

Merged
merged 1 commit into from
Apr 5, 2023
Merged
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
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"