Skip to content

Commit f727687

Browse files
committed
Ensure that gcc path is only added once to DLL search path
1 parent f7b0a7a commit f727687

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

pytensor/link/c/cmodule.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import time
2222
import warnings
2323
from collections.abc import Callable
24+
from functools import cache
2425
from io import BytesIO, StringIO
2526
from typing import TYPE_CHECKING, Protocol, cast
2627

@@ -271,6 +272,14 @@ def _get_ext_suffix():
271272
return dist_suffix
272273

273274

275+
@cache
276+
def add_gcc_dll_directory() -> None:
277+
if (sys.platform == "win32") & (hasattr(os, "add_dll_directory")):
278+
gcc_path = shutil.which("gcc")
279+
if gcc_path is not None:
280+
os.add_dll_directory(os.path.dirname(gcc_path)) # type: ignore
281+
282+
274283
def dlimport(fullpath, suffix=None):
275284
"""
276285
Dynamically load a .so, .pyd, .dll, or .py file.
@@ -320,11 +329,7 @@ def dlimport(fullpath, suffix=None):
320329
_logger.debug(f"module_name {module_name}")
321330

322331
sys.path[0:0] = [workdir] # insert workdir at beginning (temporarily)
323-
# Explicitly add gcc dll directory on Python 3.8+ on Windows
324-
if (sys.platform == "win32") & (hasattr(os, "add_dll_directory")):
325-
gcc_path = shutil.which("gcc")
326-
if gcc_path is not None:
327-
os.add_dll_directory(os.path.dirname(gcc_path))
332+
add_gcc_dll_directory()
328333
global import_time
329334
try:
330335
importlib.invalidate_caches()

0 commit comments

Comments
 (0)