Skip to content
Draft
Show file tree
Hide file tree
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
14 changes: 14 additions & 0 deletions src/diffpy/srreal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@
##############################################################################
"""Tools for real space structure analysis."""

# Windows DLL loading fix for Python 3.8+
# On Windows, add the conda library bin directory to DLL search path
# before importing extension modules that depend on DLLs
import os
import sys

if sys.platform == "win32" and sys.version_info >= (3, 8):
# Try to add conda library bin directory to DLL search path
conda_prefix = os.environ.get("CONDA_PREFIX")
if conda_prefix:
lib_bin_dir = os.path.join(conda_prefix, "Library", "bin")
if os.path.isdir(lib_bin_dir):
os.add_dll_directory(lib_bin_dir)

# package version
from diffpy.srreal.version import __version__

Expand Down
31 changes: 31 additions & 0 deletions tests/test_dll_loading.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""Unit tests for Windows DLL loading initialization."""

import os
import sys


def test_import_succeeds():
"""Ensure diffpy.srreal can be imported successfully on all platforms."""
import diffpy.srreal

assert hasattr(diffpy.srreal, "__version__")


def test_windows_dll_directory_handling():
"""Verify Windows DLL directory handling doesn't raise errors."""
# This test verifies that the Windows-specific DLL directory
# initialization in __init__.py doesn't cause issues on any platform.

# The actual DLL directory logic is executed during module import,
# so if we got this far, it succeeded.

# On Windows with Python 3.8+, verify the logic would have run
if sys.platform == "win32" and sys.version_info >= (3, 8):
# Check that CONDA_PREFIX environment variable handling works
conda_prefix = os.environ.get("CONDA_PREFIX")
if conda_prefix:
lib_bin_dir = os.path.join(conda_prefix, "Library", "bin")
# The directory should exist in a proper conda environment
# but we don't assert this as it may not exist in all test
# environments
assert isinstance(lib_bin_dir, str)