Unverified Commit 76cfd77b authored by Yining Li's avatar Yining Li Committed by GitHub
Browse files

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

* explicitly set encoding as 'utf-8'
parent ba334b43
...@@ -121,7 +121,7 @@ class Config: ...@@ -121,7 +121,7 @@ class Config:
regexp = r'\{\{\s*' + str(key) + r'\s*\}\}' regexp = r'\{\{\s*' + str(key) + r'\s*\}\}'
value = value.replace('\\', '/') value = value.replace('\\', '/')
config_file = re.sub(regexp, value, config_file) 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) tmp_config_file.write(config_file)
@staticmethod @staticmethod
...@@ -139,7 +139,7 @@ class Config: ...@@ -139,7 +139,7 @@ class Config:
base_var_dict[randstr] = base_var base_var_dict[randstr] = base_var
regexp = r'\{\{\s*' + BASE_KEY + r'\.' + base_var + r'\s*\}\}' regexp = r'\{\{\s*' + BASE_KEY + r'\.' + base_var + r'\s*\}\}'
config_file = re.sub(regexp, f'"{randstr}"', config_file) 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) tmp_config_file.write(config_file)
return base_var_dict return base_var_dict
...@@ -353,7 +353,8 @@ class Config: ...@@ -353,7 +353,8 @@ class Config:
warnings.warn( warnings.warn(
'Please check "file_format", the file format may be .py') 'Please check "file_format", the file format may be .py')
with tempfile.NamedTemporaryFile( 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) temp_file.write(cfg_str)
# on windows, previous implementation cause error # on windows, previous implementation cause error
# see PR 1077 for details # see PR 1077 for details
...@@ -536,7 +537,7 @@ class Config: ...@@ -536,7 +537,7 @@ class Config:
if file is None: if file is None:
return self.pretty_text return self.pretty_text
else: else:
with open(file, 'w') as f: with open(file, 'w', encoding='utf-8') as f:
f.write(self.pretty_text) f.write(self.pretty_text)
else: else:
import mmcv import mmcv
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment