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

* explicitly set encoding as 'utf-8'
pull/1421/head
Yining Li 2021-10-20 14:32:38 +08:00 committed by GitHub
parent ba334b43dd
commit 76cfd77b3a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 5 additions and 4 deletions

View File

@ -121,7 +121,7 @@ class Config:
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
@ -139,7 +139,7 @@ class Config:
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
@ -353,7 +353,8 @@ class Config:
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
@ -536,7 +537,7 @@ class Config:
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