Skip to content

Commit 2ecf477

Browse files
authored
fix: inline Fortran constant defaults in _fc() to prevent Homebrew sync failures (#1419)
* fix: inline Fortran constant defaults in _fc() to prevent Homebrew sync failures Previously, _fc() raised if a constant was absent from _FALLBACK_CONSTANTS, a separate dict that could silently drift from definitions.py. This caused the v5.3.1 Homebrew build to fail: NIB = _fc('num_ib_patches_max') was added to definitions.py but _FALLBACK_CONSTANTS was never updated, so every Homebrew install hit a RuntimeError on startup. Fix: _fc(name, default) now requires an inline default — the default lives alongside the call, so they can't get out of sync. Remove _FALLBACK_CONSTANTS and simplify get_fortran_constants() to return {} when src/ is unavailable. * fix: restore drift detection in _fc() — raise when src/ is present but key is missing
1 parent 462cac7 commit 2ecf477

2 files changed

Lines changed: 20 additions & 25 deletions

File tree

toolchain/mfc/params/definitions.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,28 @@
1313
from .schema import ParamDef, ParamType
1414

1515
# Index limits — sourced from Fortran compile-time constants (m_constants.fpp).
16-
# These must stay in sync with Fortran; we error if the source can't be parsed.
16+
# Falls back to the inline default when src/ is unavailable (e.g. Homebrew).
17+
# Default must match src/common/m_constants.fpp — enforced by co-location.
1718
_FC = get_fortran_constants()
1819

1920

20-
def _fc(name: str) -> int:
21-
"""Get a required Fortran constant, raising if unavailable."""
22-
if name not in _FC:
23-
raise RuntimeError(f"Fortran constant '{name}' not found in m_constants.fpp. Toolchain is out of sync with Fortran source.")
24-
return _FC[name]
21+
def _fc(name: str, default: int) -> int:
22+
"""Get a Fortran constant, using the inline default when m_constants.fpp is unavailable."""
23+
if _FC:
24+
if name not in _FC:
25+
raise RuntimeError(
26+
f"Fortran constant '{name}' not found in m_constants.fpp. "
27+
f"Toolchain is out of sync with Fortran source."
28+
)
29+
return _FC[name]
30+
return default
2531

2632

27-
NF = _fc("num_fluids_max") # fluid_pp
28-
NPR = _fc("num_probes_max") # probe, acoustic, integral
29-
NB = _fc("num_bc_patches_max") # patch_bc
30-
NUM_PATCHES_MAX = _fc("num_patches_max") # patch_icpp (Fortran array bound)
31-
NIB = _fc("num_ib_patches_max") # patch_ib (Fortran array bound)
33+
NF = _fc("num_fluids_max", 10) # fluid_pp
34+
NPR = _fc("num_probes_max", 10) # probe, acoustic, integral
35+
NB = _fc("num_bc_patches_max", 10) # patch_bc
36+
NUM_PATCHES_MAX = _fc("num_patches_max", 10) # patch_icpp (Fortran array bound)
37+
NIB = _fc("num_ib_patches_max", 50000) # patch_ib (Fortran array bound)
3238
# Enumeration limits for families not yet converted to IndexedFamily.
3339
# These are smaller than the Fortran array bounds to keep the registry compact.
3440
# The CONSTRAINTS dict below uses the Fortran constants for validation.

toolchain/mfc/params/namelist_parser.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -492,29 +492,18 @@ def get_fortran_constants() -> Dict[str, int]:
492492
"""
493493
Get Fortran compile-time constants from m_constants.fpp.
494494
495-
Cached after first call. Falls back to built-in defaults when the Fortran
496-
source is unavailable (e.g. Homebrew installs where src/ is not shipped).
495+
Cached after first call. Returns an empty dict when the Fortran source is
496+
unavailable (e.g. Homebrew installs where src/ is not shipped); callers
497+
supply their own inline defaults via _fc(name, default) in definitions.py.
497498
"""
498499
global _FORTRAN_CONSTANTS_CACHE # noqa: PLW0603
499500
if _FORTRAN_CONSTANTS_CACHE is None:
500501
root = get_mfc_root()
501502
path = root / "src" / "common" / "m_constants.fpp"
502503
_FORTRAN_CONSTANTS_CACHE = parse_fortran_constants(path)
503-
if not _FORTRAN_CONSTANTS_CACHE:
504-
_FORTRAN_CONSTANTS_CACHE = _FALLBACK_CONSTANTS.copy()
505504
return _FORTRAN_CONSTANTS_CACHE
506505

507506

508-
# Fallback values for when m_constants.fpp is not available at runtime.
509-
# Keep in sync with src/common/m_constants.fpp.
510-
_FALLBACK_CONSTANTS: Dict[str, int] = {
511-
"num_fluids_max": 10,
512-
"num_probes_max": 10,
513-
"num_patches_max": 1000,
514-
"num_bc_patches_max": 10,
515-
}
516-
517-
518507
def get_mfc_root() -> Path:
519508
"""Get the MFC root directory from this file's location."""
520509
# This file is at toolchain/mfc/params/namelist_parser.py

0 commit comments

Comments
 (0)