Skip to content

Commit 76a6c2e

Browse files
maresbtwiecki
authored andcommitted
Ensure that gcc path is only added once to DLL search path
1 parent f7b0a7a commit 76a6c2e

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

pytensor/link/c/cmodule.py

+17-5
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,21 @@ def _get_ext_suffix():
271272
return dist_suffix
272273

273274

275+
@cache # See explanation in docstring.
276+
def add_gcc_dll_directory() -> None:
277+
"""On Windows, detect and add the location of gcc to the DLL search directory.
278+
279+
On non-Windows platforms this is a noop.
280+
281+
The @cache decorator ensures that this function only executes once to avoid
282+
redundant entries. See <https://github.com/pymc-devs/pytensor/pull/678>.
283+
"""
284+
if (sys.platform == "win32") & (hasattr(os, "add_dll_directory")):
285+
gcc_path = shutil.which("gcc")
286+
if gcc_path is not None:
287+
os.add_dll_directory(os.path.dirname(gcc_path)) # type: ignore
288+
289+
274290
def dlimport(fullpath, suffix=None):
275291
"""
276292
Dynamically load a .so, .pyd, .dll, or .py file.
@@ -320,11 +336,7 @@ def dlimport(fullpath, suffix=None):
320336
_logger.debug(f"module_name {module_name}")
321337

322338
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))
339+
add_gcc_dll_directory()
328340
global import_time
329341
try:
330342
importlib.invalidate_caches()

0 commit comments

Comments
 (0)