From 6ea195388b5d9a174330bf2ffac9e7fb0eda733b Mon Sep 17 00:00:00 2001 From: ly015 Date: Mon, 18 Oct 2021 18:58:39 +0800 Subject: [PATCH] Fix config parsing error caused by non-ascii characters * explicitly set encoding as 'utf-8' --- mmcv/utils/config.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/mmcv/utils/config.py b/mmcv/utils/config.py index ea6aaf4701..c71377c071 100644 --- a/mmcv/utils/config.py +++ b/mmcv/utils/config.py @@ -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 @@ -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 @@ -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 @@ -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