1616import logging
1717import sys
1818
19- # ---------------------------------------------------------------------------
20- # Pre-load ORT/GenAI DLLs from e2e-test-pkgs BEFORE importing the SDK.
21- #
22- # The ``_brotli`` C extension (pulled in by httpx → openai → SDK) calls
23- # ``SetDefaultDllDirectories`` during import, which restricts subsequent
24- # ``LoadLibraryExW`` calls. Pre-loading here ensures ORT/GenAI are already
25- # in the process before brotli changes the DLL search behavior.
26- # ---------------------------------------------------------------------------
19+ import pytest
20+
21+ from pathlib import Path
22+
23+ from foundry_local_sdk .configuration import Configuration , LogLevel
24+ from foundry_local_sdk .foundry_local_manager import FoundryLocalManager
25+
26+ logger = logging .getLogger (__name__ )
27+
2728
2829def _get_e2e_test_pkgs_path ():
2930 """Locate the e2e-test-pkgs directory by walking up from this file."""
@@ -38,40 +39,6 @@ def _get_e2e_test_pkgs_path():
3839 return None
3940 current = parent
4041
41- if sys .platform .startswith ("win" ):
42- import ctypes
43-
44- def _preload_e2e_dlls ():
45- pkgs = _get_e2e_test_pkgs_path ()
46- if pkgs is None :
47- return
48-
49- ort_dll = pkgs / "onnxruntime.dll"
50- genai_dll = pkgs / "onnxruntime-genai.dll"
51- if not (ort_dll .exists () and genai_dll .exists ()):
52- return
53-
54- kernel32 = ctypes .windll .kernel32
55- kernel32 .SetDllDirectoryW (str (pkgs ))
56- os .add_dll_directory (str (pkgs ))
57- os .environ ["ORT_LIB_PATH" ] = str (ort_dll )
58-
59- kernel32 .LoadLibraryExW .restype = ctypes .c_void_p
60- kernel32 .LoadLibraryExW .argtypes = [ctypes .c_wchar_p , ctypes .c_void_p , ctypes .c_int ]
61- kernel32 .LoadLibraryExW (str (ort_dll ), None , 0x00000008 )
62- kernel32 .LoadLibraryExW (str (genai_dll ), None , 0x00000008 )
63-
64- _preload_e2e_dlls ()
65-
66- import pytest
67-
68- from pathlib import Path
69-
70- from foundry_local_sdk .configuration import Configuration , LogLevel
71- from foundry_local_sdk .foundry_local_manager import FoundryLocalManager
72-
73- logger = logging .getLogger (__name__ )
74-
7542TEST_MODEL_ALIAS = "qwen2.5-0.5b"
7643AUDIO_MODEL_ALIAS = "whisper-tiny"
7744EMBEDDING_MODEL_ALIAS = "qwen3-0.6b-embedding-generic-cpu"
@@ -206,11 +173,10 @@ def model_load_manager(manager):
206173# ---------------------------------------------------------------------------
207174
208175def _preload_and_init_e2e ():
209- """Pre-load DLLs from e2e-test-pkgs and initialize the SDK for E2E tests.
176+ """Load DLLs from e2e-test-pkgs and initialize the SDK for E2E tests.
210177
211- Checks whether ORT is already loaded in the process (from the DLL preload
212- above) and skips the pre-load if so. Then loads Core.dll, sets up FFI
213- function signatures, and initializes FoundryLocalManager.
178+ Loads Core.dll, sets up FFI function signatures, and initializes
179+ FoundryLocalManager pointing at the e2e-test-pkgs models directory.
214180 """
215181 import ctypes
216182 from foundry_local_sdk .detail .core_interop import (
@@ -234,29 +200,16 @@ def _preload_and_init_e2e():
234200 if not (paths .core .exists () and paths .ort .exists () and paths .genai .exists ()):
235201 return None , "E2E DLLs not found"
236202
237- kernel32 = ctypes .windll .kernel32
238-
239- # Check if ORT is already loaded (e.g. from conftest preload)
240- kernel32 .GetModuleHandleW .restype = ctypes .c_void_p
241- kernel32 .GetModuleHandleW .argtypes = [ctypes .c_wchar_p ]
242- ort_already_loaded = bool (kernel32 .GetModuleHandleW ("onnxruntime.dll" ))
203+ # Register directory so Core.dll can find ORT/GenAI via P/Invoke
204+ os .add_dll_directory (str (pkgs ))
205+ os .environ ["ORT_LIB_PATH" ] = str (paths .ort )
243206
244- if not ort_already_loaded :
245- kernel32 .SetDllDirectoryW (str (pkgs ))
246- os .add_dll_directory (str (pkgs ))
247- os .environ ["ORT_LIB_PATH" ] = str (paths .ort )
248-
249- kernel32 .LoadLibraryExW .restype = ctypes .c_void_p
250- kernel32 .LoadLibraryExW .argtypes = [ctypes .c_wchar_p , ctypes .c_void_p , ctypes .c_int ]
251- _LOAD_WITH_ALTERED_SEARCH_PATH = 0x00000008
252-
253- h_ort = kernel32 .LoadLibraryExW (str (paths .ort ), None , _LOAD_WITH_ALTERED_SEARCH_PATH )
254- if not h_ort :
255- return None , f"Failed to load ORT (WinError { kernel32 .GetLastError ()} )"
256-
257- h_genai = kernel32 .LoadLibraryExW (str (paths .genai ), None , _LOAD_WITH_ALTERED_SEARCH_PATH )
258- if not h_genai :
259- return None , f"Failed to load GenAI (WinError { kernel32 .GetLastError ()} )"
207+ # Pre-load ORT and GenAI so they are available when Core does P/Invoke
208+ try :
209+ ctypes .CDLL (str (paths .ort ))
210+ ctypes .CDLL (str (paths .genai ))
211+ except OSError as e :
212+ return None , f"Failed to load ORT/GenAI DLLs: { e } "
260213
261214 # Load Core.dll and set up function signatures
262215 CoreInterop ._flcore_library = ctypes .CDLL (str (paths .core ))
0 commit comments