Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
21 changes: 21 additions & 0 deletions f90nml/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,9 @@ def _readstream(self, nml_file, nml_patch_in=None):
except StopIteration:
break

# save whether & or $ is opening this group
opening_token = self.token

# Create the next namelist
try:
self._update_tokens()
Expand Down Expand Up @@ -384,6 +387,24 @@ def _readstream(self, nml_file, nml_patch_in=None):
# Finalise namelist group
if self.token in ('/', '&', '$'):

if self.token in ('&', '$'):
# Peek at the next non-whitespace token without
# consuming it, to distinguish F77-style terminators
# (&end or standalone &, or same with $) 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 and next_tok.lower() not in ('end', '&', '$'):
raise ValueError(
"f90nml: error: Namelist group '{}{}' was not "
"closed before the start of a new group or EOF."
.format(opening_token, g_name)
)

# Append any remaining patched variables
for v_name, v_val in grp_patch.items():
g_vars[v_name] = v_val
Expand Down
7 changes: 7 additions & 0 deletions tests/end_uppercase.nml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
&END_TOKEN_UPPERCASE
a = 1
&END

&last_grp
a = 1
&
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
/
7 changes: 7 additions & 0 deletions tests/first_grp_no_end_dollar.nml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
$open_group
a = 1


$closed_group
b = 2
$end
13 changes: 13 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 @@ -1595,6 +1597,11 @@ def test_repeat_repeating_logical(self):
line2 = out.readline()
self.assertEqual(line2.lstrip(), "b = 2*.true., .false.\n")

def test_end_uppercase(self):
test_nml = f90nml.read('end_uppercase.nml')
self.assertEqual(test_nml, {'end_token_uppercase': {'a': 1},
'last_grp': {'a': 1}})

# Failed namelist parsing
# NOTE: This is a very weak test, since '& x=1' / will pass
def test_grp_token_end(self):
Expand All @@ -1612,6 +1619,12 @@ 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')

def test_file_first_grp_no_end_dollar(self):
self.assertRaises(ValueError, f90nml.read, 'first_grp_no_end_dollar.nml')


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