11from __future__ import annotations
22
3+ import ctypes
34import os
45import runpy
56import sys
@@ -15,12 +16,23 @@ def configure_windows_dll_search_path() -> None:
1516 return
1617
1718 runtime_executable_dir = Path (sys .executable ).resolve ().parent
19+ site_packages_dirs = [
20+ runtime_executable_dir / "Lib" / "site-packages" ,
21+ BACKEND_DIR / "python" / "Lib" / "site-packages" ,
22+ ]
1823 candidates = [
1924 runtime_executable_dir ,
2025 runtime_executable_dir / "DLLs" ,
2126 BACKEND_DIR / "python" ,
2227 BACKEND_DIR / "python" / "DLLs" ,
2328 ]
29+ for site_packages_dir in site_packages_dirs :
30+ candidates .extend (
31+ [
32+ site_packages_dir / "cryptography.libs" ,
33+ site_packages_dir / "cryptography" / "hazmat" / "bindings" ,
34+ ],
35+ )
2436
2537 normalized_added : set [str ] = set ()
2638 path_entries : list [str ] = []
@@ -49,7 +61,45 @@ def configure_windows_dll_search_path() -> None:
4961 )
5062
5163
64+ def preload_windows_runtime_dlls () -> None :
65+ if sys .platform != "win32" :
66+ return
67+
68+ runtime_executable_dir = Path (sys .executable ).resolve ().parent
69+ runtime_dll_dir = runtime_executable_dir / "DLLs"
70+ backend_runtime_dir = BACKEND_DIR / "python"
71+ backend_runtime_dll_dir = backend_runtime_dir / "DLLs"
72+ candidate_dirs = [
73+ runtime_executable_dir ,
74+ runtime_dll_dir ,
75+ backend_runtime_dir ,
76+ backend_runtime_dll_dir ,
77+ ]
78+ patterns = [
79+ "python3.dll" ,
80+ "python*.dll" ,
81+ "vcruntime*.dll" ,
82+ "libcrypto-*.dll" ,
83+ "libssl-*.dll" ,
84+ ]
85+ loaded : set [str ] = set ()
86+ for candidate_dir in candidate_dirs :
87+ if not candidate_dir .is_dir ():
88+ continue
89+ for pattern in patterns :
90+ for dll_path in candidate_dir .glob (pattern ):
91+ normalized_path = str (dll_path .resolve ()).lower ()
92+ if normalized_path in loaded :
93+ continue
94+ loaded .add (normalized_path )
95+ try :
96+ ctypes .WinDLL (str (dll_path ))
97+ except OSError :
98+ continue
99+
100+
52101configure_windows_dll_search_path ()
102+ preload_windows_runtime_dlls ()
53103
54104sys .path .insert (0 , str (APP_DIR ))
55105
0 commit comments