Skip to content

Commit

Permalink
add fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
ahnaf-tahmid-chowdhury committed Dec 13, 2024
1 parent aeef2d6 commit b0995b8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 30 deletions.
10 changes: 10 additions & 0 deletions openmc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ def get_core_path(subdir, pattern="*", recursive=False):
list: A list of matched paths.
"""
path = os.path.join(__path__[0], "core", subdir)
if not os.path.exists(path):
import sysconfig
path = os.path.join(sysconfig.get_path("platlib"), "openmc", "core", subdir)
warnings.warn(
"It seems OpenMC is being run from its source directory. "
"This setup is not recommended as it may lead to unexpected behavior, "
"such as conflicts between source and installed versions. "
"Please run your script from outside the OpenMC source tree.",
RuntimeWarning
)
search_pattern = os.path.join(path, "**", pattern) if recursive else os.path.join(path, pattern)
return glob.glob(search_pattern, recursive=recursive) if os.path.exists(path) else []

Expand Down
42 changes: 12 additions & 30 deletions openmc/lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,19 @@

from ctypes import CDLL, c_bool, c_int
import os
import warnings

def load_openmc_library():
try:
# Attempt to load the library from OpenMC
import openmc
_filename = openmc.lib[0]
if os.path.isfile(_filename):
return CDLL(str(_filename))
raise FileNotFoundError
except (IndexError, FileNotFoundError):
# Attempt to load the library from the installed module
import importlib
openmc = importlib.import_module("openmc")
_filename = openmc.lib[0]
if os.path.isfile(_filename):
warnings.warn(
"It seems OpenMC is being run from its source directory. "
"This setup is not recommended as it may lead to unexpected behavior, "
"such as conflicts between source and installed versions. "
"Please run your script from outside the OpenMC source tree.",
RuntimeWarning
)
return CDLL(str(_filename))
raise RuntimeError(
"Unable to load the OpenMC library. Please ensure OpenMC "
"is installed correctly and accessible from your Python environment."
)

# Load the OpenMC shared library
_dll = load_openmc_library()
if os.environ.get('READTHEDOCS', None) != 'True':
# Open shared library
import openmc
_filename = openmc.lib[0]
_dll = CDLL(str(_filename)) # TODO: Remove str() when Python 3.12+
else:
# For documentation builds, we don't actually have the shared library
# available. Instead, we create a mock object so that when the modules
# within the openmc.lib package try to configure arguments and return
# values for symbols, no errors occur
from unittest.mock import Mock
_dll = Mock()


def _dagmc_enabled():
Expand Down

0 comments on commit b0995b8

Please sign in to comment.