From c1c5c0b93ecc93815afb3554a2812fb22b50d9c4 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Wed, 7 May 2025 18:51:40 -0600 Subject: [PATCH] Apply resolve paths to path values in `config` (#3400) --- docs/source/usersguide/data.rst | 3 ++- openmc/config.py | 2 +- tests/unit_tests/test_config.py | 7 +++++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/docs/source/usersguide/data.rst b/docs/source/usersguide/data.rst index 2a9cd36dbb..8b2938556b 100644 --- a/docs/source/usersguide/data.rst +++ b/docs/source/usersguide/data.rst @@ -30,7 +30,8 @@ responsible for specifying one or more of the following: Each of the above files can specified in several ways. In the Python API, a :ref:`runtime configuration variable ` :data:`openmc.config` can be used to specify any of the above and is initialized -using a set of environment variables. +using a set of environment variables. Data configuration paths set in +:data:`openmc.config` will be expanded to absolute paths. .. _usersguide_data_runtime: diff --git a/openmc/config.py b/openmc/config.py index ab53ab61b5..56d8a4072d 100644 --- a/openmc/config.py +++ b/openmc/config.py @@ -60,7 +60,7 @@ class _Config(MutableMapping): return repr(self._mapping) def _set_path(self, key, value): - self._mapping[key] = p = Path(value) + self._mapping[key] = p = Path(value).resolve() if not p.exists(): warnings.warn(f"'{value}' does not exist.") diff --git a/tests/unit_tests/test_config.py b/tests/unit_tests/test_config.py index 9d3f53a740..4e87de4b7e 100644 --- a/tests/unit_tests/test_config.py +++ b/tests/unit_tests/test_config.py @@ -1,5 +1,6 @@ from collections.abc import Mapping import os +from pathlib import Path import openmc import pytest @@ -34,6 +35,12 @@ def test_config_basics(): with pytest.raises(KeyError): openmc.config['🐖'] = '/like/to/eat/bacon' + # ensure relative paths are expanded into absolute + # paths + chain_path = Path('./chain.xml') + openmc.config['chain_file'] = chain_path + assert openmc.config['chain_file'] == chain_path.resolve() + def test_config_patch(): openmc.config['cross_sections'] = '/path/to/cross_sections.xml'