Skip to content

Commit

Permalink
Ensure that gcc path is only added once to DLL search path
Browse files Browse the repository at this point in the history
  • Loading branch information
maresb authored and twiecki committed Apr 3, 2024
1 parent f7b0a7a commit 76a6c2e
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions pytensor/link/c/cmodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import time
import warnings
from collections.abc import Callable
from functools import cache
from io import BytesIO, StringIO
from typing import TYPE_CHECKING, Protocol, cast

Expand Down Expand Up @@ -271,6 +272,21 @@ def _get_ext_suffix():
return dist_suffix


@cache # See explanation in docstring.
def add_gcc_dll_directory() -> None:
"""On Windows, detect and add the location of gcc to the DLL search directory.
On non-Windows platforms this is a noop.
The @cache decorator ensures that this function only executes once to avoid
redundant entries. See <https://github.com/pymc-devs/pytensor/pull/678>.
"""
if (sys.platform == "win32") & (hasattr(os, "add_dll_directory")):
gcc_path = shutil.which("gcc")
if gcc_path is not None:
os.add_dll_directory(os.path.dirname(gcc_path)) # type: ignore


def dlimport(fullpath, suffix=None):
"""
Dynamically load a .so, .pyd, .dll, or .py file.
Expand Down Expand Up @@ -320,11 +336,7 @@ def dlimport(fullpath, suffix=None):
_logger.debug(f"module_name {module_name}")

sys.path[0:0] = [workdir] # insert workdir at beginning (temporarily)
# Explicitly add gcc dll directory on Python 3.8+ on Windows
if (sys.platform == "win32") & (hasattr(os, "add_dll_directory")):
gcc_path = shutil.which("gcc")
if gcc_path is not None:
os.add_dll_directory(os.path.dirname(gcc_path))
add_gcc_dll_directory()
global import_time
try:
importlib.invalidate_caches()
Expand Down

0 comments on commit 76a6c2e

Please sign in to comment.