Skip to content

Commit b76e47b

Browse files
committed
Fix string replacements in lreprstruct test
The previous logic caused problems if "GRAIN" appeared in two (or more) strings, where one was a substring of the other. For example, in LREPRSTRUCT_Ly1_P128x1.f10_f10_mg37.I1850Clm50BgcCrop.derecho_gnu.clm-ciso--clm-cropMonthOutput, before this replacement, one line contained 'GRAINN_TO_FOOD' and a later line contained (among other things) "'GRAINN_TO_FOOD_PERHARV', 'GRAINN_TO_FOOD_ANN'". This was problematic because the first replacement of GRAINN_TO_FOOD incorrectly led to replacements in the later strings as well. This new logic should solve this issue. Resolves #3313
1 parent 4c379f2 commit b76e47b

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

cime_config/SystemTests/lreprstruct.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
1717
"""
1818

19+
import re
20+
1921
from CIME.SystemTests.system_tests_compare_two import SystemTestsCompareTwo
2022
from CIME.XML.standard_module_setup import *
2123
from CIME.SystemTests.test_utils.user_nl_utils import append_to_user_nl_files
@@ -53,13 +55,11 @@ def _case_one_setup(self):
5355
user_nl_clm_path = os.path.join(self._get_caseroot(), "user_nl_clm")
5456
with open(user_nl_clm_path) as f:
5557
user_nl_clm_text = f.read()
56-
for grain_output in re.findall("GRAIN\w*", user_nl_clm_text):
57-
user_nl_clm_text = user_nl_clm_text.replace(
58-
grain_output,
59-
grain_output.replace("GRAIN", "REPRODUCTIVE1")
60-
+ "', '"
61-
+ grain_output.replace("GRAIN", "REPRODUCTIVE2"),
62-
)
58+
def replace_grain(match):
59+
grain_output = match.group()
60+
return (grain_output.replace("GRAIN", "REPRODUCTIVE1") +
61+
"', '" + grain_output.replace("GRAIN", "REPRODUCTIVE2"))
62+
user_nl_clm_text = re.sub(r"GRAIN\w*", replace_grain, user_nl_clm_text)
6363
with open(user_nl_clm_path, "w") as f:
6464
f.write(user_nl_clm_text)
6565

0 commit comments

Comments
 (0)