Skip to content

Commit 4c0448d

Browse files
Fix input_path resolving empty filenames to cwd (#4154)
Co-authored-by: Paul Romano <paul.k.romano@gmail.com>
1 parent 210df97 commit 4c0448d

5 files changed

Lines changed: 33 additions & 4 deletions

File tree

‎openmc/utility_funcs.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ def input_path(filename: PathLike) -> Path:
6969
Path object
7070
7171
"""
72+
# An empty filename represents the absence of a path (e.g., a mesh or
73+
# DAGMC universe read back from a summary/statepoint file that was never
74+
# written with a source file) and should not be resolved to the current
75+
# working directory.
76+
filename = os.fspath(filename)
77+
if not filename:
78+
return Path(filename)
79+
7280
if openmc.config['resolve_paths']:
7381
path = Path(filename)
7482
xml_dir = _XML_INPUT_PATH.get()

‎tests/unit_tests/test_mesh.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,8 @@ def test_umesh_from_hdf5_without_filename(run_in_tmpdir):
470470
group['connectivity'] = np.zeros((1, 8), dtype=int)
471471
group['element_types'] = [0]
472472

473-
mesh = openmc.MeshBase.from_hdf5(group)
473+
with openmc.config.patch('resolve_paths', True):
474+
mesh = openmc.MeshBase.from_hdf5(group)
474475

475476
assert mesh.filename == Path()
476477
assert mesh.has_statepoint_data

‎tests/unit_tests/test_universe.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ def test_dagmc_universe_from_hdf5_without_filename(run_in_tmpdir):
198198
group.attrs['auto_mat_ids'] = 1
199199
group.attrs['length_multiplier'] = 1.0
200200

201-
univ = openmc.DAGMCUniverse.from_hdf5(group)
201+
with openmc.config.patch('resolve_paths', True):
202+
univ = openmc.DAGMCUniverse.from_hdf5(group)
202203

203204
assert univ.id == 1
204205
assert univ.filename == Path()
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from pathlib import Path
2+
3+
import openmc
4+
from openmc.utility_funcs import input_path
5+
6+
7+
def test_input_path_empty_not_resolved():
8+
"""An empty filename represents "no file" (e.g., an in-memory mesh or
9+
DAGMC universe read back from a summary/statepoint that was never
10+
written with a source file, see UnstructuredMesh.from_hdf5 and
11+
DAGMCUniverse.from_hdf5) and should stay empty rather than being
12+
resolved to the current working directory."""
13+
with openmc.config.patch('resolve_paths', True):
14+
assert input_path('') == Path()
15+
16+
17+
def test_input_path_nonempty_resolved(tmp_path, monkeypatch):
18+
monkeypatch.chdir(tmp_path)
19+
with openmc.config.patch('resolve_paths', True):
20+
assert input_path('foo.h5') == (tmp_path / 'foo.h5').resolve()

‎tests/unit_tests/weightwindows/test.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import os
22
from itertools import product
3+
from pathlib import Path
34

45
import pytest
5-
66
import numpy as np
77
from uncertainties import ufloat
8-
98
import openmc
109
import openmc.lib
1110
from openmc.stats import Discrete, Point

0 commit comments

Comments
 (0)