Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix config parsing error caused by non-ascii characters #1410

Merged
merged 1 commit into from
Oct 20, 2021
Merged
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
9 changes: 5 additions & 4 deletions mmcv/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def _substitute_predefined_vars(filename, temp_config_name):
regexp = r'\{\{\s*' + str(key) + r'\s*\}\}'
value = value.replace('\\', '/')
config_file = re.sub(regexp, value, config_file)
with open(temp_config_name, 'w') as tmp_config_file:
with open(temp_config_name, 'w', encoding='utf-8') as tmp_config_file:
tmp_config_file.write(config_file)

@staticmethod
Expand All @@ -139,7 +139,7 @@ def _pre_substitute_base_vars(filename, temp_config_name):
base_var_dict[randstr] = base_var
regexp = r'\{\{\s*' + BASE_KEY + r'\.' + base_var + r'\s*\}\}'
config_file = re.sub(regexp, f'"{randstr}"', config_file)
with open(temp_config_name, 'w') as tmp_config_file:
with open(temp_config_name, 'w', encoding='utf-8') as tmp_config_file:
tmp_config_file.write(config_file)
return base_var_dict

Expand Down Expand Up @@ -353,7 +353,8 @@ def fromstring(cfg_str, file_format):
warnings.warn(
'Please check "file_format", the file format may be .py')
with tempfile.NamedTemporaryFile(
'w', suffix=file_format, delete=False) as temp_file:
'w', encoding='utf-8', suffix=file_format,
delete=False) as temp_file:
temp_file.write(cfg_str)
# on windows, previous implementation cause error
# see PR 1077 for details
Expand Down Expand Up @@ -536,7 +537,7 @@ def dump(self, file=None):
if file is None:
return self.pretty_text
else:
with open(file, 'w') as f:
with open(file, 'w', encoding='utf-8') as f:
f.write(self.pretty_text)
else:
import mmcv
Expand Down