Skip to content

Use add_dll_directory as a context manager #708

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 21 additions & 20 deletions pytensor/link/c/cmodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import time
import warnings
from collections.abc import Callable
from functools import cache
from contextlib import AbstractContextManager, nullcontext
from io import BytesIO, StringIO
from typing import TYPE_CHECKING, Protocol, cast

Expand Down Expand Up @@ -272,23 +272,24 @@ def _get_ext_suffix():
return dist_suffix


@cache # See explanation in docstring.
def add_gcc_dll_directory() -> None:
def add_gcc_dll_directory() -> AbstractContextManager[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>.
Returns a context manager to be used with `with`. The entry is removed when the
context manager is closed. See <https://github.com/pymc-devs/pytensor/pull/678>.
"""
cm: AbstractContextManager[None] = nullcontext()
if (sys.platform == "win32") & (hasattr(os, "add_dll_directory")):
gcc_path = shutil.which("gcc")
if gcc_path is not None:
# Since add_dll_directory is only defined on windows, we need
# the ignore[attr-defined] on non-Windows platforms.
# For Windows we need ignore[unused-ignore] since the ignore
# is unnecessary with that platform.
os.add_dll_directory(os.path.dirname(gcc_path)) # type: ignore[attr-defined,unused-ignore]
cm = os.add_dll_directory(os.path.dirname(gcc_path)) # type: ignore[attr-defined,unused-ignore]
return cm


def dlimport(fullpath, suffix=None):
Expand Down Expand Up @@ -340,20 +341,20 @@ def dlimport(fullpath, suffix=None):
_logger.debug(f"module_name {module_name}")

sys.path[0:0] = [workdir] # insert workdir at beginning (temporarily)
add_gcc_dll_directory()
global import_time
try:
importlib.invalidate_caches()
t0 = time.perf_counter()
with warnings.catch_warnings():
warnings.filterwarnings("ignore", message="numpy.ndarray size changed")
rval = __import__(module_name, {}, {}, [module_name])
t1 = time.perf_counter()
import_time += t1 - t0
if not rval:
raise Exception("__import__ failed", fullpath)
finally:
del sys.path[0]
with add_gcc_dll_directory():
global import_time
try:
importlib.invalidate_caches()
t0 = time.perf_counter()
with warnings.catch_warnings():
warnings.filterwarnings("ignore", message="numpy.ndarray size changed")
rval = __import__(module_name, {}, {}, [module_name])
t1 = time.perf_counter()
import_time += t1 - t0
if not rval:
raise Exception("__import__ failed", fullpath)
finally:
del sys.path[0]

assert fullpath.startswith(rval.__file__)
return rval
Expand Down