Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions f90nml/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,22 @@ def _readstream(self, nml_file, nml_patch_in=None):
# Finalise namelist group
if self.token in ('/', '&', '$'):

if self.token == '&':
Copy link
Copy Markdown
Owner

@marshallward marshallward Mar 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this test for both '&' and '$' ?

For what it's worth, NVIDIA appears to happily match &grp with $end and every combination therein.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Oh I didnt even consider mixed opening and closing tokens. That would become really tricky with multiple groups with different conventions in one file. I would hope such files just dont exists (but you never know with fortran ;)

# Peek at the next non-whitespace token without
# consuming it, to distinguish F77-style terminators
# (&end or standalone &) from an unclosed group.
self.tokens, peek_iter = itertools.tee(self.tokens)
skip = self.comment_tokens + whitespace
next_tok = next(
(t for t in peek_iter if t[0] not in skip), None
)
if next_tok not in ('end', '&', '$', None):
raise ValueError(
"f90nml: error: Namelist group '&{}' was not "
"closed before the start of a new group."
.format(g_name)
)

# Append any remaining patched variables
for v_name, v_val in grp_patch.items():
g_vars[v_name] = v_val
Expand Down
8 changes: 8 additions & 0 deletions tests/f77.nml
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
$f77_nml_dollar
x = 123
$end

&f77_nml
x = 123
&end

&next_f77_nml
y = 'abc'
&end

&another_f77_nml
z = 99
&
8 changes: 8 additions & 0 deletions tests/f77_target.nml
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
&f77_nml_dollar
x = 123
/

&f77_nml
x = 123
/

&next_f77_nml
y = 'abc'
/

&another_f77_nml
z = 99
/
6 changes: 6 additions & 0 deletions tests/first_grp_no_end.nml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
&open_group
a = 1

&closed_group
b = 2
/
5 changes: 5 additions & 0 deletions tests/test_f90nml.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,8 +427,10 @@ def setUp(self):
}

self.f77_nml = {
'f77_nml_dollar': {'x': 123},
'f77_nml': {'x': 123},
'next_f77_nml': {'y': 'abc'},
'another_f77_nml': {'z': 99},
}

self.dollar_nml = {'dollar_nml': {'v': 1.}}
Expand Down Expand Up @@ -1612,6 +1614,9 @@ def test_string_grp_no_end(self):
def test_file_grp_no_end(self):
self.assertRaises(ValueError, f90nml.read, 'grp_no_end.nml')

def test_file_first_grp_no_end(self):
self.assertRaises(ValueError, f90nml.read, 'first_grp_no_end.nml')


if __name__ == '__main__':
if os.path.isfile('tmp.nml'):
Expand Down
Loading