From 47785bf89b56bd111080d8a26d9f8bca9bb4d25c Mon Sep 17 00:00:00 2001 From: Sander Sweers Date: Sun, 25 Feb 2018 12:18:04 +0100 Subject: Add file encoding to configure_file Input files can be in any file encoding, not just utf-8 or isolatin1. Meson should not make assumptions here and allow for the user to specify the encoding to use. --- mesonbuild/mesonlib.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'mesonbuild/mesonlib.py') diff --git a/mesonbuild/mesonlib.py b/mesonbuild/mesonlib.py index 9f1da3e..e4951f9 100644 --- a/mesonbuild/mesonlib.py +++ b/mesonbuild/mesonlib.py @@ -623,9 +623,9 @@ def do_mesondefine(line, confdata): raise MesonException('#mesondefine argument "%s" is of unknown type.' % varname) -def do_conf_file(src, dst, confdata, format): +def do_conf_file(src, dst, confdata, format, encoding='utf-8'): try: - with open(src, encoding='utf-8') as f: + with open(src, encoding=encoding) as f: data = f.readlines() except Exception as e: raise MesonException('Could not read input file %s: %s' % (src, str(e))) @@ -652,8 +652,11 @@ def do_conf_file(src, dst, confdata, format): missing_variables.update(missing) result.append(line) dst_tmp = dst + '~' - with open(dst_tmp, 'w', encoding='utf-8') as f: - f.writelines(result) + try: + with open(dst_tmp, 'w', encoding=encoding) as f: + f.writelines(result) + except Exception as e: + raise MesonException('Could not write output file %s: %s' % (dst, str(e))) shutil.copymode(src, dst_tmp) replace_if_different(dst, dst_tmp) return missing_variables -- cgit v1.1