Skip to content

Commit ffabc07

Browse files
committed
Ensure that gcc path is only added once to DLL search path
1 parent 339aab4 commit ffabc07

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
@@ -20,6 +20,7 @@
2020
import textwrap
2121
import time
2222
import warnings
23+
from functools import cache
2324
from io import BytesIO, StringIO
2425
from typing import TYPE_CHECKING, Callable, Optional, Protocol, cast
2526

@@ -270,6 +271,14 @@ def _get_ext_suffix():
270271
return dist_suffix
271272

272273

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

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

0 commit comments

Comments
 (0)