Skip to content

Commit 28c48db

Browse files
Copilotsbillinge
andcommitted
Add Windows DLL loading fix for Python 3.8+
Co-authored-by: sbillinge <4254545+sbillinge@users.noreply.github.com>
1 parent f9c9318 commit 28c48db

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

src/diffpy/srreal/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@
1414
##############################################################################
1515
"""Tools for real space structure analysis."""
1616

17+
# Windows DLL loading fix for Python 3.8+
18+
# On Windows, add the conda library bin directory to DLL search path
19+
# before importing extension modules that depend on DLLs
20+
import os
21+
import sys
22+
23+
if sys.platform == "win32" and sys.version_info >= (3, 8):
24+
# Try to add conda library bin directory to DLL search path
25+
conda_prefix = os.environ.get("CONDA_PREFIX")
26+
if conda_prefix:
27+
lib_bin_dir = os.path.join(conda_prefix, "Library", "bin")
28+
if os.path.isdir(lib_bin_dir):
29+
os.add_dll_directory(lib_bin_dir)
30+
1731
# package version
1832
from diffpy.srreal.version import __version__
1933

tests/test_dll_loading.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""Unit tests for Windows DLL loading initialization."""
2+
3+
import os
4+
import sys
5+
6+
7+
def test_import_succeeds():
8+
"""Ensure diffpy.srreal can be imported successfully on all platforms."""
9+
import diffpy.srreal
10+
11+
assert hasattr(diffpy.srreal, "__version__")
12+
13+
14+
def test_windows_dll_directory_handling():
15+
"""Verify Windows DLL directory handling doesn't raise errors."""
16+
# This test verifies that the Windows-specific DLL directory initialization
17+
# in __init__.py doesn't cause issues on any platform.
18+
19+
# The actual DLL directory logic is executed during module import,
20+
# so if we got this far, it succeeded.
21+
22+
# On Windows with Python 3.8+, verify the logic would have run
23+
if sys.platform == "win32" and sys.version_info >= (3, 8):
24+
# Check that CONDA_PREFIX environment variable handling works
25+
conda_prefix = os.environ.get("CONDA_PREFIX")
26+
if conda_prefix:
27+
lib_bin_dir = os.path.join(conda_prefix, "Library", "bin")
28+
# The directory should exist in a proper conda environment
29+
# but we don't assert this as it may not exist in all test environments
30+
assert isinstance(lib_bin_dir, str)
31+
32+
# Test passes if no exceptions are raised
33+
assert True

0 commit comments

Comments
 (0)