Skip to content

Fix input_path resolving empty filenames to cwd - #4154

Merged
paulromano merged 7 commits into
openmc-dev:developfrom
Arthur031221:fix/input-path-empty-filename
Oct 3, 2026
Merged

paulromano merged 7 commits into
openmc-dev:developfrom
Arthur031221:fix/input-path-empty-filename

Conversation

@Arthur031221

Copy link
Copy Markdown
Contributor

Description

Anyone who reads back an in-memory UnstructuredMesh or DAGMCUniverse
(one built from a MOAB/libmesh instance in memory and never backed by a
source file) from a summary or statepoint HDF5 file gets a filename
attribute that silently points at the current working directory instead
of indicating that there is no file.

UnstructuredMesh.from_hdf5 and DAGMCUniverse.from_hdf5 pass an empty
string for filename when the HDF5 group has no filename dataset
(added in #4118 to handle meshes/universes with no source file). That
empty string reaches openmc.utility_funcs.input_path, which calls
Path(filename).resolve() whenever openmc.config['resolve_paths'] is
True (the default). Path('').resolve() resolves to the process's
current working directory, not to an empty path, so mesh.filename or
dagmc_universe.filename ends up set to whatever directory happened to
be current when the file was read. Code that checks mesh.filename (for
example mesh.filename.exists() to decide whether a source file is
available) gets a wrong answer, since the working directory always
exists.

This also means the two round-trip assertions added in #4118
(tests/unit_tests/test_mesh.py::test_umesh_from_hdf5_without_filename
and
tests/unit_tests/test_universe.py::test_dagmc_universe_from_hdf5_without_filename,
both asserting filename == Path()) do not actually hold under the
default configuration.

input_path now returns an empty, unresolved Path when given an empty
filename, before the resolve_paths branch runs. No other caller of
input_path (cross section paths, source library/path, settings files)
passes an empty string in normal use, so this only changes behavior for
the "no file" case.

Fixes # (none filed; found by code review)

Checklist

  • I have performed a self-review of my own code
  • I have run clang-format (version 18) on any C++ source files (if applicable)
  • I have followed the style guidelines for Python source files (if applicable)
  • I have made corresponding changes to the documentation (if applicable)
  • I have added tests that prove my fix is effective or that my feature works (if applicable)

Arthur031221 and others added 7 commits October 1, 2026 09:09
UnstructuredMesh.from_hdf5 and DAGMCUniverse.from_hdf5 pass an empty
string for filename when the HDF5 group has no stored filename, which
happens for meshes and DAGMC universes built in memory and never
backed by a file. input_path then resolved that empty string to the
current working directory instead of leaving it empty, so filename
silently became an arbitrary real path instead of indicating no file.

input_path now returns an empty Path unresolved when given an empty
filename, matching the from_hdf5 round-trip tests added in openmc-dev#4118.
Use the filesystem protocol rather than string formatting to inspect
input filenames. An os.PathLike object can provide an empty filesystem
path while its string representation is nonempty, causing the new guard
to resolve a missing filename to the working or XML input directory.

Normalize with os.fspath and cover empty and nonempty inputs with path
resolution enabled and disabled, including an XML input context.
The session fixture disables path resolution, so the existing mesh and
DAGMC reader tests do not catch empty filenames resolving to the working
directory under the default configuration. Parameterize both tests over
the enabled and disabled settings and patch configuration while reading
the objects. The enabled cases fail against the pre-PR helper.
Replace the input-type, resolution-mode, and XML-context cross product
with focused cases for empty filenames, ordinary paths, and XML-relative
resolution. Keep one custom PathLike case for the filesystem-protocol
regression. Exercise each HDF5 reader under the default enabled path
resolution setting without duplicating its disabled-setting case.
@paulromano
paulromano requested a review from pshriwise as a code owner October 3, 2026 22:47
@paulromano
paulromano enabled auto-merge (squash) October 3, 2026 22:48
@paulromano
paulromano merged commit 4c0448d into openmc-dev:develop Oct 3, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants