|
1 |
| -import array |
2 |
| -import ctypes |
3 |
| -import mmap |
4 | 1 | import os
|
5 |
| -import pickle |
6 | 2 | import platform
|
7 | 3 | import socket
|
8 | 4 | import ssl
|
9 | 5 | import typing
|
10 | 6 |
|
11 | 7 | import _ssl # type: ignore[import]
|
12 | 8 |
|
13 |
| -from ._ssl_constants import _original_SSLContext, _original_super_SSLContext |
| 9 | +from ._ssl_constants import ( |
| 10 | + _original_SSLContext, |
| 11 | + _original_super_SSLContext, |
| 12 | + _truststore_SSLContext_dunder_class, |
| 13 | + _truststore_SSLContext_super_class, |
| 14 | +) |
14 | 15 |
|
15 | 16 | if platform.system() == "Windows":
|
16 | 17 | from ._windows import _configure_context, _verify_peercerts_impl
|
|
19 | 20 | else:
|
20 | 21 | from ._openssl import _configure_context, _verify_peercerts_impl
|
21 | 22 |
|
| 23 | +if typing.TYPE_CHECKING: |
| 24 | + from pip._vendor.typing_extensions import Buffer |
| 25 | + |
22 | 26 | # From typeshed/stdlib/ssl.pyi
|
23 | 27 | _StrOrBytesPath: typing.TypeAlias = str | bytes | os.PathLike[str] | os.PathLike[bytes]
|
24 | 28 | _PasswordType: typing.TypeAlias = str | bytes | typing.Callable[[], str | bytes]
|
25 | 29 |
|
26 |
| -# From typeshed/stdlib/_typeshed/__init__.py |
27 |
| -_ReadableBuffer: typing.TypeAlias = typing.Union[ |
28 |
| - bytes, |
29 |
| - memoryview, |
30 |
| - bytearray, |
31 |
| - "array.array[typing.Any]", |
32 |
| - mmap.mmap, |
33 |
| - "ctypes._CData", |
34 |
| - pickle.PickleBuffer, |
35 |
| -] |
36 |
| - |
37 | 30 |
|
38 | 31 | def inject_into_ssl() -> None:
|
39 | 32 | """Injects the :class:`truststore.SSLContext` into the ``ssl``
|
@@ -61,9 +54,16 @@ def extract_from_ssl() -> None:
|
61 | 54 | pass
|
62 | 55 |
|
63 | 56 |
|
64 |
| -class SSLContext(ssl.SSLContext): |
| 57 | +class SSLContext(_truststore_SSLContext_super_class): # type: ignore[misc] |
65 | 58 | """SSLContext API that uses system certificates on all platforms"""
|
66 | 59 |
|
| 60 | + @property # type: ignore[misc] |
| 61 | + def __class__(self) -> type: |
| 62 | + # Dirty hack to get around isinstance() checks |
| 63 | + # for ssl.SSLContext instances in aiohttp/trustme |
| 64 | + # when using non-CPython implementations. |
| 65 | + return _truststore_SSLContext_dunder_class or SSLContext |
| 66 | + |
67 | 67 | def __init__(self, protocol: int = None) -> None: # type: ignore[assignment]
|
68 | 68 | self._ctx = _original_SSLContext(protocol)
|
69 | 69 |
|
@@ -129,7 +129,7 @@ def load_verify_locations(
|
129 | 129 | self,
|
130 | 130 | cafile: str | bytes | os.PathLike[str] | os.PathLike[bytes] | None = None,
|
131 | 131 | capath: str | bytes | os.PathLike[str] | os.PathLike[bytes] | None = None,
|
132 |
| - cadata: str | _ReadableBuffer | None = None, |
| 132 | + cadata: typing.Union[str, "Buffer", None] = None, |
133 | 133 | ) -> None:
|
134 | 134 | return self._ctx.load_verify_locations(
|
135 | 135 | cafile=cafile, capath=capath, cadata=cadata
|
@@ -252,7 +252,7 @@ def protocol(self) -> ssl._SSLMethod:
|
252 | 252 | return self._ctx.protocol
|
253 | 253 |
|
254 | 254 | @property
|
255 |
| - def security_level(self) -> int: # type: ignore[override] |
| 255 | + def security_level(self) -> int: |
256 | 256 | return self._ctx.security_level
|
257 | 257 |
|
258 | 258 | @property
|
|
0 commit comments