Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .gitattributes +1 -0
- evalkit_llava/lib/python3.10/site-packages/pip/__pycache__/__main__.cpython-310.pyc +0 -0
- evalkit_llava/lib/python3.10/site-packages/pip/_vendor/distlib/t64-arm.exe +3 -0
- evalkit_llava/lib/python3.10/site-packages/pip/_vendor/truststore/__init__.py +36 -0
- evalkit_llava/lib/python3.10/site-packages/pip/_vendor/truststore/__pycache__/__init__.cpython-310.pyc +0 -0
- evalkit_llava/lib/python3.10/site-packages/pip/_vendor/truststore/__pycache__/_macos.cpython-310.pyc +0 -0
- evalkit_llava/lib/python3.10/site-packages/pip/_vendor/truststore/__pycache__/_openssl.cpython-310.pyc +0 -0
- evalkit_llava/lib/python3.10/site-packages/pip/_vendor/truststore/__pycache__/_ssl_constants.cpython-310.pyc +0 -0
- evalkit_llava/lib/python3.10/site-packages/pip/_vendor/truststore/_openssl.py +66 -0
- evalkit_llava/lib/python3.10/site-packages/pip/_vendor/truststore/_ssl_constants.py +31 -0
- evalkit_llava/lib/python3.10/site-packages/pip/_vendor/truststore/_windows.py +567 -0
- evalkit_llava/lib/python3.10/site-packages/pip/_vendor/truststore/py.typed +0 -0
- evalkit_llava/lib/python3.10/site-packages/pip/_vendor/urllib3/contrib/__pycache__/__init__.cpython-310.pyc +0 -0
- evalkit_llava/lib/python3.10/site-packages/pip/_vendor/urllib3/contrib/__pycache__/socks.cpython-310.pyc +0 -0
- evalkit_llava/lib/python3.10/site-packages/pip/_vendor/urllib3/contrib/_securetransport/__init__.py +0 -0
- evalkit_llava/lib/python3.10/site-packages/pip/_vendor/urllib3/contrib/_securetransport/__pycache__/__init__.cpython-310.pyc +0 -0
- evalkit_llava/lib/python3.10/site-packages/pip/_vendor/urllib3/contrib/_securetransport/__pycache__/bindings.cpython-310.pyc +0 -0
- evalkit_llava/lib/python3.10/site-packages/pip/_vendor/urllib3/contrib/_securetransport/__pycache__/low_level.cpython-310.pyc +0 -0
- evalkit_llava/lib/python3.10/site-packages/pip/_vendor/urllib3/contrib/_securetransport/bindings.py +519 -0
- evalkit_llava/lib/python3.10/site-packages/pip/_vendor/urllib3/contrib/_securetransport/low_level.py +397 -0
- evalkit_llava/lib/python3.10/site-packages/pip/_vendor/urllib3/util/ssl_.py +504 -0
- evalkit_llava/lib/python3.10/site-packages/pip/_vendor/urllib3/util/ssl_match_hostname.py +159 -0
- evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/__pycache__/_msvccompiler.cpython-310.pyc +0 -0
- evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/__pycache__/cmd.cpython-310.pyc +0 -0
- evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/__pycache__/debug.cpython-310.pyc +0 -0
- evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/__pycache__/dep_util.cpython-310.pyc +0 -0
- evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/__pycache__/extension.cpython-310.pyc +0 -0
- evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/__pycache__/unixccompiler.cpython-310.pyc +0 -0
- evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/__pycache__/test_bdist_dumb.cpython-310.pyc +0 -0
- evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/__pycache__/test_build_clib.cpython-310.pyc +0 -0
- evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/__pycache__/test_build_ext.cpython-310.pyc +0 -0
- evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/__pycache__/test_build_scripts.cpython-310.pyc +0 -0
- evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/__pycache__/test_ccompiler.cpython-310.pyc +0 -0
- evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/__pycache__/test_dir_util.cpython-310.pyc +0 -0
- evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/__pycache__/test_install_data.cpython-310.pyc +0 -0
- evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/__pycache__/test_install_headers.cpython-310.pyc +0 -0
- evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/__pycache__/test_install_scripts.cpython-310.pyc +0 -0
- evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/__pycache__/test_mingwccompiler.cpython-310.pyc +0 -0
- evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/__pycache__/test_msvccompiler.cpython-310.pyc +0 -0
- evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/compat/__pycache__/__init__.cpython-310.pyc +0 -0
- evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/compat/__pycache__/py39.cpython-310.pyc +0 -0
- evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/compat/py39.py +40 -0
- evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/support.py +134 -0
- evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/test_archive_util.py +353 -0
- evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/test_bdist.py +47 -0
- evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/test_bdist_dumb.py +78 -0
- evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/test_bdist_rpm.py +127 -0
- evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/test_build.py +49 -0
- evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/test_build_clib.py +134 -0
- evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/test_build_ext.py +560 -0
.gitattributes
CHANGED
|
@@ -85,3 +85,4 @@ evalkit_llava/lib/python3.10/site-packages/pip/_vendor/distlib/w64-arm.exe filte
|
|
| 85 |
evalkit_llava/lib/libz.so.1.2.13 filter=lfs diff=lfs merge=lfs -text
|
| 86 |
evalkit_llava/lib/python3.10/site-packages/pip/_vendor/idna/__pycache__/uts46data.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 87 |
evalkit_llava/lib/python3.10/site-packages/pip/_vendor/idna/__pycache__/idnadata.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 85 |
evalkit_llava/lib/libz.so.1.2.13 filter=lfs diff=lfs merge=lfs -text
|
| 86 |
evalkit_llava/lib/python3.10/site-packages/pip/_vendor/idna/__pycache__/uts46data.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 87 |
evalkit_llava/lib/python3.10/site-packages/pip/_vendor/idna/__pycache__/idnadata.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 88 |
+
evalkit_llava/lib/python3.10/site-packages/pip/_vendor/distlib/t64-arm.exe filter=lfs diff=lfs merge=lfs -text
|
evalkit_llava/lib/python3.10/site-packages/pip/__pycache__/__main__.cpython-310.pyc
ADDED
|
Binary file (452 Bytes). View file
|
|
|
evalkit_llava/lib/python3.10/site-packages/pip/_vendor/distlib/t64-arm.exe
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ebc4c06b7d95e74e315419ee7e88e1d0f71e9e9477538c00a93a9ff8c66a6cfc
|
| 3 |
+
size 182784
|
evalkit_llava/lib/python3.10/site-packages/pip/_vendor/truststore/__init__.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Verify certificates using native system trust stores"""
|
| 2 |
+
|
| 3 |
+
import sys as _sys
|
| 4 |
+
|
| 5 |
+
if _sys.version_info < (3, 10):
|
| 6 |
+
raise ImportError("truststore requires Python 3.10 or later")
|
| 7 |
+
|
| 8 |
+
# Detect Python runtimes which don't implement SSLObject.get_unverified_chain() API
|
| 9 |
+
# This API only became public in Python 3.13 but was available in CPython and PyPy since 3.10.
|
| 10 |
+
if _sys.version_info < (3, 13):
|
| 11 |
+
try:
|
| 12 |
+
import ssl as _ssl
|
| 13 |
+
except ImportError:
|
| 14 |
+
raise ImportError("truststore requires the 'ssl' module")
|
| 15 |
+
else:
|
| 16 |
+
_sslmem = _ssl.MemoryBIO()
|
| 17 |
+
_sslobj = _ssl.create_default_context().wrap_bio(
|
| 18 |
+
_sslmem,
|
| 19 |
+
_sslmem,
|
| 20 |
+
)
|
| 21 |
+
try:
|
| 22 |
+
while not hasattr(_sslobj, "get_unverified_chain"):
|
| 23 |
+
_sslobj = _sslobj._sslobj # type: ignore[attr-defined]
|
| 24 |
+
except AttributeError:
|
| 25 |
+
raise ImportError(
|
| 26 |
+
"truststore requires peer certificate chain APIs to be available"
|
| 27 |
+
) from None
|
| 28 |
+
|
| 29 |
+
del _ssl, _sslobj, _sslmem # noqa: F821
|
| 30 |
+
|
| 31 |
+
from ._api import SSLContext, extract_from_ssl, inject_into_ssl # noqa: E402
|
| 32 |
+
|
| 33 |
+
del _api, _sys # type: ignore[name-defined] # noqa: F821
|
| 34 |
+
|
| 35 |
+
__all__ = ["SSLContext", "inject_into_ssl", "extract_from_ssl"]
|
| 36 |
+
__version__ = "0.10.0"
|
evalkit_llava/lib/python3.10/site-packages/pip/_vendor/truststore/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (956 Bytes). View file
|
|
|
evalkit_llava/lib/python3.10/site-packages/pip/_vendor/truststore/__pycache__/_macos.cpython-310.pyc
ADDED
|
Binary file (10.1 kB). View file
|
|
|
evalkit_llava/lib/python3.10/site-packages/pip/_vendor/truststore/__pycache__/_openssl.cpython-310.pyc
ADDED
|
Binary file (1.47 kB). View file
|
|
|
evalkit_llava/lib/python3.10/site-packages/pip/_vendor/truststore/__pycache__/_ssl_constants.cpython-310.pyc
ADDED
|
Binary file (783 Bytes). View file
|
|
|
evalkit_llava/lib/python3.10/site-packages/pip/_vendor/truststore/_openssl.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import contextlib
|
| 2 |
+
import os
|
| 3 |
+
import re
|
| 4 |
+
import ssl
|
| 5 |
+
import typing
|
| 6 |
+
|
| 7 |
+
# candidates based on https://github.com/tiran/certifi-system-store by Christian Heimes
|
| 8 |
+
_CA_FILE_CANDIDATES = [
|
| 9 |
+
# Alpine, Arch, Fedora 34+, OpenWRT, RHEL 9+, BSD
|
| 10 |
+
"/etc/ssl/cert.pem",
|
| 11 |
+
# Fedora <= 34, RHEL <= 9, CentOS <= 9
|
| 12 |
+
"/etc/pki/tls/cert.pem",
|
| 13 |
+
# Debian, Ubuntu (requires ca-certificates)
|
| 14 |
+
"/etc/ssl/certs/ca-certificates.crt",
|
| 15 |
+
# SUSE
|
| 16 |
+
"/etc/ssl/ca-bundle.pem",
|
| 17 |
+
]
|
| 18 |
+
|
| 19 |
+
_HASHED_CERT_FILENAME_RE = re.compile(r"^[0-9a-fA-F]{8}\.[0-9]$")
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
@contextlib.contextmanager
|
| 23 |
+
def _configure_context(ctx: ssl.SSLContext) -> typing.Iterator[None]:
|
| 24 |
+
# First, check whether the default locations from OpenSSL
|
| 25 |
+
# seem like they will give us a usable set of CA certs.
|
| 26 |
+
# ssl.get_default_verify_paths already takes care of:
|
| 27 |
+
# - getting cafile from either the SSL_CERT_FILE env var
|
| 28 |
+
# or the path configured when OpenSSL was compiled,
|
| 29 |
+
# and verifying that that path exists
|
| 30 |
+
# - getting capath from either the SSL_CERT_DIR env var
|
| 31 |
+
# or the path configured when OpenSSL was compiled,
|
| 32 |
+
# and verifying that that path exists
|
| 33 |
+
# In addition we'll check whether capath appears to contain certs.
|
| 34 |
+
defaults = ssl.get_default_verify_paths()
|
| 35 |
+
if defaults.cafile or (defaults.capath and _capath_contains_certs(defaults.capath)):
|
| 36 |
+
ctx.set_default_verify_paths()
|
| 37 |
+
else:
|
| 38 |
+
# cafile from OpenSSL doesn't exist
|
| 39 |
+
# and capath from OpenSSL doesn't contain certs.
|
| 40 |
+
# Let's search other common locations instead.
|
| 41 |
+
for cafile in _CA_FILE_CANDIDATES:
|
| 42 |
+
if os.path.isfile(cafile):
|
| 43 |
+
ctx.load_verify_locations(cafile=cafile)
|
| 44 |
+
break
|
| 45 |
+
|
| 46 |
+
yield
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def _capath_contains_certs(capath: str) -> bool:
|
| 50 |
+
"""Check whether capath exists and contains certs in the expected format."""
|
| 51 |
+
if not os.path.isdir(capath):
|
| 52 |
+
return False
|
| 53 |
+
for name in os.listdir(capath):
|
| 54 |
+
if _HASHED_CERT_FILENAME_RE.match(name):
|
| 55 |
+
return True
|
| 56 |
+
return False
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def _verify_peercerts_impl(
|
| 60 |
+
ssl_context: ssl.SSLContext,
|
| 61 |
+
cert_chain: list[bytes],
|
| 62 |
+
server_hostname: str | None = None,
|
| 63 |
+
) -> None:
|
| 64 |
+
# This is a no-op because we've enabled SSLContext's built-in
|
| 65 |
+
# verification via verify_mode=CERT_REQUIRED, and don't need to repeat it.
|
| 66 |
+
pass
|
evalkit_llava/lib/python3.10/site-packages/pip/_vendor/truststore/_ssl_constants.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import ssl
|
| 2 |
+
import sys
|
| 3 |
+
import typing
|
| 4 |
+
|
| 5 |
+
# Hold on to the original class so we can create it consistently
|
| 6 |
+
# even if we inject our own SSLContext into the ssl module.
|
| 7 |
+
_original_SSLContext = ssl.SSLContext
|
| 8 |
+
_original_super_SSLContext = super(_original_SSLContext, _original_SSLContext)
|
| 9 |
+
|
| 10 |
+
# CPython is known to be good, but non-CPython implementations
|
| 11 |
+
# may implement SSLContext differently so to be safe we don't
|
| 12 |
+
# subclass the SSLContext.
|
| 13 |
+
|
| 14 |
+
# This is returned by truststore.SSLContext.__class__()
|
| 15 |
+
_truststore_SSLContext_dunder_class: typing.Optional[type]
|
| 16 |
+
|
| 17 |
+
# This value is the superclass of truststore.SSLContext.
|
| 18 |
+
_truststore_SSLContext_super_class: type
|
| 19 |
+
|
| 20 |
+
if sys.implementation.name == "cpython":
|
| 21 |
+
_truststore_SSLContext_super_class = _original_SSLContext
|
| 22 |
+
_truststore_SSLContext_dunder_class = None
|
| 23 |
+
else:
|
| 24 |
+
_truststore_SSLContext_super_class = object
|
| 25 |
+
_truststore_SSLContext_dunder_class = _original_SSLContext
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def _set_ssl_context_verify_mode(
|
| 29 |
+
ssl_context: ssl.SSLContext, verify_mode: ssl.VerifyMode
|
| 30 |
+
) -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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import contextlib
|
| 2 |
+
import ssl
|
| 3 |
+
import typing
|
| 4 |
+
from ctypes import WinDLL # type: ignore
|
| 5 |
+
from ctypes import WinError # type: ignore
|
| 6 |
+
from ctypes import (
|
| 7 |
+
POINTER,
|
| 8 |
+
Structure,
|
| 9 |
+
c_char_p,
|
| 10 |
+
c_ulong,
|
| 11 |
+
c_void_p,
|
| 12 |
+
c_wchar_p,
|
| 13 |
+
cast,
|
| 14 |
+
create_unicode_buffer,
|
| 15 |
+
pointer,
|
| 16 |
+
sizeof,
|
| 17 |
+
)
|
| 18 |
+
from ctypes.wintypes import (
|
| 19 |
+
BOOL,
|
| 20 |
+
DWORD,
|
| 21 |
+
HANDLE,
|
| 22 |
+
LONG,
|
| 23 |
+
LPCSTR,
|
| 24 |
+
LPCVOID,
|
| 25 |
+
LPCWSTR,
|
| 26 |
+
LPFILETIME,
|
| 27 |
+
LPSTR,
|
| 28 |
+
LPWSTR,
|
| 29 |
+
)
|
| 30 |
+
from typing import TYPE_CHECKING, Any
|
| 31 |
+
|
| 32 |
+
from ._ssl_constants import _set_ssl_context_verify_mode
|
| 33 |
+
|
| 34 |
+
HCERTCHAINENGINE = HANDLE
|
| 35 |
+
HCERTSTORE = HANDLE
|
| 36 |
+
HCRYPTPROV_LEGACY = HANDLE
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
class CERT_CONTEXT(Structure):
|
| 40 |
+
_fields_ = (
|
| 41 |
+
("dwCertEncodingType", DWORD),
|
| 42 |
+
("pbCertEncoded", c_void_p),
|
| 43 |
+
("cbCertEncoded", DWORD),
|
| 44 |
+
("pCertInfo", c_void_p),
|
| 45 |
+
("hCertStore", HCERTSTORE),
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
PCERT_CONTEXT = POINTER(CERT_CONTEXT)
|
| 50 |
+
PCCERT_CONTEXT = POINTER(PCERT_CONTEXT)
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
class CERT_ENHKEY_USAGE(Structure):
|
| 54 |
+
_fields_ = (
|
| 55 |
+
("cUsageIdentifier", DWORD),
|
| 56 |
+
("rgpszUsageIdentifier", POINTER(LPSTR)),
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
PCERT_ENHKEY_USAGE = POINTER(CERT_ENHKEY_USAGE)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
class CERT_USAGE_MATCH(Structure):
|
| 64 |
+
_fields_ = (
|
| 65 |
+
("dwType", DWORD),
|
| 66 |
+
("Usage", CERT_ENHKEY_USAGE),
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
class CERT_CHAIN_PARA(Structure):
|
| 71 |
+
_fields_ = (
|
| 72 |
+
("cbSize", DWORD),
|
| 73 |
+
("RequestedUsage", CERT_USAGE_MATCH),
|
| 74 |
+
("RequestedIssuancePolicy", CERT_USAGE_MATCH),
|
| 75 |
+
("dwUrlRetrievalTimeout", DWORD),
|
| 76 |
+
("fCheckRevocationFreshnessTime", BOOL),
|
| 77 |
+
("dwRevocationFreshnessTime", DWORD),
|
| 78 |
+
("pftCacheResync", LPFILETIME),
|
| 79 |
+
("pStrongSignPara", c_void_p),
|
| 80 |
+
("dwStrongSignFlags", DWORD),
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
if TYPE_CHECKING:
|
| 85 |
+
PCERT_CHAIN_PARA = pointer[CERT_CHAIN_PARA] # type: ignore[misc]
|
| 86 |
+
else:
|
| 87 |
+
PCERT_CHAIN_PARA = POINTER(CERT_CHAIN_PARA)
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
class CERT_TRUST_STATUS(Structure):
|
| 91 |
+
_fields_ = (
|
| 92 |
+
("dwErrorStatus", DWORD),
|
| 93 |
+
("dwInfoStatus", DWORD),
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
class CERT_CHAIN_ELEMENT(Structure):
|
| 98 |
+
_fields_ = (
|
| 99 |
+
("cbSize", DWORD),
|
| 100 |
+
("pCertContext", PCERT_CONTEXT),
|
| 101 |
+
("TrustStatus", CERT_TRUST_STATUS),
|
| 102 |
+
("pRevocationInfo", c_void_p),
|
| 103 |
+
("pIssuanceUsage", PCERT_ENHKEY_USAGE),
|
| 104 |
+
("pApplicationUsage", PCERT_ENHKEY_USAGE),
|
| 105 |
+
("pwszExtendedErrorInfo", LPCWSTR),
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
PCERT_CHAIN_ELEMENT = POINTER(CERT_CHAIN_ELEMENT)
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
class CERT_SIMPLE_CHAIN(Structure):
|
| 113 |
+
_fields_ = (
|
| 114 |
+
("cbSize", DWORD),
|
| 115 |
+
("TrustStatus", CERT_TRUST_STATUS),
|
| 116 |
+
("cElement", DWORD),
|
| 117 |
+
("rgpElement", POINTER(PCERT_CHAIN_ELEMENT)),
|
| 118 |
+
("pTrustListInfo", c_void_p),
|
| 119 |
+
("fHasRevocationFreshnessTime", BOOL),
|
| 120 |
+
("dwRevocationFreshnessTime", DWORD),
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
PCERT_SIMPLE_CHAIN = POINTER(CERT_SIMPLE_CHAIN)
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
class CERT_CHAIN_CONTEXT(Structure):
|
| 128 |
+
_fields_ = (
|
| 129 |
+
("cbSize", DWORD),
|
| 130 |
+
("TrustStatus", CERT_TRUST_STATUS),
|
| 131 |
+
("cChain", DWORD),
|
| 132 |
+
("rgpChain", POINTER(PCERT_SIMPLE_CHAIN)),
|
| 133 |
+
("cLowerQualityChainContext", DWORD),
|
| 134 |
+
("rgpLowerQualityChainContext", c_void_p),
|
| 135 |
+
("fHasRevocationFreshnessTime", BOOL),
|
| 136 |
+
("dwRevocationFreshnessTime", DWORD),
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
PCERT_CHAIN_CONTEXT = POINTER(CERT_CHAIN_CONTEXT)
|
| 141 |
+
PCCERT_CHAIN_CONTEXT = POINTER(PCERT_CHAIN_CONTEXT)
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
class SSL_EXTRA_CERT_CHAIN_POLICY_PARA(Structure):
|
| 145 |
+
_fields_ = (
|
| 146 |
+
("cbSize", DWORD),
|
| 147 |
+
("dwAuthType", DWORD),
|
| 148 |
+
("fdwChecks", DWORD),
|
| 149 |
+
("pwszServerName", LPCWSTR),
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
class CERT_CHAIN_POLICY_PARA(Structure):
|
| 154 |
+
_fields_ = (
|
| 155 |
+
("cbSize", DWORD),
|
| 156 |
+
("dwFlags", DWORD),
|
| 157 |
+
("pvExtraPolicyPara", c_void_p),
|
| 158 |
+
)
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
PCERT_CHAIN_POLICY_PARA = POINTER(CERT_CHAIN_POLICY_PARA)
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
class CERT_CHAIN_POLICY_STATUS(Structure):
|
| 165 |
+
_fields_ = (
|
| 166 |
+
("cbSize", DWORD),
|
| 167 |
+
("dwError", DWORD),
|
| 168 |
+
("lChainIndex", LONG),
|
| 169 |
+
("lElementIndex", LONG),
|
| 170 |
+
("pvExtraPolicyStatus", c_void_p),
|
| 171 |
+
)
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
PCERT_CHAIN_POLICY_STATUS = POINTER(CERT_CHAIN_POLICY_STATUS)
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
class CERT_CHAIN_ENGINE_CONFIG(Structure):
|
| 178 |
+
_fields_ = (
|
| 179 |
+
("cbSize", DWORD),
|
| 180 |
+
("hRestrictedRoot", HCERTSTORE),
|
| 181 |
+
("hRestrictedTrust", HCERTSTORE),
|
| 182 |
+
("hRestrictedOther", HCERTSTORE),
|
| 183 |
+
("cAdditionalStore", DWORD),
|
| 184 |
+
("rghAdditionalStore", c_void_p),
|
| 185 |
+
("dwFlags", DWORD),
|
| 186 |
+
("dwUrlRetrievalTimeout", DWORD),
|
| 187 |
+
("MaximumCachedCertificates", DWORD),
|
| 188 |
+
("CycleDetectionModulus", DWORD),
|
| 189 |
+
("hExclusiveRoot", HCERTSTORE),
|
| 190 |
+
("hExclusiveTrustedPeople", HCERTSTORE),
|
| 191 |
+
("dwExclusiveFlags", DWORD),
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
PCERT_CHAIN_ENGINE_CONFIG = POINTER(CERT_CHAIN_ENGINE_CONFIG)
|
| 196 |
+
PHCERTCHAINENGINE = POINTER(HCERTCHAINENGINE)
|
| 197 |
+
|
| 198 |
+
X509_ASN_ENCODING = 0x00000001
|
| 199 |
+
PKCS_7_ASN_ENCODING = 0x00010000
|
| 200 |
+
CERT_STORE_PROV_MEMORY = b"Memory"
|
| 201 |
+
CERT_STORE_ADD_USE_EXISTING = 2
|
| 202 |
+
USAGE_MATCH_TYPE_OR = 1
|
| 203 |
+
OID_PKIX_KP_SERVER_AUTH = c_char_p(b"1.3.6.1.5.5.7.3.1")
|
| 204 |
+
CERT_CHAIN_REVOCATION_CHECK_END_CERT = 0x10000000
|
| 205 |
+
CERT_CHAIN_REVOCATION_CHECK_CHAIN = 0x20000000
|
| 206 |
+
CERT_CHAIN_POLICY_IGNORE_ALL_NOT_TIME_VALID_FLAGS = 0x00000007
|
| 207 |
+
CERT_CHAIN_POLICY_IGNORE_INVALID_BASIC_CONSTRAINTS_FLAG = 0x00000008
|
| 208 |
+
CERT_CHAIN_POLICY_ALLOW_UNKNOWN_CA_FLAG = 0x00000010
|
| 209 |
+
CERT_CHAIN_POLICY_IGNORE_INVALID_NAME_FLAG = 0x00000040
|
| 210 |
+
CERT_CHAIN_POLICY_IGNORE_WRONG_USAGE_FLAG = 0x00000020
|
| 211 |
+
CERT_CHAIN_POLICY_IGNORE_INVALID_POLICY_FLAG = 0x00000080
|
| 212 |
+
CERT_CHAIN_POLICY_IGNORE_ALL_REV_UNKNOWN_FLAGS = 0x00000F00
|
| 213 |
+
CERT_CHAIN_POLICY_ALLOW_TESTROOT_FLAG = 0x00008000
|
| 214 |
+
CERT_CHAIN_POLICY_TRUST_TESTROOT_FLAG = 0x00004000
|
| 215 |
+
SECURITY_FLAG_IGNORE_CERT_CN_INVALID = 0x00001000
|
| 216 |
+
AUTHTYPE_SERVER = 2
|
| 217 |
+
CERT_CHAIN_POLICY_SSL = 4
|
| 218 |
+
FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000
|
| 219 |
+
FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200
|
| 220 |
+
|
| 221 |
+
# Flags to set for SSLContext.verify_mode=CERT_NONE
|
| 222 |
+
CERT_CHAIN_POLICY_VERIFY_MODE_NONE_FLAGS = (
|
| 223 |
+
CERT_CHAIN_POLICY_IGNORE_ALL_NOT_TIME_VALID_FLAGS
|
| 224 |
+
| CERT_CHAIN_POLICY_IGNORE_INVALID_BASIC_CONSTRAINTS_FLAG
|
| 225 |
+
| CERT_CHAIN_POLICY_ALLOW_UNKNOWN_CA_FLAG
|
| 226 |
+
| CERT_CHAIN_POLICY_IGNORE_INVALID_NAME_FLAG
|
| 227 |
+
| CERT_CHAIN_POLICY_IGNORE_WRONG_USAGE_FLAG
|
| 228 |
+
| CERT_CHAIN_POLICY_IGNORE_INVALID_POLICY_FLAG
|
| 229 |
+
| CERT_CHAIN_POLICY_IGNORE_ALL_REV_UNKNOWN_FLAGS
|
| 230 |
+
| CERT_CHAIN_POLICY_ALLOW_TESTROOT_FLAG
|
| 231 |
+
| CERT_CHAIN_POLICY_TRUST_TESTROOT_FLAG
|
| 232 |
+
)
|
| 233 |
+
|
| 234 |
+
wincrypt = WinDLL("crypt32.dll")
|
| 235 |
+
kernel32 = WinDLL("kernel32.dll")
|
| 236 |
+
|
| 237 |
+
|
| 238 |
+
def _handle_win_error(result: bool, _: Any, args: Any) -> Any:
|
| 239 |
+
if not result:
|
| 240 |
+
# Note, actually raises OSError after calling GetLastError and FormatMessage
|
| 241 |
+
raise WinError()
|
| 242 |
+
return args
|
| 243 |
+
|
| 244 |
+
|
| 245 |
+
CertCreateCertificateChainEngine = wincrypt.CertCreateCertificateChainEngine
|
| 246 |
+
CertCreateCertificateChainEngine.argtypes = (
|
| 247 |
+
PCERT_CHAIN_ENGINE_CONFIG,
|
| 248 |
+
PHCERTCHAINENGINE,
|
| 249 |
+
)
|
| 250 |
+
CertCreateCertificateChainEngine.errcheck = _handle_win_error
|
| 251 |
+
|
| 252 |
+
CertOpenStore = wincrypt.CertOpenStore
|
| 253 |
+
CertOpenStore.argtypes = (LPCSTR, DWORD, HCRYPTPROV_LEGACY, DWORD, c_void_p)
|
| 254 |
+
CertOpenStore.restype = HCERTSTORE
|
| 255 |
+
CertOpenStore.errcheck = _handle_win_error
|
| 256 |
+
|
| 257 |
+
CertAddEncodedCertificateToStore = wincrypt.CertAddEncodedCertificateToStore
|
| 258 |
+
CertAddEncodedCertificateToStore.argtypes = (
|
| 259 |
+
HCERTSTORE,
|
| 260 |
+
DWORD,
|
| 261 |
+
c_char_p,
|
| 262 |
+
DWORD,
|
| 263 |
+
DWORD,
|
| 264 |
+
PCCERT_CONTEXT,
|
| 265 |
+
)
|
| 266 |
+
CertAddEncodedCertificateToStore.restype = BOOL
|
| 267 |
+
|
| 268 |
+
CertCreateCertificateContext = wincrypt.CertCreateCertificateContext
|
| 269 |
+
CertCreateCertificateContext.argtypes = (DWORD, c_char_p, DWORD)
|
| 270 |
+
CertCreateCertificateContext.restype = PCERT_CONTEXT
|
| 271 |
+
CertCreateCertificateContext.errcheck = _handle_win_error
|
| 272 |
+
|
| 273 |
+
CertGetCertificateChain = wincrypt.CertGetCertificateChain
|
| 274 |
+
CertGetCertificateChain.argtypes = (
|
| 275 |
+
HCERTCHAINENGINE,
|
| 276 |
+
PCERT_CONTEXT,
|
| 277 |
+
LPFILETIME,
|
| 278 |
+
HCERTSTORE,
|
| 279 |
+
PCERT_CHAIN_PARA,
|
| 280 |
+
DWORD,
|
| 281 |
+
c_void_p,
|
| 282 |
+
PCCERT_CHAIN_CONTEXT,
|
| 283 |
+
)
|
| 284 |
+
CertGetCertificateChain.restype = BOOL
|
| 285 |
+
CertGetCertificateChain.errcheck = _handle_win_error
|
| 286 |
+
|
| 287 |
+
CertVerifyCertificateChainPolicy = wincrypt.CertVerifyCertificateChainPolicy
|
| 288 |
+
CertVerifyCertificateChainPolicy.argtypes = (
|
| 289 |
+
c_ulong,
|
| 290 |
+
PCERT_CHAIN_CONTEXT,
|
| 291 |
+
PCERT_CHAIN_POLICY_PARA,
|
| 292 |
+
PCERT_CHAIN_POLICY_STATUS,
|
| 293 |
+
)
|
| 294 |
+
CertVerifyCertificateChainPolicy.restype = BOOL
|
| 295 |
+
|
| 296 |
+
CertCloseStore = wincrypt.CertCloseStore
|
| 297 |
+
CertCloseStore.argtypes = (HCERTSTORE, DWORD)
|
| 298 |
+
CertCloseStore.restype = BOOL
|
| 299 |
+
CertCloseStore.errcheck = _handle_win_error
|
| 300 |
+
|
| 301 |
+
CertFreeCertificateChain = wincrypt.CertFreeCertificateChain
|
| 302 |
+
CertFreeCertificateChain.argtypes = (PCERT_CHAIN_CONTEXT,)
|
| 303 |
+
|
| 304 |
+
CertFreeCertificateContext = wincrypt.CertFreeCertificateContext
|
| 305 |
+
CertFreeCertificateContext.argtypes = (PCERT_CONTEXT,)
|
| 306 |
+
|
| 307 |
+
CertFreeCertificateChainEngine = wincrypt.CertFreeCertificateChainEngine
|
| 308 |
+
CertFreeCertificateChainEngine.argtypes = (HCERTCHAINENGINE,)
|
| 309 |
+
|
| 310 |
+
FormatMessageW = kernel32.FormatMessageW
|
| 311 |
+
FormatMessageW.argtypes = (
|
| 312 |
+
DWORD,
|
| 313 |
+
LPCVOID,
|
| 314 |
+
DWORD,
|
| 315 |
+
DWORD,
|
| 316 |
+
LPWSTR,
|
| 317 |
+
DWORD,
|
| 318 |
+
c_void_p,
|
| 319 |
+
)
|
| 320 |
+
FormatMessageW.restype = DWORD
|
| 321 |
+
|
| 322 |
+
|
| 323 |
+
def _verify_peercerts_impl(
|
| 324 |
+
ssl_context: ssl.SSLContext,
|
| 325 |
+
cert_chain: list[bytes],
|
| 326 |
+
server_hostname: str | None = None,
|
| 327 |
+
) -> None:
|
| 328 |
+
"""Verify the cert_chain from the server using Windows APIs."""
|
| 329 |
+
|
| 330 |
+
# If the peer didn't send any certificates then
|
| 331 |
+
# we can't do verification. Raise an error.
|
| 332 |
+
if not cert_chain:
|
| 333 |
+
raise ssl.SSLCertVerificationError("Peer sent no certificates to verify")
|
| 334 |
+
|
| 335 |
+
pCertContext = None
|
| 336 |
+
hIntermediateCertStore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, None, 0, None)
|
| 337 |
+
try:
|
| 338 |
+
# Add intermediate certs to an in-memory cert store
|
| 339 |
+
for cert_bytes in cert_chain[1:]:
|
| 340 |
+
CertAddEncodedCertificateToStore(
|
| 341 |
+
hIntermediateCertStore,
|
| 342 |
+
X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
|
| 343 |
+
cert_bytes,
|
| 344 |
+
len(cert_bytes),
|
| 345 |
+
CERT_STORE_ADD_USE_EXISTING,
|
| 346 |
+
None,
|
| 347 |
+
)
|
| 348 |
+
|
| 349 |
+
# Cert context for leaf cert
|
| 350 |
+
leaf_cert = cert_chain[0]
|
| 351 |
+
pCertContext = CertCreateCertificateContext(
|
| 352 |
+
X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, leaf_cert, len(leaf_cert)
|
| 353 |
+
)
|
| 354 |
+
|
| 355 |
+
# Chain params to match certs for serverAuth extended usage
|
| 356 |
+
cert_enhkey_usage = CERT_ENHKEY_USAGE()
|
| 357 |
+
cert_enhkey_usage.cUsageIdentifier = 1
|
| 358 |
+
cert_enhkey_usage.rgpszUsageIdentifier = (c_char_p * 1)(OID_PKIX_KP_SERVER_AUTH)
|
| 359 |
+
cert_usage_match = CERT_USAGE_MATCH()
|
| 360 |
+
cert_usage_match.Usage = cert_enhkey_usage
|
| 361 |
+
chain_params = CERT_CHAIN_PARA()
|
| 362 |
+
chain_params.RequestedUsage = cert_usage_match
|
| 363 |
+
chain_params.cbSize = sizeof(chain_params)
|
| 364 |
+
pChainPara = pointer(chain_params)
|
| 365 |
+
|
| 366 |
+
if ssl_context.verify_flags & ssl.VERIFY_CRL_CHECK_CHAIN:
|
| 367 |
+
chain_flags = CERT_CHAIN_REVOCATION_CHECK_CHAIN
|
| 368 |
+
elif ssl_context.verify_flags & ssl.VERIFY_CRL_CHECK_LEAF:
|
| 369 |
+
chain_flags = CERT_CHAIN_REVOCATION_CHECK_END_CERT
|
| 370 |
+
else:
|
| 371 |
+
chain_flags = 0
|
| 372 |
+
|
| 373 |
+
try:
|
| 374 |
+
# First attempt to verify using the default Windows system trust roots
|
| 375 |
+
# (default chain engine).
|
| 376 |
+
_get_and_verify_cert_chain(
|
| 377 |
+
ssl_context,
|
| 378 |
+
None,
|
| 379 |
+
hIntermediateCertStore,
|
| 380 |
+
pCertContext,
|
| 381 |
+
pChainPara,
|
| 382 |
+
server_hostname,
|
| 383 |
+
chain_flags=chain_flags,
|
| 384 |
+
)
|
| 385 |
+
except ssl.SSLCertVerificationError as e:
|
| 386 |
+
# If that fails but custom CA certs have been added
|
| 387 |
+
# to the SSLContext using load_verify_locations,
|
| 388 |
+
# try verifying using a custom chain engine
|
| 389 |
+
# that trusts the custom CA certs.
|
| 390 |
+
custom_ca_certs: list[bytes] | None = ssl_context.get_ca_certs(
|
| 391 |
+
binary_form=True
|
| 392 |
+
)
|
| 393 |
+
if custom_ca_certs:
|
| 394 |
+
try:
|
| 395 |
+
_verify_using_custom_ca_certs(
|
| 396 |
+
ssl_context,
|
| 397 |
+
custom_ca_certs,
|
| 398 |
+
hIntermediateCertStore,
|
| 399 |
+
pCertContext,
|
| 400 |
+
pChainPara,
|
| 401 |
+
server_hostname,
|
| 402 |
+
chain_flags=chain_flags,
|
| 403 |
+
)
|
| 404 |
+
# Raise the original error, not the new error.
|
| 405 |
+
except ssl.SSLCertVerificationError:
|
| 406 |
+
raise e from None
|
| 407 |
+
else:
|
| 408 |
+
raise
|
| 409 |
+
finally:
|
| 410 |
+
CertCloseStore(hIntermediateCertStore, 0)
|
| 411 |
+
if pCertContext:
|
| 412 |
+
CertFreeCertificateContext(pCertContext)
|
| 413 |
+
|
| 414 |
+
|
| 415 |
+
def _get_and_verify_cert_chain(
|
| 416 |
+
ssl_context: ssl.SSLContext,
|
| 417 |
+
hChainEngine: HCERTCHAINENGINE | None,
|
| 418 |
+
hIntermediateCertStore: HCERTSTORE,
|
| 419 |
+
pPeerCertContext: c_void_p,
|
| 420 |
+
pChainPara: PCERT_CHAIN_PARA, # type: ignore[valid-type]
|
| 421 |
+
server_hostname: str | None,
|
| 422 |
+
chain_flags: int,
|
| 423 |
+
) -> None:
|
| 424 |
+
ppChainContext = None
|
| 425 |
+
try:
|
| 426 |
+
# Get cert chain
|
| 427 |
+
ppChainContext = pointer(PCERT_CHAIN_CONTEXT())
|
| 428 |
+
CertGetCertificateChain(
|
| 429 |
+
hChainEngine, # chain engine
|
| 430 |
+
pPeerCertContext, # leaf cert context
|
| 431 |
+
None, # current system time
|
| 432 |
+
hIntermediateCertStore, # additional in-memory cert store
|
| 433 |
+
pChainPara, # chain-building parameters
|
| 434 |
+
chain_flags,
|
| 435 |
+
None, # reserved
|
| 436 |
+
ppChainContext, # the resulting chain context
|
| 437 |
+
)
|
| 438 |
+
pChainContext = ppChainContext.contents
|
| 439 |
+
|
| 440 |
+
# Verify cert chain
|
| 441 |
+
ssl_extra_cert_chain_policy_para = SSL_EXTRA_CERT_CHAIN_POLICY_PARA()
|
| 442 |
+
ssl_extra_cert_chain_policy_para.cbSize = sizeof(
|
| 443 |
+
ssl_extra_cert_chain_policy_para
|
| 444 |
+
)
|
| 445 |
+
ssl_extra_cert_chain_policy_para.dwAuthType = AUTHTYPE_SERVER
|
| 446 |
+
ssl_extra_cert_chain_policy_para.fdwChecks = 0
|
| 447 |
+
if ssl_context.check_hostname is False:
|
| 448 |
+
ssl_extra_cert_chain_policy_para.fdwChecks = (
|
| 449 |
+
SECURITY_FLAG_IGNORE_CERT_CN_INVALID
|
| 450 |
+
)
|
| 451 |
+
if server_hostname:
|
| 452 |
+
ssl_extra_cert_chain_policy_para.pwszServerName = c_wchar_p(server_hostname)
|
| 453 |
+
|
| 454 |
+
chain_policy = CERT_CHAIN_POLICY_PARA()
|
| 455 |
+
chain_policy.pvExtraPolicyPara = cast(
|
| 456 |
+
pointer(ssl_extra_cert_chain_policy_para), c_void_p
|
| 457 |
+
)
|
| 458 |
+
if ssl_context.verify_mode == ssl.CERT_NONE:
|
| 459 |
+
chain_policy.dwFlags |= CERT_CHAIN_POLICY_VERIFY_MODE_NONE_FLAGS
|
| 460 |
+
chain_policy.cbSize = sizeof(chain_policy)
|
| 461 |
+
|
| 462 |
+
pPolicyPara = pointer(chain_policy)
|
| 463 |
+
policy_status = CERT_CHAIN_POLICY_STATUS()
|
| 464 |
+
policy_status.cbSize = sizeof(policy_status)
|
| 465 |
+
pPolicyStatus = pointer(policy_status)
|
| 466 |
+
CertVerifyCertificateChainPolicy(
|
| 467 |
+
CERT_CHAIN_POLICY_SSL,
|
| 468 |
+
pChainContext,
|
| 469 |
+
pPolicyPara,
|
| 470 |
+
pPolicyStatus,
|
| 471 |
+
)
|
| 472 |
+
|
| 473 |
+
# Check status
|
| 474 |
+
error_code = policy_status.dwError
|
| 475 |
+
if error_code:
|
| 476 |
+
# Try getting a human readable message for an error code.
|
| 477 |
+
error_message_buf = create_unicode_buffer(1024)
|
| 478 |
+
error_message_chars = FormatMessageW(
|
| 479 |
+
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
| 480 |
+
None,
|
| 481 |
+
error_code,
|
| 482 |
+
0,
|
| 483 |
+
error_message_buf,
|
| 484 |
+
sizeof(error_message_buf),
|
| 485 |
+
None,
|
| 486 |
+
)
|
| 487 |
+
|
| 488 |
+
# See if we received a message for the error,
|
| 489 |
+
# otherwise we use a generic error with the
|
| 490 |
+
# error code and hope that it's search-able.
|
| 491 |
+
if error_message_chars <= 0:
|
| 492 |
+
error_message = f"Certificate chain policy error {error_code:#x} [{policy_status.lElementIndex}]"
|
| 493 |
+
else:
|
| 494 |
+
error_message = error_message_buf.value.strip()
|
| 495 |
+
|
| 496 |
+
err = ssl.SSLCertVerificationError(error_message)
|
| 497 |
+
err.verify_message = error_message
|
| 498 |
+
err.verify_code = error_code
|
| 499 |
+
raise err from None
|
| 500 |
+
finally:
|
| 501 |
+
if ppChainContext:
|
| 502 |
+
CertFreeCertificateChain(ppChainContext.contents)
|
| 503 |
+
|
| 504 |
+
|
| 505 |
+
def _verify_using_custom_ca_certs(
|
| 506 |
+
ssl_context: ssl.SSLContext,
|
| 507 |
+
custom_ca_certs: list[bytes],
|
| 508 |
+
hIntermediateCertStore: HCERTSTORE,
|
| 509 |
+
pPeerCertContext: c_void_p,
|
| 510 |
+
pChainPara: PCERT_CHAIN_PARA, # type: ignore[valid-type]
|
| 511 |
+
server_hostname: str | None,
|
| 512 |
+
chain_flags: int,
|
| 513 |
+
) -> None:
|
| 514 |
+
hChainEngine = None
|
| 515 |
+
hRootCertStore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, None, 0, None)
|
| 516 |
+
try:
|
| 517 |
+
# Add custom CA certs to an in-memory cert store
|
| 518 |
+
for cert_bytes in custom_ca_certs:
|
| 519 |
+
CertAddEncodedCertificateToStore(
|
| 520 |
+
hRootCertStore,
|
| 521 |
+
X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
|
| 522 |
+
cert_bytes,
|
| 523 |
+
len(cert_bytes),
|
| 524 |
+
CERT_STORE_ADD_USE_EXISTING,
|
| 525 |
+
None,
|
| 526 |
+
)
|
| 527 |
+
|
| 528 |
+
# Create a custom cert chain engine which exclusively trusts
|
| 529 |
+
# certs from our hRootCertStore
|
| 530 |
+
cert_chain_engine_config = CERT_CHAIN_ENGINE_CONFIG()
|
| 531 |
+
cert_chain_engine_config.cbSize = sizeof(cert_chain_engine_config)
|
| 532 |
+
cert_chain_engine_config.hExclusiveRoot = hRootCertStore
|
| 533 |
+
pConfig = pointer(cert_chain_engine_config)
|
| 534 |
+
phChainEngine = pointer(HCERTCHAINENGINE())
|
| 535 |
+
CertCreateCertificateChainEngine(
|
| 536 |
+
pConfig,
|
| 537 |
+
phChainEngine,
|
| 538 |
+
)
|
| 539 |
+
hChainEngine = phChainEngine.contents
|
| 540 |
+
|
| 541 |
+
# Get and verify a cert chain using the custom chain engine
|
| 542 |
+
_get_and_verify_cert_chain(
|
| 543 |
+
ssl_context,
|
| 544 |
+
hChainEngine,
|
| 545 |
+
hIntermediateCertStore,
|
| 546 |
+
pPeerCertContext,
|
| 547 |
+
pChainPara,
|
| 548 |
+
server_hostname,
|
| 549 |
+
chain_flags,
|
| 550 |
+
)
|
| 551 |
+
finally:
|
| 552 |
+
if hChainEngine:
|
| 553 |
+
CertFreeCertificateChainEngine(hChainEngine)
|
| 554 |
+
CertCloseStore(hRootCertStore, 0)
|
| 555 |
+
|
| 556 |
+
|
| 557 |
+
@contextlib.contextmanager
|
| 558 |
+
def _configure_context(ctx: ssl.SSLContext) -> typing.Iterator[None]:
|
| 559 |
+
check_hostname = ctx.check_hostname
|
| 560 |
+
verify_mode = ctx.verify_mode
|
| 561 |
+
ctx.check_hostname = False
|
| 562 |
+
_set_ssl_context_verify_mode(ctx, ssl.CERT_NONE)
|
| 563 |
+
try:
|
| 564 |
+
yield
|
| 565 |
+
finally:
|
| 566 |
+
ctx.check_hostname = check_hostname
|
| 567 |
+
_set_ssl_context_verify_mode(ctx, verify_mode)
|
evalkit_llava/lib/python3.10/site-packages/pip/_vendor/truststore/py.typed
ADDED
|
File without changes
|
evalkit_llava/lib/python3.10/site-packages/pip/_vendor/urllib3/contrib/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (186 Bytes). View file
|
|
|
evalkit_llava/lib/python3.10/site-packages/pip/_vendor/urllib3/contrib/__pycache__/socks.cpython-310.pyc
ADDED
|
Binary file (5.59 kB). View file
|
|
|
evalkit_llava/lib/python3.10/site-packages/pip/_vendor/urllib3/contrib/_securetransport/__init__.py
ADDED
|
File without changes
|
evalkit_llava/lib/python3.10/site-packages/pip/_vendor/urllib3/contrib/_securetransport/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (203 Bytes). View file
|
|
|
evalkit_llava/lib/python3.10/site-packages/pip/_vendor/urllib3/contrib/_securetransport/__pycache__/bindings.cpython-310.pyc
ADDED
|
Binary file (10.7 kB). View file
|
|
|
evalkit_llava/lib/python3.10/site-packages/pip/_vendor/urllib3/contrib/_securetransport/__pycache__/low_level.cpython-310.pyc
ADDED
|
Binary file (9.09 kB). View file
|
|
|
evalkit_llava/lib/python3.10/site-packages/pip/_vendor/urllib3/contrib/_securetransport/bindings.py
ADDED
|
@@ -0,0 +1,519 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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""
|
| 2 |
+
This module uses ctypes to bind a whole bunch of functions and constants from
|
| 3 |
+
SecureTransport. The goal here is to provide the low-level API to
|
| 4 |
+
SecureTransport. These are essentially the C-level functions and constants, and
|
| 5 |
+
they're pretty gross to work with.
|
| 6 |
+
|
| 7 |
+
This code is a bastardised version of the code found in Will Bond's oscrypto
|
| 8 |
+
library. An enormous debt is owed to him for blazing this trail for us. For
|
| 9 |
+
that reason, this code should be considered to be covered both by urllib3's
|
| 10 |
+
license and by oscrypto's:
|
| 11 |
+
|
| 12 |
+
Copyright (c) 2015-2016 Will Bond <will@wbond.net>
|
| 13 |
+
|
| 14 |
+
Permission is hereby granted, free of charge, to any person obtaining a
|
| 15 |
+
copy of this software and associated documentation files (the "Software"),
|
| 16 |
+
to deal in the Software without restriction, including without limitation
|
| 17 |
+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
| 18 |
+
and/or sell copies of the Software, and to permit persons to whom the
|
| 19 |
+
Software is furnished to do so, subject to the following conditions:
|
| 20 |
+
|
| 21 |
+
The above copyright notice and this permission notice shall be included in
|
| 22 |
+
all copies or substantial portions of the Software.
|
| 23 |
+
|
| 24 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 25 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 26 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 27 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 28 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
| 29 |
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
| 30 |
+
DEALINGS IN THE SOFTWARE.
|
| 31 |
+
"""
|
| 32 |
+
from __future__ import absolute_import
|
| 33 |
+
|
| 34 |
+
import platform
|
| 35 |
+
from ctypes import (
|
| 36 |
+
CDLL,
|
| 37 |
+
CFUNCTYPE,
|
| 38 |
+
POINTER,
|
| 39 |
+
c_bool,
|
| 40 |
+
c_byte,
|
| 41 |
+
c_char_p,
|
| 42 |
+
c_int32,
|
| 43 |
+
c_long,
|
| 44 |
+
c_size_t,
|
| 45 |
+
c_uint32,
|
| 46 |
+
c_ulong,
|
| 47 |
+
c_void_p,
|
| 48 |
+
)
|
| 49 |
+
from ctypes.util import find_library
|
| 50 |
+
|
| 51 |
+
from ...packages.six import raise_from
|
| 52 |
+
|
| 53 |
+
if platform.system() != "Darwin":
|
| 54 |
+
raise ImportError("Only macOS is supported")
|
| 55 |
+
|
| 56 |
+
version = platform.mac_ver()[0]
|
| 57 |
+
version_info = tuple(map(int, version.split(".")))
|
| 58 |
+
if version_info < (10, 8):
|
| 59 |
+
raise OSError(
|
| 60 |
+
"Only OS X 10.8 and newer are supported, not %s.%s"
|
| 61 |
+
% (version_info[0], version_info[1])
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def load_cdll(name, macos10_16_path):
|
| 66 |
+
"""Loads a CDLL by name, falling back to known path on 10.16+"""
|
| 67 |
+
try:
|
| 68 |
+
# Big Sur is technically 11 but we use 10.16 due to the Big Sur
|
| 69 |
+
# beta being labeled as 10.16.
|
| 70 |
+
if version_info >= (10, 16):
|
| 71 |
+
path = macos10_16_path
|
| 72 |
+
else:
|
| 73 |
+
path = find_library(name)
|
| 74 |
+
if not path:
|
| 75 |
+
raise OSError # Caught and reraised as 'ImportError'
|
| 76 |
+
return CDLL(path, use_errno=True)
|
| 77 |
+
except OSError:
|
| 78 |
+
raise_from(ImportError("The library %s failed to load" % name), None)
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
Security = load_cdll(
|
| 82 |
+
"Security", "/System/Library/Frameworks/Security.framework/Security"
|
| 83 |
+
)
|
| 84 |
+
CoreFoundation = load_cdll(
|
| 85 |
+
"CoreFoundation",
|
| 86 |
+
"/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation",
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
Boolean = c_bool
|
| 91 |
+
CFIndex = c_long
|
| 92 |
+
CFStringEncoding = c_uint32
|
| 93 |
+
CFData = c_void_p
|
| 94 |
+
CFString = c_void_p
|
| 95 |
+
CFArray = c_void_p
|
| 96 |
+
CFMutableArray = c_void_p
|
| 97 |
+
CFDictionary = c_void_p
|
| 98 |
+
CFError = c_void_p
|
| 99 |
+
CFType = c_void_p
|
| 100 |
+
CFTypeID = c_ulong
|
| 101 |
+
|
| 102 |
+
CFTypeRef = POINTER(CFType)
|
| 103 |
+
CFAllocatorRef = c_void_p
|
| 104 |
+
|
| 105 |
+
OSStatus = c_int32
|
| 106 |
+
|
| 107 |
+
CFDataRef = POINTER(CFData)
|
| 108 |
+
CFStringRef = POINTER(CFString)
|
| 109 |
+
CFArrayRef = POINTER(CFArray)
|
| 110 |
+
CFMutableArrayRef = POINTER(CFMutableArray)
|
| 111 |
+
CFDictionaryRef = POINTER(CFDictionary)
|
| 112 |
+
CFArrayCallBacks = c_void_p
|
| 113 |
+
CFDictionaryKeyCallBacks = c_void_p
|
| 114 |
+
CFDictionaryValueCallBacks = c_void_p
|
| 115 |
+
|
| 116 |
+
SecCertificateRef = POINTER(c_void_p)
|
| 117 |
+
SecExternalFormat = c_uint32
|
| 118 |
+
SecExternalItemType = c_uint32
|
| 119 |
+
SecIdentityRef = POINTER(c_void_p)
|
| 120 |
+
SecItemImportExportFlags = c_uint32
|
| 121 |
+
SecItemImportExportKeyParameters = c_void_p
|
| 122 |
+
SecKeychainRef = POINTER(c_void_p)
|
| 123 |
+
SSLProtocol = c_uint32
|
| 124 |
+
SSLCipherSuite = c_uint32
|
| 125 |
+
SSLContextRef = POINTER(c_void_p)
|
| 126 |
+
SecTrustRef = POINTER(c_void_p)
|
| 127 |
+
SSLConnectionRef = c_uint32
|
| 128 |
+
SecTrustResultType = c_uint32
|
| 129 |
+
SecTrustOptionFlags = c_uint32
|
| 130 |
+
SSLProtocolSide = c_uint32
|
| 131 |
+
SSLConnectionType = c_uint32
|
| 132 |
+
SSLSessionOption = c_uint32
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
try:
|
| 136 |
+
Security.SecItemImport.argtypes = [
|
| 137 |
+
CFDataRef,
|
| 138 |
+
CFStringRef,
|
| 139 |
+
POINTER(SecExternalFormat),
|
| 140 |
+
POINTER(SecExternalItemType),
|
| 141 |
+
SecItemImportExportFlags,
|
| 142 |
+
POINTER(SecItemImportExportKeyParameters),
|
| 143 |
+
SecKeychainRef,
|
| 144 |
+
POINTER(CFArrayRef),
|
| 145 |
+
]
|
| 146 |
+
Security.SecItemImport.restype = OSStatus
|
| 147 |
+
|
| 148 |
+
Security.SecCertificateGetTypeID.argtypes = []
|
| 149 |
+
Security.SecCertificateGetTypeID.restype = CFTypeID
|
| 150 |
+
|
| 151 |
+
Security.SecIdentityGetTypeID.argtypes = []
|
| 152 |
+
Security.SecIdentityGetTypeID.restype = CFTypeID
|
| 153 |
+
|
| 154 |
+
Security.SecKeyGetTypeID.argtypes = []
|
| 155 |
+
Security.SecKeyGetTypeID.restype = CFTypeID
|
| 156 |
+
|
| 157 |
+
Security.SecCertificateCreateWithData.argtypes = [CFAllocatorRef, CFDataRef]
|
| 158 |
+
Security.SecCertificateCreateWithData.restype = SecCertificateRef
|
| 159 |
+
|
| 160 |
+
Security.SecCertificateCopyData.argtypes = [SecCertificateRef]
|
| 161 |
+
Security.SecCertificateCopyData.restype = CFDataRef
|
| 162 |
+
|
| 163 |
+
Security.SecCopyErrorMessageString.argtypes = [OSStatus, c_void_p]
|
| 164 |
+
Security.SecCopyErrorMessageString.restype = CFStringRef
|
| 165 |
+
|
| 166 |
+
Security.SecIdentityCreateWithCertificate.argtypes = [
|
| 167 |
+
CFTypeRef,
|
| 168 |
+
SecCertificateRef,
|
| 169 |
+
POINTER(SecIdentityRef),
|
| 170 |
+
]
|
| 171 |
+
Security.SecIdentityCreateWithCertificate.restype = OSStatus
|
| 172 |
+
|
| 173 |
+
Security.SecKeychainCreate.argtypes = [
|
| 174 |
+
c_char_p,
|
| 175 |
+
c_uint32,
|
| 176 |
+
c_void_p,
|
| 177 |
+
Boolean,
|
| 178 |
+
c_void_p,
|
| 179 |
+
POINTER(SecKeychainRef),
|
| 180 |
+
]
|
| 181 |
+
Security.SecKeychainCreate.restype = OSStatus
|
| 182 |
+
|
| 183 |
+
Security.SecKeychainDelete.argtypes = [SecKeychainRef]
|
| 184 |
+
Security.SecKeychainDelete.restype = OSStatus
|
| 185 |
+
|
| 186 |
+
Security.SecPKCS12Import.argtypes = [
|
| 187 |
+
CFDataRef,
|
| 188 |
+
CFDictionaryRef,
|
| 189 |
+
POINTER(CFArrayRef),
|
| 190 |
+
]
|
| 191 |
+
Security.SecPKCS12Import.restype = OSStatus
|
| 192 |
+
|
| 193 |
+
SSLReadFunc = CFUNCTYPE(OSStatus, SSLConnectionRef, c_void_p, POINTER(c_size_t))
|
| 194 |
+
SSLWriteFunc = CFUNCTYPE(
|
| 195 |
+
OSStatus, SSLConnectionRef, POINTER(c_byte), POINTER(c_size_t)
|
| 196 |
+
)
|
| 197 |
+
|
| 198 |
+
Security.SSLSetIOFuncs.argtypes = [SSLContextRef, SSLReadFunc, SSLWriteFunc]
|
| 199 |
+
Security.SSLSetIOFuncs.restype = OSStatus
|
| 200 |
+
|
| 201 |
+
Security.SSLSetPeerID.argtypes = [SSLContextRef, c_char_p, c_size_t]
|
| 202 |
+
Security.SSLSetPeerID.restype = OSStatus
|
| 203 |
+
|
| 204 |
+
Security.SSLSetCertificate.argtypes = [SSLContextRef, CFArrayRef]
|
| 205 |
+
Security.SSLSetCertificate.restype = OSStatus
|
| 206 |
+
|
| 207 |
+
Security.SSLSetCertificateAuthorities.argtypes = [SSLContextRef, CFTypeRef, Boolean]
|
| 208 |
+
Security.SSLSetCertificateAuthorities.restype = OSStatus
|
| 209 |
+
|
| 210 |
+
Security.SSLSetConnection.argtypes = [SSLContextRef, SSLConnectionRef]
|
| 211 |
+
Security.SSLSetConnection.restype = OSStatus
|
| 212 |
+
|
| 213 |
+
Security.SSLSetPeerDomainName.argtypes = [SSLContextRef, c_char_p, c_size_t]
|
| 214 |
+
Security.SSLSetPeerDomainName.restype = OSStatus
|
| 215 |
+
|
| 216 |
+
Security.SSLHandshake.argtypes = [SSLContextRef]
|
| 217 |
+
Security.SSLHandshake.restype = OSStatus
|
| 218 |
+
|
| 219 |
+
Security.SSLRead.argtypes = [SSLContextRef, c_char_p, c_size_t, POINTER(c_size_t)]
|
| 220 |
+
Security.SSLRead.restype = OSStatus
|
| 221 |
+
|
| 222 |
+
Security.SSLWrite.argtypes = [SSLContextRef, c_char_p, c_size_t, POINTER(c_size_t)]
|
| 223 |
+
Security.SSLWrite.restype = OSStatus
|
| 224 |
+
|
| 225 |
+
Security.SSLClose.argtypes = [SSLContextRef]
|
| 226 |
+
Security.SSLClose.restype = OSStatus
|
| 227 |
+
|
| 228 |
+
Security.SSLGetNumberSupportedCiphers.argtypes = [SSLContextRef, POINTER(c_size_t)]
|
| 229 |
+
Security.SSLGetNumberSupportedCiphers.restype = OSStatus
|
| 230 |
+
|
| 231 |
+
Security.SSLGetSupportedCiphers.argtypes = [
|
| 232 |
+
SSLContextRef,
|
| 233 |
+
POINTER(SSLCipherSuite),
|
| 234 |
+
POINTER(c_size_t),
|
| 235 |
+
]
|
| 236 |
+
Security.SSLGetSupportedCiphers.restype = OSStatus
|
| 237 |
+
|
| 238 |
+
Security.SSLSetEnabledCiphers.argtypes = [
|
| 239 |
+
SSLContextRef,
|
| 240 |
+
POINTER(SSLCipherSuite),
|
| 241 |
+
c_size_t,
|
| 242 |
+
]
|
| 243 |
+
Security.SSLSetEnabledCiphers.restype = OSStatus
|
| 244 |
+
|
| 245 |
+
Security.SSLGetNumberEnabledCiphers.argtype = [SSLContextRef, POINTER(c_size_t)]
|
| 246 |
+
Security.SSLGetNumberEnabledCiphers.restype = OSStatus
|
| 247 |
+
|
| 248 |
+
Security.SSLGetEnabledCiphers.argtypes = [
|
| 249 |
+
SSLContextRef,
|
| 250 |
+
POINTER(SSLCipherSuite),
|
| 251 |
+
POINTER(c_size_t),
|
| 252 |
+
]
|
| 253 |
+
Security.SSLGetEnabledCiphers.restype = OSStatus
|
| 254 |
+
|
| 255 |
+
Security.SSLGetNegotiatedCipher.argtypes = [SSLContextRef, POINTER(SSLCipherSuite)]
|
| 256 |
+
Security.SSLGetNegotiatedCipher.restype = OSStatus
|
| 257 |
+
|
| 258 |
+
Security.SSLGetNegotiatedProtocolVersion.argtypes = [
|
| 259 |
+
SSLContextRef,
|
| 260 |
+
POINTER(SSLProtocol),
|
| 261 |
+
]
|
| 262 |
+
Security.SSLGetNegotiatedProtocolVersion.restype = OSStatus
|
| 263 |
+
|
| 264 |
+
Security.SSLCopyPeerTrust.argtypes = [SSLContextRef, POINTER(SecTrustRef)]
|
| 265 |
+
Security.SSLCopyPeerTrust.restype = OSStatus
|
| 266 |
+
|
| 267 |
+
Security.SecTrustSetAnchorCertificates.argtypes = [SecTrustRef, CFArrayRef]
|
| 268 |
+
Security.SecTrustSetAnchorCertificates.restype = OSStatus
|
| 269 |
+
|
| 270 |
+
Security.SecTrustSetAnchorCertificatesOnly.argstypes = [SecTrustRef, Boolean]
|
| 271 |
+
Security.SecTrustSetAnchorCertificatesOnly.restype = OSStatus
|
| 272 |
+
|
| 273 |
+
Security.SecTrustEvaluate.argtypes = [SecTrustRef, POINTER(SecTrustResultType)]
|
| 274 |
+
Security.SecTrustEvaluate.restype = OSStatus
|
| 275 |
+
|
| 276 |
+
Security.SecTrustGetCertificateCount.argtypes = [SecTrustRef]
|
| 277 |
+
Security.SecTrustGetCertificateCount.restype = CFIndex
|
| 278 |
+
|
| 279 |
+
Security.SecTrustGetCertificateAtIndex.argtypes = [SecTrustRef, CFIndex]
|
| 280 |
+
Security.SecTrustGetCertificateAtIndex.restype = SecCertificateRef
|
| 281 |
+
|
| 282 |
+
Security.SSLCreateContext.argtypes = [
|
| 283 |
+
CFAllocatorRef,
|
| 284 |
+
SSLProtocolSide,
|
| 285 |
+
SSLConnectionType,
|
| 286 |
+
]
|
| 287 |
+
Security.SSLCreateContext.restype = SSLContextRef
|
| 288 |
+
|
| 289 |
+
Security.SSLSetSessionOption.argtypes = [SSLContextRef, SSLSessionOption, Boolean]
|
| 290 |
+
Security.SSLSetSessionOption.restype = OSStatus
|
| 291 |
+
|
| 292 |
+
Security.SSLSetProtocolVersionMin.argtypes = [SSLContextRef, SSLProtocol]
|
| 293 |
+
Security.SSLSetProtocolVersionMin.restype = OSStatus
|
| 294 |
+
|
| 295 |
+
Security.SSLSetProtocolVersionMax.argtypes = [SSLContextRef, SSLProtocol]
|
| 296 |
+
Security.SSLSetProtocolVersionMax.restype = OSStatus
|
| 297 |
+
|
| 298 |
+
try:
|
| 299 |
+
Security.SSLSetALPNProtocols.argtypes = [SSLContextRef, CFArrayRef]
|
| 300 |
+
Security.SSLSetALPNProtocols.restype = OSStatus
|
| 301 |
+
except AttributeError:
|
| 302 |
+
# Supported only in 10.12+
|
| 303 |
+
pass
|
| 304 |
+
|
| 305 |
+
Security.SecCopyErrorMessageString.argtypes = [OSStatus, c_void_p]
|
| 306 |
+
Security.SecCopyErrorMessageString.restype = CFStringRef
|
| 307 |
+
|
| 308 |
+
Security.SSLReadFunc = SSLReadFunc
|
| 309 |
+
Security.SSLWriteFunc = SSLWriteFunc
|
| 310 |
+
Security.SSLContextRef = SSLContextRef
|
| 311 |
+
Security.SSLProtocol = SSLProtocol
|
| 312 |
+
Security.SSLCipherSuite = SSLCipherSuite
|
| 313 |
+
Security.SecIdentityRef = SecIdentityRef
|
| 314 |
+
Security.SecKeychainRef = SecKeychainRef
|
| 315 |
+
Security.SecTrustRef = SecTrustRef
|
| 316 |
+
Security.SecTrustResultType = SecTrustResultType
|
| 317 |
+
Security.SecExternalFormat = SecExternalFormat
|
| 318 |
+
Security.OSStatus = OSStatus
|
| 319 |
+
|
| 320 |
+
Security.kSecImportExportPassphrase = CFStringRef.in_dll(
|
| 321 |
+
Security, "kSecImportExportPassphrase"
|
| 322 |
+
)
|
| 323 |
+
Security.kSecImportItemIdentity = CFStringRef.in_dll(
|
| 324 |
+
Security, "kSecImportItemIdentity"
|
| 325 |
+
)
|
| 326 |
+
|
| 327 |
+
# CoreFoundation time!
|
| 328 |
+
CoreFoundation.CFRetain.argtypes = [CFTypeRef]
|
| 329 |
+
CoreFoundation.CFRetain.restype = CFTypeRef
|
| 330 |
+
|
| 331 |
+
CoreFoundation.CFRelease.argtypes = [CFTypeRef]
|
| 332 |
+
CoreFoundation.CFRelease.restype = None
|
| 333 |
+
|
| 334 |
+
CoreFoundation.CFGetTypeID.argtypes = [CFTypeRef]
|
| 335 |
+
CoreFoundation.CFGetTypeID.restype = CFTypeID
|
| 336 |
+
|
| 337 |
+
CoreFoundation.CFStringCreateWithCString.argtypes = [
|
| 338 |
+
CFAllocatorRef,
|
| 339 |
+
c_char_p,
|
| 340 |
+
CFStringEncoding,
|
| 341 |
+
]
|
| 342 |
+
CoreFoundation.CFStringCreateWithCString.restype = CFStringRef
|
| 343 |
+
|
| 344 |
+
CoreFoundation.CFStringGetCStringPtr.argtypes = [CFStringRef, CFStringEncoding]
|
| 345 |
+
CoreFoundation.CFStringGetCStringPtr.restype = c_char_p
|
| 346 |
+
|
| 347 |
+
CoreFoundation.CFStringGetCString.argtypes = [
|
| 348 |
+
CFStringRef,
|
| 349 |
+
c_char_p,
|
| 350 |
+
CFIndex,
|
| 351 |
+
CFStringEncoding,
|
| 352 |
+
]
|
| 353 |
+
CoreFoundation.CFStringGetCString.restype = c_bool
|
| 354 |
+
|
| 355 |
+
CoreFoundation.CFDataCreate.argtypes = [CFAllocatorRef, c_char_p, CFIndex]
|
| 356 |
+
CoreFoundation.CFDataCreate.restype = CFDataRef
|
| 357 |
+
|
| 358 |
+
CoreFoundation.CFDataGetLength.argtypes = [CFDataRef]
|
| 359 |
+
CoreFoundation.CFDataGetLength.restype = CFIndex
|
| 360 |
+
|
| 361 |
+
CoreFoundation.CFDataGetBytePtr.argtypes = [CFDataRef]
|
| 362 |
+
CoreFoundation.CFDataGetBytePtr.restype = c_void_p
|
| 363 |
+
|
| 364 |
+
CoreFoundation.CFDictionaryCreate.argtypes = [
|
| 365 |
+
CFAllocatorRef,
|
| 366 |
+
POINTER(CFTypeRef),
|
| 367 |
+
POINTER(CFTypeRef),
|
| 368 |
+
CFIndex,
|
| 369 |
+
CFDictionaryKeyCallBacks,
|
| 370 |
+
CFDictionaryValueCallBacks,
|
| 371 |
+
]
|
| 372 |
+
CoreFoundation.CFDictionaryCreate.restype = CFDictionaryRef
|
| 373 |
+
|
| 374 |
+
CoreFoundation.CFDictionaryGetValue.argtypes = [CFDictionaryRef, CFTypeRef]
|
| 375 |
+
CoreFoundation.CFDictionaryGetValue.restype = CFTypeRef
|
| 376 |
+
|
| 377 |
+
CoreFoundation.CFArrayCreate.argtypes = [
|
| 378 |
+
CFAllocatorRef,
|
| 379 |
+
POINTER(CFTypeRef),
|
| 380 |
+
CFIndex,
|
| 381 |
+
CFArrayCallBacks,
|
| 382 |
+
]
|
| 383 |
+
CoreFoundation.CFArrayCreate.restype = CFArrayRef
|
| 384 |
+
|
| 385 |
+
CoreFoundation.CFArrayCreateMutable.argtypes = [
|
| 386 |
+
CFAllocatorRef,
|
| 387 |
+
CFIndex,
|
| 388 |
+
CFArrayCallBacks,
|
| 389 |
+
]
|
| 390 |
+
CoreFoundation.CFArrayCreateMutable.restype = CFMutableArrayRef
|
| 391 |
+
|
| 392 |
+
CoreFoundation.CFArrayAppendValue.argtypes = [CFMutableArrayRef, c_void_p]
|
| 393 |
+
CoreFoundation.CFArrayAppendValue.restype = None
|
| 394 |
+
|
| 395 |
+
CoreFoundation.CFArrayGetCount.argtypes = [CFArrayRef]
|
| 396 |
+
CoreFoundation.CFArrayGetCount.restype = CFIndex
|
| 397 |
+
|
| 398 |
+
CoreFoundation.CFArrayGetValueAtIndex.argtypes = [CFArrayRef, CFIndex]
|
| 399 |
+
CoreFoundation.CFArrayGetValueAtIndex.restype = c_void_p
|
| 400 |
+
|
| 401 |
+
CoreFoundation.kCFAllocatorDefault = CFAllocatorRef.in_dll(
|
| 402 |
+
CoreFoundation, "kCFAllocatorDefault"
|
| 403 |
+
)
|
| 404 |
+
CoreFoundation.kCFTypeArrayCallBacks = c_void_p.in_dll(
|
| 405 |
+
CoreFoundation, "kCFTypeArrayCallBacks"
|
| 406 |
+
)
|
| 407 |
+
CoreFoundation.kCFTypeDictionaryKeyCallBacks = c_void_p.in_dll(
|
| 408 |
+
CoreFoundation, "kCFTypeDictionaryKeyCallBacks"
|
| 409 |
+
)
|
| 410 |
+
CoreFoundation.kCFTypeDictionaryValueCallBacks = c_void_p.in_dll(
|
| 411 |
+
CoreFoundation, "kCFTypeDictionaryValueCallBacks"
|
| 412 |
+
)
|
| 413 |
+
|
| 414 |
+
CoreFoundation.CFTypeRef = CFTypeRef
|
| 415 |
+
CoreFoundation.CFArrayRef = CFArrayRef
|
| 416 |
+
CoreFoundation.CFStringRef = CFStringRef
|
| 417 |
+
CoreFoundation.CFDictionaryRef = CFDictionaryRef
|
| 418 |
+
|
| 419 |
+
except (AttributeError):
|
| 420 |
+
raise ImportError("Error initializing ctypes")
|
| 421 |
+
|
| 422 |
+
|
| 423 |
+
class CFConst(object):
|
| 424 |
+
"""
|
| 425 |
+
A class object that acts as essentially a namespace for CoreFoundation
|
| 426 |
+
constants.
|
| 427 |
+
"""
|
| 428 |
+
|
| 429 |
+
kCFStringEncodingUTF8 = CFStringEncoding(0x08000100)
|
| 430 |
+
|
| 431 |
+
|
| 432 |
+
class SecurityConst(object):
|
| 433 |
+
"""
|
| 434 |
+
A class object that acts as essentially a namespace for Security constants.
|
| 435 |
+
"""
|
| 436 |
+
|
| 437 |
+
kSSLSessionOptionBreakOnServerAuth = 0
|
| 438 |
+
|
| 439 |
+
kSSLProtocol2 = 1
|
| 440 |
+
kSSLProtocol3 = 2
|
| 441 |
+
kTLSProtocol1 = 4
|
| 442 |
+
kTLSProtocol11 = 7
|
| 443 |
+
kTLSProtocol12 = 8
|
| 444 |
+
# SecureTransport does not support TLS 1.3 even if there's a constant for it
|
| 445 |
+
kTLSProtocol13 = 10
|
| 446 |
+
kTLSProtocolMaxSupported = 999
|
| 447 |
+
|
| 448 |
+
kSSLClientSide = 1
|
| 449 |
+
kSSLStreamType = 0
|
| 450 |
+
|
| 451 |
+
kSecFormatPEMSequence = 10
|
| 452 |
+
|
| 453 |
+
kSecTrustResultInvalid = 0
|
| 454 |
+
kSecTrustResultProceed = 1
|
| 455 |
+
# This gap is present on purpose: this was kSecTrustResultConfirm, which
|
| 456 |
+
# is deprecated.
|
| 457 |
+
kSecTrustResultDeny = 3
|
| 458 |
+
kSecTrustResultUnspecified = 4
|
| 459 |
+
kSecTrustResultRecoverableTrustFailure = 5
|
| 460 |
+
kSecTrustResultFatalTrustFailure = 6
|
| 461 |
+
kSecTrustResultOtherError = 7
|
| 462 |
+
|
| 463 |
+
errSSLProtocol = -9800
|
| 464 |
+
errSSLWouldBlock = -9803
|
| 465 |
+
errSSLClosedGraceful = -9805
|
| 466 |
+
errSSLClosedNoNotify = -9816
|
| 467 |
+
errSSLClosedAbort = -9806
|
| 468 |
+
|
| 469 |
+
errSSLXCertChainInvalid = -9807
|
| 470 |
+
errSSLCrypto = -9809
|
| 471 |
+
errSSLInternal = -9810
|
| 472 |
+
errSSLCertExpired = -9814
|
| 473 |
+
errSSLCertNotYetValid = -9815
|
| 474 |
+
errSSLUnknownRootCert = -9812
|
| 475 |
+
errSSLNoRootCert = -9813
|
| 476 |
+
errSSLHostNameMismatch = -9843
|
| 477 |
+
errSSLPeerHandshakeFail = -9824
|
| 478 |
+
errSSLPeerUserCancelled = -9839
|
| 479 |
+
errSSLWeakPeerEphemeralDHKey = -9850
|
| 480 |
+
errSSLServerAuthCompleted = -9841
|
| 481 |
+
errSSLRecordOverflow = -9847
|
| 482 |
+
|
| 483 |
+
errSecVerifyFailed = -67808
|
| 484 |
+
errSecNoTrustSettings = -25263
|
| 485 |
+
errSecItemNotFound = -25300
|
| 486 |
+
errSecInvalidTrustSettings = -25262
|
| 487 |
+
|
| 488 |
+
# Cipher suites. We only pick the ones our default cipher string allows.
|
| 489 |
+
# Source: https://developer.apple.com/documentation/security/1550981-ssl_cipher_suite_values
|
| 490 |
+
TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 = 0xC02C
|
| 491 |
+
TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 = 0xC030
|
| 492 |
+
TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 = 0xC02B
|
| 493 |
+
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 = 0xC02F
|
| 494 |
+
TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 = 0xCCA9
|
| 495 |
+
TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 = 0xCCA8
|
| 496 |
+
TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 = 0x009F
|
| 497 |
+
TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 = 0x009E
|
| 498 |
+
TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 = 0xC024
|
| 499 |
+
TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 = 0xC028
|
| 500 |
+
TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA = 0xC00A
|
| 501 |
+
TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA = 0xC014
|
| 502 |
+
TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 = 0x006B
|
| 503 |
+
TLS_DHE_RSA_WITH_AES_256_CBC_SHA = 0x0039
|
| 504 |
+
TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 = 0xC023
|
| 505 |
+
TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 = 0xC027
|
| 506 |
+
TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA = 0xC009
|
| 507 |
+
TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA = 0xC013
|
| 508 |
+
TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 = 0x0067
|
| 509 |
+
TLS_DHE_RSA_WITH_AES_128_CBC_SHA = 0x0033
|
| 510 |
+
TLS_RSA_WITH_AES_256_GCM_SHA384 = 0x009D
|
| 511 |
+
TLS_RSA_WITH_AES_128_GCM_SHA256 = 0x009C
|
| 512 |
+
TLS_RSA_WITH_AES_256_CBC_SHA256 = 0x003D
|
| 513 |
+
TLS_RSA_WITH_AES_128_CBC_SHA256 = 0x003C
|
| 514 |
+
TLS_RSA_WITH_AES_256_CBC_SHA = 0x0035
|
| 515 |
+
TLS_RSA_WITH_AES_128_CBC_SHA = 0x002F
|
| 516 |
+
TLS_AES_128_GCM_SHA256 = 0x1301
|
| 517 |
+
TLS_AES_256_GCM_SHA384 = 0x1302
|
| 518 |
+
TLS_AES_128_CCM_8_SHA256 = 0x1305
|
| 519 |
+
TLS_AES_128_CCM_SHA256 = 0x1304
|
evalkit_llava/lib/python3.10/site-packages/pip/_vendor/urllib3/contrib/_securetransport/low_level.py
ADDED
|
@@ -0,0 +1,397 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
+
|
| 4 |
+
These are Python functions that are not directly related to the high-level APIs
|
| 5 |
+
but are necessary to get them to work. They include a whole bunch of low-level
|
| 6 |
+
CoreFoundation messing about and memory management. The concerns in this module
|
| 7 |
+
are almost entirely about trying to avoid memory leaks and providing
|
| 8 |
+
appropriate and useful assistance to the higher-level code.
|
| 9 |
+
"""
|
| 10 |
+
import base64
|
| 11 |
+
import ctypes
|
| 12 |
+
import itertools
|
| 13 |
+
import os
|
| 14 |
+
import re
|
| 15 |
+
import ssl
|
| 16 |
+
import struct
|
| 17 |
+
import tempfile
|
| 18 |
+
|
| 19 |
+
from .bindings import CFConst, CoreFoundation, Security
|
| 20 |
+
|
| 21 |
+
# This regular expression is used to grab PEM data out of a PEM bundle.
|
| 22 |
+
_PEM_CERTS_RE = re.compile(
|
| 23 |
+
b"-----BEGIN CERTIFICATE-----\n(.*?)\n-----END CERTIFICATE-----", re.DOTALL
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def _cf_data_from_bytes(bytestring):
|
| 28 |
+
"""
|
| 29 |
+
Given a bytestring, create a CFData object from it. This CFData object must
|
| 30 |
+
be CFReleased by the caller.
|
| 31 |
+
"""
|
| 32 |
+
return CoreFoundation.CFDataCreate(
|
| 33 |
+
CoreFoundation.kCFAllocatorDefault, bytestring, len(bytestring)
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def _cf_dictionary_from_tuples(tuples):
|
| 38 |
+
"""
|
| 39 |
+
Given a list of Python tuples, create an associated CFDictionary.
|
| 40 |
+
"""
|
| 41 |
+
dictionary_size = len(tuples)
|
| 42 |
+
|
| 43 |
+
# We need to get the dictionary keys and values out in the same order.
|
| 44 |
+
keys = (t[0] for t in tuples)
|
| 45 |
+
values = (t[1] for t in tuples)
|
| 46 |
+
cf_keys = (CoreFoundation.CFTypeRef * dictionary_size)(*keys)
|
| 47 |
+
cf_values = (CoreFoundation.CFTypeRef * dictionary_size)(*values)
|
| 48 |
+
|
| 49 |
+
return CoreFoundation.CFDictionaryCreate(
|
| 50 |
+
CoreFoundation.kCFAllocatorDefault,
|
| 51 |
+
cf_keys,
|
| 52 |
+
cf_values,
|
| 53 |
+
dictionary_size,
|
| 54 |
+
CoreFoundation.kCFTypeDictionaryKeyCallBacks,
|
| 55 |
+
CoreFoundation.kCFTypeDictionaryValueCallBacks,
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def _cfstr(py_bstr):
|
| 60 |
+
"""
|
| 61 |
+
Given a Python binary data, create a CFString.
|
| 62 |
+
The string must be CFReleased by the caller.
|
| 63 |
+
"""
|
| 64 |
+
c_str = ctypes.c_char_p(py_bstr)
|
| 65 |
+
cf_str = CoreFoundation.CFStringCreateWithCString(
|
| 66 |
+
CoreFoundation.kCFAllocatorDefault,
|
| 67 |
+
c_str,
|
| 68 |
+
CFConst.kCFStringEncodingUTF8,
|
| 69 |
+
)
|
| 70 |
+
return cf_str
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def _create_cfstring_array(lst):
|
| 74 |
+
"""
|
| 75 |
+
Given a list of Python binary data, create an associated CFMutableArray.
|
| 76 |
+
The array must be CFReleased by the caller.
|
| 77 |
+
|
| 78 |
+
Raises an ssl.SSLError on failure.
|
| 79 |
+
"""
|
| 80 |
+
cf_arr = None
|
| 81 |
+
try:
|
| 82 |
+
cf_arr = CoreFoundation.CFArrayCreateMutable(
|
| 83 |
+
CoreFoundation.kCFAllocatorDefault,
|
| 84 |
+
0,
|
| 85 |
+
ctypes.byref(CoreFoundation.kCFTypeArrayCallBacks),
|
| 86 |
+
)
|
| 87 |
+
if not cf_arr:
|
| 88 |
+
raise MemoryError("Unable to allocate memory!")
|
| 89 |
+
for item in lst:
|
| 90 |
+
cf_str = _cfstr(item)
|
| 91 |
+
if not cf_str:
|
| 92 |
+
raise MemoryError("Unable to allocate memory!")
|
| 93 |
+
try:
|
| 94 |
+
CoreFoundation.CFArrayAppendValue(cf_arr, cf_str)
|
| 95 |
+
finally:
|
| 96 |
+
CoreFoundation.CFRelease(cf_str)
|
| 97 |
+
except BaseException as e:
|
| 98 |
+
if cf_arr:
|
| 99 |
+
CoreFoundation.CFRelease(cf_arr)
|
| 100 |
+
raise ssl.SSLError("Unable to allocate array: %s" % (e,))
|
| 101 |
+
return cf_arr
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
def _cf_string_to_unicode(value):
|
| 105 |
+
"""
|
| 106 |
+
Creates a Unicode string from a CFString object. Used entirely for error
|
| 107 |
+
reporting.
|
| 108 |
+
|
| 109 |
+
Yes, it annoys me quite a lot that this function is this complex.
|
| 110 |
+
"""
|
| 111 |
+
value_as_void_p = ctypes.cast(value, ctypes.POINTER(ctypes.c_void_p))
|
| 112 |
+
|
| 113 |
+
string = CoreFoundation.CFStringGetCStringPtr(
|
| 114 |
+
value_as_void_p, CFConst.kCFStringEncodingUTF8
|
| 115 |
+
)
|
| 116 |
+
if string is None:
|
| 117 |
+
buffer = ctypes.create_string_buffer(1024)
|
| 118 |
+
result = CoreFoundation.CFStringGetCString(
|
| 119 |
+
value_as_void_p, buffer, 1024, CFConst.kCFStringEncodingUTF8
|
| 120 |
+
)
|
| 121 |
+
if not result:
|
| 122 |
+
raise OSError("Error copying C string from CFStringRef")
|
| 123 |
+
string = buffer.value
|
| 124 |
+
if string is not None:
|
| 125 |
+
string = string.decode("utf-8")
|
| 126 |
+
return string
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
def _assert_no_error(error, exception_class=None):
|
| 130 |
+
"""
|
| 131 |
+
Checks the return code and throws an exception if there is an error to
|
| 132 |
+
report
|
| 133 |
+
"""
|
| 134 |
+
if error == 0:
|
| 135 |
+
return
|
| 136 |
+
|
| 137 |
+
cf_error_string = Security.SecCopyErrorMessageString(error, None)
|
| 138 |
+
output = _cf_string_to_unicode(cf_error_string)
|
| 139 |
+
CoreFoundation.CFRelease(cf_error_string)
|
| 140 |
+
|
| 141 |
+
if output is None or output == u"":
|
| 142 |
+
output = u"OSStatus %s" % error
|
| 143 |
+
|
| 144 |
+
if exception_class is None:
|
| 145 |
+
exception_class = ssl.SSLError
|
| 146 |
+
|
| 147 |
+
raise exception_class(output)
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
def _cert_array_from_pem(pem_bundle):
|
| 151 |
+
"""
|
| 152 |
+
Given a bundle of certs in PEM format, turns them into a CFArray of certs
|
| 153 |
+
that can be used to validate a cert chain.
|
| 154 |
+
"""
|
| 155 |
+
# Normalize the PEM bundle's line endings.
|
| 156 |
+
pem_bundle = pem_bundle.replace(b"\r\n", b"\n")
|
| 157 |
+
|
| 158 |
+
der_certs = [
|
| 159 |
+
base64.b64decode(match.group(1)) for match in _PEM_CERTS_RE.finditer(pem_bundle)
|
| 160 |
+
]
|
| 161 |
+
if not der_certs:
|
| 162 |
+
raise ssl.SSLError("No root certificates specified")
|
| 163 |
+
|
| 164 |
+
cert_array = CoreFoundation.CFArrayCreateMutable(
|
| 165 |
+
CoreFoundation.kCFAllocatorDefault,
|
| 166 |
+
0,
|
| 167 |
+
ctypes.byref(CoreFoundation.kCFTypeArrayCallBacks),
|
| 168 |
+
)
|
| 169 |
+
if not cert_array:
|
| 170 |
+
raise ssl.SSLError("Unable to allocate memory!")
|
| 171 |
+
|
| 172 |
+
try:
|
| 173 |
+
for der_bytes in der_certs:
|
| 174 |
+
certdata = _cf_data_from_bytes(der_bytes)
|
| 175 |
+
if not certdata:
|
| 176 |
+
raise ssl.SSLError("Unable to allocate memory!")
|
| 177 |
+
cert = Security.SecCertificateCreateWithData(
|
| 178 |
+
CoreFoundation.kCFAllocatorDefault, certdata
|
| 179 |
+
)
|
| 180 |
+
CoreFoundation.CFRelease(certdata)
|
| 181 |
+
if not cert:
|
| 182 |
+
raise ssl.SSLError("Unable to build cert object!")
|
| 183 |
+
|
| 184 |
+
CoreFoundation.CFArrayAppendValue(cert_array, cert)
|
| 185 |
+
CoreFoundation.CFRelease(cert)
|
| 186 |
+
except Exception:
|
| 187 |
+
# We need to free the array before the exception bubbles further.
|
| 188 |
+
# We only want to do that if an error occurs: otherwise, the caller
|
| 189 |
+
# should free.
|
| 190 |
+
CoreFoundation.CFRelease(cert_array)
|
| 191 |
+
raise
|
| 192 |
+
|
| 193 |
+
return cert_array
|
| 194 |
+
|
| 195 |
+
|
| 196 |
+
def _is_cert(item):
|
| 197 |
+
"""
|
| 198 |
+
Returns True if a given CFTypeRef is a certificate.
|
| 199 |
+
"""
|
| 200 |
+
expected = Security.SecCertificateGetTypeID()
|
| 201 |
+
return CoreFoundation.CFGetTypeID(item) == expected
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
def _is_identity(item):
|
| 205 |
+
"""
|
| 206 |
+
Returns True if a given CFTypeRef is an identity.
|
| 207 |
+
"""
|
| 208 |
+
expected = Security.SecIdentityGetTypeID()
|
| 209 |
+
return CoreFoundation.CFGetTypeID(item) == expected
|
| 210 |
+
|
| 211 |
+
|
| 212 |
+
def _temporary_keychain():
|
| 213 |
+
"""
|
| 214 |
+
This function creates a temporary Mac keychain that we can use to work with
|
| 215 |
+
credentials. This keychain uses a one-time password and a temporary file to
|
| 216 |
+
store the data. We expect to have one keychain per socket. The returned
|
| 217 |
+
SecKeychainRef must be freed by the caller, including calling
|
| 218 |
+
SecKeychainDelete.
|
| 219 |
+
|
| 220 |
+
Returns a tuple of the SecKeychainRef and the path to the temporary
|
| 221 |
+
directory that contains it.
|
| 222 |
+
"""
|
| 223 |
+
# Unfortunately, SecKeychainCreate requires a path to a keychain. This
|
| 224 |
+
# means we cannot use mkstemp to use a generic temporary file. Instead,
|
| 225 |
+
# we're going to create a temporary directory and a filename to use there.
|
| 226 |
+
# This filename will be 8 random bytes expanded into base64. We also need
|
| 227 |
+
# some random bytes to password-protect the keychain we're creating, so we
|
| 228 |
+
# ask for 40 random bytes.
|
| 229 |
+
random_bytes = os.urandom(40)
|
| 230 |
+
filename = base64.b16encode(random_bytes[:8]).decode("utf-8")
|
| 231 |
+
password = base64.b16encode(random_bytes[8:]) # Must be valid UTF-8
|
| 232 |
+
tempdirectory = tempfile.mkdtemp()
|
| 233 |
+
|
| 234 |
+
keychain_path = os.path.join(tempdirectory, filename).encode("utf-8")
|
| 235 |
+
|
| 236 |
+
# We now want to create the keychain itself.
|
| 237 |
+
keychain = Security.SecKeychainRef()
|
| 238 |
+
status = Security.SecKeychainCreate(
|
| 239 |
+
keychain_path, len(password), password, False, None, ctypes.byref(keychain)
|
| 240 |
+
)
|
| 241 |
+
_assert_no_error(status)
|
| 242 |
+
|
| 243 |
+
# Having created the keychain, we want to pass it off to the caller.
|
| 244 |
+
return keychain, tempdirectory
|
| 245 |
+
|
| 246 |
+
|
| 247 |
+
def _load_items_from_file(keychain, path):
|
| 248 |
+
"""
|
| 249 |
+
Given a single file, loads all the trust objects from it into arrays and
|
| 250 |
+
the keychain.
|
| 251 |
+
Returns a tuple of lists: the first list is a list of identities, the
|
| 252 |
+
second a list of certs.
|
| 253 |
+
"""
|
| 254 |
+
certificates = []
|
| 255 |
+
identities = []
|
| 256 |
+
result_array = None
|
| 257 |
+
|
| 258 |
+
with open(path, "rb") as f:
|
| 259 |
+
raw_filedata = f.read()
|
| 260 |
+
|
| 261 |
+
try:
|
| 262 |
+
filedata = CoreFoundation.CFDataCreate(
|
| 263 |
+
CoreFoundation.kCFAllocatorDefault, raw_filedata, len(raw_filedata)
|
| 264 |
+
)
|
| 265 |
+
result_array = CoreFoundation.CFArrayRef()
|
| 266 |
+
result = Security.SecItemImport(
|
| 267 |
+
filedata, # cert data
|
| 268 |
+
None, # Filename, leaving it out for now
|
| 269 |
+
None, # What the type of the file is, we don't care
|
| 270 |
+
None, # what's in the file, we don't care
|
| 271 |
+
0, # import flags
|
| 272 |
+
None, # key params, can include passphrase in the future
|
| 273 |
+
keychain, # The keychain to insert into
|
| 274 |
+
ctypes.byref(result_array), # Results
|
| 275 |
+
)
|
| 276 |
+
_assert_no_error(result)
|
| 277 |
+
|
| 278 |
+
# A CFArray is not very useful to us as an intermediary
|
| 279 |
+
# representation, so we are going to extract the objects we want
|
| 280 |
+
# and then free the array. We don't need to keep hold of keys: the
|
| 281 |
+
# keychain already has them!
|
| 282 |
+
result_count = CoreFoundation.CFArrayGetCount(result_array)
|
| 283 |
+
for index in range(result_count):
|
| 284 |
+
item = CoreFoundation.CFArrayGetValueAtIndex(result_array, index)
|
| 285 |
+
item = ctypes.cast(item, CoreFoundation.CFTypeRef)
|
| 286 |
+
|
| 287 |
+
if _is_cert(item):
|
| 288 |
+
CoreFoundation.CFRetain(item)
|
| 289 |
+
certificates.append(item)
|
| 290 |
+
elif _is_identity(item):
|
| 291 |
+
CoreFoundation.CFRetain(item)
|
| 292 |
+
identities.append(item)
|
| 293 |
+
finally:
|
| 294 |
+
if result_array:
|
| 295 |
+
CoreFoundation.CFRelease(result_array)
|
| 296 |
+
|
| 297 |
+
CoreFoundation.CFRelease(filedata)
|
| 298 |
+
|
| 299 |
+
return (identities, certificates)
|
| 300 |
+
|
| 301 |
+
|
| 302 |
+
def _load_client_cert_chain(keychain, *paths):
|
| 303 |
+
"""
|
| 304 |
+
Load certificates and maybe keys from a number of files. Has the end goal
|
| 305 |
+
of returning a CFArray containing one SecIdentityRef, and then zero or more
|
| 306 |
+
SecCertificateRef objects, suitable for use as a client certificate trust
|
| 307 |
+
chain.
|
| 308 |
+
"""
|
| 309 |
+
# Ok, the strategy.
|
| 310 |
+
#
|
| 311 |
+
# This relies on knowing that macOS will not give you a SecIdentityRef
|
| 312 |
+
# unless you have imported a key into a keychain. This is a somewhat
|
| 313 |
+
# artificial limitation of macOS (for example, it doesn't necessarily
|
| 314 |
+
# affect iOS), but there is nothing inside Security.framework that lets you
|
| 315 |
+
# get a SecIdentityRef without having a key in a keychain.
|
| 316 |
+
#
|
| 317 |
+
# So the policy here is we take all the files and iterate them in order.
|
| 318 |
+
# Each one will use SecItemImport to have one or more objects loaded from
|
| 319 |
+
# it. We will also point at a keychain that macOS can use to work with the
|
| 320 |
+
# private key.
|
| 321 |
+
#
|
| 322 |
+
# Once we have all the objects, we'll check what we actually have. If we
|
| 323 |
+
# already have a SecIdentityRef in hand, fab: we'll use that. Otherwise,
|
| 324 |
+
# we'll take the first certificate (which we assume to be our leaf) and
|
| 325 |
+
# ask the keychain to give us a SecIdentityRef with that cert's associated
|
| 326 |
+
# key.
|
| 327 |
+
#
|
| 328 |
+
# We'll then return a CFArray containing the trust chain: one
|
| 329 |
+
# SecIdentityRef and then zero-or-more SecCertificateRef objects. The
|
| 330 |
+
# responsibility for freeing this CFArray will be with the caller. This
|
| 331 |
+
# CFArray must remain alive for the entire connection, so in practice it
|
| 332 |
+
# will be stored with a single SSLSocket, along with the reference to the
|
| 333 |
+
# keychain.
|
| 334 |
+
certificates = []
|
| 335 |
+
identities = []
|
| 336 |
+
|
| 337 |
+
# Filter out bad paths.
|
| 338 |
+
paths = (path for path in paths if path)
|
| 339 |
+
|
| 340 |
+
try:
|
| 341 |
+
for file_path in paths:
|
| 342 |
+
new_identities, new_certs = _load_items_from_file(keychain, file_path)
|
| 343 |
+
identities.extend(new_identities)
|
| 344 |
+
certificates.extend(new_certs)
|
| 345 |
+
|
| 346 |
+
# Ok, we have everything. The question is: do we have an identity? If
|
| 347 |
+
# not, we want to grab one from the first cert we have.
|
| 348 |
+
if not identities:
|
| 349 |
+
new_identity = Security.SecIdentityRef()
|
| 350 |
+
status = Security.SecIdentityCreateWithCertificate(
|
| 351 |
+
keychain, certificates[0], ctypes.byref(new_identity)
|
| 352 |
+
)
|
| 353 |
+
_assert_no_error(status)
|
| 354 |
+
identities.append(new_identity)
|
| 355 |
+
|
| 356 |
+
# We now want to release the original certificate, as we no longer
|
| 357 |
+
# need it.
|
| 358 |
+
CoreFoundation.CFRelease(certificates.pop(0))
|
| 359 |
+
|
| 360 |
+
# We now need to build a new CFArray that holds the trust chain.
|
| 361 |
+
trust_chain = CoreFoundation.CFArrayCreateMutable(
|
| 362 |
+
CoreFoundation.kCFAllocatorDefault,
|
| 363 |
+
0,
|
| 364 |
+
ctypes.byref(CoreFoundation.kCFTypeArrayCallBacks),
|
| 365 |
+
)
|
| 366 |
+
for item in itertools.chain(identities, certificates):
|
| 367 |
+
# ArrayAppendValue does a CFRetain on the item. That's fine,
|
| 368 |
+
# because the finally block will release our other refs to them.
|
| 369 |
+
CoreFoundation.CFArrayAppendValue(trust_chain, item)
|
| 370 |
+
|
| 371 |
+
return trust_chain
|
| 372 |
+
finally:
|
| 373 |
+
for obj in itertools.chain(identities, certificates):
|
| 374 |
+
CoreFoundation.CFRelease(obj)
|
| 375 |
+
|
| 376 |
+
|
| 377 |
+
TLS_PROTOCOL_VERSIONS = {
|
| 378 |
+
"SSLv2": (0, 2),
|
| 379 |
+
"SSLv3": (3, 0),
|
| 380 |
+
"TLSv1": (3, 1),
|
| 381 |
+
"TLSv1.1": (3, 2),
|
| 382 |
+
"TLSv1.2": (3, 3),
|
| 383 |
+
}
|
| 384 |
+
|
| 385 |
+
|
| 386 |
+
def _build_tls_unknown_ca_alert(version):
|
| 387 |
+
"""
|
| 388 |
+
Builds a TLS alert record for an unknown CA.
|
| 389 |
+
"""
|
| 390 |
+
ver_maj, ver_min = TLS_PROTOCOL_VERSIONS[version]
|
| 391 |
+
severity_fatal = 0x02
|
| 392 |
+
description_unknown_ca = 0x30
|
| 393 |
+
msg = struct.pack(">BB", severity_fatal, description_unknown_ca)
|
| 394 |
+
msg_len = len(msg)
|
| 395 |
+
record_type_alert = 0x15
|
| 396 |
+
record = struct.pack(">BBBH", record_type_alert, ver_maj, ver_min, msg_len) + msg
|
| 397 |
+
return record
|
evalkit_llava/lib/python3.10/site-packages/pip/_vendor/urllib3/util/ssl_.py
ADDED
|
@@ -0,0 +1,504 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import absolute_import
|
| 2 |
+
|
| 3 |
+
import hashlib
|
| 4 |
+
import hmac
|
| 5 |
+
import os
|
| 6 |
+
import sys
|
| 7 |
+
import warnings
|
| 8 |
+
from binascii import hexlify, unhexlify
|
| 9 |
+
|
| 10 |
+
from ..exceptions import (
|
| 11 |
+
InsecurePlatformWarning,
|
| 12 |
+
ProxySchemeUnsupported,
|
| 13 |
+
SNIMissingWarning,
|
| 14 |
+
SSLError,
|
| 15 |
+
)
|
| 16 |
+
from ..packages import six
|
| 17 |
+
from .url import BRACELESS_IPV6_ADDRZ_RE, IPV4_RE
|
| 18 |
+
|
| 19 |
+
SSLContext = None
|
| 20 |
+
SSLTransport = None
|
| 21 |
+
HAS_SNI = False
|
| 22 |
+
IS_PYOPENSSL = False
|
| 23 |
+
IS_SECURETRANSPORT = False
|
| 24 |
+
ALPN_PROTOCOLS = ["http/1.1"]
|
| 25 |
+
|
| 26 |
+
# Maps the length of a digest to a possible hash function producing this digest
|
| 27 |
+
HASHFUNC_MAP = {
|
| 28 |
+
length: getattr(hashlib, algorithm, None)
|
| 29 |
+
for length, algorithm in ((32, "md5"), (40, "sha1"), (64, "sha256"))
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def _const_compare_digest_backport(a, b):
|
| 34 |
+
"""
|
| 35 |
+
Compare two digests of equal length in constant time.
|
| 36 |
+
|
| 37 |
+
The digests must be of type str/bytes.
|
| 38 |
+
Returns True if the digests match, and False otherwise.
|
| 39 |
+
"""
|
| 40 |
+
result = abs(len(a) - len(b))
|
| 41 |
+
for left, right in zip(bytearray(a), bytearray(b)):
|
| 42 |
+
result |= left ^ right
|
| 43 |
+
return result == 0
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
_const_compare_digest = getattr(hmac, "compare_digest", _const_compare_digest_backport)
|
| 47 |
+
|
| 48 |
+
try: # Test for SSL features
|
| 49 |
+
import ssl
|
| 50 |
+
from ssl import CERT_REQUIRED, wrap_socket
|
| 51 |
+
except ImportError:
|
| 52 |
+
pass
|
| 53 |
+
|
| 54 |
+
try:
|
| 55 |
+
from ssl import HAS_SNI # Has SNI?
|
| 56 |
+
except ImportError:
|
| 57 |
+
pass
|
| 58 |
+
|
| 59 |
+
try:
|
| 60 |
+
from .ssltransport import SSLTransport
|
| 61 |
+
except ImportError:
|
| 62 |
+
pass
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
try: # Platform-specific: Python 3.6
|
| 66 |
+
from ssl import PROTOCOL_TLS
|
| 67 |
+
|
| 68 |
+
PROTOCOL_SSLv23 = PROTOCOL_TLS
|
| 69 |
+
except ImportError:
|
| 70 |
+
try:
|
| 71 |
+
from ssl import PROTOCOL_SSLv23 as PROTOCOL_TLS
|
| 72 |
+
|
| 73 |
+
PROTOCOL_SSLv23 = PROTOCOL_TLS
|
| 74 |
+
except ImportError:
|
| 75 |
+
PROTOCOL_SSLv23 = PROTOCOL_TLS = 2
|
| 76 |
+
|
| 77 |
+
try:
|
| 78 |
+
from ssl import PROTOCOL_TLS_CLIENT
|
| 79 |
+
except ImportError:
|
| 80 |
+
PROTOCOL_TLS_CLIENT = PROTOCOL_TLS
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
try:
|
| 84 |
+
from ssl import OP_NO_COMPRESSION, OP_NO_SSLv2, OP_NO_SSLv3
|
| 85 |
+
except ImportError:
|
| 86 |
+
OP_NO_SSLv2, OP_NO_SSLv3 = 0x1000000, 0x2000000
|
| 87 |
+
OP_NO_COMPRESSION = 0x20000
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
try: # OP_NO_TICKET was added in Python 3.6
|
| 91 |
+
from ssl import OP_NO_TICKET
|
| 92 |
+
except ImportError:
|
| 93 |
+
OP_NO_TICKET = 0x4000
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
# A secure default.
|
| 97 |
+
# Sources for more information on TLS ciphers:
|
| 98 |
+
#
|
| 99 |
+
# - https://wiki.mozilla.org/Security/Server_Side_TLS
|
| 100 |
+
# - https://www.ssllabs.com/projects/best-practices/index.html
|
| 101 |
+
# - https://hynek.me/articles/hardening-your-web-servers-ssl-ciphers/
|
| 102 |
+
#
|
| 103 |
+
# The general intent is:
|
| 104 |
+
# - prefer cipher suites that offer perfect forward secrecy (DHE/ECDHE),
|
| 105 |
+
# - prefer ECDHE over DHE for better performance,
|
| 106 |
+
# - prefer any AES-GCM and ChaCha20 over any AES-CBC for better performance and
|
| 107 |
+
# security,
|
| 108 |
+
# - prefer AES-GCM over ChaCha20 because hardware-accelerated AES is common,
|
| 109 |
+
# - disable NULL authentication, MD5 MACs, DSS, and other
|
| 110 |
+
# insecure ciphers for security reasons.
|
| 111 |
+
# - NOTE: TLS 1.3 cipher suites are managed through a different interface
|
| 112 |
+
# not exposed by CPython (yet!) and are enabled by default if they're available.
|
| 113 |
+
DEFAULT_CIPHERS = ":".join(
|
| 114 |
+
[
|
| 115 |
+
"ECDHE+AESGCM",
|
| 116 |
+
"ECDHE+CHACHA20",
|
| 117 |
+
"DHE+AESGCM",
|
| 118 |
+
"DHE+CHACHA20",
|
| 119 |
+
"ECDH+AESGCM",
|
| 120 |
+
"DH+AESGCM",
|
| 121 |
+
"ECDH+AES",
|
| 122 |
+
"DH+AES",
|
| 123 |
+
"RSA+AESGCM",
|
| 124 |
+
"RSA+AES",
|
| 125 |
+
"!aNULL",
|
| 126 |
+
"!eNULL",
|
| 127 |
+
"!MD5",
|
| 128 |
+
"!DSS",
|
| 129 |
+
]
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
try:
|
| 133 |
+
from ssl import SSLContext # Modern SSL?
|
| 134 |
+
except ImportError:
|
| 135 |
+
|
| 136 |
+
class SSLContext(object): # Platform-specific: Python 2
|
| 137 |
+
def __init__(self, protocol_version):
|
| 138 |
+
self.protocol = protocol_version
|
| 139 |
+
# Use default values from a real SSLContext
|
| 140 |
+
self.check_hostname = False
|
| 141 |
+
self.verify_mode = ssl.CERT_NONE
|
| 142 |
+
self.ca_certs = None
|
| 143 |
+
self.options = 0
|
| 144 |
+
self.certfile = None
|
| 145 |
+
self.keyfile = None
|
| 146 |
+
self.ciphers = None
|
| 147 |
+
|
| 148 |
+
def load_cert_chain(self, certfile, keyfile):
|
| 149 |
+
self.certfile = certfile
|
| 150 |
+
self.keyfile = keyfile
|
| 151 |
+
|
| 152 |
+
def load_verify_locations(self, cafile=None, capath=None, cadata=None):
|
| 153 |
+
self.ca_certs = cafile
|
| 154 |
+
|
| 155 |
+
if capath is not None:
|
| 156 |
+
raise SSLError("CA directories not supported in older Pythons")
|
| 157 |
+
|
| 158 |
+
if cadata is not None:
|
| 159 |
+
raise SSLError("CA data not supported in older Pythons")
|
| 160 |
+
|
| 161 |
+
def set_ciphers(self, cipher_suite):
|
| 162 |
+
self.ciphers = cipher_suite
|
| 163 |
+
|
| 164 |
+
def wrap_socket(self, socket, server_hostname=None, server_side=False):
|
| 165 |
+
warnings.warn(
|
| 166 |
+
"A true SSLContext object is not available. This prevents "
|
| 167 |
+
"urllib3 from configuring SSL appropriately and may cause "
|
| 168 |
+
"certain SSL connections to fail. You can upgrade to a newer "
|
| 169 |
+
"version of Python to solve this. For more information, see "
|
| 170 |
+
"https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html"
|
| 171 |
+
"#ssl-warnings",
|
| 172 |
+
InsecurePlatformWarning,
|
| 173 |
+
)
|
| 174 |
+
kwargs = {
|
| 175 |
+
"keyfile": self.keyfile,
|
| 176 |
+
"certfile": self.certfile,
|
| 177 |
+
"ca_certs": self.ca_certs,
|
| 178 |
+
"cert_reqs": self.verify_mode,
|
| 179 |
+
"ssl_version": self.protocol,
|
| 180 |
+
"server_side": server_side,
|
| 181 |
+
}
|
| 182 |
+
return wrap_socket(socket, ciphers=self.ciphers, **kwargs)
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
def assert_fingerprint(cert, fingerprint):
|
| 186 |
+
"""
|
| 187 |
+
Checks if given fingerprint matches the supplied certificate.
|
| 188 |
+
|
| 189 |
+
:param cert:
|
| 190 |
+
Certificate as bytes object.
|
| 191 |
+
:param fingerprint:
|
| 192 |
+
Fingerprint as string of hexdigits, can be interspersed by colons.
|
| 193 |
+
"""
|
| 194 |
+
|
| 195 |
+
fingerprint = fingerprint.replace(":", "").lower()
|
| 196 |
+
digest_length = len(fingerprint)
|
| 197 |
+
if digest_length not in HASHFUNC_MAP:
|
| 198 |
+
raise SSLError("Fingerprint of invalid length: {0}".format(fingerprint))
|
| 199 |
+
hashfunc = HASHFUNC_MAP.get(digest_length)
|
| 200 |
+
if hashfunc is None:
|
| 201 |
+
raise SSLError(
|
| 202 |
+
"Hash function implementation unavailable for fingerprint length: {0}".format(
|
| 203 |
+
digest_length
|
| 204 |
+
)
|
| 205 |
+
)
|
| 206 |
+
|
| 207 |
+
# We need encode() here for py32; works on py2 and p33.
|
| 208 |
+
fingerprint_bytes = unhexlify(fingerprint.encode())
|
| 209 |
+
|
| 210 |
+
cert_digest = hashfunc(cert).digest()
|
| 211 |
+
|
| 212 |
+
if not _const_compare_digest(cert_digest, fingerprint_bytes):
|
| 213 |
+
raise SSLError(
|
| 214 |
+
'Fingerprints did not match. Expected "{0}", got "{1}".'.format(
|
| 215 |
+
fingerprint, hexlify(cert_digest)
|
| 216 |
+
)
|
| 217 |
+
)
|
| 218 |
+
|
| 219 |
+
|
| 220 |
+
def resolve_cert_reqs(candidate):
|
| 221 |
+
"""
|
| 222 |
+
Resolves the argument to a numeric constant, which can be passed to
|
| 223 |
+
the wrap_socket function/method from the ssl module.
|
| 224 |
+
Defaults to :data:`ssl.CERT_REQUIRED`.
|
| 225 |
+
If given a string it is assumed to be the name of the constant in the
|
| 226 |
+
:mod:`ssl` module or its abbreviation.
|
| 227 |
+
(So you can specify `REQUIRED` instead of `CERT_REQUIRED`.
|
| 228 |
+
If it's neither `None` nor a string we assume it is already the numeric
|
| 229 |
+
constant which can directly be passed to wrap_socket.
|
| 230 |
+
"""
|
| 231 |
+
if candidate is None:
|
| 232 |
+
return CERT_REQUIRED
|
| 233 |
+
|
| 234 |
+
if isinstance(candidate, str):
|
| 235 |
+
res = getattr(ssl, candidate, None)
|
| 236 |
+
if res is None:
|
| 237 |
+
res = getattr(ssl, "CERT_" + candidate)
|
| 238 |
+
return res
|
| 239 |
+
|
| 240 |
+
return candidate
|
| 241 |
+
|
| 242 |
+
|
| 243 |
+
def resolve_ssl_version(candidate):
|
| 244 |
+
"""
|
| 245 |
+
like resolve_cert_reqs
|
| 246 |
+
"""
|
| 247 |
+
if candidate is None:
|
| 248 |
+
return PROTOCOL_TLS
|
| 249 |
+
|
| 250 |
+
if isinstance(candidate, str):
|
| 251 |
+
res = getattr(ssl, candidate, None)
|
| 252 |
+
if res is None:
|
| 253 |
+
res = getattr(ssl, "PROTOCOL_" + candidate)
|
| 254 |
+
return res
|
| 255 |
+
|
| 256 |
+
return candidate
|
| 257 |
+
|
| 258 |
+
|
| 259 |
+
def create_urllib3_context(
|
| 260 |
+
ssl_version=None, cert_reqs=None, options=None, ciphers=None
|
| 261 |
+
):
|
| 262 |
+
"""All arguments have the same meaning as ``ssl_wrap_socket``.
|
| 263 |
+
|
| 264 |
+
By default, this function does a lot of the same work that
|
| 265 |
+
``ssl.create_default_context`` does on Python 3.4+. It:
|
| 266 |
+
|
| 267 |
+
- Disables SSLv2, SSLv3, and compression
|
| 268 |
+
- Sets a restricted set of server ciphers
|
| 269 |
+
|
| 270 |
+
If you wish to enable SSLv3, you can do::
|
| 271 |
+
|
| 272 |
+
from pip._vendor.urllib3.util import ssl_
|
| 273 |
+
context = ssl_.create_urllib3_context()
|
| 274 |
+
context.options &= ~ssl_.OP_NO_SSLv3
|
| 275 |
+
|
| 276 |
+
You can do the same to enable compression (substituting ``COMPRESSION``
|
| 277 |
+
for ``SSLv3`` in the last line above).
|
| 278 |
+
|
| 279 |
+
:param ssl_version:
|
| 280 |
+
The desired protocol version to use. This will default to
|
| 281 |
+
PROTOCOL_SSLv23 which will negotiate the highest protocol that both
|
| 282 |
+
the server and your installation of OpenSSL support.
|
| 283 |
+
:param cert_reqs:
|
| 284 |
+
Whether to require the certificate verification. This defaults to
|
| 285 |
+
``ssl.CERT_REQUIRED``.
|
| 286 |
+
:param options:
|
| 287 |
+
Specific OpenSSL options. These default to ``ssl.OP_NO_SSLv2``,
|
| 288 |
+
``ssl.OP_NO_SSLv3``, ``ssl.OP_NO_COMPRESSION``, and ``ssl.OP_NO_TICKET``.
|
| 289 |
+
:param ciphers:
|
| 290 |
+
Which cipher suites to allow the server to select.
|
| 291 |
+
:returns:
|
| 292 |
+
Constructed SSLContext object with specified options
|
| 293 |
+
:rtype: SSLContext
|
| 294 |
+
"""
|
| 295 |
+
# PROTOCOL_TLS is deprecated in Python 3.10
|
| 296 |
+
if not ssl_version or ssl_version == PROTOCOL_TLS:
|
| 297 |
+
ssl_version = PROTOCOL_TLS_CLIENT
|
| 298 |
+
|
| 299 |
+
context = SSLContext(ssl_version)
|
| 300 |
+
|
| 301 |
+
context.set_ciphers(ciphers or DEFAULT_CIPHERS)
|
| 302 |
+
|
| 303 |
+
# Setting the default here, as we may have no ssl module on import
|
| 304 |
+
cert_reqs = ssl.CERT_REQUIRED if cert_reqs is None else cert_reqs
|
| 305 |
+
|
| 306 |
+
if options is None:
|
| 307 |
+
options = 0
|
| 308 |
+
# SSLv2 is easily broken and is considered harmful and dangerous
|
| 309 |
+
options |= OP_NO_SSLv2
|
| 310 |
+
# SSLv3 has several problems and is now dangerous
|
| 311 |
+
options |= OP_NO_SSLv3
|
| 312 |
+
# Disable compression to prevent CRIME attacks for OpenSSL 1.0+
|
| 313 |
+
# (issue #309)
|
| 314 |
+
options |= OP_NO_COMPRESSION
|
| 315 |
+
# TLSv1.2 only. Unless set explicitly, do not request tickets.
|
| 316 |
+
# This may save some bandwidth on wire, and although the ticket is encrypted,
|
| 317 |
+
# there is a risk associated with it being on wire,
|
| 318 |
+
# if the server is not rotating its ticketing keys properly.
|
| 319 |
+
options |= OP_NO_TICKET
|
| 320 |
+
|
| 321 |
+
context.options |= options
|
| 322 |
+
|
| 323 |
+
# Enable post-handshake authentication for TLS 1.3, see GH #1634. PHA is
|
| 324 |
+
# necessary for conditional client cert authentication with TLS 1.3.
|
| 325 |
+
# The attribute is None for OpenSSL <= 1.1.0 or does not exist in older
|
| 326 |
+
# versions of Python. We only enable on Python 3.7.4+ or if certificate
|
| 327 |
+
# verification is enabled to work around Python issue #37428
|
| 328 |
+
# See: https://bugs.python.org/issue37428
|
| 329 |
+
if (cert_reqs == ssl.CERT_REQUIRED or sys.version_info >= (3, 7, 4)) and getattr(
|
| 330 |
+
context, "post_handshake_auth", None
|
| 331 |
+
) is not None:
|
| 332 |
+
context.post_handshake_auth = True
|
| 333 |
+
|
| 334 |
+
def disable_check_hostname():
|
| 335 |
+
if (
|
| 336 |
+
getattr(context, "check_hostname", None) is not None
|
| 337 |
+
): # Platform-specific: Python 3.2
|
| 338 |
+
# We do our own verification, including fingerprints and alternative
|
| 339 |
+
# hostnames. So disable it here
|
| 340 |
+
context.check_hostname = False
|
| 341 |
+
|
| 342 |
+
# The order of the below lines setting verify_mode and check_hostname
|
| 343 |
+
# matter due to safe-guards SSLContext has to prevent an SSLContext with
|
| 344 |
+
# check_hostname=True, verify_mode=NONE/OPTIONAL. This is made even more
|
| 345 |
+
# complex because we don't know whether PROTOCOL_TLS_CLIENT will be used
|
| 346 |
+
# or not so we don't know the initial state of the freshly created SSLContext.
|
| 347 |
+
if cert_reqs == ssl.CERT_REQUIRED:
|
| 348 |
+
context.verify_mode = cert_reqs
|
| 349 |
+
disable_check_hostname()
|
| 350 |
+
else:
|
| 351 |
+
disable_check_hostname()
|
| 352 |
+
context.verify_mode = cert_reqs
|
| 353 |
+
|
| 354 |
+
# Enable logging of TLS session keys via defacto standard environment variable
|
| 355 |
+
# 'SSLKEYLOGFILE', if the feature is available (Python 3.8+). Skip empty values.
|
| 356 |
+
if hasattr(context, "keylog_filename"):
|
| 357 |
+
sslkeylogfile = os.environ.get("SSLKEYLOGFILE")
|
| 358 |
+
if sslkeylogfile:
|
| 359 |
+
context.keylog_filename = sslkeylogfile
|
| 360 |
+
|
| 361 |
+
return context
|
| 362 |
+
|
| 363 |
+
|
| 364 |
+
def ssl_wrap_socket(
|
| 365 |
+
sock,
|
| 366 |
+
keyfile=None,
|
| 367 |
+
certfile=None,
|
| 368 |
+
cert_reqs=None,
|
| 369 |
+
ca_certs=None,
|
| 370 |
+
server_hostname=None,
|
| 371 |
+
ssl_version=None,
|
| 372 |
+
ciphers=None,
|
| 373 |
+
ssl_context=None,
|
| 374 |
+
ca_cert_dir=None,
|
| 375 |
+
key_password=None,
|
| 376 |
+
ca_cert_data=None,
|
| 377 |
+
tls_in_tls=False,
|
| 378 |
+
):
|
| 379 |
+
"""
|
| 380 |
+
All arguments except for server_hostname, ssl_context, and ca_cert_dir have
|
| 381 |
+
the same meaning as they do when using :func:`ssl.wrap_socket`.
|
| 382 |
+
|
| 383 |
+
:param server_hostname:
|
| 384 |
+
When SNI is supported, the expected hostname of the certificate
|
| 385 |
+
:param ssl_context:
|
| 386 |
+
A pre-made :class:`SSLContext` object. If none is provided, one will
|
| 387 |
+
be created using :func:`create_urllib3_context`.
|
| 388 |
+
:param ciphers:
|
| 389 |
+
A string of ciphers we wish the client to support.
|
| 390 |
+
:param ca_cert_dir:
|
| 391 |
+
A directory containing CA certificates in multiple separate files, as
|
| 392 |
+
supported by OpenSSL's -CApath flag or the capath argument to
|
| 393 |
+
SSLContext.load_verify_locations().
|
| 394 |
+
:param key_password:
|
| 395 |
+
Optional password if the keyfile is encrypted.
|
| 396 |
+
:param ca_cert_data:
|
| 397 |
+
Optional string containing CA certificates in PEM format suitable for
|
| 398 |
+
passing as the cadata parameter to SSLContext.load_verify_locations()
|
| 399 |
+
:param tls_in_tls:
|
| 400 |
+
Use SSLTransport to wrap the existing socket.
|
| 401 |
+
"""
|
| 402 |
+
context = ssl_context
|
| 403 |
+
if context is None:
|
| 404 |
+
# Note: This branch of code and all the variables in it are no longer
|
| 405 |
+
# used by urllib3 itself. We should consider deprecating and removing
|
| 406 |
+
# this code.
|
| 407 |
+
context = create_urllib3_context(ssl_version, cert_reqs, ciphers=ciphers)
|
| 408 |
+
|
| 409 |
+
if ca_certs or ca_cert_dir or ca_cert_data:
|
| 410 |
+
try:
|
| 411 |
+
context.load_verify_locations(ca_certs, ca_cert_dir, ca_cert_data)
|
| 412 |
+
except (IOError, OSError) as e:
|
| 413 |
+
raise SSLError(e)
|
| 414 |
+
|
| 415 |
+
elif ssl_context is None and hasattr(context, "load_default_certs"):
|
| 416 |
+
# try to load OS default certs; works well on Windows (require Python3.4+)
|
| 417 |
+
context.load_default_certs()
|
| 418 |
+
|
| 419 |
+
# Attempt to detect if we get the goofy behavior of the
|
| 420 |
+
# keyfile being encrypted and OpenSSL asking for the
|
| 421 |
+
# passphrase via the terminal and instead error out.
|
| 422 |
+
if keyfile and key_password is None and _is_key_file_encrypted(keyfile):
|
| 423 |
+
raise SSLError("Client private key is encrypted, password is required")
|
| 424 |
+
|
| 425 |
+
if certfile:
|
| 426 |
+
if key_password is None:
|
| 427 |
+
context.load_cert_chain(certfile, keyfile)
|
| 428 |
+
else:
|
| 429 |
+
context.load_cert_chain(certfile, keyfile, key_password)
|
| 430 |
+
|
| 431 |
+
try:
|
| 432 |
+
if hasattr(context, "set_alpn_protocols"):
|
| 433 |
+
context.set_alpn_protocols(ALPN_PROTOCOLS)
|
| 434 |
+
except NotImplementedError: # Defensive: in CI, we always have set_alpn_protocols
|
| 435 |
+
pass
|
| 436 |
+
|
| 437 |
+
# If we detect server_hostname is an IP address then the SNI
|
| 438 |
+
# extension should not be used according to RFC3546 Section 3.1
|
| 439 |
+
use_sni_hostname = server_hostname and not is_ipaddress(server_hostname)
|
| 440 |
+
# SecureTransport uses server_hostname in certificate verification.
|
| 441 |
+
send_sni = (use_sni_hostname and HAS_SNI) or (
|
| 442 |
+
IS_SECURETRANSPORT and server_hostname
|
| 443 |
+
)
|
| 444 |
+
# Do not warn the user if server_hostname is an invalid SNI hostname.
|
| 445 |
+
if not HAS_SNI and use_sni_hostname:
|
| 446 |
+
warnings.warn(
|
| 447 |
+
"An HTTPS request has been made, but the SNI (Server Name "
|
| 448 |
+
"Indication) extension to TLS is not available on this platform. "
|
| 449 |
+
"This may cause the server to present an incorrect TLS "
|
| 450 |
+
"certificate, which can cause validation failures. You can upgrade to "
|
| 451 |
+
"a newer version of Python to solve this. For more information, see "
|
| 452 |
+
"https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html"
|
| 453 |
+
"#ssl-warnings",
|
| 454 |
+
SNIMissingWarning,
|
| 455 |
+
)
|
| 456 |
+
|
| 457 |
+
if send_sni:
|
| 458 |
+
ssl_sock = _ssl_wrap_socket_impl(
|
| 459 |
+
sock, context, tls_in_tls, server_hostname=server_hostname
|
| 460 |
+
)
|
| 461 |
+
else:
|
| 462 |
+
ssl_sock = _ssl_wrap_socket_impl(sock, context, tls_in_tls)
|
| 463 |
+
return ssl_sock
|
| 464 |
+
|
| 465 |
+
|
| 466 |
+
def is_ipaddress(hostname):
|
| 467 |
+
"""Detects whether the hostname given is an IPv4 or IPv6 address.
|
| 468 |
+
Also detects IPv6 addresses with Zone IDs.
|
| 469 |
+
|
| 470 |
+
:param str hostname: Hostname to examine.
|
| 471 |
+
:return: True if the hostname is an IP address, False otherwise.
|
| 472 |
+
"""
|
| 473 |
+
if not six.PY2 and isinstance(hostname, bytes):
|
| 474 |
+
# IDN A-label bytes are ASCII compatible.
|
| 475 |
+
hostname = hostname.decode("ascii")
|
| 476 |
+
return bool(IPV4_RE.match(hostname) or BRACELESS_IPV6_ADDRZ_RE.match(hostname))
|
| 477 |
+
|
| 478 |
+
|
| 479 |
+
def _is_key_file_encrypted(key_file):
|
| 480 |
+
"""Detects if a key file is encrypted or not."""
|
| 481 |
+
with open(key_file, "r") as f:
|
| 482 |
+
for line in f:
|
| 483 |
+
# Look for Proc-Type: 4,ENCRYPTED
|
| 484 |
+
if "ENCRYPTED" in line:
|
| 485 |
+
return True
|
| 486 |
+
|
| 487 |
+
return False
|
| 488 |
+
|
| 489 |
+
|
| 490 |
+
def _ssl_wrap_socket_impl(sock, ssl_context, tls_in_tls, server_hostname=None):
|
| 491 |
+
if tls_in_tls:
|
| 492 |
+
if not SSLTransport:
|
| 493 |
+
# Import error, ssl is not available.
|
| 494 |
+
raise ProxySchemeUnsupported(
|
| 495 |
+
"TLS in TLS requires support for the 'ssl' module"
|
| 496 |
+
)
|
| 497 |
+
|
| 498 |
+
SSLTransport._validate_ssl_context_for_tls_in_tls(ssl_context)
|
| 499 |
+
return SSLTransport(sock, ssl_context, server_hostname)
|
| 500 |
+
|
| 501 |
+
if server_hostname:
|
| 502 |
+
return ssl_context.wrap_socket(sock, server_hostname=server_hostname)
|
| 503 |
+
else:
|
| 504 |
+
return ssl_context.wrap_socket(sock)
|
evalkit_llava/lib/python3.10/site-packages/pip/_vendor/urllib3/util/ssl_match_hostname.py
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""The match_hostname() function from Python 3.3.3, essential when using SSL."""
|
| 2 |
+
|
| 3 |
+
# Note: This file is under the PSF license as the code comes from the python
|
| 4 |
+
# stdlib. http://docs.python.org/3/license.html
|
| 5 |
+
|
| 6 |
+
import re
|
| 7 |
+
import sys
|
| 8 |
+
|
| 9 |
+
# ipaddress has been backported to 2.6+ in pypi. If it is installed on the
|
| 10 |
+
# system, use it to handle IPAddress ServerAltnames (this was added in
|
| 11 |
+
# python-3.5) otherwise only do DNS matching. This allows
|
| 12 |
+
# util.ssl_match_hostname to continue to be used in Python 2.7.
|
| 13 |
+
try:
|
| 14 |
+
import ipaddress
|
| 15 |
+
except ImportError:
|
| 16 |
+
ipaddress = None
|
| 17 |
+
|
| 18 |
+
__version__ = "3.5.0.1"
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class CertificateError(ValueError):
|
| 22 |
+
pass
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def _dnsname_match(dn, hostname, max_wildcards=1):
|
| 26 |
+
"""Matching according to RFC 6125, section 6.4.3
|
| 27 |
+
|
| 28 |
+
http://tools.ietf.org/html/rfc6125#section-6.4.3
|
| 29 |
+
"""
|
| 30 |
+
pats = []
|
| 31 |
+
if not dn:
|
| 32 |
+
return False
|
| 33 |
+
|
| 34 |
+
# Ported from python3-syntax:
|
| 35 |
+
# leftmost, *remainder = dn.split(r'.')
|
| 36 |
+
parts = dn.split(r".")
|
| 37 |
+
leftmost = parts[0]
|
| 38 |
+
remainder = parts[1:]
|
| 39 |
+
|
| 40 |
+
wildcards = leftmost.count("*")
|
| 41 |
+
if wildcards > max_wildcards:
|
| 42 |
+
# Issue #17980: avoid denials of service by refusing more
|
| 43 |
+
# than one wildcard per fragment. A survey of established
|
| 44 |
+
# policy among SSL implementations showed it to be a
|
| 45 |
+
# reasonable choice.
|
| 46 |
+
raise CertificateError(
|
| 47 |
+
"too many wildcards in certificate DNS name: " + repr(dn)
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
# speed up common case w/o wildcards
|
| 51 |
+
if not wildcards:
|
| 52 |
+
return dn.lower() == hostname.lower()
|
| 53 |
+
|
| 54 |
+
# RFC 6125, section 6.4.3, subitem 1.
|
| 55 |
+
# The client SHOULD NOT attempt to match a presented identifier in which
|
| 56 |
+
# the wildcard character comprises a label other than the left-most label.
|
| 57 |
+
if leftmost == "*":
|
| 58 |
+
# When '*' is a fragment by itself, it matches a non-empty dotless
|
| 59 |
+
# fragment.
|
| 60 |
+
pats.append("[^.]+")
|
| 61 |
+
elif leftmost.startswith("xn--") or hostname.startswith("xn--"):
|
| 62 |
+
# RFC 6125, section 6.4.3, subitem 3.
|
| 63 |
+
# The client SHOULD NOT attempt to match a presented identifier
|
| 64 |
+
# where the wildcard character is embedded within an A-label or
|
| 65 |
+
# U-label of an internationalized domain name.
|
| 66 |
+
pats.append(re.escape(leftmost))
|
| 67 |
+
else:
|
| 68 |
+
# Otherwise, '*' matches any dotless string, e.g. www*
|
| 69 |
+
pats.append(re.escape(leftmost).replace(r"\*", "[^.]*"))
|
| 70 |
+
|
| 71 |
+
# add the remaining fragments, ignore any wildcards
|
| 72 |
+
for frag in remainder:
|
| 73 |
+
pats.append(re.escape(frag))
|
| 74 |
+
|
| 75 |
+
pat = re.compile(r"\A" + r"\.".join(pats) + r"\Z", re.IGNORECASE)
|
| 76 |
+
return pat.match(hostname)
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def _to_unicode(obj):
|
| 80 |
+
if isinstance(obj, str) and sys.version_info < (3,):
|
| 81 |
+
# ignored flake8 # F821 to support python 2.7 function
|
| 82 |
+
obj = unicode(obj, encoding="ascii", errors="strict") # noqa: F821
|
| 83 |
+
return obj
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
def _ipaddress_match(ipname, host_ip):
|
| 87 |
+
"""Exact matching of IP addresses.
|
| 88 |
+
|
| 89 |
+
RFC 6125 explicitly doesn't define an algorithm for this
|
| 90 |
+
(section 1.7.2 - "Out of Scope").
|
| 91 |
+
"""
|
| 92 |
+
# OpenSSL may add a trailing newline to a subjectAltName's IP address
|
| 93 |
+
# Divergence from upstream: ipaddress can't handle byte str
|
| 94 |
+
ip = ipaddress.ip_address(_to_unicode(ipname).rstrip())
|
| 95 |
+
return ip == host_ip
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
def match_hostname(cert, hostname):
|
| 99 |
+
"""Verify that *cert* (in decoded format as returned by
|
| 100 |
+
SSLSocket.getpeercert()) matches the *hostname*. RFC 2818 and RFC 6125
|
| 101 |
+
rules are followed, but IP addresses are not accepted for *hostname*.
|
| 102 |
+
|
| 103 |
+
CertificateError is raised on failure. On success, the function
|
| 104 |
+
returns nothing.
|
| 105 |
+
"""
|
| 106 |
+
if not cert:
|
| 107 |
+
raise ValueError(
|
| 108 |
+
"empty or no certificate, match_hostname needs a "
|
| 109 |
+
"SSL socket or SSL context with either "
|
| 110 |
+
"CERT_OPTIONAL or CERT_REQUIRED"
|
| 111 |
+
)
|
| 112 |
+
try:
|
| 113 |
+
# Divergence from upstream: ipaddress can't handle byte str
|
| 114 |
+
host_ip = ipaddress.ip_address(_to_unicode(hostname))
|
| 115 |
+
except (UnicodeError, ValueError):
|
| 116 |
+
# ValueError: Not an IP address (common case)
|
| 117 |
+
# UnicodeError: Divergence from upstream: Have to deal with ipaddress not taking
|
| 118 |
+
# byte strings. addresses should be all ascii, so we consider it not
|
| 119 |
+
# an ipaddress in this case
|
| 120 |
+
host_ip = None
|
| 121 |
+
except AttributeError:
|
| 122 |
+
# Divergence from upstream: Make ipaddress library optional
|
| 123 |
+
if ipaddress is None:
|
| 124 |
+
host_ip = None
|
| 125 |
+
else: # Defensive
|
| 126 |
+
raise
|
| 127 |
+
dnsnames = []
|
| 128 |
+
san = cert.get("subjectAltName", ())
|
| 129 |
+
for key, value in san:
|
| 130 |
+
if key == "DNS":
|
| 131 |
+
if host_ip is None and _dnsname_match(value, hostname):
|
| 132 |
+
return
|
| 133 |
+
dnsnames.append(value)
|
| 134 |
+
elif key == "IP Address":
|
| 135 |
+
if host_ip is not None and _ipaddress_match(value, host_ip):
|
| 136 |
+
return
|
| 137 |
+
dnsnames.append(value)
|
| 138 |
+
if not dnsnames:
|
| 139 |
+
# The subject is only checked when there is no dNSName entry
|
| 140 |
+
# in subjectAltName
|
| 141 |
+
for sub in cert.get("subject", ()):
|
| 142 |
+
for key, value in sub:
|
| 143 |
+
# XXX according to RFC 2818, the most specific Common Name
|
| 144 |
+
# must be used.
|
| 145 |
+
if key == "commonName":
|
| 146 |
+
if _dnsname_match(value, hostname):
|
| 147 |
+
return
|
| 148 |
+
dnsnames.append(value)
|
| 149 |
+
if len(dnsnames) > 1:
|
| 150 |
+
raise CertificateError(
|
| 151 |
+
"hostname %r "
|
| 152 |
+
"doesn't match either of %s" % (hostname, ", ".join(map(repr, dnsnames)))
|
| 153 |
+
)
|
| 154 |
+
elif len(dnsnames) == 1:
|
| 155 |
+
raise CertificateError("hostname %r doesn't match %r" % (hostname, dnsnames[0]))
|
| 156 |
+
else:
|
| 157 |
+
raise CertificateError(
|
| 158 |
+
"no appropriate commonName or subjectAltName fields were found"
|
| 159 |
+
)
|
evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/__pycache__/_msvccompiler.cpython-310.pyc
ADDED
|
Binary file (14.8 kB). View file
|
|
|
evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/__pycache__/cmd.cpython-310.pyc
ADDED
|
Binary file (14.6 kB). View file
|
|
|
evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/__pycache__/debug.cpython-310.pyc
ADDED
|
Binary file (218 Bytes). View file
|
|
|
evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/__pycache__/dep_util.cpython-310.pyc
ADDED
|
Binary file (554 Bytes). View file
|
|
|
evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/__pycache__/extension.cpython-310.pyc
ADDED
|
Binary file (7.19 kB). View file
|
|
|
evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/__pycache__/unixccompiler.cpython-310.pyc
ADDED
|
Binary file (10.3 kB). View file
|
|
|
evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/__pycache__/test_bdist_dumb.cpython-310.pyc
ADDED
|
Binary file (2.09 kB). View file
|
|
|
evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/__pycache__/test_build_clib.cpython-310.pyc
ADDED
|
Binary file (3.74 kB). View file
|
|
|
evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/__pycache__/test_build_ext.cpython-310.pyc
ADDED
|
Binary file (13.7 kB). View file
|
|
|
evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/__pycache__/test_build_scripts.cpython-310.pyc
ADDED
|
Binary file (3.11 kB). View file
|
|
|
evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/__pycache__/test_ccompiler.cpython-310.pyc
ADDED
|
Binary file (2.75 kB). View file
|
|
|
evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/__pycache__/test_dir_util.cpython-310.pyc
ADDED
|
Binary file (4.94 kB). View file
|
|
|
evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/__pycache__/test_install_data.cpython-310.pyc
ADDED
|
Binary file (1.85 kB). View file
|
|
|
evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/__pycache__/test_install_headers.cpython-310.pyc
ADDED
|
Binary file (1.15 kB). View file
|
|
|
evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/__pycache__/test_install_scripts.cpython-310.pyc
ADDED
|
Binary file (1.67 kB). View file
|
|
|
evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/__pycache__/test_mingwccompiler.cpython-310.pyc
ADDED
|
Binary file (2.56 kB). View file
|
|
|
evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/__pycache__/test_msvccompiler.cpython-310.pyc
ADDED
|
Binary file (5.18 kB). View file
|
|
|
evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/compat/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (168 Bytes). View file
|
|
|
evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/compat/__pycache__/py39.cpython-310.pyc
ADDED
|
Binary file (583 Bytes). View file
|
|
|
evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/compat/py39.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
|
| 3 |
+
if sys.version_info >= (3, 10):
|
| 4 |
+
from test.support.import_helper import (
|
| 5 |
+
CleanImport as CleanImport,
|
| 6 |
+
)
|
| 7 |
+
from test.support.import_helper import (
|
| 8 |
+
DirsOnSysPath as DirsOnSysPath,
|
| 9 |
+
)
|
| 10 |
+
from test.support.os_helper import (
|
| 11 |
+
EnvironmentVarGuard as EnvironmentVarGuard,
|
| 12 |
+
)
|
| 13 |
+
from test.support.os_helper import (
|
| 14 |
+
rmtree as rmtree,
|
| 15 |
+
)
|
| 16 |
+
from test.support.os_helper import (
|
| 17 |
+
skip_unless_symlink as skip_unless_symlink,
|
| 18 |
+
)
|
| 19 |
+
from test.support.os_helper import (
|
| 20 |
+
unlink as unlink,
|
| 21 |
+
)
|
| 22 |
+
else:
|
| 23 |
+
from test.support import (
|
| 24 |
+
CleanImport as CleanImport,
|
| 25 |
+
)
|
| 26 |
+
from test.support import (
|
| 27 |
+
DirsOnSysPath as DirsOnSysPath,
|
| 28 |
+
)
|
| 29 |
+
from test.support import (
|
| 30 |
+
EnvironmentVarGuard as EnvironmentVarGuard,
|
| 31 |
+
)
|
| 32 |
+
from test.support import (
|
| 33 |
+
rmtree as rmtree,
|
| 34 |
+
)
|
| 35 |
+
from test.support import (
|
| 36 |
+
skip_unless_symlink as skip_unless_symlink,
|
| 37 |
+
)
|
| 38 |
+
from test.support import (
|
| 39 |
+
unlink as unlink,
|
| 40 |
+
)
|
evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/support.py
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Support code for distutils test cases."""
|
| 2 |
+
|
| 3 |
+
import itertools
|
| 4 |
+
import os
|
| 5 |
+
import pathlib
|
| 6 |
+
import shutil
|
| 7 |
+
import sys
|
| 8 |
+
import sysconfig
|
| 9 |
+
import tempfile
|
| 10 |
+
from distutils.core import Distribution
|
| 11 |
+
|
| 12 |
+
import pytest
|
| 13 |
+
from more_itertools import always_iterable
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
@pytest.mark.usefixtures('distutils_managed_tempdir')
|
| 17 |
+
class TempdirManager:
|
| 18 |
+
"""
|
| 19 |
+
Mix-in class that handles temporary directories for test cases.
|
| 20 |
+
"""
|
| 21 |
+
|
| 22 |
+
def mkdtemp(self):
|
| 23 |
+
"""Create a temporary directory that will be cleaned up.
|
| 24 |
+
|
| 25 |
+
Returns the path of the directory.
|
| 26 |
+
"""
|
| 27 |
+
d = tempfile.mkdtemp()
|
| 28 |
+
self.tempdirs.append(d)
|
| 29 |
+
return d
|
| 30 |
+
|
| 31 |
+
def write_file(self, path, content='xxx'):
|
| 32 |
+
"""Writes a file in the given path.
|
| 33 |
+
|
| 34 |
+
path can be a string or a sequence.
|
| 35 |
+
"""
|
| 36 |
+
pathlib.Path(*always_iterable(path)).write_text(content, encoding='utf-8')
|
| 37 |
+
|
| 38 |
+
def create_dist(self, pkg_name='foo', **kw):
|
| 39 |
+
"""Will generate a test environment.
|
| 40 |
+
|
| 41 |
+
This function creates:
|
| 42 |
+
- a Distribution instance using keywords
|
| 43 |
+
- a temporary directory with a package structure
|
| 44 |
+
|
| 45 |
+
It returns the package directory and the distribution
|
| 46 |
+
instance.
|
| 47 |
+
"""
|
| 48 |
+
tmp_dir = self.mkdtemp()
|
| 49 |
+
pkg_dir = os.path.join(tmp_dir, pkg_name)
|
| 50 |
+
os.mkdir(pkg_dir)
|
| 51 |
+
dist = Distribution(attrs=kw)
|
| 52 |
+
|
| 53 |
+
return pkg_dir, dist
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
class DummyCommand:
|
| 57 |
+
"""Class to store options for retrieval via set_undefined_options()."""
|
| 58 |
+
|
| 59 |
+
def __init__(self, **kwargs):
|
| 60 |
+
vars(self).update(kwargs)
|
| 61 |
+
|
| 62 |
+
def ensure_finalized(self):
|
| 63 |
+
pass
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def copy_xxmodule_c(directory):
|
| 67 |
+
"""Helper for tests that need the xxmodule.c source file.
|
| 68 |
+
|
| 69 |
+
Example use:
|
| 70 |
+
|
| 71 |
+
def test_compile(self):
|
| 72 |
+
copy_xxmodule_c(self.tmpdir)
|
| 73 |
+
self.assertIn('xxmodule.c', os.listdir(self.tmpdir))
|
| 74 |
+
|
| 75 |
+
If the source file can be found, it will be copied to *directory*. If not,
|
| 76 |
+
the test will be skipped. Errors during copy are not caught.
|
| 77 |
+
"""
|
| 78 |
+
shutil.copy(_get_xxmodule_path(), os.path.join(directory, 'xxmodule.c'))
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def _get_xxmodule_path():
|
| 82 |
+
source_name = 'xxmodule.c' if sys.version_info > (3, 9) else 'xxmodule-3.8.c'
|
| 83 |
+
return os.path.join(os.path.dirname(__file__), source_name)
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
def fixup_build_ext(cmd):
|
| 87 |
+
"""Function needed to make build_ext tests pass.
|
| 88 |
+
|
| 89 |
+
When Python was built with --enable-shared on Unix, -L. is not enough to
|
| 90 |
+
find libpython<blah>.so, because regrtest runs in a tempdir, not in the
|
| 91 |
+
source directory where the .so lives.
|
| 92 |
+
|
| 93 |
+
When Python was built with in debug mode on Windows, build_ext commands
|
| 94 |
+
need their debug attribute set, and it is not done automatically for
|
| 95 |
+
some reason.
|
| 96 |
+
|
| 97 |
+
This function handles both of these things. Example use:
|
| 98 |
+
|
| 99 |
+
cmd = build_ext(dist)
|
| 100 |
+
support.fixup_build_ext(cmd)
|
| 101 |
+
cmd.ensure_finalized()
|
| 102 |
+
|
| 103 |
+
Unlike most other Unix platforms, Mac OS X embeds absolute paths
|
| 104 |
+
to shared libraries into executables, so the fixup is not needed there.
|
| 105 |
+
"""
|
| 106 |
+
if os.name == 'nt':
|
| 107 |
+
cmd.debug = sys.executable.endswith('_d.exe')
|
| 108 |
+
elif sysconfig.get_config_var('Py_ENABLE_SHARED'):
|
| 109 |
+
# To further add to the shared builds fun on Unix, we can't just add
|
| 110 |
+
# library_dirs to the Extension() instance because that doesn't get
|
| 111 |
+
# plumbed through to the final compiler command.
|
| 112 |
+
runshared = sysconfig.get_config_var('RUNSHARED')
|
| 113 |
+
if runshared is None:
|
| 114 |
+
cmd.library_dirs = ['.']
|
| 115 |
+
else:
|
| 116 |
+
if sys.platform == 'darwin':
|
| 117 |
+
cmd.library_dirs = []
|
| 118 |
+
else:
|
| 119 |
+
name, equals, value = runshared.partition('=')
|
| 120 |
+
cmd.library_dirs = [d for d in value.split(os.pathsep) if d]
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
def combine_markers(cls):
|
| 124 |
+
"""
|
| 125 |
+
pytest will honor markers as found on the class, but when
|
| 126 |
+
markers are on multiple subclasses, only one appears. Use
|
| 127 |
+
this decorator to combine those markers.
|
| 128 |
+
"""
|
| 129 |
+
cls.pytestmark = [
|
| 130 |
+
mark
|
| 131 |
+
for base in itertools.chain([cls], cls.__bases__)
|
| 132 |
+
for mark in getattr(base, 'pytestmark', [])
|
| 133 |
+
]
|
| 134 |
+
return cls
|
evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Tests for distutils.archive_util."""
|
| 2 |
+
|
| 3 |
+
import functools
|
| 4 |
+
import operator
|
| 5 |
+
import os
|
| 6 |
+
import pathlib
|
| 7 |
+
import sys
|
| 8 |
+
import tarfile
|
| 9 |
+
from distutils import archive_util
|
| 10 |
+
from distutils.archive_util import (
|
| 11 |
+
ARCHIVE_FORMATS,
|
| 12 |
+
check_archive_formats,
|
| 13 |
+
make_archive,
|
| 14 |
+
make_tarball,
|
| 15 |
+
make_zipfile,
|
| 16 |
+
)
|
| 17 |
+
from distutils.spawn import spawn
|
| 18 |
+
from distutils.tests import support
|
| 19 |
+
from os.path import splitdrive
|
| 20 |
+
|
| 21 |
+
import path
|
| 22 |
+
import pytest
|
| 23 |
+
from test.support import patch
|
| 24 |
+
|
| 25 |
+
from .unix_compat import UID_0_SUPPORT, grp, pwd, require_uid_0, require_unix_id
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def can_fs_encode(filename):
|
| 29 |
+
"""
|
| 30 |
+
Return True if the filename can be saved in the file system.
|
| 31 |
+
"""
|
| 32 |
+
if os.path.supports_unicode_filenames:
|
| 33 |
+
return True
|
| 34 |
+
try:
|
| 35 |
+
filename.encode(sys.getfilesystemencoding())
|
| 36 |
+
except UnicodeEncodeError:
|
| 37 |
+
return False
|
| 38 |
+
return True
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def all_equal(values):
|
| 42 |
+
return functools.reduce(operator.eq, values)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def same_drive(*paths):
|
| 46 |
+
return all_equal(pathlib.Path(path).drive for path in paths)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
class ArchiveUtilTestCase(support.TempdirManager):
|
| 50 |
+
@pytest.mark.usefixtures('needs_zlib')
|
| 51 |
+
def test_make_tarball(self, name='archive'):
|
| 52 |
+
# creating something to tar
|
| 53 |
+
tmpdir = self._create_files()
|
| 54 |
+
self._make_tarball(tmpdir, name, '.tar.gz')
|
| 55 |
+
# trying an uncompressed one
|
| 56 |
+
self._make_tarball(tmpdir, name, '.tar', compress=None)
|
| 57 |
+
|
| 58 |
+
@pytest.mark.usefixtures('needs_zlib')
|
| 59 |
+
def test_make_tarball_gzip(self):
|
| 60 |
+
tmpdir = self._create_files()
|
| 61 |
+
self._make_tarball(tmpdir, 'archive', '.tar.gz', compress='gzip')
|
| 62 |
+
|
| 63 |
+
def test_make_tarball_bzip2(self):
|
| 64 |
+
pytest.importorskip('bz2')
|
| 65 |
+
tmpdir = self._create_files()
|
| 66 |
+
self._make_tarball(tmpdir, 'archive', '.tar.bz2', compress='bzip2')
|
| 67 |
+
|
| 68 |
+
def test_make_tarball_xz(self):
|
| 69 |
+
pytest.importorskip('lzma')
|
| 70 |
+
tmpdir = self._create_files()
|
| 71 |
+
self._make_tarball(tmpdir, 'archive', '.tar.xz', compress='xz')
|
| 72 |
+
|
| 73 |
+
@pytest.mark.skipif("not can_fs_encode('årchiv')")
|
| 74 |
+
def test_make_tarball_latin1(self):
|
| 75 |
+
"""
|
| 76 |
+
Mirror test_make_tarball, except filename contains latin characters.
|
| 77 |
+
"""
|
| 78 |
+
self.test_make_tarball('årchiv') # note this isn't a real word
|
| 79 |
+
|
| 80 |
+
@pytest.mark.skipif("not can_fs_encode('のアーカイブ')")
|
| 81 |
+
def test_make_tarball_extended(self):
|
| 82 |
+
"""
|
| 83 |
+
Mirror test_make_tarball, except filename contains extended
|
| 84 |
+
characters outside the latin charset.
|
| 85 |
+
"""
|
| 86 |
+
self.test_make_tarball('のアーカイブ') # japanese for archive
|
| 87 |
+
|
| 88 |
+
def _make_tarball(self, tmpdir, target_name, suffix, **kwargs):
|
| 89 |
+
tmpdir2 = self.mkdtemp()
|
| 90 |
+
if same_drive(tmpdir, tmpdir2):
|
| 91 |
+
pytest.skip("source and target should be on same drive")
|
| 92 |
+
|
| 93 |
+
base_name = os.path.join(tmpdir2, target_name)
|
| 94 |
+
|
| 95 |
+
# working with relative paths to avoid tar warnings
|
| 96 |
+
with path.Path(tmpdir):
|
| 97 |
+
make_tarball(splitdrive(base_name)[1], 'dist', **kwargs)
|
| 98 |
+
|
| 99 |
+
# check if the compressed tarball was created
|
| 100 |
+
tarball = base_name + suffix
|
| 101 |
+
assert os.path.exists(tarball)
|
| 102 |
+
assert self._tarinfo(tarball) == self._created_files
|
| 103 |
+
|
| 104 |
+
def _tarinfo(self, path):
|
| 105 |
+
tar = tarfile.open(path)
|
| 106 |
+
try:
|
| 107 |
+
names = tar.getnames()
|
| 108 |
+
names.sort()
|
| 109 |
+
return names
|
| 110 |
+
finally:
|
| 111 |
+
tar.close()
|
| 112 |
+
|
| 113 |
+
_zip_created_files = [
|
| 114 |
+
'dist/',
|
| 115 |
+
'dist/file1',
|
| 116 |
+
'dist/file2',
|
| 117 |
+
'dist/sub/',
|
| 118 |
+
'dist/sub/file3',
|
| 119 |
+
'dist/sub2/',
|
| 120 |
+
]
|
| 121 |
+
_created_files = [p.rstrip('/') for p in _zip_created_files]
|
| 122 |
+
|
| 123 |
+
def _create_files(self):
|
| 124 |
+
# creating something to tar
|
| 125 |
+
tmpdir = self.mkdtemp()
|
| 126 |
+
dist = os.path.join(tmpdir, 'dist')
|
| 127 |
+
os.mkdir(dist)
|
| 128 |
+
self.write_file([dist, 'file1'], 'xxx')
|
| 129 |
+
self.write_file([dist, 'file2'], 'xxx')
|
| 130 |
+
os.mkdir(os.path.join(dist, 'sub'))
|
| 131 |
+
self.write_file([dist, 'sub', 'file3'], 'xxx')
|
| 132 |
+
os.mkdir(os.path.join(dist, 'sub2'))
|
| 133 |
+
return tmpdir
|
| 134 |
+
|
| 135 |
+
@pytest.mark.usefixtures('needs_zlib')
|
| 136 |
+
@pytest.mark.skipif("not (shutil.which('tar') and shutil.which('gzip'))")
|
| 137 |
+
def test_tarfile_vs_tar(self):
|
| 138 |
+
tmpdir = self._create_files()
|
| 139 |
+
tmpdir2 = self.mkdtemp()
|
| 140 |
+
base_name = os.path.join(tmpdir2, 'archive')
|
| 141 |
+
old_dir = os.getcwd()
|
| 142 |
+
os.chdir(tmpdir)
|
| 143 |
+
try:
|
| 144 |
+
make_tarball(base_name, 'dist')
|
| 145 |
+
finally:
|
| 146 |
+
os.chdir(old_dir)
|
| 147 |
+
|
| 148 |
+
# check if the compressed tarball was created
|
| 149 |
+
tarball = base_name + '.tar.gz'
|
| 150 |
+
assert os.path.exists(tarball)
|
| 151 |
+
|
| 152 |
+
# now create another tarball using `tar`
|
| 153 |
+
tarball2 = os.path.join(tmpdir, 'archive2.tar.gz')
|
| 154 |
+
tar_cmd = ['tar', '-cf', 'archive2.tar', 'dist']
|
| 155 |
+
gzip_cmd = ['gzip', '-f', '-9', 'archive2.tar']
|
| 156 |
+
old_dir = os.getcwd()
|
| 157 |
+
os.chdir(tmpdir)
|
| 158 |
+
try:
|
| 159 |
+
spawn(tar_cmd)
|
| 160 |
+
spawn(gzip_cmd)
|
| 161 |
+
finally:
|
| 162 |
+
os.chdir(old_dir)
|
| 163 |
+
|
| 164 |
+
assert os.path.exists(tarball2)
|
| 165 |
+
# let's compare both tarballs
|
| 166 |
+
assert self._tarinfo(tarball) == self._created_files
|
| 167 |
+
assert self._tarinfo(tarball2) == self._created_files
|
| 168 |
+
|
| 169 |
+
# trying an uncompressed one
|
| 170 |
+
base_name = os.path.join(tmpdir2, 'archive')
|
| 171 |
+
old_dir = os.getcwd()
|
| 172 |
+
os.chdir(tmpdir)
|
| 173 |
+
try:
|
| 174 |
+
make_tarball(base_name, 'dist', compress=None)
|
| 175 |
+
finally:
|
| 176 |
+
os.chdir(old_dir)
|
| 177 |
+
tarball = base_name + '.tar'
|
| 178 |
+
assert os.path.exists(tarball)
|
| 179 |
+
|
| 180 |
+
# now for a dry_run
|
| 181 |
+
base_name = os.path.join(tmpdir2, 'archive')
|
| 182 |
+
old_dir = os.getcwd()
|
| 183 |
+
os.chdir(tmpdir)
|
| 184 |
+
try:
|
| 185 |
+
make_tarball(base_name, 'dist', compress=None, dry_run=True)
|
| 186 |
+
finally:
|
| 187 |
+
os.chdir(old_dir)
|
| 188 |
+
tarball = base_name + '.tar'
|
| 189 |
+
assert os.path.exists(tarball)
|
| 190 |
+
|
| 191 |
+
@pytest.mark.usefixtures('needs_zlib')
|
| 192 |
+
def test_make_zipfile(self):
|
| 193 |
+
zipfile = pytest.importorskip('zipfile')
|
| 194 |
+
# creating something to tar
|
| 195 |
+
tmpdir = self._create_files()
|
| 196 |
+
base_name = os.path.join(self.mkdtemp(), 'archive')
|
| 197 |
+
with path.Path(tmpdir):
|
| 198 |
+
make_zipfile(base_name, 'dist')
|
| 199 |
+
|
| 200 |
+
# check if the compressed tarball was created
|
| 201 |
+
tarball = base_name + '.zip'
|
| 202 |
+
assert os.path.exists(tarball)
|
| 203 |
+
with zipfile.ZipFile(tarball) as zf:
|
| 204 |
+
assert sorted(zf.namelist()) == self._zip_created_files
|
| 205 |
+
|
| 206 |
+
def test_make_zipfile_no_zlib(self):
|
| 207 |
+
zipfile = pytest.importorskip('zipfile')
|
| 208 |
+
patch(self, archive_util.zipfile, 'zlib', None) # force zlib ImportError
|
| 209 |
+
|
| 210 |
+
called = []
|
| 211 |
+
zipfile_class = zipfile.ZipFile
|
| 212 |
+
|
| 213 |
+
def fake_zipfile(*a, **kw):
|
| 214 |
+
if kw.get('compression', None) == zipfile.ZIP_STORED:
|
| 215 |
+
called.append((a, kw))
|
| 216 |
+
return zipfile_class(*a, **kw)
|
| 217 |
+
|
| 218 |
+
patch(self, archive_util.zipfile, 'ZipFile', fake_zipfile)
|
| 219 |
+
|
| 220 |
+
# create something to tar and compress
|
| 221 |
+
tmpdir = self._create_files()
|
| 222 |
+
base_name = os.path.join(self.mkdtemp(), 'archive')
|
| 223 |
+
with path.Path(tmpdir):
|
| 224 |
+
make_zipfile(base_name, 'dist')
|
| 225 |
+
|
| 226 |
+
tarball = base_name + '.zip'
|
| 227 |
+
assert called == [((tarball, "w"), {'compression': zipfile.ZIP_STORED})]
|
| 228 |
+
assert os.path.exists(tarball)
|
| 229 |
+
with zipfile.ZipFile(tarball) as zf:
|
| 230 |
+
assert sorted(zf.namelist()) == self._zip_created_files
|
| 231 |
+
|
| 232 |
+
def test_check_archive_formats(self):
|
| 233 |
+
assert check_archive_formats(['gztar', 'xxx', 'zip']) == 'xxx'
|
| 234 |
+
assert (
|
| 235 |
+
check_archive_formats(['gztar', 'bztar', 'xztar', 'ztar', 'tar', 'zip'])
|
| 236 |
+
is None
|
| 237 |
+
)
|
| 238 |
+
|
| 239 |
+
def test_make_archive(self):
|
| 240 |
+
tmpdir = self.mkdtemp()
|
| 241 |
+
base_name = os.path.join(tmpdir, 'archive')
|
| 242 |
+
with pytest.raises(ValueError):
|
| 243 |
+
make_archive(base_name, 'xxx')
|
| 244 |
+
|
| 245 |
+
def test_make_archive_cwd(self):
|
| 246 |
+
current_dir = os.getcwd()
|
| 247 |
+
|
| 248 |
+
def _breaks(*args, **kw):
|
| 249 |
+
raise RuntimeError()
|
| 250 |
+
|
| 251 |
+
ARCHIVE_FORMATS['xxx'] = (_breaks, [], 'xxx file')
|
| 252 |
+
try:
|
| 253 |
+
try:
|
| 254 |
+
make_archive('xxx', 'xxx', root_dir=self.mkdtemp())
|
| 255 |
+
except Exception:
|
| 256 |
+
pass
|
| 257 |
+
assert os.getcwd() == current_dir
|
| 258 |
+
finally:
|
| 259 |
+
ARCHIVE_FORMATS.pop('xxx')
|
| 260 |
+
|
| 261 |
+
def test_make_archive_tar(self):
|
| 262 |
+
base_dir = self._create_files()
|
| 263 |
+
base_name = os.path.join(self.mkdtemp(), 'archive')
|
| 264 |
+
res = make_archive(base_name, 'tar', base_dir, 'dist')
|
| 265 |
+
assert os.path.exists(res)
|
| 266 |
+
assert os.path.basename(res) == 'archive.tar'
|
| 267 |
+
assert self._tarinfo(res) == self._created_files
|
| 268 |
+
|
| 269 |
+
@pytest.mark.usefixtures('needs_zlib')
|
| 270 |
+
def test_make_archive_gztar(self):
|
| 271 |
+
base_dir = self._create_files()
|
| 272 |
+
base_name = os.path.join(self.mkdtemp(), 'archive')
|
| 273 |
+
res = make_archive(base_name, 'gztar', base_dir, 'dist')
|
| 274 |
+
assert os.path.exists(res)
|
| 275 |
+
assert os.path.basename(res) == 'archive.tar.gz'
|
| 276 |
+
assert self._tarinfo(res) == self._created_files
|
| 277 |
+
|
| 278 |
+
def test_make_archive_bztar(self):
|
| 279 |
+
pytest.importorskip('bz2')
|
| 280 |
+
base_dir = self._create_files()
|
| 281 |
+
base_name = os.path.join(self.mkdtemp(), 'archive')
|
| 282 |
+
res = make_archive(base_name, 'bztar', base_dir, 'dist')
|
| 283 |
+
assert os.path.exists(res)
|
| 284 |
+
assert os.path.basename(res) == 'archive.tar.bz2'
|
| 285 |
+
assert self._tarinfo(res) == self._created_files
|
| 286 |
+
|
| 287 |
+
def test_make_archive_xztar(self):
|
| 288 |
+
pytest.importorskip('lzma')
|
| 289 |
+
base_dir = self._create_files()
|
| 290 |
+
base_name = os.path.join(self.mkdtemp(), 'archive')
|
| 291 |
+
res = make_archive(base_name, 'xztar', base_dir, 'dist')
|
| 292 |
+
assert os.path.exists(res)
|
| 293 |
+
assert os.path.basename(res) == 'archive.tar.xz'
|
| 294 |
+
assert self._tarinfo(res) == self._created_files
|
| 295 |
+
|
| 296 |
+
def test_make_archive_owner_group(self):
|
| 297 |
+
# testing make_archive with owner and group, with various combinations
|
| 298 |
+
# this works even if there's not gid/uid support
|
| 299 |
+
if UID_0_SUPPORT:
|
| 300 |
+
group = grp.getgrgid(0)[0]
|
| 301 |
+
owner = pwd.getpwuid(0)[0]
|
| 302 |
+
else:
|
| 303 |
+
group = owner = 'root'
|
| 304 |
+
|
| 305 |
+
base_dir = self._create_files()
|
| 306 |
+
root_dir = self.mkdtemp()
|
| 307 |
+
base_name = os.path.join(self.mkdtemp(), 'archive')
|
| 308 |
+
res = make_archive(
|
| 309 |
+
base_name, 'zip', root_dir, base_dir, owner=owner, group=group
|
| 310 |
+
)
|
| 311 |
+
assert os.path.exists(res)
|
| 312 |
+
|
| 313 |
+
res = make_archive(base_name, 'zip', root_dir, base_dir)
|
| 314 |
+
assert os.path.exists(res)
|
| 315 |
+
|
| 316 |
+
res = make_archive(
|
| 317 |
+
base_name, 'tar', root_dir, base_dir, owner=owner, group=group
|
| 318 |
+
)
|
| 319 |
+
assert os.path.exists(res)
|
| 320 |
+
|
| 321 |
+
res = make_archive(
|
| 322 |
+
base_name, 'tar', root_dir, base_dir, owner='kjhkjhkjg', group='oihohoh'
|
| 323 |
+
)
|
| 324 |
+
assert os.path.exists(res)
|
| 325 |
+
|
| 326 |
+
@pytest.mark.usefixtures('needs_zlib')
|
| 327 |
+
@require_unix_id
|
| 328 |
+
@require_uid_0
|
| 329 |
+
def test_tarfile_root_owner(self):
|
| 330 |
+
tmpdir = self._create_files()
|
| 331 |
+
base_name = os.path.join(self.mkdtemp(), 'archive')
|
| 332 |
+
old_dir = os.getcwd()
|
| 333 |
+
os.chdir(tmpdir)
|
| 334 |
+
group = grp.getgrgid(0)[0]
|
| 335 |
+
owner = pwd.getpwuid(0)[0]
|
| 336 |
+
try:
|
| 337 |
+
archive_name = make_tarball(
|
| 338 |
+
base_name, 'dist', compress=None, owner=owner, group=group
|
| 339 |
+
)
|
| 340 |
+
finally:
|
| 341 |
+
os.chdir(old_dir)
|
| 342 |
+
|
| 343 |
+
# check if the compressed tarball was created
|
| 344 |
+
assert os.path.exists(archive_name)
|
| 345 |
+
|
| 346 |
+
# now checks the rights
|
| 347 |
+
archive = tarfile.open(archive_name)
|
| 348 |
+
try:
|
| 349 |
+
for member in archive.getmembers():
|
| 350 |
+
assert member.uid == 0
|
| 351 |
+
assert member.gid == 0
|
| 352 |
+
finally:
|
| 353 |
+
archive.close()
|
evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/test_bdist.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Tests for distutils.command.bdist."""
|
| 2 |
+
|
| 3 |
+
from distutils.command.bdist import bdist
|
| 4 |
+
from distutils.tests import support
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class TestBuild(support.TempdirManager):
|
| 8 |
+
def test_formats(self):
|
| 9 |
+
# let's create a command and make sure
|
| 10 |
+
# we can set the format
|
| 11 |
+
dist = self.create_dist()[1]
|
| 12 |
+
cmd = bdist(dist)
|
| 13 |
+
cmd.formats = ['gztar']
|
| 14 |
+
cmd.ensure_finalized()
|
| 15 |
+
assert cmd.formats == ['gztar']
|
| 16 |
+
|
| 17 |
+
# what formats does bdist offer?
|
| 18 |
+
formats = [
|
| 19 |
+
'bztar',
|
| 20 |
+
'gztar',
|
| 21 |
+
'rpm',
|
| 22 |
+
'tar',
|
| 23 |
+
'xztar',
|
| 24 |
+
'zip',
|
| 25 |
+
'ztar',
|
| 26 |
+
]
|
| 27 |
+
found = sorted(cmd.format_commands)
|
| 28 |
+
assert found == formats
|
| 29 |
+
|
| 30 |
+
def test_skip_build(self):
|
| 31 |
+
# bug #10946: bdist --skip-build should trickle down to subcommands
|
| 32 |
+
dist = self.create_dist()[1]
|
| 33 |
+
cmd = bdist(dist)
|
| 34 |
+
cmd.skip_build = True
|
| 35 |
+
cmd.ensure_finalized()
|
| 36 |
+
dist.command_obj['bdist'] = cmd
|
| 37 |
+
|
| 38 |
+
names = [
|
| 39 |
+
'bdist_dumb',
|
| 40 |
+
] # bdist_rpm does not support --skip-build
|
| 41 |
+
|
| 42 |
+
for name in names:
|
| 43 |
+
subcmd = cmd.get_finalized_command(name)
|
| 44 |
+
if getattr(subcmd, '_unsupported', False):
|
| 45 |
+
# command is not supported on this build
|
| 46 |
+
continue
|
| 47 |
+
assert subcmd.skip_build, f'{name} should take --skip-build from bdist'
|
evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/test_bdist_dumb.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Tests for distutils.command.bdist_dumb."""
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
import sys
|
| 5 |
+
import zipfile
|
| 6 |
+
from distutils.command.bdist_dumb import bdist_dumb
|
| 7 |
+
from distutils.core import Distribution
|
| 8 |
+
from distutils.tests import support
|
| 9 |
+
|
| 10 |
+
import pytest
|
| 11 |
+
|
| 12 |
+
SETUP_PY = """\
|
| 13 |
+
from distutils.core import setup
|
| 14 |
+
import foo
|
| 15 |
+
|
| 16 |
+
setup(name='foo', version='0.1', py_modules=['foo'],
|
| 17 |
+
url='xxx', author='xxx', author_email='xxx')
|
| 18 |
+
|
| 19 |
+
"""
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
@support.combine_markers
|
| 23 |
+
@pytest.mark.usefixtures('save_env')
|
| 24 |
+
@pytest.mark.usefixtures('save_argv')
|
| 25 |
+
@pytest.mark.usefixtures('save_cwd')
|
| 26 |
+
class TestBuildDumb(
|
| 27 |
+
support.TempdirManager,
|
| 28 |
+
):
|
| 29 |
+
@pytest.mark.usefixtures('needs_zlib')
|
| 30 |
+
def test_simple_built(self):
|
| 31 |
+
# let's create a simple package
|
| 32 |
+
tmp_dir = self.mkdtemp()
|
| 33 |
+
pkg_dir = os.path.join(tmp_dir, 'foo')
|
| 34 |
+
os.mkdir(pkg_dir)
|
| 35 |
+
self.write_file((pkg_dir, 'setup.py'), SETUP_PY)
|
| 36 |
+
self.write_file((pkg_dir, 'foo.py'), '#')
|
| 37 |
+
self.write_file((pkg_dir, 'MANIFEST.in'), 'include foo.py')
|
| 38 |
+
self.write_file((pkg_dir, 'README'), '')
|
| 39 |
+
|
| 40 |
+
dist = Distribution({
|
| 41 |
+
'name': 'foo',
|
| 42 |
+
'version': '0.1',
|
| 43 |
+
'py_modules': ['foo'],
|
| 44 |
+
'url': 'xxx',
|
| 45 |
+
'author': 'xxx',
|
| 46 |
+
'author_email': 'xxx',
|
| 47 |
+
})
|
| 48 |
+
dist.script_name = 'setup.py'
|
| 49 |
+
os.chdir(pkg_dir)
|
| 50 |
+
|
| 51 |
+
sys.argv = ['setup.py']
|
| 52 |
+
cmd = bdist_dumb(dist)
|
| 53 |
+
|
| 54 |
+
# so the output is the same no matter
|
| 55 |
+
# what is the platform
|
| 56 |
+
cmd.format = 'zip'
|
| 57 |
+
|
| 58 |
+
cmd.ensure_finalized()
|
| 59 |
+
cmd.run()
|
| 60 |
+
|
| 61 |
+
# see what we have
|
| 62 |
+
dist_created = os.listdir(os.path.join(pkg_dir, 'dist'))
|
| 63 |
+
base = f"{dist.get_fullname()}.{cmd.plat_name}.zip"
|
| 64 |
+
|
| 65 |
+
assert dist_created == [base]
|
| 66 |
+
|
| 67 |
+
# now let's check what we have in the zip file
|
| 68 |
+
fp = zipfile.ZipFile(os.path.join('dist', base))
|
| 69 |
+
try:
|
| 70 |
+
contents = fp.namelist()
|
| 71 |
+
finally:
|
| 72 |
+
fp.close()
|
| 73 |
+
|
| 74 |
+
contents = sorted(filter(None, map(os.path.basename, contents)))
|
| 75 |
+
wanted = ['foo-0.1-py{}.{}.egg-info'.format(*sys.version_info[:2]), 'foo.py']
|
| 76 |
+
if not sys.dont_write_bytecode:
|
| 77 |
+
wanted.append(f'foo.{sys.implementation.cache_tag}.pyc')
|
| 78 |
+
assert contents == sorted(wanted)
|
evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/test_bdist_rpm.py
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Tests for distutils.command.bdist_rpm."""
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
import shutil # noqa: F401
|
| 5 |
+
import sys
|
| 6 |
+
from distutils.command.bdist_rpm import bdist_rpm
|
| 7 |
+
from distutils.core import Distribution
|
| 8 |
+
from distutils.tests import support
|
| 9 |
+
|
| 10 |
+
import pytest
|
| 11 |
+
from test.support import requires_zlib
|
| 12 |
+
|
| 13 |
+
SETUP_PY = """\
|
| 14 |
+
from distutils.core import setup
|
| 15 |
+
import foo
|
| 16 |
+
|
| 17 |
+
setup(name='foo', version='0.1', py_modules=['foo'],
|
| 18 |
+
url='xxx', author='xxx', author_email='xxx')
|
| 19 |
+
|
| 20 |
+
"""
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
@pytest.fixture(autouse=True)
|
| 24 |
+
def sys_executable_encodable():
|
| 25 |
+
try:
|
| 26 |
+
sys.executable.encode('UTF-8')
|
| 27 |
+
except UnicodeEncodeError:
|
| 28 |
+
pytest.skip("sys.executable is not encodable to UTF-8")
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
mac_woes = pytest.mark.skipif(
|
| 32 |
+
"not sys.platform.startswith('linux')",
|
| 33 |
+
reason='spurious sdtout/stderr output under macOS',
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
@pytest.mark.usefixtures('save_env')
|
| 38 |
+
@pytest.mark.usefixtures('save_argv')
|
| 39 |
+
@pytest.mark.usefixtures('save_cwd')
|
| 40 |
+
class TestBuildRpm(
|
| 41 |
+
support.TempdirManager,
|
| 42 |
+
):
|
| 43 |
+
@mac_woes
|
| 44 |
+
@requires_zlib()
|
| 45 |
+
@pytest.mark.skipif("not shutil.which('rpm')")
|
| 46 |
+
@pytest.mark.skipif("not shutil.which('rpmbuild')")
|
| 47 |
+
def test_quiet(self):
|
| 48 |
+
# let's create a package
|
| 49 |
+
tmp_dir = self.mkdtemp()
|
| 50 |
+
os.environ['HOME'] = tmp_dir # to confine dir '.rpmdb' creation
|
| 51 |
+
pkg_dir = os.path.join(tmp_dir, 'foo')
|
| 52 |
+
os.mkdir(pkg_dir)
|
| 53 |
+
self.write_file((pkg_dir, 'setup.py'), SETUP_PY)
|
| 54 |
+
self.write_file((pkg_dir, 'foo.py'), '#')
|
| 55 |
+
self.write_file((pkg_dir, 'MANIFEST.in'), 'include foo.py')
|
| 56 |
+
self.write_file((pkg_dir, 'README'), '')
|
| 57 |
+
|
| 58 |
+
dist = Distribution({
|
| 59 |
+
'name': 'foo',
|
| 60 |
+
'version': '0.1',
|
| 61 |
+
'py_modules': ['foo'],
|
| 62 |
+
'url': 'xxx',
|
| 63 |
+
'author': 'xxx',
|
| 64 |
+
'author_email': 'xxx',
|
| 65 |
+
})
|
| 66 |
+
dist.script_name = 'setup.py'
|
| 67 |
+
os.chdir(pkg_dir)
|
| 68 |
+
|
| 69 |
+
sys.argv = ['setup.py']
|
| 70 |
+
cmd = bdist_rpm(dist)
|
| 71 |
+
cmd.fix_python = True
|
| 72 |
+
|
| 73 |
+
# running in quiet mode
|
| 74 |
+
cmd.quiet = True
|
| 75 |
+
cmd.ensure_finalized()
|
| 76 |
+
cmd.run()
|
| 77 |
+
|
| 78 |
+
dist_created = os.listdir(os.path.join(pkg_dir, 'dist'))
|
| 79 |
+
assert 'foo-0.1-1.noarch.rpm' in dist_created
|
| 80 |
+
|
| 81 |
+
# bug #2945: upload ignores bdist_rpm files
|
| 82 |
+
assert ('bdist_rpm', 'any', 'dist/foo-0.1-1.src.rpm') in dist.dist_files
|
| 83 |
+
assert ('bdist_rpm', 'any', 'dist/foo-0.1-1.noarch.rpm') in dist.dist_files
|
| 84 |
+
|
| 85 |
+
@mac_woes
|
| 86 |
+
@requires_zlib()
|
| 87 |
+
# https://bugs.python.org/issue1533164
|
| 88 |
+
@pytest.mark.skipif("not shutil.which('rpm')")
|
| 89 |
+
@pytest.mark.skipif("not shutil.which('rpmbuild')")
|
| 90 |
+
def test_no_optimize_flag(self):
|
| 91 |
+
# let's create a package that breaks bdist_rpm
|
| 92 |
+
tmp_dir = self.mkdtemp()
|
| 93 |
+
os.environ['HOME'] = tmp_dir # to confine dir '.rpmdb' creation
|
| 94 |
+
pkg_dir = os.path.join(tmp_dir, 'foo')
|
| 95 |
+
os.mkdir(pkg_dir)
|
| 96 |
+
self.write_file((pkg_dir, 'setup.py'), SETUP_PY)
|
| 97 |
+
self.write_file((pkg_dir, 'foo.py'), '#')
|
| 98 |
+
self.write_file((pkg_dir, 'MANIFEST.in'), 'include foo.py')
|
| 99 |
+
self.write_file((pkg_dir, 'README'), '')
|
| 100 |
+
|
| 101 |
+
dist = Distribution({
|
| 102 |
+
'name': 'foo',
|
| 103 |
+
'version': '0.1',
|
| 104 |
+
'py_modules': ['foo'],
|
| 105 |
+
'url': 'xxx',
|
| 106 |
+
'author': 'xxx',
|
| 107 |
+
'author_email': 'xxx',
|
| 108 |
+
})
|
| 109 |
+
dist.script_name = 'setup.py'
|
| 110 |
+
os.chdir(pkg_dir)
|
| 111 |
+
|
| 112 |
+
sys.argv = ['setup.py']
|
| 113 |
+
cmd = bdist_rpm(dist)
|
| 114 |
+
cmd.fix_python = True
|
| 115 |
+
|
| 116 |
+
cmd.quiet = True
|
| 117 |
+
cmd.ensure_finalized()
|
| 118 |
+
cmd.run()
|
| 119 |
+
|
| 120 |
+
dist_created = os.listdir(os.path.join(pkg_dir, 'dist'))
|
| 121 |
+
assert 'foo-0.1-1.noarch.rpm' in dist_created
|
| 122 |
+
|
| 123 |
+
# bug #2945: upload ignores bdist_rpm files
|
| 124 |
+
assert ('bdist_rpm', 'any', 'dist/foo-0.1-1.src.rpm') in dist.dist_files
|
| 125 |
+
assert ('bdist_rpm', 'any', 'dist/foo-0.1-1.noarch.rpm') in dist.dist_files
|
| 126 |
+
|
| 127 |
+
os.remove(os.path.join(pkg_dir, 'dist', 'foo-0.1-1.noarch.rpm'))
|
evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/test_build.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Tests for distutils.command.build."""
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
import sys
|
| 5 |
+
from distutils.command.build import build
|
| 6 |
+
from distutils.tests import support
|
| 7 |
+
from sysconfig import get_config_var, get_platform
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class TestBuild(support.TempdirManager):
|
| 11 |
+
def test_finalize_options(self):
|
| 12 |
+
pkg_dir, dist = self.create_dist()
|
| 13 |
+
cmd = build(dist)
|
| 14 |
+
cmd.finalize_options()
|
| 15 |
+
|
| 16 |
+
# if not specified, plat_name gets the current platform
|
| 17 |
+
assert cmd.plat_name == get_platform()
|
| 18 |
+
|
| 19 |
+
# build_purelib is build + lib
|
| 20 |
+
wanted = os.path.join(cmd.build_base, 'lib')
|
| 21 |
+
assert cmd.build_purelib == wanted
|
| 22 |
+
|
| 23 |
+
# build_platlib is 'build/lib.platform-cache_tag[-pydebug]'
|
| 24 |
+
# examples:
|
| 25 |
+
# build/lib.macosx-10.3-i386-cpython39
|
| 26 |
+
plat_spec = f'.{cmd.plat_name}-{sys.implementation.cache_tag}'
|
| 27 |
+
if get_config_var('Py_GIL_DISABLED'):
|
| 28 |
+
plat_spec += 't'
|
| 29 |
+
if hasattr(sys, 'gettotalrefcount'):
|
| 30 |
+
assert cmd.build_platlib.endswith('-pydebug')
|
| 31 |
+
plat_spec += '-pydebug'
|
| 32 |
+
wanted = os.path.join(cmd.build_base, 'lib' + plat_spec)
|
| 33 |
+
assert cmd.build_platlib == wanted
|
| 34 |
+
|
| 35 |
+
# by default, build_lib = build_purelib
|
| 36 |
+
assert cmd.build_lib == cmd.build_purelib
|
| 37 |
+
|
| 38 |
+
# build_temp is build/temp.<plat>
|
| 39 |
+
wanted = os.path.join(cmd.build_base, 'temp' + plat_spec)
|
| 40 |
+
assert cmd.build_temp == wanted
|
| 41 |
+
|
| 42 |
+
# build_scripts is build/scripts-x.x
|
| 43 |
+
wanted = os.path.join(
|
| 44 |
+
cmd.build_base, f'scripts-{sys.version_info.major}.{sys.version_info.minor}'
|
| 45 |
+
)
|
| 46 |
+
assert cmd.build_scripts == wanted
|
| 47 |
+
|
| 48 |
+
# executable is os.path.normpath(sys.executable)
|
| 49 |
+
assert cmd.executable == os.path.normpath(sys.executable)
|
evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/test_build_clib.py
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Tests for distutils.command.build_clib."""
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
from distutils.command.build_clib import build_clib
|
| 5 |
+
from distutils.errors import DistutilsSetupError
|
| 6 |
+
from distutils.tests import missing_compiler_executable, support
|
| 7 |
+
|
| 8 |
+
import pytest
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class TestBuildCLib(support.TempdirManager):
|
| 12 |
+
def test_check_library_dist(self):
|
| 13 |
+
pkg_dir, dist = self.create_dist()
|
| 14 |
+
cmd = build_clib(dist)
|
| 15 |
+
|
| 16 |
+
# 'libraries' option must be a list
|
| 17 |
+
with pytest.raises(DistutilsSetupError):
|
| 18 |
+
cmd.check_library_list('foo')
|
| 19 |
+
|
| 20 |
+
# each element of 'libraries' must a 2-tuple
|
| 21 |
+
with pytest.raises(DistutilsSetupError):
|
| 22 |
+
cmd.check_library_list(['foo1', 'foo2'])
|
| 23 |
+
|
| 24 |
+
# first element of each tuple in 'libraries'
|
| 25 |
+
# must be a string (the library name)
|
| 26 |
+
with pytest.raises(DistutilsSetupError):
|
| 27 |
+
cmd.check_library_list([(1, 'foo1'), ('name', 'foo2')])
|
| 28 |
+
|
| 29 |
+
# library name may not contain directory separators
|
| 30 |
+
with pytest.raises(DistutilsSetupError):
|
| 31 |
+
cmd.check_library_list(
|
| 32 |
+
[('name', 'foo1'), ('another/name', 'foo2')],
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# second element of each tuple must be a dictionary (build info)
|
| 36 |
+
with pytest.raises(DistutilsSetupError):
|
| 37 |
+
cmd.check_library_list(
|
| 38 |
+
[('name', {}), ('another', 'foo2')],
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# those work
|
| 42 |
+
libs = [('name', {}), ('name', {'ok': 'good'})]
|
| 43 |
+
cmd.check_library_list(libs)
|
| 44 |
+
|
| 45 |
+
def test_get_source_files(self):
|
| 46 |
+
pkg_dir, dist = self.create_dist()
|
| 47 |
+
cmd = build_clib(dist)
|
| 48 |
+
|
| 49 |
+
# "in 'libraries' option 'sources' must be present and must be
|
| 50 |
+
# a list of source filenames
|
| 51 |
+
cmd.libraries = [('name', {})]
|
| 52 |
+
with pytest.raises(DistutilsSetupError):
|
| 53 |
+
cmd.get_source_files()
|
| 54 |
+
|
| 55 |
+
cmd.libraries = [('name', {'sources': 1})]
|
| 56 |
+
with pytest.raises(DistutilsSetupError):
|
| 57 |
+
cmd.get_source_files()
|
| 58 |
+
|
| 59 |
+
cmd.libraries = [('name', {'sources': ['a', 'b']})]
|
| 60 |
+
assert cmd.get_source_files() == ['a', 'b']
|
| 61 |
+
|
| 62 |
+
cmd.libraries = [('name', {'sources': ('a', 'b')})]
|
| 63 |
+
assert cmd.get_source_files() == ['a', 'b']
|
| 64 |
+
|
| 65 |
+
cmd.libraries = [
|
| 66 |
+
('name', {'sources': ('a', 'b')}),
|
| 67 |
+
('name2', {'sources': ['c', 'd']}),
|
| 68 |
+
]
|
| 69 |
+
assert cmd.get_source_files() == ['a', 'b', 'c', 'd']
|
| 70 |
+
|
| 71 |
+
def test_build_libraries(self):
|
| 72 |
+
pkg_dir, dist = self.create_dist()
|
| 73 |
+
cmd = build_clib(dist)
|
| 74 |
+
|
| 75 |
+
class FakeCompiler:
|
| 76 |
+
def compile(*args, **kw):
|
| 77 |
+
pass
|
| 78 |
+
|
| 79 |
+
create_static_lib = compile
|
| 80 |
+
|
| 81 |
+
cmd.compiler = FakeCompiler()
|
| 82 |
+
|
| 83 |
+
# build_libraries is also doing a bit of typo checking
|
| 84 |
+
lib = [('name', {'sources': 'notvalid'})]
|
| 85 |
+
with pytest.raises(DistutilsSetupError):
|
| 86 |
+
cmd.build_libraries(lib)
|
| 87 |
+
|
| 88 |
+
lib = [('name', {'sources': list()})]
|
| 89 |
+
cmd.build_libraries(lib)
|
| 90 |
+
|
| 91 |
+
lib = [('name', {'sources': tuple()})]
|
| 92 |
+
cmd.build_libraries(lib)
|
| 93 |
+
|
| 94 |
+
def test_finalize_options(self):
|
| 95 |
+
pkg_dir, dist = self.create_dist()
|
| 96 |
+
cmd = build_clib(dist)
|
| 97 |
+
|
| 98 |
+
cmd.include_dirs = 'one-dir'
|
| 99 |
+
cmd.finalize_options()
|
| 100 |
+
assert cmd.include_dirs == ['one-dir']
|
| 101 |
+
|
| 102 |
+
cmd.include_dirs = None
|
| 103 |
+
cmd.finalize_options()
|
| 104 |
+
assert cmd.include_dirs == []
|
| 105 |
+
|
| 106 |
+
cmd.distribution.libraries = 'WONTWORK'
|
| 107 |
+
with pytest.raises(DistutilsSetupError):
|
| 108 |
+
cmd.finalize_options()
|
| 109 |
+
|
| 110 |
+
@pytest.mark.skipif('platform.system() == "Windows"')
|
| 111 |
+
def test_run(self):
|
| 112 |
+
pkg_dir, dist = self.create_dist()
|
| 113 |
+
cmd = build_clib(dist)
|
| 114 |
+
|
| 115 |
+
foo_c = os.path.join(pkg_dir, 'foo.c')
|
| 116 |
+
self.write_file(foo_c, 'int main(void) { return 1;}\n')
|
| 117 |
+
cmd.libraries = [('foo', {'sources': [foo_c]})]
|
| 118 |
+
|
| 119 |
+
build_temp = os.path.join(pkg_dir, 'build')
|
| 120 |
+
os.mkdir(build_temp)
|
| 121 |
+
cmd.build_temp = build_temp
|
| 122 |
+
cmd.build_clib = build_temp
|
| 123 |
+
|
| 124 |
+
# Before we run the command, we want to make sure
|
| 125 |
+
# all commands are present on the system.
|
| 126 |
+
ccmd = missing_compiler_executable()
|
| 127 |
+
if ccmd is not None:
|
| 128 |
+
self.skipTest(f'The {ccmd!r} command is not found')
|
| 129 |
+
|
| 130 |
+
# this should work
|
| 131 |
+
cmd.run()
|
| 132 |
+
|
| 133 |
+
# let's check the result
|
| 134 |
+
assert 'libfoo.a' in os.listdir(build_temp)
|
evalkit_llava/lib/python3.10/site-packages/setuptools/_distutils/tests/test_build_ext.py
ADDED
|
@@ -0,0 +1,560 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import contextlib
|
| 2 |
+
import importlib
|
| 3 |
+
import os
|
| 4 |
+
import platform
|
| 5 |
+
import re
|
| 6 |
+
import shutil
|
| 7 |
+
import site
|
| 8 |
+
import sys
|
| 9 |
+
import tempfile
|
| 10 |
+
import textwrap
|
| 11 |
+
from distutils import sysconfig
|
| 12 |
+
from distutils.command.build_ext import build_ext
|
| 13 |
+
from distutils.core import Distribution
|
| 14 |
+
from distutils.errors import (
|
| 15 |
+
CompileError,
|
| 16 |
+
DistutilsPlatformError,
|
| 17 |
+
DistutilsSetupError,
|
| 18 |
+
UnknownFileError,
|
| 19 |
+
)
|
| 20 |
+
from distutils.extension import Extension
|
| 21 |
+
from distutils.tests import missing_compiler_executable
|
| 22 |
+
from distutils.tests.support import TempdirManager, copy_xxmodule_c, fixup_build_ext
|
| 23 |
+
from io import StringIO
|
| 24 |
+
|
| 25 |
+
import jaraco.path
|
| 26 |
+
import path
|
| 27 |
+
import pytest
|
| 28 |
+
from test import support
|
| 29 |
+
|
| 30 |
+
from .compat import py39 as import_helper
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
@pytest.fixture()
|
| 34 |
+
def user_site_dir(request):
|
| 35 |
+
self = request.instance
|
| 36 |
+
self.tmp_dir = self.mkdtemp()
|
| 37 |
+
self.tmp_path = path.Path(self.tmp_dir)
|
| 38 |
+
from distutils.command import build_ext
|
| 39 |
+
|
| 40 |
+
orig_user_base = site.USER_BASE
|
| 41 |
+
|
| 42 |
+
site.USER_BASE = self.mkdtemp()
|
| 43 |
+
build_ext.USER_BASE = site.USER_BASE
|
| 44 |
+
|
| 45 |
+
# bpo-30132: On Windows, a .pdb file may be created in the current
|
| 46 |
+
# working directory. Create a temporary working directory to cleanup
|
| 47 |
+
# everything at the end of the test.
|
| 48 |
+
with self.tmp_path:
|
| 49 |
+
yield
|
| 50 |
+
|
| 51 |
+
site.USER_BASE = orig_user_base
|
| 52 |
+
build_ext.USER_BASE = orig_user_base
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
@contextlib.contextmanager
|
| 56 |
+
def safe_extension_import(name, path):
|
| 57 |
+
with import_helper.CleanImport(name):
|
| 58 |
+
with extension_redirect(name, path) as new_path:
|
| 59 |
+
with import_helper.DirsOnSysPath(new_path):
|
| 60 |
+
yield
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
@contextlib.contextmanager
|
| 64 |
+
def extension_redirect(mod, path):
|
| 65 |
+
"""
|
| 66 |
+
Tests will fail to tear down an extension module if it's been imported.
|
| 67 |
+
|
| 68 |
+
Before importing, copy the file to a temporary directory that won't
|
| 69 |
+
be cleaned up. Yield the new path.
|
| 70 |
+
"""
|
| 71 |
+
if platform.system() != "Windows" and sys.platform != "cygwin":
|
| 72 |
+
yield path
|
| 73 |
+
return
|
| 74 |
+
with import_helper.DirsOnSysPath(path):
|
| 75 |
+
spec = importlib.util.find_spec(mod)
|
| 76 |
+
filename = os.path.basename(spec.origin)
|
| 77 |
+
trash_dir = tempfile.mkdtemp(prefix='deleteme')
|
| 78 |
+
dest = os.path.join(trash_dir, os.path.basename(filename))
|
| 79 |
+
shutil.copy(spec.origin, dest)
|
| 80 |
+
yield trash_dir
|
| 81 |
+
# TODO: can the file be scheduled for deletion?
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
@pytest.mark.usefixtures('user_site_dir')
|
| 85 |
+
class TestBuildExt(TempdirManager):
|
| 86 |
+
def build_ext(self, *args, **kwargs):
|
| 87 |
+
return build_ext(*args, **kwargs)
|
| 88 |
+
|
| 89 |
+
def test_build_ext(self):
|
| 90 |
+
missing_compiler_executable()
|
| 91 |
+
copy_xxmodule_c(self.tmp_dir)
|
| 92 |
+
xx_c = os.path.join(self.tmp_dir, 'xxmodule.c')
|
| 93 |
+
xx_ext = Extension('xx', [xx_c])
|
| 94 |
+
dist = Distribution({'name': 'xx', 'ext_modules': [xx_ext]})
|
| 95 |
+
dist.package_dir = self.tmp_dir
|
| 96 |
+
cmd = self.build_ext(dist)
|
| 97 |
+
fixup_build_ext(cmd)
|
| 98 |
+
cmd.build_lib = self.tmp_dir
|
| 99 |
+
cmd.build_temp = self.tmp_dir
|
| 100 |
+
|
| 101 |
+
old_stdout = sys.stdout
|
| 102 |
+
if not support.verbose:
|
| 103 |
+
# silence compiler output
|
| 104 |
+
sys.stdout = StringIO()
|
| 105 |
+
try:
|
| 106 |
+
cmd.ensure_finalized()
|
| 107 |
+
cmd.run()
|
| 108 |
+
finally:
|
| 109 |
+
sys.stdout = old_stdout
|
| 110 |
+
|
| 111 |
+
with safe_extension_import('xx', self.tmp_dir):
|
| 112 |
+
self._test_xx()
|
| 113 |
+
|
| 114 |
+
@staticmethod
|
| 115 |
+
def _test_xx():
|
| 116 |
+
import xx
|
| 117 |
+
|
| 118 |
+
for attr in ('error', 'foo', 'new', 'roj'):
|
| 119 |
+
assert hasattr(xx, attr)
|
| 120 |
+
|
| 121 |
+
assert xx.foo(2, 5) == 7
|
| 122 |
+
assert xx.foo(13, 15) == 28
|
| 123 |
+
assert xx.new().demo() is None
|
| 124 |
+
if support.HAVE_DOCSTRINGS:
|
| 125 |
+
doc = 'This is a template module just for instruction.'
|
| 126 |
+
assert xx.__doc__ == doc
|
| 127 |
+
assert isinstance(xx.Null(), xx.Null)
|
| 128 |
+
assert isinstance(xx.Str(), xx.Str)
|
| 129 |
+
|
| 130 |
+
def test_solaris_enable_shared(self):
|
| 131 |
+
dist = Distribution({'name': 'xx'})
|
| 132 |
+
cmd = self.build_ext(dist)
|
| 133 |
+
old = sys.platform
|
| 134 |
+
|
| 135 |
+
sys.platform = 'sunos' # fooling finalize_options
|
| 136 |
+
from distutils.sysconfig import _config_vars
|
| 137 |
+
|
| 138 |
+
old_var = _config_vars.get('Py_ENABLE_SHARED')
|
| 139 |
+
_config_vars['Py_ENABLE_SHARED'] = True
|
| 140 |
+
try:
|
| 141 |
+
cmd.ensure_finalized()
|
| 142 |
+
finally:
|
| 143 |
+
sys.platform = old
|
| 144 |
+
if old_var is None:
|
| 145 |
+
del _config_vars['Py_ENABLE_SHARED']
|
| 146 |
+
else:
|
| 147 |
+
_config_vars['Py_ENABLE_SHARED'] = old_var
|
| 148 |
+
|
| 149 |
+
# make sure we get some library dirs under solaris
|
| 150 |
+
assert len(cmd.library_dirs) > 0
|
| 151 |
+
|
| 152 |
+
def test_user_site(self):
|
| 153 |
+
import site
|
| 154 |
+
|
| 155 |
+
dist = Distribution({'name': 'xx'})
|
| 156 |
+
cmd = self.build_ext(dist)
|
| 157 |
+
|
| 158 |
+
# making sure the user option is there
|
| 159 |
+
options = [name for name, short, label in cmd.user_options]
|
| 160 |
+
assert 'user' in options
|
| 161 |
+
|
| 162 |
+
# setting a value
|
| 163 |
+
cmd.user = True
|
| 164 |
+
|
| 165 |
+
# setting user based lib and include
|
| 166 |
+
lib = os.path.join(site.USER_BASE, 'lib')
|
| 167 |
+
incl = os.path.join(site.USER_BASE, 'include')
|
| 168 |
+
os.mkdir(lib)
|
| 169 |
+
os.mkdir(incl)
|
| 170 |
+
|
| 171 |
+
# let's run finalize
|
| 172 |
+
cmd.ensure_finalized()
|
| 173 |
+
|
| 174 |
+
# see if include_dirs and library_dirs
|
| 175 |
+
# were set
|
| 176 |
+
assert lib in cmd.library_dirs
|
| 177 |
+
assert lib in cmd.rpath
|
| 178 |
+
assert incl in cmd.include_dirs
|
| 179 |
+
|
| 180 |
+
def test_optional_extension(self):
|
| 181 |
+
# this extension will fail, but let's ignore this failure
|
| 182 |
+
# with the optional argument.
|
| 183 |
+
modules = [Extension('foo', ['xxx'], optional=False)]
|
| 184 |
+
dist = Distribution({'name': 'xx', 'ext_modules': modules})
|
| 185 |
+
cmd = self.build_ext(dist)
|
| 186 |
+
cmd.ensure_finalized()
|
| 187 |
+
with pytest.raises((UnknownFileError, CompileError)):
|
| 188 |
+
cmd.run() # should raise an error
|
| 189 |
+
|
| 190 |
+
modules = [Extension('foo', ['xxx'], optional=True)]
|
| 191 |
+
dist = Distribution({'name': 'xx', 'ext_modules': modules})
|
| 192 |
+
cmd = self.build_ext(dist)
|
| 193 |
+
cmd.ensure_finalized()
|
| 194 |
+
cmd.run() # should pass
|
| 195 |
+
|
| 196 |
+
def test_finalize_options(self):
|
| 197 |
+
# Make sure Python's include directories (for Python.h, pyconfig.h,
|
| 198 |
+
# etc.) are in the include search path.
|
| 199 |
+
modules = [Extension('foo', ['xxx'], optional=False)]
|
| 200 |
+
dist = Distribution({'name': 'xx', 'ext_modules': modules})
|
| 201 |
+
cmd = self.build_ext(dist)
|
| 202 |
+
cmd.finalize_options()
|
| 203 |
+
|
| 204 |
+
py_include = sysconfig.get_python_inc()
|
| 205 |
+
for p in py_include.split(os.path.pathsep):
|
| 206 |
+
assert p in cmd.include_dirs
|
| 207 |
+
|
| 208 |
+
plat_py_include = sysconfig.get_python_inc(plat_specific=True)
|
| 209 |
+
for p in plat_py_include.split(os.path.pathsep):
|
| 210 |
+
assert p in cmd.include_dirs
|
| 211 |
+
|
| 212 |
+
# make sure cmd.libraries is turned into a list
|
| 213 |
+
# if it's a string
|
| 214 |
+
cmd = self.build_ext(dist)
|
| 215 |
+
cmd.libraries = 'my_lib, other_lib lastlib'
|
| 216 |
+
cmd.finalize_options()
|
| 217 |
+
assert cmd.libraries == ['my_lib', 'other_lib', 'lastlib']
|
| 218 |
+
|
| 219 |
+
# make sure cmd.library_dirs is turned into a list
|
| 220 |
+
# if it's a string
|
| 221 |
+
cmd = self.build_ext(dist)
|
| 222 |
+
cmd.library_dirs = f'my_lib_dir{os.pathsep}other_lib_dir'
|
| 223 |
+
cmd.finalize_options()
|
| 224 |
+
assert 'my_lib_dir' in cmd.library_dirs
|
| 225 |
+
assert 'other_lib_dir' in cmd.library_dirs
|
| 226 |
+
|
| 227 |
+
# make sure rpath is turned into a list
|
| 228 |
+
# if it's a string
|
| 229 |
+
cmd = self.build_ext(dist)
|
| 230 |
+
cmd.rpath = f'one{os.pathsep}two'
|
| 231 |
+
cmd.finalize_options()
|
| 232 |
+
assert cmd.rpath == ['one', 'two']
|
| 233 |
+
|
| 234 |
+
# make sure cmd.link_objects is turned into a list
|
| 235 |
+
# if it's a string
|
| 236 |
+
cmd = build_ext(dist)
|
| 237 |
+
cmd.link_objects = 'one two,three'
|
| 238 |
+
cmd.finalize_options()
|
| 239 |
+
assert cmd.link_objects == ['one', 'two', 'three']
|
| 240 |
+
|
| 241 |
+
# XXX more tests to perform for win32
|
| 242 |
+
|
| 243 |
+
# make sure define is turned into 2-tuples
|
| 244 |
+
# strings if they are ','-separated strings
|
| 245 |
+
cmd = self.build_ext(dist)
|
| 246 |
+
cmd.define = 'one,two'
|
| 247 |
+
cmd.finalize_options()
|
| 248 |
+
assert cmd.define == [('one', '1'), ('two', '1')]
|
| 249 |
+
|
| 250 |
+
# make sure undef is turned into a list of
|
| 251 |
+
# strings if they are ','-separated strings
|
| 252 |
+
cmd = self.build_ext(dist)
|
| 253 |
+
cmd.undef = 'one,two'
|
| 254 |
+
cmd.finalize_options()
|
| 255 |
+
assert cmd.undef == ['one', 'two']
|
| 256 |
+
|
| 257 |
+
# make sure swig_opts is turned into a list
|
| 258 |
+
cmd = self.build_ext(dist)
|
| 259 |
+
cmd.swig_opts = None
|
| 260 |
+
cmd.finalize_options()
|
| 261 |
+
assert cmd.swig_opts == []
|
| 262 |
+
|
| 263 |
+
cmd = self.build_ext(dist)
|
| 264 |
+
cmd.swig_opts = '1 2'
|
| 265 |
+
cmd.finalize_options()
|
| 266 |
+
assert cmd.swig_opts == ['1', '2']
|
| 267 |
+
|
| 268 |
+
def test_check_extensions_list(self):
|
| 269 |
+
dist = Distribution()
|
| 270 |
+
cmd = self.build_ext(dist)
|
| 271 |
+
cmd.finalize_options()
|
| 272 |
+
|
| 273 |
+
# 'extensions' option must be a list of Extension instances
|
| 274 |
+
with pytest.raises(DistutilsSetupError):
|
| 275 |
+
cmd.check_extensions_list('foo')
|
| 276 |
+
|
| 277 |
+
# each element of 'ext_modules' option must be an
|
| 278 |
+
# Extension instance or 2-tuple
|
| 279 |
+
exts = [('bar', 'foo', 'bar'), 'foo']
|
| 280 |
+
with pytest.raises(DistutilsSetupError):
|
| 281 |
+
cmd.check_extensions_list(exts)
|
| 282 |
+
|
| 283 |
+
# first element of each tuple in 'ext_modules'
|
| 284 |
+
# must be the extension name (a string) and match
|
| 285 |
+
# a python dotted-separated name
|
| 286 |
+
exts = [('foo-bar', '')]
|
| 287 |
+
with pytest.raises(DistutilsSetupError):
|
| 288 |
+
cmd.check_extensions_list(exts)
|
| 289 |
+
|
| 290 |
+
# second element of each tuple in 'ext_modules'
|
| 291 |
+
# must be a dictionary (build info)
|
| 292 |
+
exts = [('foo.bar', '')]
|
| 293 |
+
with pytest.raises(DistutilsSetupError):
|
| 294 |
+
cmd.check_extensions_list(exts)
|
| 295 |
+
|
| 296 |
+
# ok this one should pass
|
| 297 |
+
exts = [('foo.bar', {'sources': [''], 'libraries': 'foo', 'some': 'bar'})]
|
| 298 |
+
cmd.check_extensions_list(exts)
|
| 299 |
+
ext = exts[0]
|
| 300 |
+
assert isinstance(ext, Extension)
|
| 301 |
+
|
| 302 |
+
# check_extensions_list adds in ext the values passed
|
| 303 |
+
# when they are in ('include_dirs', 'library_dirs', 'libraries'
|
| 304 |
+
# 'extra_objects', 'extra_compile_args', 'extra_link_args')
|
| 305 |
+
assert ext.libraries == 'foo'
|
| 306 |
+
assert not hasattr(ext, 'some')
|
| 307 |
+
|
| 308 |
+
# 'macros' element of build info dict must be 1- or 2-tuple
|
| 309 |
+
exts = [
|
| 310 |
+
(
|
| 311 |
+
'foo.bar',
|
| 312 |
+
{
|
| 313 |
+
'sources': [''],
|
| 314 |
+
'libraries': 'foo',
|
| 315 |
+
'some': 'bar',
|
| 316 |
+
'macros': [('1', '2', '3'), 'foo'],
|
| 317 |
+
},
|
| 318 |
+
)
|
| 319 |
+
]
|
| 320 |
+
with pytest.raises(DistutilsSetupError):
|
| 321 |
+
cmd.check_extensions_list(exts)
|
| 322 |
+
|
| 323 |
+
exts[0][1]['macros'] = [('1', '2'), ('3',)]
|
| 324 |
+
cmd.check_extensions_list(exts)
|
| 325 |
+
assert exts[0].undef_macros == ['3']
|
| 326 |
+
assert exts[0].define_macros == [('1', '2')]
|
| 327 |
+
|
| 328 |
+
def test_get_source_files(self):
|
| 329 |
+
modules = [Extension('foo', ['xxx'], optional=False)]
|
| 330 |
+
dist = Distribution({'name': 'xx', 'ext_modules': modules})
|
| 331 |
+
cmd = self.build_ext(dist)
|
| 332 |
+
cmd.ensure_finalized()
|
| 333 |
+
assert cmd.get_source_files() == ['xxx']
|
| 334 |
+
|
| 335 |
+
def test_unicode_module_names(self):
|
| 336 |
+
modules = [
|
| 337 |
+
Extension('foo', ['aaa'], optional=False),
|
| 338 |
+
Extension('föö', ['uuu'], optional=False),
|
| 339 |
+
]
|
| 340 |
+
dist = Distribution({'name': 'xx', 'ext_modules': modules})
|
| 341 |
+
cmd = self.build_ext(dist)
|
| 342 |
+
cmd.ensure_finalized()
|
| 343 |
+
assert re.search(r'foo(_d)?\..*', cmd.get_ext_filename(modules[0].name))
|
| 344 |
+
assert re.search(r'föö(_d)?\..*', cmd.get_ext_filename(modules[1].name))
|
| 345 |
+
assert cmd.get_export_symbols(modules[0]) == ['PyInit_foo']
|
| 346 |
+
assert cmd.get_export_symbols(modules[1]) == ['PyInitU_f_1gaa']
|
| 347 |
+
|
| 348 |
+
def test_compiler_option(self):
|
| 349 |
+
# cmd.compiler is an option and
|
| 350 |
+
# should not be overridden by a compiler instance
|
| 351 |
+
# when the command is run
|
| 352 |
+
dist = Distribution()
|
| 353 |
+
cmd = self.build_ext(dist)
|
| 354 |
+
cmd.compiler = 'unix'
|
| 355 |
+
cmd.ensure_finalized()
|
| 356 |
+
cmd.run()
|
| 357 |
+
assert cmd.compiler == 'unix'
|
| 358 |
+
|
| 359 |
+
def test_get_outputs(self):
|
| 360 |
+
missing_compiler_executable()
|
| 361 |
+
tmp_dir = self.mkdtemp()
|
| 362 |
+
c_file = os.path.join(tmp_dir, 'foo.c')
|
| 363 |
+
self.write_file(c_file, 'void PyInit_foo(void) {}\n')
|
| 364 |
+
ext = Extension('foo', [c_file], optional=False)
|
| 365 |
+
dist = Distribution({'name': 'xx', 'ext_modules': [ext]})
|
| 366 |
+
cmd = self.build_ext(dist)
|
| 367 |
+
fixup_build_ext(cmd)
|
| 368 |
+
cmd.ensure_finalized()
|
| 369 |
+
assert len(cmd.get_outputs()) == 1
|
| 370 |
+
|
| 371 |
+
cmd.build_lib = os.path.join(self.tmp_dir, 'build')
|
| 372 |
+
cmd.build_temp = os.path.join(self.tmp_dir, 'tempt')
|
| 373 |
+
|
| 374 |
+
# issue #5977 : distutils build_ext.get_outputs
|
| 375 |
+
# returns wrong result with --inplace
|
| 376 |
+
other_tmp_dir = os.path.realpath(self.mkdtemp())
|
| 377 |
+
old_wd = os.getcwd()
|
| 378 |
+
os.chdir(other_tmp_dir)
|
| 379 |
+
try:
|
| 380 |
+
cmd.inplace = True
|
| 381 |
+
cmd.run()
|
| 382 |
+
so_file = cmd.get_outputs()[0]
|
| 383 |
+
finally:
|
| 384 |
+
os.chdir(old_wd)
|
| 385 |
+
assert os.path.exists(so_file)
|
| 386 |
+
ext_suffix = sysconfig.get_config_var('EXT_SUFFIX')
|
| 387 |
+
assert so_file.endswith(ext_suffix)
|
| 388 |
+
so_dir = os.path.dirname(so_file)
|
| 389 |
+
assert so_dir == other_tmp_dir
|
| 390 |
+
|
| 391 |
+
cmd.inplace = False
|
| 392 |
+
cmd.compiler = None
|
| 393 |
+
cmd.run()
|
| 394 |
+
so_file = cmd.get_outputs()[0]
|
| 395 |
+
assert os.path.exists(so_file)
|
| 396 |
+
assert so_file.endswith(ext_suffix)
|
| 397 |
+
so_dir = os.path.dirname(so_file)
|
| 398 |
+
assert so_dir == cmd.build_lib
|
| 399 |
+
|
| 400 |
+
# inplace = False, cmd.package = 'bar'
|
| 401 |
+
build_py = cmd.get_finalized_command('build_py')
|
| 402 |
+
build_py.package_dir = {'': 'bar'}
|
| 403 |
+
path = cmd.get_ext_fullpath('foo')
|
| 404 |
+
# checking that the last directory is the build_dir
|
| 405 |
+
path = os.path.split(path)[0]
|
| 406 |
+
assert path == cmd.build_lib
|
| 407 |
+
|
| 408 |
+
# inplace = True, cmd.package = 'bar'
|
| 409 |
+
cmd.inplace = True
|
| 410 |
+
other_tmp_dir = os.path.realpath(self.mkdtemp())
|
| 411 |
+
old_wd = os.getcwd()
|
| 412 |
+
os.chdir(other_tmp_dir)
|
| 413 |
+
try:
|
| 414 |
+
path = cmd.get_ext_fullpath('foo')
|
| 415 |
+
finally:
|
| 416 |
+
os.chdir(old_wd)
|
| 417 |
+
# checking that the last directory is bar
|
| 418 |
+
path = os.path.split(path)[0]
|
| 419 |
+
lastdir = os.path.split(path)[-1]
|
| 420 |
+
assert lastdir == 'bar'
|
| 421 |
+
|
| 422 |
+
def test_ext_fullpath(self):
|
| 423 |
+
ext = sysconfig.get_config_var('EXT_SUFFIX')
|
| 424 |
+
# building lxml.etree inplace
|
| 425 |
+
# etree_c = os.path.join(self.tmp_dir, 'lxml.etree.c')
|
| 426 |
+
# etree_ext = Extension('lxml.etree', [etree_c])
|
| 427 |
+
# dist = Distribution({'name': 'lxml', 'ext_modules': [etree_ext]})
|
| 428 |
+
dist = Distribution()
|
| 429 |
+
cmd = self.build_ext(dist)
|
| 430 |
+
cmd.inplace = True
|
| 431 |
+
cmd.distribution.package_dir = {'': 'src'}
|
| 432 |
+
cmd.distribution.packages = ['lxml', 'lxml.html']
|
| 433 |
+
curdir = os.getcwd()
|
| 434 |
+
wanted = os.path.join(curdir, 'src', 'lxml', 'etree' + ext)
|
| 435 |
+
path = cmd.get_ext_fullpath('lxml.etree')
|
| 436 |
+
assert wanted == path
|
| 437 |
+
|
| 438 |
+
# building lxml.etree not inplace
|
| 439 |
+
cmd.inplace = False
|
| 440 |
+
cmd.build_lib = os.path.join(curdir, 'tmpdir')
|
| 441 |
+
wanted = os.path.join(curdir, 'tmpdir', 'lxml', 'etree' + ext)
|
| 442 |
+
path = cmd.get_ext_fullpath('lxml.etree')
|
| 443 |
+
assert wanted == path
|
| 444 |
+
|
| 445 |
+
# building twisted.runner.portmap not inplace
|
| 446 |
+
build_py = cmd.get_finalized_command('build_py')
|
| 447 |
+
build_py.package_dir = {}
|
| 448 |
+
cmd.distribution.packages = ['twisted', 'twisted.runner.portmap']
|
| 449 |
+
path = cmd.get_ext_fullpath('twisted.runner.portmap')
|
| 450 |
+
wanted = os.path.join(curdir, 'tmpdir', 'twisted', 'runner', 'portmap' + ext)
|
| 451 |
+
assert wanted == path
|
| 452 |
+
|
| 453 |
+
# building twisted.runner.portmap inplace
|
| 454 |
+
cmd.inplace = True
|
| 455 |
+
path = cmd.get_ext_fullpath('twisted.runner.portmap')
|
| 456 |
+
wanted = os.path.join(curdir, 'twisted', 'runner', 'portmap' + ext)
|
| 457 |
+
assert wanted == path
|
| 458 |
+
|
| 459 |
+
@pytest.mark.skipif('platform.system() != "Darwin"')
|
| 460 |
+
@pytest.mark.usefixtures('save_env')
|
| 461 |
+
def test_deployment_target_default(self):
|
| 462 |
+
# Issue 9516: Test that, in the absence of the environment variable,
|
| 463 |
+
# an extension module is compiled with the same deployment target as
|
| 464 |
+
# the interpreter.
|
| 465 |
+
self._try_compile_deployment_target('==', None)
|
| 466 |
+
|
| 467 |
+
@pytest.mark.skipif('platform.system() != "Darwin"')
|
| 468 |
+
@pytest.mark.usefixtures('save_env')
|
| 469 |
+
def test_deployment_target_too_low(self):
|
| 470 |
+
# Issue 9516: Test that an extension module is not allowed to be
|
| 471 |
+
# compiled with a deployment target less than that of the interpreter.
|
| 472 |
+
with pytest.raises(DistutilsPlatformError):
|
| 473 |
+
self._try_compile_deployment_target('>', '10.1')
|
| 474 |
+
|
| 475 |
+
@pytest.mark.skipif('platform.system() != "Darwin"')
|
| 476 |
+
@pytest.mark.usefixtures('save_env')
|
| 477 |
+
def test_deployment_target_higher_ok(self): # pragma: no cover
|
| 478 |
+
# Issue 9516: Test that an extension module can be compiled with a
|
| 479 |
+
# deployment target higher than that of the interpreter: the ext
|
| 480 |
+
# module may depend on some newer OS feature.
|
| 481 |
+
deptarget = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET')
|
| 482 |
+
if deptarget:
|
| 483 |
+
# increment the minor version number (i.e. 10.6 -> 10.7)
|
| 484 |
+
deptarget = [int(x) for x in deptarget.split('.')]
|
| 485 |
+
deptarget[-1] += 1
|
| 486 |
+
deptarget = '.'.join(str(i) for i in deptarget)
|
| 487 |
+
self._try_compile_deployment_target('<', deptarget)
|
| 488 |
+
|
| 489 |
+
def _try_compile_deployment_target(self, operator, target): # pragma: no cover
|
| 490 |
+
if target is None:
|
| 491 |
+
if os.environ.get('MACOSX_DEPLOYMENT_TARGET'):
|
| 492 |
+
del os.environ['MACOSX_DEPLOYMENT_TARGET']
|
| 493 |
+
else:
|
| 494 |
+
os.environ['MACOSX_DEPLOYMENT_TARGET'] = target
|
| 495 |
+
|
| 496 |
+
jaraco.path.build(
|
| 497 |
+
{
|
| 498 |
+
'deptargetmodule.c': textwrap.dedent(f"""\
|
| 499 |
+
#include <AvailabilityMacros.h>
|
| 500 |
+
|
| 501 |
+
int dummy;
|
| 502 |
+
|
| 503 |
+
#if TARGET {operator} MAC_OS_X_VERSION_MIN_REQUIRED
|
| 504 |
+
#else
|
| 505 |
+
#error "Unexpected target"
|
| 506 |
+
#endif
|
| 507 |
+
|
| 508 |
+
"""),
|
| 509 |
+
},
|
| 510 |
+
self.tmp_path,
|
| 511 |
+
)
|
| 512 |
+
|
| 513 |
+
# get the deployment target that the interpreter was built with
|
| 514 |
+
target = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET')
|
| 515 |
+
target = tuple(map(int, target.split('.')[0:2]))
|
| 516 |
+
# format the target value as defined in the Apple
|
| 517 |
+
# Availability Macros. We can't use the macro names since
|
| 518 |
+
# at least one value we test with will not exist yet.
|
| 519 |
+
if target[:2] < (10, 10):
|
| 520 |
+
# for 10.1 through 10.9.x -> "10n0"
|
| 521 |
+
tmpl = '{:02}{:01}0'
|
| 522 |
+
else:
|
| 523 |
+
# for 10.10 and beyond -> "10nn00"
|
| 524 |
+
if len(target) >= 2:
|
| 525 |
+
tmpl = '{:02}{:02}00'
|
| 526 |
+
else:
|
| 527 |
+
# 11 and later can have no minor version (11 instead of 11.0)
|
| 528 |
+
tmpl = '{:02}0000'
|
| 529 |
+
target = tmpl.format(*target)
|
| 530 |
+
deptarget_ext = Extension(
|
| 531 |
+
'deptarget',
|
| 532 |
+
[self.tmp_path / 'deptargetmodule.c'],
|
| 533 |
+
extra_compile_args=[f'-DTARGET={target}'],
|
| 534 |
+
)
|
| 535 |
+
dist = Distribution({'name': 'deptarget', 'ext_modules': [deptarget_ext]})
|
| 536 |
+
dist.package_dir = self.tmp_dir
|
| 537 |
+
cmd = self.build_ext(dist)
|
| 538 |
+
cmd.build_lib = self.tmp_dir
|
| 539 |
+
cmd.build_temp = self.tmp_dir
|
| 540 |
+
|
| 541 |
+
try:
|
| 542 |
+
old_stdout = sys.stdout
|
| 543 |
+
if not support.verbose:
|
| 544 |
+
# silence compiler output
|
| 545 |
+
sys.stdout = StringIO()
|
| 546 |
+
try:
|
| 547 |
+
cmd.ensure_finalized()
|
| 548 |
+
cmd.run()
|
| 549 |
+
finally:
|
| 550 |
+
sys.stdout = old_stdout
|
| 551 |
+
|
| 552 |
+
except CompileError:
|
| 553 |
+
self.fail("Wrong deployment target during compilation")
|
| 554 |
+
|
| 555 |
+
|
| 556 |
+
class TestParallelBuildExt(TestBuildExt):
|
| 557 |
+
def build_ext(self, *args, **kwargs):
|
| 558 |
+
build_ext = super().build_ext(*args, **kwargs)
|
| 559 |
+
build_ext.parallel = True
|
| 560 |
+
return build_ext
|