提交 76a6c2e9 authored 作者: Ben Mares's avatar Ben Mares 提交者: Thomas Wiecki

Ensure that gcc path is only added once to DLL search path

上级 f7b0a7a4
...@@ -21,6 +21,7 @@ import textwrap ...@@ -21,6 +21,7 @@ import textwrap
import time import time
import warnings import warnings
from collections.abc import Callable from collections.abc import Callable
from functools import cache
from io import BytesIO, StringIO from io import BytesIO, StringIO
from typing import TYPE_CHECKING, Protocol, cast from typing import TYPE_CHECKING, Protocol, cast
...@@ -271,6 +272,21 @@ def _get_ext_suffix(): ...@@ -271,6 +272,21 @@ def _get_ext_suffix():
return dist_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): def dlimport(fullpath, suffix=None):
""" """
Dynamically load a .so, .pyd, .dll, or .py file. Dynamically load a .so, .pyd, .dll, or .py file.
...@@ -320,11 +336,7 @@ def dlimport(fullpath, suffix=None): ...@@ -320,11 +336,7 @@ def dlimport(fullpath, suffix=None):
_logger.debug(f"module_name {module_name}") _logger.debug(f"module_name {module_name}")
sys.path[0:0] = [workdir] # insert workdir at beginning (temporarily) sys.path[0:0] = [workdir] # insert workdir at beginning (temporarily)
# Explicitly add gcc dll directory on Python 3.8+ on Windows add_gcc_dll_directory()
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))
global import_time global import_time
try: try:
importlib.invalidate_caches() importlib.invalidate_caches()
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论