Skip to content

Commit

Permalink
Fix critical bug with py3 str handling
Browse files Browse the repository at this point in the history
There were many places hardcoded to check for the 'str' type.
With the python3 conversion, some of our strings have become
type unicode instead. This caused CIME to mistakenly think
these values were not strings in some cases.
  • Loading branch information
jgfouca committed Oct 27, 2017
1 parent 09b7b8e commit 425cdd2
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 12 deletions.
3 changes: 2 additions & 1 deletion scripts/lib/CIME/XML/entry_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from CIME.XML.generic_xml import GenericXML

from copy import deepcopy
import six

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -229,7 +230,7 @@ def _set_value(self, node, value, vid=None, subgroup=None, ignore_type=False):
def get_valid_value_string(self, node, value,vid=None, ignore_type=False):
valid_values = self._get_valid_values(node)
if ignore_type:
expect(type(value) is str, "Value must be type string if ignore_type is true")
expect(isinstance(value, six.string_types), "Value must be type string if ignore_type is true")
str_value = value
return str_value
type_str = self._get_type_info(node)
Expand Down
2 changes: 1 addition & 1 deletion scripts/lib/CIME/XML/generic_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def get_resolved_value(self, raw_value):
if item_data is None:
return None

if type(item_data) is not str:
if not isinstance(item_data, six.string_types):
return item_data

for m in env_ref_re.finditer(item_data):
Expand Down
12 changes: 6 additions & 6 deletions scripts/lib/CIME/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
through the Case module.
"""
from copy import deepcopy
import glob, os, shutil, math
import glob, os, shutil, math, six
from CIME.XML.standard_module_setup import *
#pylint: disable=import-error,redefined-builtin
from six.moves import input
Expand Down Expand Up @@ -250,7 +250,7 @@ def get_values(self, item, attribute=None, resolved=True, subgroup=None):
vtype = env_file.get_type_info(item)
if resolved:
for result in results:
if type(result) is str:
if isinstance(result, six.string_types):
result = self.get_resolved_value(result)
new_results.append(convert_to_type(result, vtype, item))
else:
Expand All @@ -264,7 +264,7 @@ def get_values(self, item, attribute=None, resolved=True, subgroup=None):
if len(results) > 0:
if resolved:
for result in results:
if type(result) is str:
if isinstance(result, six.string_types):
new_results.append(self.get_resolved_value(result))
else:
new_results.append(result)
Expand All @@ -281,7 +281,7 @@ def get_value(self, item, attribute=None, resolved=True, subgroup=None):
result = env_file.get_value(item, attribute, resolved=False, subgroup=subgroup)

if result is not None:
if resolved and type(result) is str:
if resolved and isinstance(result, six.string_types):
result = self.get_resolved_value(result)
vtype = env_file.get_type_info(item)
if vtype is not None:
Expand All @@ -293,7 +293,7 @@ def get_value(self, item, attribute=None, resolved=True, subgroup=None):
result = env_file.get_value(item, attribute, resolved=False, subgroup=subgroup)

if result is not None:
if resolved and type(result) is str:
if resolved and isinstance(result, six.string_types):
return self.get_resolved_value(result)
return result

Expand Down Expand Up @@ -576,7 +576,7 @@ def get_compset_components(self):
def __iter__(self):
for entryid_file in self._env_entryid_files:
for key, val in entryid_file:
if type(val) is str and '$' in val:
if isinstance(val, six.string_types) and '$' in val:
yield key, self.get_resolved_value(val)
else:
yield key, val
Expand Down
4 changes: 2 additions & 2 deletions scripts/lib/CIME/case_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from CIME.utils import get_cime_root, run_and_log_case_status, get_model
from CIME.test_status import *

import shutil
import shutil, six

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -80,7 +80,7 @@ def _case_setup_impl(case, caseroot, clean=False, test_mode=False, reset=False):

# Check that userdefine settings are specified before expanding variable
for vid, value in case:
expect(not (type(value) is str and "USERDEFINED_required_build" in value),
expect(not (isinstance(value, six.string_types) and "USERDEFINED_required_build" in value),
"Parameter '{}' must be defined".format(vid))

# Remove batch scripts
Expand Down
4 changes: 2 additions & 2 deletions scripts/lib/CIME/compare_namelists.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import os, re, logging
import os, re, logging, six

from collections import OrderedDict
from CIME.utils import expect
Expand Down Expand Up @@ -365,7 +365,7 @@ def _compare_values(name, gold_value, comp_value, case):
comments += " dict variable '{}' has extra key {} with value {}\n".format(name, key, comp_value[key])

else:
expect(type(gold_value) is str, "Unexpected type found: '{}'".format(type(gold_value)))
expect(isinstance(gold_value, six.string_types), "Unexpected type found: '{}'".format(type(gold_value)))
norm_gold_value = _normalize_string_value(name, gold_value, case)
norm_comp_value = _normalize_string_value(name, comp_value, case)

Expand Down

0 comments on commit 425cdd2

Please sign in to comment.