diff options
author | Daniel Mensinger <daniel@mensinger-ka.de> | 2021-06-22 22:59:16 +0200 |
---|---|---|
committer | Daniel Mensinger <daniel@mensinger-ka.de> | 2021-06-29 11:28:08 +0200 |
commit | 3e396b3782813d36d46195564cd0e111422bcaf5 (patch) | |
tree | f315e990f71984745fcb8f22dac2f0e400fecadb | |
parent | 28175bbee2c111cf41b80c97bbadd7dbabaa8990 (diff) | |
download | meson-3e396b3782813d36d46195564cd0e111422bcaf5.zip meson-3e396b3782813d36d46195564cd0e111422bcaf5.tar.gz meson-3e396b3782813d36d46195564cd0e111422bcaf5.tar.bz2 |
fix: Always explicitly set encoding for text files (fixes #8263)
79 files changed, 310 insertions, 305 deletions
diff --git a/ci/ciimage/build.py b/ci/ciimage/build.py index dcc11a9..2cf0736 100755 --- a/ci/ciimage/build.py +++ b/ci/ciimage/build.py @@ -18,7 +18,7 @@ install_script = 'install.sh' class ImageDef: def __init__(self, image_dir: Path) -> None: path = image_dir / image_def_file - data = json.loads(path.read_text()) + data = json.loads(path.read_text(encoding='utf-8')) assert isinstance(data, dict) assert all([x in data for x in ['base_image', 'env']]) @@ -74,7 +74,7 @@ class Builder(BuilderBase): # Also add /ci to PATH out_data += 'export PATH="/ci:$PATH"\n' - out_file.write_text(out_data) + out_file.write_text(out_data, encoding='utf-8') # make it executable mode = out_file.stat().st_mode @@ -91,7 +91,7 @@ class Builder(BuilderBase): RUN /ci/install.sh ''') - out_file.write_text(out_data) + out_file.write_text(out_data, encoding='utf-8') def do_build(self) -> None: # copy files @@ -131,7 +131,7 @@ class ImageTester(BuilderBase): ADD meson /meson ''') - out_file.write_text(out_data) + out_file.write_text(out_data, encoding='utf-8') def copy_meson(self) -> None: shutil.copytree( diff --git a/docs/genrelnotes.py b/docs/genrelnotes.py index 5dad924..082ab45 100755 --- a/docs/genrelnotes.py +++ b/docs/genrelnotes.py @@ -36,10 +36,10 @@ def add_to_sitemap(from_version, to_version): Adds release note entry to sitemap.txt. ''' sitemapfile = '../sitemap.txt' - s_f = open(sitemapfile) + s_f = open(sitemapfile, encoding='utf-8') lines = s_f.readlines() s_f.close() - with open(sitemapfile, 'w') as s_f: + with open(sitemapfile, 'w', encoding='utf-8') as s_f: for line in lines: if 'Release-notes' in line and from_version in line: new_line = line.replace(from_version, to_version) @@ -51,10 +51,10 @@ def generate(from_version, to_version): Generate notes for Meson build next release. ''' ofilename = f'Release-notes-for-{to_version}.md' - with open(ofilename, 'w') as ofile: + with open(ofilename, 'w', encoding='utf-8') as ofile: ofile.write(RELNOTE_TEMPLATE.format(to_version, to_version)) for snippetfile in glob('snippets/*.md'): - snippet = open(snippetfile).read() + snippet = open(snippetfile, encoding='utf-8').read() ofile.write(snippet) if not snippet.endswith('\n'): ofile.write('\n') diff --git a/mesonbuild/ast/interpreter.py b/mesonbuild/ast/interpreter.py index b9dfc7b..99c6979 100644 --- a/mesonbuild/ast/interpreter.py +++ b/mesonbuild/ast/interpreter.py @@ -179,7 +179,7 @@ class AstInterpreter(InterpreterBase): if not os.path.isfile(absname): sys.stderr.write(f'Unable to find build file {buildfilename} --> Skipping\n') return - with open(absname, encoding='utf8') as f: + with open(absname, encoding='utf-8') as f: code = f.read() assert(isinstance(code, str)) try: diff --git a/mesonbuild/backend/backends.py b/mesonbuild/backend/backends.py index c489dce..21a6c83 100644 --- a/mesonbuild/backend/backends.py +++ b/mesonbuild/backend/backends.py @@ -374,7 +374,7 @@ class Backend: if not os.path.exists(outfileabs_tmp_dir): os.makedirs(outfileabs_tmp_dir) result.append(unity_src) - return open(outfileabs_tmp, 'w') + return open(outfileabs_tmp, 'w', encoding='utf-8') # For each language, generate unity source files and return the list for comp, srcs in compsrcs.items(): @@ -765,7 +765,7 @@ class Backend: content = f'#include "{os.path.basename(pch_header)}"' pch_file_tmp = pch_file + '.tmp' - with open(pch_file_tmp, 'w') as f: + with open(pch_file_tmp, 'w', encoding='utf-8') as f: f.write(content) mesonlib.replace_if_different(pch_file, pch_file_tmp) return pch_rel_to_build @@ -1019,7 +1019,7 @@ class Backend: ifilename = os.path.join(self.environment.get_build_dir(), 'depmf.json') ofilename = os.path.join(self.environment.get_prefix(), self.build.dep_manifest_name) mfobj = {'type': 'dependency manifest', 'version': '1.0', 'projects': self.build.dep_manifest} - with open(ifilename, 'w') as f: + with open(ifilename, 'w', encoding='utf-8') as f: f.write(json.dumps(mfobj)) # Copy file from, to, and with mode unchanged d.data.append(InstallDataBase(ifilename, ofilename, None, '')) diff --git a/mesonbuild/backend/ninjabackend.py b/mesonbuild/backend/ninjabackend.py index 192cef3..597789b 100644 --- a/mesonbuild/backend/ninjabackend.py +++ b/mesonbuild/backend/ninjabackend.py @@ -20,6 +20,7 @@ import subprocess from collections import OrderedDict from enum import Enum, unique import itertools +from textwrap import dedent from pathlib import PurePath, Path from functools import lru_cache @@ -462,10 +463,11 @@ class NinjaBackend(backends.Backend): return open(tempfilename, 'a', encoding='utf-8') filename = os.path.join(self.environment.get_scratch_dir(), 'incdetect.c') - with open(filename, 'w') as f: - f.write('''#include<stdio.h> -int dummy; -''') + with open(filename, 'w', encoding='utf-8') as f: + f.write(dedent('''\ + #include<stdio.h> + int dummy; + ''')) # The output of cl dependency information is language # and locale dependent. Any attempt at converting it to @@ -1215,7 +1217,7 @@ int dummy; manifest_path = os.path.join(self.get_target_private_dir(target), 'META-INF', 'MANIFEST.MF') manifest_fullpath = os.path.join(self.environment.get_build_dir(), manifest_path) os.makedirs(os.path.dirname(manifest_fullpath), exist_ok=True) - with open(manifest_fullpath, 'w') as manifest: + with open(manifest_fullpath, 'w', encoding='utf-8') as manifest: if any(target.link_targets): manifest.write('Class-Path: ') cp_paths = [os.path.join(self.get_target_dir(l), l.get_filename()) for l in target.link_targets] diff --git a/mesonbuild/backend/vs2010backend.py b/mesonbuild/backend/vs2010backend.py index 5595fd0..7fcc324 100644 --- a/mesonbuild/backend/vs2010backend.py +++ b/mesonbuild/backend/vs2010backend.py @@ -206,7 +206,7 @@ class Vs2010Backend(backends.Backend): @staticmethod def touch_regen_timestamp(build_dir: str) -> None: - with open(Vs2010Backend.get_regen_stampfile(build_dir), 'w'): + with open(Vs2010Backend.get_regen_stampfile(build_dir), 'w', encoding='utf-8'): pass def get_vcvars_command(self): diff --git a/mesonbuild/cmake/fileapi.py b/mesonbuild/cmake/fileapi.py index 6773e9a..5d4d01a 100644 --- a/mesonbuild/cmake/fileapi.py +++ b/mesonbuild/cmake/fileapi.py @@ -51,7 +51,7 @@ class CMakeFileAPI: } query_file = self.request_dir / 'query.json' - query_file.write_text(json.dumps(query, indent=2)) + query_file.write_text(json.dumps(query, indent=2), encoding='utf-8') def load_reply(self) -> None: if not self.reply_dir.is_dir(): @@ -75,7 +75,7 @@ class CMakeFileAPI: # Debug output debug_json = self.build_dir / '..' / 'fileAPI.json' debug_json = debug_json.resolve() - debug_json.write_text(json.dumps(index, indent=2)) + debug_json.write_text(json.dumps(index, indent=2), encoding='utf-8') mlog.cmd_ci_include(debug_json.as_posix()) # parse the JSON @@ -313,7 +313,7 @@ class CMakeFileAPI: if not real_path.exists(): raise CMakeException(f'File "{real_path}" does not exist') - data = json.loads(real_path.read_text()) + data = json.loads(real_path.read_text(encoding='utf-8')) assert isinstance(data, dict) for i in data.keys(): assert isinstance(i, str) diff --git a/mesonbuild/cmake/toolchain.py b/mesonbuild/cmake/toolchain.py index f3e487d..1a6dcf0 100644 --- a/mesonbuild/cmake/toolchain.py +++ b/mesonbuild/cmake/toolchain.py @@ -66,8 +66,8 @@ class CMakeToolchain: def write(self) -> Path: if not self.toolchain_file.parent.exists(): self.toolchain_file.parent.mkdir(parents=True) - self.toolchain_file.write_text(self.generate()) - self.cmcache_file.write_text(self.generate_cache()) + self.toolchain_file.write_text(self.generate(), encoding='utf-8') + self.cmcache_file.write_text(self.generate_cache(), encoding='utf-8') mlog.cmd_ci_include(self.toolchain_file.as_posix()) return self.toolchain_file @@ -215,11 +215,11 @@ class CMakeToolchain: build_dir = Path(self.env.scratch_dir) / '__CMake_compiler_info__' build_dir.mkdir(parents=True, exist_ok=True) cmake_file = build_dir / 'CMakeLists.txt' - cmake_file.write_text(cmake_content) + cmake_file.write_text(cmake_content, encoding='utf-8') # Generate the temporary toolchain file temp_toolchain_file = build_dir / 'CMakeMesonTempToolchainFile.cmake' - temp_toolchain_file.write_text(CMakeToolchain._print_vars(self.variables)) + temp_toolchain_file.write_text(CMakeToolchain._print_vars(self.variables), encoding='utf-8') # Configure trace = CMakeTraceParser(self.cmakebin.version(), build_dir) diff --git a/mesonbuild/cmake/traceparser.py b/mesonbuild/cmake/traceparser.py index 23a1852..42ceec6 100644 --- a/mesonbuild/cmake/traceparser.py +++ b/mesonbuild/cmake/traceparser.py @@ -158,7 +158,7 @@ class CMakeTraceParser: if not self.requires_stderr(): if not self.trace_file_path.exists and not self.trace_file_path.is_file(): raise CMakeException('CMake: Trace file "{}" not found'.format(str(self.trace_file_path))) - trace = self.trace_file_path.read_text(errors='ignore') + trace = self.trace_file_path.read_text(errors='ignore', encoding='utf-8') if not trace: raise CMakeException('CMake: The CMake trace was not provided or is empty') diff --git a/mesonbuild/compilers/compilers.py b/mesonbuild/compilers/compilers.py index ff87819..52529f7 100644 --- a/mesonbuild/compilers/compilers.py +++ b/mesonbuild/compilers/compilers.py @@ -765,14 +765,14 @@ class Compiler(HoldableObject, metaclass=abc.ABCMeta): if isinstance(code, str): srcname = os.path.join(tmpdirname, 'testfile.' + self.default_suffix) - with open(srcname, 'w') as ofile: + with open(srcname, 'w', encoding='utf-8') as ofile: ofile.write(code) # ccache would result in a cache miss no_ccache = True contents = code elif isinstance(code, mesonlib.File): srcname = code.fname - with open(code.fname) as f: + with open(code.fname, encoding='utf-8') as f: contents = f.read() # Construct the compiler command-line diff --git a/mesonbuild/compilers/cs.py b/mesonbuild/compilers/cs.py index 218942c..7ebb66d 100644 --- a/mesonbuild/compilers/cs.py +++ b/mesonbuild/compilers/cs.py @@ -87,7 +87,7 @@ class CsCompiler(BasicLinkerIsCompilerMixin, Compiler): src = 'sanity.cs' obj = 'sanity.exe' source_name = os.path.join(work_dir, src) - with open(source_name, 'w') as ofile: + with open(source_name, 'w', encoding='utf-8') as ofile: ofile.write(textwrap.dedent(''' public class Sanity { static public void Main () { diff --git a/mesonbuild/compilers/cuda.py b/mesonbuild/compilers/cuda.py index 145b7c8..4c0d0a6 100644 --- a/mesonbuild/compilers/cuda.py +++ b/mesonbuild/compilers/cuda.py @@ -504,7 +504,7 @@ class CudaCompiler(Compiler): binname += '_cross' if self.is_cross else '' source_name = os.path.join(work_dir, sname) binary_name = os.path.join(work_dir, binname + '.exe') - with open(source_name, 'w') as ofile: + with open(source_name, 'w', encoding='utf-8') as ofile: ofile.write(code) # The Sanity Test for CUDA language will serve as both a sanity test diff --git a/mesonbuild/compilers/d.py b/mesonbuild/compilers/d.py index 78d0f62..b5ec905 100644 --- a/mesonbuild/compilers/d.py +++ b/mesonbuild/compilers/d.py @@ -535,7 +535,7 @@ class DCompiler(Compiler): def sanity_check(self, work_dir: str, environment: 'Environment') -> None: source_name = os.path.join(work_dir, 'sanity.d') output_name = os.path.join(work_dir, 'dtest') - with open(source_name, 'w') as ofile: + with open(source_name, 'w', encoding='utf-8') as ofile: ofile.write('''void main() { }''') pc = subprocess.Popen(self.exelist + self.get_output_args(output_name) + self._get_target_arch_args() + [source_name], cwd=work_dir) pc.wait() diff --git a/mesonbuild/compilers/fortran.py b/mesonbuild/compilers/fortran.py index 8264638..925eff6 100644 --- a/mesonbuild/compilers/fortran.py +++ b/mesonbuild/compilers/fortran.py @@ -71,7 +71,7 @@ class FortranCompiler(CLikeCompiler, Compiler): if binary_name.is_file(): binary_name.unlink() - source_name.write_text('print *, "Fortran compilation is working."; end') + source_name.write_text('print *, "Fortran compilation is working."; end', encoding='utf-8') extra_flags: T.List[str] = [] extra_flags += environment.coredata.get_external_args(self.for_machine, self.language) diff --git a/mesonbuild/compilers/java.py b/mesonbuild/compilers/java.py index 77e1a9b..ab82450 100644 --- a/mesonbuild/compilers/java.py +++ b/mesonbuild/compilers/java.py @@ -70,7 +70,7 @@ class JavaCompiler(BasicLinkerIsCompilerMixin, Compiler): src = 'SanityCheck.java' obj = 'SanityCheck' source_name = os.path.join(work_dir, src) - with open(source_name, 'w') as ofile: + with open(source_name, 'w', encoding='utf-8') as ofile: ofile.write(textwrap.dedent( '''class SanityCheck { public static void main(String[] args) { diff --git a/mesonbuild/compilers/mixins/clike.py b/mesonbuild/compilers/mixins/clike.py index 3210dd7..09ad837 100644 --- a/mesonbuild/compilers/mixins/clike.py +++ b/mesonbuild/compilers/mixins/clike.py @@ -306,7 +306,7 @@ class CLikeCompiler(Compiler): binname += '.exe' # Write binary check source binary_name = os.path.join(work_dir, binname) - with open(source_name, 'w') as ofile: + with open(source_name, 'w', encoding='utf-8') as ofile: ofile.write(code) # Compile sanity check # NOTE: extra_flags must be added at the end. On MSVC, it might contain a '/link' argument diff --git a/mesonbuild/compilers/rust.py b/mesonbuild/compilers/rust.py index 6a7bd04..2b566c8 100644 --- a/mesonbuild/compilers/rust.py +++ b/mesonbuild/compilers/rust.py @@ -65,7 +65,7 @@ class RustCompiler(Compiler): def sanity_check(self, work_dir: str, environment: 'Environment') -> None: source_name = os.path.join(work_dir, 'sanity.rs') output_name = os.path.join(work_dir, 'rusttest') - with open(source_name, 'w') as ofile: + with open(source_name, 'w', encoding='utf-8') as ofile: ofile.write(textwrap.dedent( '''fn main() { } diff --git a/mesonbuild/compilers/swift.py b/mesonbuild/compilers/swift.py index 7b18591..2d52e21 100644 --- a/mesonbuild/compilers/swift.py +++ b/mesonbuild/compilers/swift.py @@ -107,7 +107,7 @@ class SwiftCompiler(Compiler): extra_flags += self.get_compile_only_args() else: extra_flags += environment.coredata.get_external_link_args(self.for_machine, self.language) - with open(source_name, 'w') as ofile: + with open(source_name, 'w', encoding='utf-8') as ofile: ofile.write('''print("Swift compilation is working.") ''') pc = subprocess.Popen(self.exelist + extra_flags + ['-emit-executable', '-o', output_name, src], cwd=work_dir) diff --git a/mesonbuild/coredata.py b/mesonbuild/coredata.py index a0ee7df..97edeb2 100644 --- a/mesonbuild/coredata.py +++ b/mesonbuild/coredata.py @@ -481,8 +481,8 @@ class CoreData: # the contents of that file into the meson private (scratch) # directory so that it can be re-read when wiping/reconfiguring copy = os.path.join(scratch_dir, f'{uuid.uuid4()}.{ftype}.ini') - with open(f) as rf: - with open(copy, 'w') as wf: + with open(f, encoding='utf-8') as rf: + with open(copy, 'w', encoding='utf-8') as wf: wf.write(rf.read()) real.append(copy) @@ -973,7 +973,7 @@ def write_cmd_line_file(build_dir: str, options: argparse.Namespace) -> None: config['options'] = {str(k): str(v) for k, v in options.cmd_line_options.items()} config['properties'] = properties - with open(filename, 'w') as f: + with open(filename, 'w', encoding='utf-8') as f: config.write(f) def update_cmd_line_file(build_dir: str, options: argparse.Namespace): @@ -981,7 +981,7 @@ def update_cmd_line_file(build_dir: str, options: argparse.Namespace): config = CmdLineFileParser() config.read(filename) config['options'].update({str(k): str(v) for k, v in options.cmd_line_options.items()}) - with open(filename, 'w') as f: + with open(filename, 'w', encoding='utf-8') as f: config.write(f) def get_cmd_line_options(build_dir: str, options: argparse.Namespace) -> str: diff --git a/mesonbuild/dependencies/boost.py b/mesonbuild/dependencies/boost.py index dc317cd..4e5af90 100644 --- a/mesonbuild/dependencies/boost.py +++ b/mesonbuild/dependencies/boost.py @@ -717,7 +717,7 @@ class BoostDependency(SystemDependency): # also work, however, this is slower (since it the compiler has to be # invoked) and overkill since the layout of the header is always the same. assert hfile.exists() - raw = hfile.read_text() + raw = hfile.read_text(encoding='utf-8') m = re.search(r'#define\s+BOOST_VERSION\s+([0-9]+)', raw) if not m: mlog.debug(f'Failed to extract version information from {hfile}') diff --git a/mesonbuild/dependencies/cmake.py b/mesonbuild/dependencies/cmake.py index 7dee62e..8219cc5 100644 --- a/mesonbuild/dependencies/cmake.py +++ b/mesonbuild/dependencies/cmake.py @@ -620,7 +620,7 @@ class CMakeDependency(ExternalDependency): """).format(' '.join(cmake_language)) + cmake_txt cm_file = build_dir / 'CMakeLists.txt' - cm_file.write_text(cmake_txt) + cm_file.write_text(cmake_txt, encoding='utf-8') mlog.cmd_ci_include(cm_file.absolute().as_posix()) return build_dir diff --git a/mesonbuild/dependencies/cuda.py b/mesonbuild/dependencies/cuda.py index 43bb622..21cc7ab 100644 --- a/mesonbuild/dependencies/cuda.py +++ b/mesonbuild/dependencies/cuda.py @@ -184,7 +184,7 @@ class CudaDependency(SystemDependency): def _read_cuda_runtime_api_version(self, path_str: str) -> T.Optional[str]: path = Path(path_str) for i in path.rglob('cuda_runtime_api.h'): - raw = i.read_text() + raw = i.read_text(encoding='utf-8') m = self.cudart_version_regex.search(raw) if not m: continue @@ -202,7 +202,7 @@ class CudaDependency(SystemDependency): # Read 'version.txt' at the root of the CUDA Toolkit directory to determine the tookit version version_file_path = os.path.join(path, 'version.txt') try: - with open(version_file_path) as version_file: + with open(version_file_path, encoding='utf-8') as version_file: version_str = version_file.readline() # e.g. 'CUDA Version 10.1.168' m = self.toolkit_version_regex.match(version_str) if m: diff --git a/mesonbuild/dependencies/pkgconfig.py b/mesonbuild/dependencies/pkgconfig.py index be5412e..1e8d913 100644 --- a/mesonbuild/dependencies/pkgconfig.py +++ b/mesonbuild/dependencies/pkgconfig.py @@ -423,7 +423,7 @@ class PkgConfigDependency(ExternalDependency): return out.strip() def extract_field(self, la_file: str, fieldname: str) -> T.Optional[str]: - with open(la_file) as f: + with open(la_file, encoding='utf-8') as f: for line in f: arr = line.strip().split('=') if arr[0] == fieldname: diff --git a/mesonbuild/interpreter/interpreter.py b/mesonbuild/interpreter/interpreter.py index e4fb0d4..1a9872d 100644 --- a/mesonbuild/interpreter/interpreter.py +++ b/mesonbuild/interpreter/interpreter.py @@ -973,7 +973,7 @@ external dependencies (including libraries) must go to "dependencies".''') ast.accept(printer) printer.post_process() meson_filename = os.path.join(self.build.environment.get_build_dir(), subdir, 'meson.build') - with open(meson_filename, "w") as f: + with open(meson_filename, "w", encoding='utf-8') as f: f.write(printer.result) mlog.log('Build file:', meson_filename) @@ -1964,7 +1964,7 @@ This will become a hard error in the future.''' % kwargs['input'], location=self if not os.path.isfile(absname): self.subdir = prev_subdir raise InterpreterException(f"Non-existent build file '{buildfilename!s}'") - with open(absname, encoding='utf8') as f: + with open(absname, encoding='utf-8') as f: code = f.read() assert(isinstance(code, str)) try: @@ -2198,7 +2198,7 @@ This will become a hard error in the future.''' % kwargs['input'], location=self mesonlib.replace_if_different(ofile_abs, dst_tmp) if depfile: mlog.log('Reading depfile:', mlog.bold(depfile)) - with open(depfile) as f: + with open(depfile, encoding='utf-8') as f: df = DepFile(f.readlines()) deps = df.get_all_dependencies(ofile_fname) for dep in deps: diff --git a/mesonbuild/interpreterbase/interpreterbase.py b/mesonbuild/interpreterbase/interpreterbase.py index 3f5726b..d66fb9c 100644 --- a/mesonbuild/interpreterbase/interpreterbase.py +++ b/mesonbuild/interpreterbase/interpreterbase.py @@ -95,7 +95,7 @@ class InterpreterBase: mesonfile = os.path.join(self.source_root, self.subdir, environment.build_filename) if not os.path.isfile(mesonfile): raise InvalidArguments('Missing Meson file in %s' % mesonfile) - with open(mesonfile, encoding='utf8') as mf: + with open(mesonfile, encoding='utf-8') as mf: code = mf.read() if code.isspace(): raise InvalidCode('Builder file is empty.') diff --git a/mesonbuild/mcompile.py b/mesonbuild/mcompile.py index d6f54e3..6e526f6 100644 --- a/mesonbuild/mcompile.py +++ b/mesonbuild/mcompile.py @@ -58,7 +58,7 @@ def parse_introspect_data(builddir: Path) -> T.Dict[str, T.List[dict]]: path_to_intro = builddir / 'meson-info' / 'intro-targets.json' if not path_to_intro.exists(): raise MesonException(f'`{path_to_intro.name}` is missing! Directory is not configured yet?') - with path_to_intro.open() as f: + with path_to_intro.open(encoding='utf-8') as f: schema = json.load(f) parsed_data = defaultdict(list) # type: T.Dict[str, T.List[dict]] diff --git a/mesonbuild/mdist.py b/mesonbuild/mdist.py index 397f8cd..22196d8 100644 --- a/mesonbuild/mdist.py +++ b/mesonbuild/mdist.py @@ -49,7 +49,7 @@ def create_hash(fname): hashname = fname + '.sha256sum' m = hashlib.sha256() m.update(open(fname, 'rb').read()) - with open(hashname, 'w') as f: + with open(hashname, 'w', encoding='utf-8') as f: # A space and an asterisk because that is the format defined by GNU coreutils # and accepted by busybox and the Perl shasum tool. f.write('{} *{}\n'.format(m.hexdigest(), os.path.basename(fname))) @@ -67,7 +67,7 @@ def process_submodules(dirname): if not os.path.exists(module_file): return subprocess.check_call(['git', 'submodule', 'update', '--init', '--recursive'], cwd=dirname) - for line in open(module_file): + for line in open(module_file, encoding='utf-8'): line = line.strip() if '=' not in line: continue @@ -242,7 +242,7 @@ def check_dist(packagename, meson_command, extra_meson_args, bld_root, privdir): unpacked_files = glob(os.path.join(unpackdir, '*')) assert(len(unpacked_files) == 1) unpacked_src_dir = unpacked_files[0] - with open(os.path.join(bld_root, 'meson-info', 'intro-buildoptions.json')) as boptions: + with open(os.path.join(bld_root, 'meson-info', 'intro-buildoptions.json'), encoding='utf-8') as boptions: meson_command += ['-D{name}={value}'.format(**o) for o in json.load(boptions) if o['name'] not in ['backend', 'install_umask', 'buildtype']] meson_command += extra_meson_args diff --git a/mesonbuild/mesondata.py b/mesonbuild/mesondata.py index b9a4c10..43b7bde 100644 --- a/mesonbuild/mesondata.py +++ b/mesonbuild/mesondata.py @@ -361,7 +361,7 @@ class DataFile: def write_once(self, path: Path) -> None: if not path.exists(): - path.write_text(self.data) + path.write_text(self.data, encoding='utf-8') def write_to_private(self, env: 'Environment') -> Path: out_file = Path(env.scratch_dir) / 'data' / self.path.name diff --git a/mesonbuild/mesonlib/posix.py b/mesonbuild/mesonlib/posix.py index 1d8ba8c..67f9a44 100644 --- a/mesonbuild/mesonlib/posix.py +++ b/mesonbuild/mesonlib/posix.py @@ -27,7 +27,7 @@ __all__ = ['BuildDirLock'] class BuildDirLock(BuildDirLockBase): def __enter__(self) -> None: - self.lockfile = open(self.lockfilename, 'w') + self.lockfile = open(self.lockfilename, 'w', encoding='utf-8') try: fcntl.flock(self.lockfile, fcntl.LOCK_EX | fcntl.LOCK_NB) except (BlockingIOError, PermissionError): diff --git a/mesonbuild/mesonlib/universal.py b/mesonbuild/mesonlib/universal.py index 0ed99a5..c500e15 100644 --- a/mesonbuild/mesonlib/universal.py +++ b/mesonbuild/mesonlib/universal.py @@ -1359,7 +1359,7 @@ def expand_arguments(args: T.Iterable[str]) -> T.Optional[T.List[str]]: args_file = arg[1:] try: - with open(args_file) as f: + with open(args_file, encoding='utf-8') as f: extended_args = f.read().split() expended_args += extended_args except Exception as e: @@ -1854,11 +1854,11 @@ def get_wine_shortpath(winecmd: T.List[str], wine_paths: T.Sequence[str]) -> str wine_paths = list(OrderedSet(wine_paths)) getShortPathScript = '%s.bat' % str(uuid.uuid4()).lower()[:5] - with open(getShortPathScript, mode='w') as f: + with open(getShortPathScript, mode='w', encoding='utf-8') as f: f.write("@ECHO OFF\nfor %%x in (%*) do (\n echo|set /p=;%~sx\n)\n") f.flush() try: - with open(os.devnull, 'w') as stderr: + with open(os.devnull, 'w', encoding='utf-8') as stderr: wine_path = subprocess.check_output( winecmd + ['cmd', '/C', getShortPathScript] + wine_paths, diff --git a/mesonbuild/mesonlib/win32.py b/mesonbuild/mesonlib/win32.py index 0919ef7..bc0caec 100644 --- a/mesonbuild/mesonlib/win32.py +++ b/mesonbuild/mesonlib/win32.py @@ -27,7 +27,7 @@ __all__ = ['BuildDirLock'] class BuildDirLock(BuildDirLockBase): def __enter__(self) -> None: - self.lockfile = open(self.lockfilename, 'w') + self.lockfile = open(self.lockfilename, 'w', encoding='utf-8') try: msvcrt.locking(self.lockfile.fileno(), msvcrt.LK_NBLCK, 1) except (BlockingIOError, PermissionError): diff --git a/mesonbuild/mesonmain.py b/mesonbuild/mesonmain.py index a3fd27d..86ae973 100644 --- a/mesonbuild/mesonmain.py +++ b/mesonbuild/mesonmain.py @@ -100,7 +100,7 @@ def setup_vsenv() -> None: bat_separator = '---SPLIT---' bat_contents = bat_template.format(bat_path, bat_separator) - bat_file.write_text(bat_contents) + bat_file.write_text(bat_contents, encoding='utf-8') try: bat_output = subprocess.check_output(str(bat_file), universal_newlines=True) finally: diff --git a/mesonbuild/minstall.py b/mesonbuild/minstall.py index 7011c42..b631d4a 100644 --- a/mesonbuild/minstall.py +++ b/mesonbuild/minstall.py @@ -708,7 +708,7 @@ def run(opts: 'ArgumentType') -> int: if not rebuild_all(opts.wd): sys.exit(-1) os.chdir(opts.wd) - with open(os.path.join(log_dir, 'install-log.txt'), 'w') as lf: + with open(os.path.join(log_dir, 'install-log.txt'), 'w', encoding='utf-8') as lf: installer = Installer(opts, lf) append_to_log(lf, '# List of files installed by Meson') append_to_log(lf, '# Does not contain files installed by custom scripts.') diff --git a/mesonbuild/mintro.py b/mesonbuild/mintro.py index 430b2f1..73790a5 100644 --- a/mesonbuild/mintro.py +++ b/mesonbuild/mintro.py @@ -394,7 +394,7 @@ def get_info_file(infodir: str, kind: T.Optional[str] = None) -> str: 'meson-info.json' if not kind else f'intro-{kind}.json') def load_info_file(infodir: str, kind: T.Optional[str] = None) -> T.Any: - with open(get_info_file(infodir, kind)) as fp: + with open(get_info_file(infodir, kind), encoding='utf-8') as fp: return json.load(fp) def run(options: argparse.Namespace) -> int: @@ -464,7 +464,7 @@ def write_intro_info(intro_info: T.Sequence[T.Tuple[str, T.Union[dict, T.List[T. for i in intro_info: out_file = os.path.join(info_dir, 'intro-{}.json'.format(i[0])) tmp_file = os.path.join(info_dir, 'tmp_dump.json') - with open(tmp_file, 'w') as fp: + with open(tmp_file, 'w', encoding='utf-8') as fp: json.dump(i[1], fp) fp.flush() # Not sure if this is needed os.replace(tmp_file, out_file) @@ -535,7 +535,7 @@ def write_meson_info_file(builddata: build.Build, errors: list, build_files_upda # Write the data to disc tmp_file = os.path.join(info_dir, 'tmp_dump.json') - with open(tmp_file, 'w') as fp: + with open(tmp_file, 'w', encoding='utf-8') as fp: json.dump(info_data, fp) fp.flush() os.replace(tmp_file, info_file) diff --git a/mesonbuild/mlog.py b/mesonbuild/mlog.py index 5c76677..314e1b7 100644 --- a/mesonbuild/mlog.py +++ b/mesonbuild/mlog.py @@ -100,7 +100,7 @@ def set_verbose() -> None: def initialize(logdir: str, fatal_warnings: bool = False) -> None: global log_dir, log_file, log_fatal_warnings log_dir = logdir - log_file = open(os.path.join(logdir, log_fname), 'w', encoding='utf8') + log_file = open(os.path.join(logdir, log_fname), 'w', encoding='utf-8') log_fatal_warnings = fatal_warnings def set_timestamp_start(start: float) -> None: diff --git a/mesonbuild/modules/cmake.py b/mesonbuild/modules/cmake.py index cb37edc..c390b01 100644 --- a/mesonbuild/modules/cmake.py +++ b/mesonbuild/modules/cmake.py @@ -298,7 +298,7 @@ class CmakeModule(ExtensionModule): package_init += PACKAGE_INIT_SET_AND_CHECK try: - with open(infile) as fin: + with open(infile, encoding='utf-8') as fin: data = fin.readlines() except Exception as e: raise mesonlib.MesonException('Could not read input file {}: {}'.format(infile, str(e))) diff --git a/mesonbuild/modules/dlang.py b/mesonbuild/modules/dlang.py index e2a55b6..b6efc86 100644 --- a/mesonbuild/modules/dlang.py +++ b/mesonbuild/modules/dlang.py @@ -70,7 +70,7 @@ class DlangModule(ExtensionModule): config_path = os.path.join(args[1], 'dub.json') if os.path.exists(config_path): - with open(config_path, encoding='utf8') as ofile: + with open(config_path, encoding='utf-8') as ofile: try: config = json.load(ofile) except ValueError: @@ -108,7 +108,7 @@ class DlangModule(ExtensionModule): else: config[key] = value - with open(config_path, 'w', encoding='utf8') as ofile: + with open(config_path, 'w', encoding='utf-8') as ofile: ofile.write(json.dumps(config, indent=4, ensure_ascii=False)) def _call_dubbin(self, args, env=None): diff --git a/mesonbuild/modules/gnome.py b/mesonbuild/modules/gnome.py index efd1f00..1b68f6c 100644 --- a/mesonbuild/modules/gnome.py +++ b/mesonbuild/modules/gnome.py @@ -1713,7 +1713,7 @@ G_END_DECLS''' def _generate_deps(self, state, library, packages, install_dir): outdir = state.environment.scratch_dir fname = os.path.join(outdir, library + '.deps') - with open(fname, 'w') as ofile: + with open(fname, 'w', encoding='utf-8') as ofile: for package in packages: ofile.write(package + '\n') return build.Data([mesonlib.File(True, outdir, fname)], install_dir, None, state.subproject) diff --git a/mesonbuild/modules/hotdoc.py b/mesonbuild/modules/hotdoc.py index 26026fb..4dccd06 100644 --- a/mesonbuild/modules/hotdoc.py +++ b/mesonbuild/modules/hotdoc.py @@ -310,7 +310,7 @@ class HotdocTargetBuilder: hotdoc_config_name = fullname + '.json' hotdoc_config_path = os.path.join( self.builddir, self.subdir, hotdoc_config_name) - with open(hotdoc_config_path, 'w') as f: + with open(hotdoc_config_path, 'w', encoding='utf-8') as f: f.write('{}') self.cmd += ['--conf-file', hotdoc_config_path] diff --git a/mesonbuild/modules/keyval.py b/mesonbuild/modules/keyval.py index d637ac0..b2d54db 100644 --- a/mesonbuild/modules/keyval.py +++ b/mesonbuild/modules/keyval.py @@ -32,7 +32,7 @@ class KeyvalModule(ExtensionModule): def _load_file(self, path_to_config): result = dict() try: - with open(path_to_config) as f: + with open(path_to_config, encoding='utf-8') as f: for line in f: if '#' in line: comment_idx = line.index('#') diff --git a/mesonbuild/modules/rpm.py b/mesonbuild/modules/rpm.py index 704e82b..1fae144 100644 --- a/mesonbuild/modules/rpm.py +++ b/mesonbuild/modules/rpm.py @@ -77,7 +77,7 @@ class RPMModule(ExtensionModule): filename = os.path.join(state.environment.get_build_dir(), '%s.spec' % proj) - with open(filename, 'w+') as fn: + with open(filename, 'w+', encoding='utf-8') as fn: fn.write('Name: %s\n' % proj) fn.write('Version: # FIXME\n') fn.write('Release: 1%{?dist}\n') diff --git a/mesonbuild/modules/unstable_external_project.py b/mesonbuild/modules/unstable_external_project.py index e997f6a..4f8ac56 100644 --- a/mesonbuild/modules/unstable_external_project.py +++ b/mesonbuild/modules/unstable_external_project.py @@ -162,7 +162,7 @@ class ExternalProject(ModuleObject): log_filename = Path(mlog.log_dir, f'{self.name}-{step}.log') output = None if not self.verbose: - output = open(log_filename, 'w') + output = open(log_filename, 'w', encoding='utf-8') output.write(m + '\n') output.flush() else: diff --git a/mesonbuild/msetup.py b/mesonbuild/msetup.py index b348948..9ed2981 100644 --- a/mesonbuild/msetup.py +++ b/mesonbuild/msetup.py @@ -148,9 +148,9 @@ class MesonApp: def add_vcs_ignore_files(self, build_dir: str) -> None: if os.listdir(build_dir): return - with open(os.path.join(build_dir, '.gitignore'), 'w') as ofile: + with open(os.path.join(build_dir, '.gitignore'), 'w', encoding='utf-8') as ofile: ofile.write(git_ignore_file) - with open(os.path.join(build_dir, '.hgignore'), 'w') as ofile: + with open(os.path.join(build_dir, '.hgignore'), 'w', encoding='utf-8') as ofile: ofile.write(hg_ignore_file) def validate_dirs(self, dir1: str, dir2: str, reconfigure: bool, wipe: bool) -> T.Tuple[str, str]: diff --git a/mesonbuild/mtest.py b/mesonbuild/mtest.py index e54740e..826619f 100644 --- a/mesonbuild/mtest.py +++ b/mesonbuild/mtest.py @@ -467,7 +467,7 @@ class TestLogger: class TestFileLogger(TestLogger): def __init__(self, filename: str, errors: str = 'replace') -> None: self.filename = filename - self.file = open(filename, 'w', encoding='utf8', errors=errors) + self.file = open(filename, 'w', encoding='utf-8', errors=errors) def close(self) -> None: if self.file: diff --git a/mesonbuild/optinterpreter.py b/mesonbuild/optinterpreter.py index 6043691..72bf865 100644 --- a/mesonbuild/optinterpreter.py +++ b/mesonbuild/optinterpreter.py @@ -142,7 +142,7 @@ class OptionInterpreter: def process(self, option_file: str) -> None: try: - with open(option_file, encoding='utf8') as f: + with open(option_file, encoding='utf-8') as f: ast = mparser.Parser(f.read(), option_file).parse() except mesonlib.MesonException as me: me.file = option_file diff --git a/mesonbuild/programs.py b/mesonbuild/programs.py index bb14f96..ffd177f 100644 --- a/mesonbuild/programs.py +++ b/mesonbuild/programs.py @@ -176,7 +176,7 @@ class ExternalProgram(mesonlib.HoldableObject): or if we're on Windows (which does not understand shebangs). """ try: - with open(script) as f: + with open(script, encoding='utf-8') as f: first_line = f.readline().strip() if first_line.startswith('#!'): # In a shebang, everything before the first space is assumed to diff --git a/mesonbuild/rewriter.py b/mesonbuild/rewriter.py index ddd712f..3f661a4 100644 --- a/mesonbuild/rewriter.py +++ b/mesonbuild/rewriter.py @@ -818,9 +818,9 @@ class Rewriter: fdata = '' # Create an empty file if it does not exist if not os.path.exists(fpath): - with open(fpath, 'w'): + with open(fpath, 'w', encoding='utf-8'): pass - with open(fpath) as fp: + with open(fpath, encoding='utf-8') as fp: fdata = fp.read() # Generate line offsets numbers @@ -871,7 +871,7 @@ class Rewriter: # Write the files back for key, val in files.items(): mlog.log('Rewriting', mlog.yellow(key)) - with open(val['path'], 'w') as fp: + with open(val['path'], 'w', encoding='utf-8') as fp: fp.write(val['raw']) target_operation_map = { @@ -923,7 +923,7 @@ def generate_def_opts(options) -> T.List[dict]: def generate_cmd(options) -> T.List[dict]: if os.path.exists(options.json): - with open(options.json) as fp: + with open(options.json, encoding='utf-8') as fp: return json.load(fp) else: return json.loads(options.json) diff --git a/mesonbuild/scripts/coverage.py b/mesonbuild/scripts/coverage.py index f8a4924..b60d5d1 100644 --- a/mesonbuild/scripts/coverage.py +++ b/mesonbuild/scripts/coverage.py @@ -68,11 +68,11 @@ def coverage(outputs: T.List[str], source_root: str, subproject_root: str, build # Create a shim to allow using llvm-cov as a gcov tool. if mesonlib.is_windows(): llvm_cov_shim_path = os.path.join(log_dir, 'llvm-cov.bat') - with open(llvm_cov_shim_path, 'w') as llvm_cov_bat: + with open(llvm_cov_shim_path, 'w', encoding='utf-8') as llvm_cov_bat: llvm_cov_bat.write(f'@"{llvm_cov_exe}" gcov %*') else: llvm_cov_shim_path = os.path.join(log_dir, 'llvm-cov.sh') - with open(llvm_cov_shim_path, 'w') as llvm_cov_sh: + with open(llvm_cov_shim_path, 'w', encoding='utf-8') as llvm_cov_sh: llvm_cov_sh.write(f'#!/usr/bin/env sh\nexec "{llvm_cov_exe}" gcov $@') os.chmod(llvm_cov_shim_path, os.stat(llvm_cov_shim_path).st_mode | stat.S_IEXEC) gcov_tool_args = ['--gcov-tool', llvm_cov_shim_path] diff --git a/mesonbuild/scripts/depfixer.py b/mesonbuild/scripts/depfixer.py index c215749..52c7ba9 100644 --- a/mesonbuild/scripts/depfixer.py +++ b/mesonbuild/scripts/depfixer.py @@ -468,7 +468,7 @@ def fix_darwin(fname: str, new_rpath: str, final_path: str, install_name_mapping def fix_jar(fname: str) -> None: subprocess.check_call(['jar', 'xfv', fname, 'META-INF/MANIFEST.MF']) - with open('META-INF/MANIFEST.MF', 'r+') as f: + with open('META-INF/MANIFEST.MF', 'r+', encoding='utf-8') as f: lines = f.readlines() f.seek(0) for line in lines: diff --git a/mesonbuild/scripts/depscan.py b/mesonbuild/scripts/depscan.py index 207bbc6..9fc435b 100644 --- a/mesonbuild/scripts/depscan.py +++ b/mesonbuild/scripts/depscan.py @@ -57,7 +57,7 @@ class DependencyScanner: def scan_fortran_file(self, fname: str) -> None: fpath = pathlib.Path(fname) modules_in_this_file = set() - for line in fpath.read_text().split('\n'): + for line in fpath.read_text(encoding='utf-8').split('\n'): import_match = FORTRAN_USE_RE.match(line) export_match = FORTRAN_MODULE_RE.match(line) submodule_export_match = FORTRAN_SUBMOD_RE.match(line) @@ -108,7 +108,7 @@ class DependencyScanner: def scan_cpp_file(self, fname: str) -> None: fpath = pathlib.Path(fname) - for line in fpath.read_text().split('\n'): + for line in fpath.read_text(encoding='utf-8').split('\n'): import_match = CPP_IMPORT_RE.match(line) export_match = CPP_EXPORT_RE.match(line) if import_match: @@ -150,7 +150,7 @@ class DependencyScanner: def scan(self) -> int: for s in self.sources: self.scan_file(s) - with open(self.outfile, 'w') as ofile: + with open(self.outfile, 'w', encoding='utf-8') as ofile: ofile.write('ninja_dyndep_version = 1\n') for src in self.sources: objfilename = self.objname_for(src) diff --git a/mesonbuild/scripts/externalproject.py b/mesonbuild/scripts/externalproject.py index 657ef2f..a8e3bfe 100644 --- a/mesonbuild/scripts/externalproject.py +++ b/mesonbuild/scripts/externalproject.py @@ -34,7 +34,7 @@ class ExternalProject: self.make = options.make def write_depfile(self) -> None: - with open(self.depfile, 'w') as f: + with open(self.depfile, 'w', encoding='utf-8') as f: f.write(f'{self.stampfile}: \\\n') for dirpath, dirnames, filenames in os.walk(self.src_dir): dirnames[:] = [d for d in dirnames if not d.startswith('.')] @@ -45,7 +45,7 @@ class ExternalProject: f.write(' {} \\\n'.format(path.as_posix().replace(' ', '\\ '))) def write_stampfile(self) -> None: - with open(self.stampfile, 'w') as f: + with open(self.stampfile, 'w', encoding='utf-8') as f: pass def gnu_make(self) -> bool: @@ -78,7 +78,7 @@ class ExternalProject: log_filename = Path(self.log_dir, f'{self.name}-{step}.log') output = None if not self.verbose: - output = open(log_filename, 'w') + output = open(log_filename, 'w', encoding='utf-8') output.write(m + '\n') output.flush() else: diff --git a/mesonbuild/scripts/gettext.py b/mesonbuild/scripts/gettext.py index 92b55d3..b1ce6af 100644 --- a/mesonbuild/scripts/gettext.py +++ b/mesonbuild/scripts/gettext.py @@ -34,7 +34,7 @@ def read_linguas(src_sub: str) -> T.List[str]: linguas = os.path.join(src_sub, 'LINGUAS') try: langs = [] - with open(linguas) as f: + with open(linguas, encoding='utf-8') as f: for line in f: line = line.strip() if line and not line.startswith('#'): diff --git a/mesonbuild/scripts/symbolextractor.py b/mesonbuild/scripts/symbolextractor.py index 728d5e2..17501e2 100644 --- a/mesonbuild/scripts/symbolextractor.py +++ b/mesonbuild/scripts/symbolextractor.py @@ -38,18 +38,18 @@ RELINKING_WARNING = 'Relinking will always happen on source changes.' def dummy_syms(outfilename: str) -> None: """Just touch it so relinking happens always.""" - with open(outfilename, 'w'): + with open(outfilename, 'w', encoding='utf-8'): pass def write_if_changed(text: str, outfilename: str) -> None: try: - with open(outfilename) as f: + with open(outfilename, encoding='utf-8') as f: oldtext = f.read() if text == oldtext: return except FileNotFoundError: pass - with open(outfilename, 'w') as f: + with open(outfilename, 'w', encoding='utf-8') as f: f.write(text) def print_tool_warning(tools: T.List[str], msg: str, stderr: T.Optional[str] = None) -> None: @@ -61,7 +61,7 @@ def print_tool_warning(tools: T.List[str], msg: str, stderr: T.Optional[str] = N m += '\n' + stderr mlog.warning(m) # Write it out so we don't warn again - with open(TOOL_WARNING_FILE, 'w'): + with open(TOOL_WARNING_FILE, 'w', encoding='utf-8'): pass def get_tool(name: str) -> T.List[str]: @@ -309,7 +309,7 @@ def gen_symbols(libfilename: str, impfilename: str, outfilename: str, cross_host mlog.warning('Symbol extracting has not been implemented for this ' 'platform. ' + RELINKING_WARNING) # Write it out so we don't warn again - with open(TOOL_WARNING_FILE, 'w'): + with open(TOOL_WARNING_FILE, 'w', encoding='utf-8'): pass dummy_syms(outfilename) diff --git a/mesonbuild/scripts/uninstall.py b/mesonbuild/scripts/uninstall.py index 8db04dd..f08490f 100644 --- a/mesonbuild/scripts/uninstall.py +++ b/mesonbuild/scripts/uninstall.py @@ -20,7 +20,7 @@ logfile = 'meson-logs/install-log.txt' def do_uninstall(log: str) -> None: failures = 0 successes = 0 - for line in open(log): + for line in open(log, encoding='utf-8'): if line.startswith('#'): continue fname = line.strip() diff --git a/mesonbuild/scripts/vcstagger.py b/mesonbuild/scripts/vcstagger.py index 64985f6..18cf5f7 100644 --- a/mesonbuild/scripts/vcstagger.py +++ b/mesonbuild/scripts/vcstagger.py @@ -22,15 +22,15 @@ def config_vcs_tag(infile: str, outfile: str, fallback: str, source_dir: str, re except Exception: new_string = fallback - with open(infile, encoding='utf8') as f: + with open(infile, encoding='utf-8') as f: new_data = f.read().replace(replace_string, new_string) if os.path.exists(outfile): - with open(outfile, encoding='utf8') as f: + with open(outfile, encoding='utf-8') as f: needs_update = (f.read() != new_data) else: needs_update = True if needs_update: - with open(outfile, 'w', encoding='utf8') as f: + with open(outfile, 'w', encoding='utf-8') as f: f.write(new_data) diff --git a/mesonbuild/templates/cpptemplates.py b/mesonbuild/templates/cpptemplates.py index 2d28d3a..61c2737 100644 --- a/mesonbuild/templates/cpptemplates.py +++ b/mesonbuild/templates/cpptemplates.py @@ -149,11 +149,12 @@ class CppProject(SampleImpl): def create_executable(self) -> None: lowercase_token = re.sub(r'[^a-z0-9]', '_', self.name.lower()) source_name = lowercase_token + '.cpp' - open(source_name, 'w').write(hello_cpp_template.format(project_name=self.name)) - open('meson.build', 'w').write(hello_cpp_meson_template.format(project_name=self.name, - exe_name=lowercase_token, - source_name=source_name, - version=self.version)) + open(source_name, 'w', encoding='utf-8').write(hello_cpp_template.format(project_name=self.name)) + open('meson.build', 'w', encoding='utf-8').write( + hello_cpp_meson_template.format(project_name=self.name, + exe_name=lowercase_token, + source_name=source_name, + version=self.version)) def create_library(self) -> None: lowercase_token = re.sub(r'[^a-z0-9]', '_', self.name.lower()) @@ -178,7 +179,7 @@ class CppProject(SampleImpl): 'test_name': lowercase_token, 'version': self.version, } - open(lib_hpp_name, 'w').write(lib_hpp_template.format(**kwargs)) - open(lib_cpp_name, 'w').write(lib_cpp_template.format(**kwargs)) - open(test_cpp_name, 'w').write(lib_cpp_test_template.format(**kwargs)) - open('meson.build', 'w').write(lib_cpp_meson_template.format(**kwargs)) + open(lib_hpp_name, 'w', encoding='utf-8').write(lib_hpp_template.format(**kwargs)) + open(lib_cpp_name, 'w', encoding='utf-8').write(lib_cpp_template.format(**kwargs)) + open(test_cpp_name, 'w', encoding='utf-8').write(lib_cpp_test_template.format(**kwargs)) + open('meson.build', 'w', encoding='utf-8').write(lib_cpp_meson_template.format(**kwargs)) diff --git a/mesonbuild/templates/cstemplates.py b/mesonbuild/templates/cstemplates.py index 8524c97..bad7984 100644 --- a/mesonbuild/templates/cstemplates.py +++ b/mesonbuild/templates/cstemplates.py @@ -100,12 +100,14 @@ class CSharpProject(SampleImpl): uppercase_token = lowercase_token.upper() class_name = uppercase_token[0] + lowercase_token[1:] source_name = uppercase_token[0] + lowercase_token[1:] + '.cs' - open(source_name, 'w').write(hello_cs_template.format(project_name=self.name, - class_name=class_name)) - open('meson.build', 'w').write(hello_cs_meson_template.format(project_name=self.name, - exe_name=self.name, - source_name=source_name, - version=self.version)) + open(source_name, 'w', encoding='utf-8').write( + hello_cs_template.format(project_name=self.name, + class_name=class_name)) + open('meson.build', 'w', encoding='utf-8').write( + hello_cs_meson_template.format(project_name=self.name, + exe_name=self.name, + source_name=source_name, + version=self.version)) def create_library(self) -> None: lowercase_token = re.sub(r'[^a-z0-9]', '_', self.name.lower()) @@ -127,6 +129,6 @@ class CSharpProject(SampleImpl): 'test_name': lowercase_token, 'version': self.version, } - open(lib_cs_name, 'w').write(lib_cs_template.format(**kwargs)) - open(test_cs_name, 'w').write(lib_cs_test_template.format(**kwargs)) - open('meson.build', 'w').write(lib_cs_meson_template.format(**kwargs)) + open(lib_cs_name, 'w', encoding='utf-8').write(lib_cs_template.format(**kwargs)) + open(test_cs_name, 'w', encoding='utf-8').write(lib_cs_test_template.format(**kwargs)) + open('meson.build', 'w', encoding='utf-8').write(lib_cs_meson_template.format(**kwargs)) diff --git a/mesonbuild/templates/ctemplates.py b/mesonbuild/templates/ctemplates.py index 440731c..9b651bc 100644 --- a/mesonbuild/templates/ctemplates.py +++ b/mesonbuild/templates/ctemplates.py @@ -132,11 +132,12 @@ class CProject(SampleImpl): def create_executable(self) -> None: lowercase_token = re.sub(r'[^a-z0-9]', '_', self.name.lower()) source_name = lowercase_token + '.c' - open(source_name, 'w').write(hello_c_template.format(project_name=self.name)) - open('meson.build', 'w').write(hello_c_meson_template.format(project_name=self.name, - exe_name=lowercase_token, - source_name=source_name, - version=self.version)) + open(source_name, 'w', encoding='utf-8').write(hello_c_template.format(project_name=self.name)) + open('meson.build', 'w', encoding='utf-8').write( + hello_c_meson_template.format(project_name=self.name, + exe_name=lowercase_token, + source_name=source_name, + version=self.version)) def create_library(self) -> None: lowercase_token = re.sub(r'[^a-z0-9]', '_', self.name.lower()) @@ -159,7 +160,7 @@ class CProject(SampleImpl): 'test_name': lowercase_token, 'version': self.version, } - open(lib_h_name, 'w').write(lib_h_template.format(**kwargs)) - open(lib_c_name, 'w').write(lib_c_template.format(**kwargs)) - open(test_c_name, 'w').write(lib_c_test_template.format(**kwargs)) - open('meson.build', 'w').write(lib_c_meson_template.format(**kwargs)) + open(lib_h_name, 'w', encoding='utf-8').write(lib_h_template.format(**kwargs)) + open(lib_c_name, 'w', encoding='utf-8').write(lib_c_template.format(**kwargs)) + open(test_c_name, 'w', encoding='utf-8').write(lib_c_test_template.format(**kwargs)) + open('meson.build', 'w', encoding='utf-8').write(lib_c_meson_template.format(**kwargs)) diff --git a/mesonbuild/templates/cudatemplates.py b/mesonbuild/templates/cudatemplates.py index 4fa9a2b..919db21 100644 --- a/mesonbuild/templates/cudatemplates.py +++ b/mesonbuild/templates/cudatemplates.py @@ -149,11 +149,12 @@ class CudaProject(SampleImpl): def create_executable(self) -> None: lowercase_token = re.sub(r'[^a-z0-9]', '_', self.name.lower()) source_name = lowercase_token + '.cu' - open(source_name, 'w').write(hello_cuda_template.format(project_name=self.name)) - open('meson.build', 'w').write(hello_cuda_meson_template.format(project_name=self.name, - exe_name=lowercase_token, - source_name=source_name, - version=self.version)) + open(source_name, 'w', encoding='utf-8').write(hello_cuda_template.format(project_name=self.name)) + open('meson.build', 'w', encoding='utf-8').write( + hello_cuda_meson_template.format(project_name=self.name, + exe_name=lowercase_token, + source_name=source_name, + version=self.version)) def create_library(self) -> None: lowercase_token = re.sub(r'[^a-z0-9]', '_', self.name.lower()) @@ -178,7 +179,7 @@ class CudaProject(SampleImpl): 'test_name': lowercase_token, 'version': self.version, } - open(lib_h_name, 'w').write(lib_h_template.format(**kwargs)) - open(lib_cuda_name, 'w').write(lib_cuda_template.format(**kwargs)) - open(test_cuda_name, 'w').write(lib_cuda_test_template.format(**kwargs)) - open('meson.build', 'w').write(lib_cuda_meson_template.format(**kwargs)) + open(lib_h_name, 'w', encoding='utf-8').write(lib_h_template.format(**kwargs)) + open(lib_cuda_name, 'w', encoding='utf-8').write(lib_cuda_template.format(**kwargs)) + open(test_cuda_name, 'w', encoding='utf-8').write(lib_cuda_test_template.format(**kwargs)) + open('meson.build', 'w', encoding='utf-8').write(lib_cuda_meson_template.format(**kwargs)) diff --git a/mesonbuild/templates/dlangtemplates.py b/mesonbuild/templates/dlangtemplates.py index 4aacda8..3d939d8 100644 --- a/mesonbuild/templates/dlangtemplates.py +++ b/mesonbuild/templates/dlangtemplates.py @@ -110,11 +110,12 @@ class DlangProject(SampleImpl): def create_executable(self) -> None: lowercase_token = re.sub(r'[^a-z0-9]', '_', self.name.lower()) source_name = lowercase_token + '.d' - open(source_name, 'w').write(hello_d_template.format(project_name=self.name)) - open('meson.build', 'w').write(hello_d_meson_template.format(project_name=self.name, - exe_name=lowercase_token, - source_name=source_name, - version=self.version)) + open(source_name, 'w', encoding='utf-8').write(hello_d_template.format(project_name=self.name)) + open('meson.build', 'w', encoding='utf-8').write( + hello_d_meson_template.format(project_name=self.name, + exe_name=lowercase_token, + source_name=source_name, + version=self.version)) def create_library(self) -> None: lowercase_token = re.sub(r'[^a-z0-9]', '_', self.name.lower()) @@ -137,6 +138,6 @@ class DlangProject(SampleImpl): 'test_name': lowercase_token, 'version': self.version, } - open(lib_d_name, 'w').write(lib_d_template.format(**kwargs)) - open(test_d_name, 'w').write(lib_d_test_template.format(**kwargs)) - open('meson.build', 'w').write(lib_d_meson_template.format(**kwargs)) + open(lib_d_name, 'w', encoding='utf-8').write(lib_d_template.format(**kwargs)) + open(test_d_name, 'w', encoding='utf-8').write(lib_d_test_template.format(**kwargs)) + open('meson.build', 'w', encoding='utf-8').write(lib_d_meson_template.format(**kwargs)) diff --git a/mesonbuild/templates/fortrantemplates.py b/mesonbuild/templates/fortrantemplates.py index f4cae66..8fc1bca 100644 --- a/mesonbuild/templates/fortrantemplates.py +++ b/mesonbuild/templates/fortrantemplates.py @@ -109,11 +109,12 @@ class FortranProject(SampleImpl): def create_executable(self) -> None: lowercase_token = re.sub(r'[^a-z0-9]', '_', self.name.lower()) source_name = lowercase_token + '.f90' - open(source_name, 'w').write(hello_fortran_template.format(project_name=self.name)) - open('meson.build', 'w').write(hello_fortran_meson_template.format(project_name=self.name, - exe_name=lowercase_token, - source_name=source_name, - version=self.version)) + open(source_name, 'w', encoding='utf-8').write(hello_fortran_template.format(project_name=self.name)) + open('meson.build', 'w', encoding='utf-8').write( + hello_fortran_meson_template.format(project_name=self.name, + exe_name=lowercase_token, + source_name=source_name, + version=self.version)) def create_library(self) -> None: lowercase_token = re.sub(r'[^a-z0-9]', '_', self.name.lower()) @@ -134,6 +135,6 @@ class FortranProject(SampleImpl): 'test_name': lowercase_token, 'version': self.version, } - open(lib_fortran_name, 'w').write(lib_fortran_template.format(**kwargs)) - open(test_fortran_name, 'w').write(lib_fortran_test_template.format(**kwargs)) - open('meson.build', 'w').write(lib_fortran_meson_template.format(**kwargs)) + open(lib_fortran_name, 'w', encoding='utf-8').write(lib_fortran_template.format(**kwargs)) + open(test_fortran_name, 'w', encoding='utf-8').write(lib_fortran_test_template.format(**kwargs)) + open('meson.build', 'w', encoding='utf-8').write(lib_fortran_meson_template.format(**kwargs)) diff --git a/mesonbuild/templates/javatemplates.py b/mesonbuild/templates/javatemplates.py index 9c64743..e432961 100644 --- a/mesonbuild/templates/javatemplates.py +++ b/mesonbuild/templates/javatemplates.py @@ -104,12 +104,14 @@ class JavaProject(SampleImpl): uppercase_token = lowercase_token.upper() class_name = uppercase_token[0] + lowercase_token[1:] source_name = uppercase_token[0] + lowercase_token[1:] + '.java' - open(source_name, 'w').write(hello_java_template.format(project_name=self.name, - class_name=class_name)) - open('meson.build', 'w').write(hello_java_meson_template.format(project_name=self.name, - exe_name=class_name, - source_name=source_name, - version=self.version)) + open(source_name, 'w', encoding='utf-8').write( + hello_java_template.format(project_name=self.name, + class_name=class_name)) + open('meson.build', 'w', encoding='utf-8').write( + hello_java_meson_template.format(project_name=self.name, + exe_name=class_name, + source_name=source_name, + version=self.version)) def create_library(self) -> None: lowercase_token = re.sub(r'[^a-z0-9]', '_', self.name.lower()) @@ -129,6 +131,6 @@ class JavaProject(SampleImpl): 'test_name': lowercase_token, 'version': self.version, } - open(lib_java_name, 'w').write(lib_java_template.format(**kwargs)) - open(test_java_name, 'w').write(lib_java_test_template.format(**kwargs)) - open('meson.build', 'w').write(lib_java_meson_template.format(**kwargs)) + open(lib_java_name, 'w', encoding='utf-8').write(lib_java_template.format(**kwargs)) + open(test_java_name, 'w', encoding='utf-8').write(lib_java_test_template.format(**kwargs)) + open('meson.build', 'w', encoding='utf-8').write(lib_java_meson_template.format(**kwargs)) diff --git a/mesonbuild/templates/mesontemplates.py b/mesonbuild/templates/mesontemplates.py index 4c4d66f..a29ac6f 100644 --- a/mesonbuild/templates/mesontemplates.py +++ b/mesonbuild/templates/mesontemplates.py @@ -71,5 +71,5 @@ def create_meson_build(options: argparse.Namespace) -> None: sourcespec=sourcespec, depspec=depspec, default_options=formatted_default_options) - open('meson.build', 'w').write(content) + open('meson.build', 'w', encoding='utf-8').write(content) print('Generated meson.build file:\n\n' + content) diff --git a/mesonbuild/templates/objcpptemplates.py b/mesonbuild/templates/objcpptemplates.py index f4f4b51..4f61d6c 100644 --- a/mesonbuild/templates/objcpptemplates.py +++ b/mesonbuild/templates/objcpptemplates.py @@ -132,11 +132,12 @@ class ObjCppProject(SampleImpl): def create_executable(self) -> None: lowercase_token = re.sub(r'[^a-z0-9]', '_', self.name.lower()) source_name = lowercase_token + '.mm' - open(source_name, 'w').write(hello_objcpp_template.format(project_name=self.name)) - open('meson.build', 'w').write(hello_objcpp_meson_template.format(project_name=self.name, - exe_name=lowercase_token, - source_name=source_name, - version=self.version)) + open(source_name, 'w', encoding='utf-8').write(hello_objcpp_template.format(project_name=self.name)) + open('meson.build', 'w', encoding='utf-8').write( + hello_objcpp_meson_template.format(project_name=self.name, + exe_name=lowercase_token, + source_name=source_name, + version=self.version)) def create_library(self) -> None: lowercase_token = re.sub(r'[^a-z0-9]', '_', self.name.lower()) @@ -159,8 +160,8 @@ class ObjCppProject(SampleImpl): 'test_name': lowercase_token, 'version': self.version, } - open(lib_h_name, 'w').write(lib_h_template.format(**kwargs)) - open(lib_objcpp_name, 'w').write(lib_objcpp_template.format(**kwargs)) - open(test_objcpp_name, 'w').write(lib_objcpp_test_template.format(**kwargs)) - open('meson.build', 'w').write(lib_objcpp_meson_template.format(**kwargs)) + open(lib_h_name, 'w', encoding='utf-8').write(lib_h_template.format(**kwargs)) + open(lib_objcpp_name, 'w', encoding='utf-8').write(lib_objcpp_template.format(**kwargs)) + open(test_objcpp_name, 'w', encoding='utf-8').write(lib_objcpp_test_template.format(**kwargs)) + open('meson.build', 'w', encoding='utf-8').write(lib_objcpp_meson_template.format(**kwargs)) diff --git a/mesonbuild/templates/objctemplates.py b/mesonbuild/templates/objctemplates.py index 4243024..dac638d 100644 --- a/mesonbuild/templates/objctemplates.py +++ b/mesonbuild/templates/objctemplates.py @@ -132,11 +132,12 @@ class ObjCProject(SampleImpl): def create_executable(self) -> None: lowercase_token = re.sub(r'[^a-z0-9]', '_', self.name.lower()) source_name = lowercase_token + '.m' - open(source_name, 'w').write(hello_objc_template.format(project_name=self.name)) - open('meson.build', 'w').write(hello_objc_meson_template.format(project_name=self.name, - exe_name=lowercase_token, - source_name=source_name, - version=self.version)) + open(source_name, 'w', encoding='utf-8').write(hello_objc_template.format(project_name=self.name)) + open('meson.build', 'w', encoding='utf-8').write( + hello_objc_meson_template.format(project_name=self.name, + exe_name=lowercase_token, + source_name=source_name, + version=self.version)) def create_library(self) -> None: lowercase_token = re.sub(r'[^a-z0-9]', '_', self.name.lower()) @@ -159,7 +160,7 @@ class ObjCProject(SampleImpl): 'test_name': lowercase_token, 'version': self.version, } - open(lib_h_name, 'w').write(lib_h_template.format(**kwargs)) - open(lib_objc_name, 'w').write(lib_objc_template.format(**kwargs)) - open(test_objc_name, 'w').write(lib_objc_test_template.format(**kwargs)) - open('meson.build', 'w').write(lib_objc_meson_template.format(**kwargs)) + open(lib_h_name, 'w', encoding='utf-8').write(lib_h_template.format(**kwargs)) + open(lib_objc_name, 'w', encoding='utf-8').write(lib_objc_template.format(**kwargs)) + open(test_objc_name, 'w', encoding='utf-8').write(lib_objc_test_template.format(**kwargs)) + open('meson.build', 'w', encoding='utf-8').write(lib_objc_meson_template.format(**kwargs)) diff --git a/mesonbuild/templates/rusttemplates.py b/mesonbuild/templates/rusttemplates.py index 6e99586..95a937c 100644 --- a/mesonbuild/templates/rusttemplates.py +++ b/mesonbuild/templates/rusttemplates.py @@ -80,11 +80,12 @@ class RustProject(SampleImpl): def create_executable(self) -> None: lowercase_token = re.sub(r'[^a-z0-9]', '_', self.name.lower()) source_name = lowercase_token + '.rs' - open(source_name, 'w').write(hello_rust_template.format(project_name=self.name)) - open('meson.build', 'w').write(hello_rust_meson_template.format(project_name=self.name, - exe_name=lowercase_token, - source_name=source_name, - version=self.version)) + open(source_name, 'w', encoding='utf-8').write(hello_rust_template.format(project_name=self.name)) + open('meson.build', 'w', encoding='utf-8').write( + hello_rust_meson_template.format(project_name=self.name, + exe_name=lowercase_token, + source_name=source_name, + version=self.version)) def create_library(self) -> None: lowercase_token = re.sub(r'[^a-z0-9]', '_', self.name.lower()) @@ -107,6 +108,6 @@ class RustProject(SampleImpl): 'test_name': lowercase_token, 'version': self.version, } - open(lib_rs_name, 'w').write(lib_rust_template.format(**kwargs)) - open(test_rs_name, 'w').write(lib_rust_test_template.format(**kwargs)) - open('meson.build', 'w').write(lib_rust_meson_template.format(**kwargs)) + open(lib_rs_name, 'w', encoding='utf-8').write(lib_rust_template.format(**kwargs)) + open(test_rs_name, 'w', encoding='utf-8').write(lib_rust_test_template.format(**kwargs)) + open('meson.build', 'w', encoding='utf-8').write(lib_rust_meson_template.format(**kwargs)) diff --git a/mesonbuild/wrap/wrap.py b/mesonbuild/wrap/wrap.py index e88d658..4a6583f 100644 --- a/mesonbuild/wrap/wrap.py +++ b/mesonbuild/wrap/wrap.py @@ -291,7 +291,7 @@ class Resolver: mlog.log('Using', mlog.bold(rel)) # Write a dummy wrap file in main project that redirect to the # wrap we picked. - with open(main_fname, 'w') as f: + with open(main_fname, 'w', encoding='utf-8') as f: f.write(textwrap.dedent('''\ [wrap-redirect] filename = {} diff --git a/run_cross_test.py b/run_cross_test.py index 5ce3e55..bafdbb6 100755 --- a/run_cross_test.py +++ b/run_cross_test.py @@ -49,7 +49,7 @@ def main(): options = parser.parse_args() cf_path = Path(options.cross_file) try: - data = json.loads(cf_path.read_text()) + data = json.loads(cf_path.read_text(encoding='utf-8')) real_cf = cf_path.resolve().parent / data['file'] assert real_cf.exists() env = os.environ.copy() diff --git a/run_meson_command_tests.py b/run_meson_command_tests.py index c84e415..45096c6 100755 --- a/run_meson_command_tests.py +++ b/run_meson_command_tests.py @@ -165,7 +165,7 @@ class CommandTests(unittest.TestCase): builddir = str(self.tmpdir / 'build4') (bindir / 'meson').rename(bindir / 'meson.real') wrapper = (bindir / 'meson') - wrapper.open('w').write('#!/bin/sh\n\nmeson.real "$@"') + wrapper.write_text('#!/bin/sh\n\nmeson.real "$@"', encoding='utf-8') wrapper.chmod(0o755) meson_setup = [str(wrapper), 'setup'] meson_command = meson_setup + self.meson_args diff --git a/run_mypy.py b/run_mypy.py index 7ad831d..22a9bdd 100755 --- a/run_mypy.py +++ b/run_mypy.py @@ -42,6 +42,7 @@ modules = [ 'mesonbuild/optinterpreter.py', 'mesonbuild/programs.py', + 'run_encoding_tests.py', 'run_mypy.py', 'run_project_tests.py', 'run_single_test.py', diff --git a/run_project_tests.py b/run_project_tests.py index fcd856f..7631b84 100755 --- a/run_project_tests.py +++ b/run_project_tests.py @@ -522,7 +522,7 @@ def run_test_inprocess(testdir: str) -> T.Tuple[int, str, str, str]: try: returncode_test = mtest.run_with_args(['--no-rebuild']) if test_log_fname.exists(): - test_log = test_log_fname.open(errors='ignore').read() + test_log = test_log_fname.open(encoding='utf-8', errors='ignore').read() else: test_log = '' returncode_benchmark = mtest.run_with_args(['--no-rebuild', '--benchmark', '--logbase', 'benchmarklog']) @@ -736,7 +736,7 @@ def load_test_json(t: TestDef, stdout_mandatory: bool) -> T.List[TestDef]: test_def = {} test_def_file = t.path / 'test.json' if test_def_file.is_file(): - test_def = json.loads(test_def_file.read_text()) + test_def = json.loads(test_def_file.read_text(encoding='utf-8')) # Handle additional environment variables env = {} # type: T.Dict[str, str] @@ -1022,18 +1022,17 @@ def skip_csharp(backend: Backend) -> bool: # on all compilation attempts. def has_broken_rustc() -> bool: - dirname = 'brokenrusttest' - if os.path.exists(dirname): - mesonlib.windows_proof_rmtree(dirname) - os.mkdir(dirname) - open(dirname + '/sanity.rs', 'w').write('''fn main() { -} -''') + dirname = Path('brokenrusttest') + if dirname.exists(): + mesonlib.windows_proof_rmtree(dirname.as_posix()) + dirname.mkdir() + sanity_file = dirname / 'sanity.rs' + sanity_file.write_text('fn main() {\n}\n', encoding='utf-8') pc = subprocess.run(['rustc', '-o', 'sanity.exe', 'sanity.rs'], - cwd=dirname, + cwd=dirname.as_posix(), stdout = subprocess.DEVNULL, stderr = subprocess.DEVNULL) - mesonlib.windows_proof_rmtree(dirname) + mesonlib.windows_proof_rmtree(dirname.as_posix()) return pc.returncode != 0 def should_skip_rust(backend: Backend) -> bool: diff --git a/run_unittests.py b/run_unittests.py index 9da20e6..99c56e2 100755 --- a/run_unittests.py +++ b/run_unittests.py @@ -809,16 +809,9 @@ class InternalTests(unittest.TestCase): if platform != 'openbsd': return with tempfile.TemporaryDirectory() as tmpdir: - with open(os.path.join(tmpdir, 'libfoo.so.6.0'), 'w') as f: - f.write('') - with open(os.path.join(tmpdir, 'libfoo.so.5.0'), 'w') as f: - f.write('') - with open(os.path.join(tmpdir, 'libfoo.so.54.0'), 'w') as f: - f.write('') - with open(os.path.join(tmpdir, 'libfoo.so.66a.0b'), 'w') as f: - f.write('') - with open(os.path.join(tmpdir, 'libfoo.so.70.0.so.1'), 'w') as f: - f.write('') + for i in ['libfoo.so.6.0', 'libfoo.so.5.0', 'libfoo.so.54.0', 'libfoo.so.66a.0b', 'libfoo.so.70.0.so.1']: + libpath = Path(tmpdir) / i + libpath.write_text('', encoding='utf-8') found = cc._find_library_real('foo', env, [tmpdir], '', LibType.PREFER_SHARED) self.assertEqual(os.path.basename(found[0]), 'libfoo.so.54.0') @@ -876,11 +869,11 @@ class InternalTests(unittest.TestCase): ''' def create_static_lib(name): if not is_osx(): - name.open('w').close() + name.open('w', encoding='utf-8').close() return src = name.with_suffix('.c') out = name.with_suffix('.o') - with src.open('w') as f: + with src.open('w', encoding='utf-8') as f: f.write('int meson_foobar (void) { return 0; }') subprocess.check_call(['clang', '-c', str(src), '-o', str(out)]) subprocess.check_call(['ar', 'csr', str(name), str(out)]) @@ -1084,7 +1077,7 @@ class InternalTests(unittest.TestCase): else: self.assertIn('VCINSTALLDIR', os.environ) # See https://devblogs.microsoft.com/cppblog/finding-the-visual-c-compiler-tools-in-visual-studio-2017/ - vctools_ver = (Path(os.environ['VCINSTALLDIR']) / 'Auxiliary' / 'Build' / 'Microsoft.VCToolsVersion.default.txt').read_text() + vctools_ver = (Path(os.environ['VCINSTALLDIR']) / 'Auxiliary' / 'Build' / 'Microsoft.VCToolsVersion.default.txt').read_text(encoding='utf-8') self.assertTrue(vctools_ver.startswith(toolset_ver), msg=f'{vctools_ver!r} does not start with {toolset_ver!r}') @@ -1292,16 +1285,14 @@ class InternalTests(unittest.TestCase): raise raise unittest.SkipTest('Python jsonschema module not found.') - with Path('data/test.schema.json').open() as f: - schema = json.load(f) + schema = json.loads(Path('data/test.schema.json').read_text(encoding='utf-8')) errors = [] # type: T.Tuple[str, Exception] for p in Path('test cases').glob('**/test.json'): - with p.open() as f: - try: - validate(json.load(f), schema=schema) - except ValidationError as e: - errors.append((p.resolve(), e)) + try: + validate(json.loads(p.read_text(encoding='utf-8')), schema=schema) + except ValidationError as e: + errors.append((p.resolve(), e)) for f, e in errors: print(f'Failed to validate: "{f}"') @@ -1731,7 +1722,7 @@ class DataTests(unittest.TestCase): continue if f.suffix == '.md': in_code_block = False - with f.open() as snippet: + with f.open(encoding='utf-8') as snippet: for line in snippet: if line.startswith(' '): continue @@ -1895,7 +1886,7 @@ class DataTests(unittest.TestCase): ''' env = get_fake_env() interp = Interpreter(FakeBuild(env), mock=True) - with open('data/syntax-highlighting/vim/syntax/meson.vim') as f: + with open('data/syntax-highlighting/vim/syntax/meson.vim', encoding='utf-8') as f: res = re.search(r'syn keyword mesonBuiltin(\s+\\\s\w+)+', f.read(), re.MULTILINE) defined = set([a.strip() for a in res.group().split('\\')][1:]) self.assertEqual(defined, set(chain(interp.funcs.keys(), interp.builtin.keys()))) @@ -2163,7 +2154,7 @@ class BasePlatformTests(unittest.TestCase): if self.backend is not Backend.ninja: raise unittest.SkipTest(f'Compiler db not available with {self.backend.name} backend') try: - with open(os.path.join(self.builddir, 'compile_commands.json')) as ifile: + with open(os.path.join(self.builddir, 'compile_commands.json'), encoding='utf-8') as ifile: contents = json.load(ifile) except FileNotFoundError: raise unittest.SkipTest('Compiler db not found') @@ -2182,7 +2173,7 @@ class BasePlatformTests(unittest.TestCase): return contents def get_meson_log(self): - with open(os.path.join(self.builddir, 'meson-logs', 'meson-log.txt')) as f: + with open(os.path.join(self.builddir, 'meson-logs', 'meson-log.txt'), encoding='utf-8') as f: return f.readlines() def get_meson_log_compiler_checks(self): @@ -2726,7 +2717,7 @@ class AllPlatformTests(BasePlatformTests): expected[name] = 0 def read_logs(): # Find logged files and directories - with Path(self.builddir, 'meson-logs', 'install-log.txt').open() as f: + with Path(self.builddir, 'meson-logs', 'install-log.txt').open(encoding='utf-8') as f: return list(map(lambda l: Path(l.strip()), filter(lambda l: not l.startswith('#'), f.readlines()))) @@ -3410,7 +3401,7 @@ class AllPlatformTests(BasePlatformTests): def hg_init(project_dir): subprocess.check_call(['hg', 'init'], cwd=project_dir) - with open(os.path.join(project_dir, '.hg', 'hgrc'), 'w') as f: + with open(os.path.join(project_dir, '.hg', 'hgrc'), 'w', encoding='utf-8') as f: print('[ui]', file=f) print('username=Author Person <teh_coderz@example.com>', file=f) subprocess.check_call(['hg', 'add', 'meson.build', 'distexe.c'], cwd=project_dir) @@ -3451,7 +3442,7 @@ class AllPlatformTests(BasePlatformTests): def create_dummy_subproject(self, project_dir, name): path = os.path.join(project_dir, 'subprojects', name) os.makedirs(path) - with open(os.path.join(path, 'meson.build'), 'w') as ofile: + with open(os.path.join(path, 'meson.build'), 'w', encoding='utf-8') as ofile: ofile.write(f"project('{name}', version: '1.0')") return path @@ -3459,7 +3450,7 @@ class AllPlatformTests(BasePlatformTests): # Create this on the fly because having rogue .git directories inside # the source tree leads to all kinds of trouble. with tempfile.TemporaryDirectory() as project_dir: - with open(os.path.join(project_dir, 'meson.build'), 'w') as ofile: + with open(os.path.join(project_dir, 'meson.build'), 'w', encoding='utf-8') as ofile: ofile.write(textwrap.dedent('''\ project('disttest', 'c', version : '1.4.3') e = executable('distexe', 'distexe.c') @@ -3468,7 +3459,7 @@ class AllPlatformTests(BasePlatformTests): subproject('tarballsub', required : false) subproject('samerepo', required : false) ''')) - with open(os.path.join(project_dir, 'distexe.c'), 'w') as ofile: + with open(os.path.join(project_dir, 'distexe.c'), 'w', encoding='utf-8') as ofile: ofile.write(textwrap.dedent('''\ #include<stdio.h> @@ -3558,7 +3549,7 @@ class AllPlatformTests(BasePlatformTests): xz_checksumfile = xz_distfile + '.sha256sum' self.assertPathExists(xz_distfile) self.assertPathExists(xz_checksumfile) - tar = tarfile.open(xz_distfile, "r:xz") + tar = tarfile.open(xz_distfile, "r:xz") # [ignore encoding] self.assertEqual(sorted(['samerepo-1.0', 'samerepo-1.0/meson.build']), sorted([i.name for i in tar])) @@ -4092,12 +4083,12 @@ class AllPlatformTests(BasePlatformTests): # test directory with existing code file if lang in {'c', 'cpp', 'd'}: with tempfile.TemporaryDirectory() as tmpdir: - with open(os.path.join(tmpdir, 'foo.' + lang), 'w') as f: + with open(os.path.join(tmpdir, 'foo.' + lang), 'w', encoding='utf-8') as f: f.write('int main(void) {}') self._run(self.meson_command + ['init', '-b'], workdir=tmpdir) elif lang in {'java'}: with tempfile.TemporaryDirectory() as tmpdir: - with open(os.path.join(tmpdir, 'Foo.' + lang), 'w') as f: + with open(os.path.join(tmpdir, 'Foo.' + lang), 'w', encoding='utf-8') as f: f.write('public class Foo { public static void main() {} }') self._run(self.meson_command + ['init', '-b'], workdir=tmpdir) @@ -4489,7 +4480,7 @@ class AllPlatformTests(BasePlatformTests): with tempfile.TemporaryDirectory() as containing: with tempfile.TemporaryDirectory(dir=containing) as srcdir: mfile = os.path.join(srcdir, 'meson.build') - of = open(mfile, 'w') + of = open(mfile, 'w', encoding='utf-8') of.write("project('foobar', 'c')\n") of.close() pc = subprocess.run(self.setup_command, @@ -4614,7 +4605,7 @@ class AllPlatformTests(BasePlatformTests): # Create a file in builddir and verify wipe command removes it filename = os.path.join(self.builddir, 'something') - open(filename, 'w').close() + open(filename, 'w', encoding='utf-8').close() self.assertTrue(os.path.exists(filename)) out = self.init(testdir, extra_args=['--wipe', '-Dopt4=val4']) self.assertFalse(os.path.exists(filename)) @@ -4771,13 +4762,13 @@ class AllPlatformTests(BasePlatformTests): shutil.copyfile(badfile, testfile) shutil.copyfile(badheader, testheader) self.init(testdir) - self.assertNotEqual(Path(testfile).read_text(), - Path(goodfile).read_text()) - self.assertNotEqual(Path(testheader).read_text(), - Path(goodheader).read_text()) + self.assertNotEqual(Path(testfile).read_text(encoding='utf-8'), + Path(goodfile).read_text(encoding='utf-8')) + self.assertNotEqual(Path(testheader).read_text(encoding='utf-8'), + Path(goodheader).read_text(encoding='utf-8')) self.run_target('clang-format') - self.assertEqual(Path(testheader).read_text(), - Path(goodheader).read_text()) + self.assertEqual(Path(testheader).read_text(encoding='utf-8'), + Path(goodheader).read_text(encoding='utf-8')) finally: if os.path.exists(testfile): os.unlink(testfile) @@ -4837,7 +4828,7 @@ class AllPlatformTests(BasePlatformTests): infodir = os.path.join(self.builddir, 'meson-info') self.assertPathExists(infodir) - with open(os.path.join(infodir, 'intro-targets.json')) as fp: + with open(os.path.join(infodir, 'intro-targets.json'), encoding='utf-8') as fp: targets = json.load(fp) for i in targets: @@ -4946,7 +4937,7 @@ class AllPlatformTests(BasePlatformTests): for i in root_keylist: curr = os.path.join(infodir, 'intro-{}.json'.format(i[0])) self.assertPathExists(curr) - with open(curr) as fp: + with open(curr, encoding='utf-8') as fp: res[i[0]] = json.load(fp) assertKeyTypes(root_keylist, res) @@ -5068,7 +5059,7 @@ class AllPlatformTests(BasePlatformTests): for i in root_keylist: curr = os.path.join(infodir, f'intro-{i}.json') self.assertPathExists(curr) - with open(curr) as fp: + with open(curr, encoding='utf-8') as fp: res_file[i] = json.load(fp) self.assertEqual(res_all, res_file) @@ -5078,7 +5069,7 @@ class AllPlatformTests(BasePlatformTests): introfile = os.path.join(self.builddir, 'meson-info', 'meson-info.json') self.init(testdir) self.assertPathExists(introfile) - with open(introfile) as fp: + with open(introfile, encoding='utf-8') as fp: res1 = json.load(fp) for i in ['meson_version', 'directories', 'introspection', 'build_files_updated', 'error']: @@ -5092,7 +5083,7 @@ class AllPlatformTests(BasePlatformTests): introfile = os.path.join(self.builddir, 'meson-info', 'intro-buildoptions.json') self.init(testdir) self.assertPathExists(introfile) - with open(introfile) as fp: + with open(introfile, encoding='utf-8') as fp: res1 = json.load(fp) for i in res1: @@ -5110,7 +5101,7 @@ class AllPlatformTests(BasePlatformTests): self.setconf('-Dcpp_std=c++14') self.setconf('-Dbuildtype=release') - with open(introfile) as fp: + with open(introfile, encoding='utf-8') as fp: res2 = json.load(fp) self.assertListEqual(res1, res2) @@ -5121,7 +5112,7 @@ class AllPlatformTests(BasePlatformTests): introfile = os.path.join(self.builddir, 'meson-info', 'intro-targets.json') self.init(testdir) self.assertPathExists(introfile) - with open(introfile) as fp: + with open(introfile, encoding='utf-8') as fp: res_wb = json.load(fp) res_nb = self.introspect_directory(testfile, ['--targets'] + self.meson_args) @@ -5663,13 +5654,13 @@ class AllPlatformTests(BasePlatformTests): def test_cross_file_constants(self): with temp_filename() as crossfile1, temp_filename() as crossfile2: - with open(crossfile1, 'w') as f: + with open(crossfile1, 'w', encoding='utf-8') as f: f.write(textwrap.dedent( ''' [constants] compiler = 'gcc' ''')) - with open(crossfile2, 'w') as f: + with open(crossfile2, 'w', encoding='utf-8') as f: f.write(textwrap.dedent( ''' [constants] @@ -5699,7 +5690,7 @@ class AllPlatformTests(BasePlatformTests): upstream = os.path.join(srcdir, 'subprojects', 'wrap_git_upstream') upstream_uri = Path(upstream).as_uri() _git_init(upstream) - with open(os.path.join(srcdir, 'subprojects', 'wrap_git.wrap'), 'w') as f: + with open(os.path.join(srcdir, 'subprojects', 'wrap_git.wrap'), 'w', encoding='utf-8') as f: f.write(textwrap.dedent(''' [wrap-git] url = {} @@ -5723,7 +5714,7 @@ class AllPlatformTests(BasePlatformTests): def test_nostdlib(self): testdir = os.path.join(self.unit_test_dir, '78 nostdlib') machinefile = os.path.join(self.builddir, 'machine.txt') - with open(machinefile, 'w') as f: + with open(machinefile, 'w', encoding='utf-8') as f: f.write(textwrap.dedent(''' [properties] c_stdlib = 'mylibc' @@ -5752,7 +5743,7 @@ class AllPlatformTests(BasePlatformTests): os.makedirs(os.path.dirname(real_wrap)) # Invalid redirect, filename must have .wrap extension - with open(redirect_wrap, 'w') as f: + with open(redirect_wrap, 'w', encoding='utf-8') as f: f.write(textwrap.dedent(''' [wrap-redirect] filename = foo/subprojects/real.wrapper @@ -5761,7 +5752,7 @@ class AllPlatformTests(BasePlatformTests): PackageDefinition(redirect_wrap) # Invalid redirect, filename cannot be in parent directory - with open(redirect_wrap, 'w') as f: + with open(redirect_wrap, 'w', encoding='utf-8') as f: f.write(textwrap.dedent(''' [wrap-redirect] filename = ../real.wrap @@ -5770,7 +5761,7 @@ class AllPlatformTests(BasePlatformTests): PackageDefinition(redirect_wrap) # Invalid redirect, filename must be in foo/subprojects/real.wrap - with open(redirect_wrap, 'w') as f: + with open(redirect_wrap, 'w', encoding='utf-8') as f: f.write(textwrap.dedent(''' [wrap-redirect] filename = foo/real.wrap @@ -5779,12 +5770,12 @@ class AllPlatformTests(BasePlatformTests): wrap = PackageDefinition(redirect_wrap) # Correct redirect - with open(redirect_wrap, 'w') as f: + with open(redirect_wrap, 'w', encoding='utf-8') as f: f.write(textwrap.dedent(''' [wrap-redirect] filename = foo/subprojects/real.wrap ''')) - with open(real_wrap, 'w') as f: + with open(real_wrap, 'w', encoding='utf-8') as f: f.write(textwrap.dedent(''' [wrap-git] url = http://invalid @@ -5801,7 +5792,7 @@ class AllPlatformTests(BasePlatformTests): cmakefile = Path(testdir) / 'subprojects' / 'sub2' / 'CMakeLists.txt' self.init(testdir) self.build() - with cmakefile.open('a') as f: + with cmakefile.open('a', encoding='utf-8') as f: os.utime(str(cmakefile)) self.assertReconfiguredBuildIsNoop() @@ -5991,7 +5982,7 @@ class FailureTests(BasePlatformTests): ''' if langs is None: langs = [] - with open(self.mbuild, 'w') as f: + with open(self.mbuild, 'w', encoding='utf-8') as f: f.write("project('failure test', 'c', 'cpp'") if meson_version: f.write(f", meson_version: '{meson_version}'") @@ -6000,7 +5991,7 @@ class FailureTests(BasePlatformTests): f.write(f"add_languages('{lang}', required : false)\n") f.write(contents) if options is not None: - with open(self.moptions, 'w') as f: + with open(self.moptions, 'w', encoding='utf-8') as f: f.write(options) o = {'MESON_FORCE_BACKTRACE': '1'} if override_envvars is None: @@ -6017,7 +6008,7 @@ class FailureTests(BasePlatformTests): def obtainMesonOutput(self, contents, match, extra_args, langs, meson_version=None): if langs is None: langs = [] - with open(self.mbuild, 'w') as f: + with open(self.mbuild, 'w', encoding='utf-8') as f: f.write("project('output test', 'c', 'cpp'") if meson_version: f.write(f", meson_version: '{meson_version}'") @@ -6838,7 +6829,7 @@ class LinuxlikeTests(BasePlatformTests): } if is_osx() or is_haiku(): expected['Cflags'] = expected['Cflags'].replace('-pthread ', '') - with open(os.path.join(privatedir2, 'dependency-test.pc')) as f: + with open(os.path.join(privatedir2, 'dependency-test.pc'), encoding='utf-8') as f: matched_lines = 0 for line in f: parts = line.split(':', 1) @@ -6869,16 +6860,16 @@ class LinuxlikeTests(BasePlatformTests): self.assertEqual(out, ['-llibmain2', '-llibinternal']) # See common/44 pkgconfig-gen/meson.build for description of the case this test - with open(os.path.join(privatedir1, 'simple2.pc')) as f: + with open(os.path.join(privatedir1, 'simple2.pc'), encoding='utf-8') as f: content = f.read() self.assertIn('Libs: -L${libdir} -lsimple2 -lsimple1', content) self.assertIn('Libs.private: -lz', content) - with open(os.path.join(privatedir1, 'simple3.pc')) as f: + with open(os.path.join(privatedir1, 'simple3.pc'), encoding='utf-8') as f: content = f.read() self.assertEqual(1, content.count('-lsimple3')) - with open(os.path.join(privatedir1, 'simple5.pc')) as f: + with open(os.path.join(privatedir1, 'simple5.pc'), encoding='utf-8') as f: content = f.read() self.assertNotIn('-lstat2', content) @@ -6901,7 +6892,7 @@ class LinuxlikeTests(BasePlatformTests): def test_pkg_unfound(self): testdir = os.path.join(self.unit_test_dir, '23 unfound pkgconfig') self.init(testdir) - with open(os.path.join(self.privatedir, 'somename.pc')) as f: + with open(os.path.join(self.privatedir, 'somename.pc'), encoding='utf-8') as f: pcfile = f.read() self.assertFalse('blub_blob_blib' in pcfile) @@ -7421,7 +7412,7 @@ class LinuxlikeTests(BasePlatformTests): ('-L/me/fourth', '-lfoo4'), ('-lfoo3', '-lfoo4'), ] - with open(os.path.join(self.builddir, 'build.ninja')) as ifile: + with open(os.path.join(self.builddir, 'build.ninja'), encoding='utf-8') as ifile: for line in ifile: if expected_order[0][0] in line: for first, second in expected_order: @@ -7833,7 +7824,7 @@ class LinuxlikeTests(BasePlatformTests): ''' testdir = os.path.join(self.unit_test_dir, '43 dep order') self.init(testdir) - with open(os.path.join(self.builddir, 'build.ninja')) as bfile: + with open(os.path.join(self.builddir, 'build.ninja'), encoding='utf-8') as bfile: for line in bfile: if 'build myexe:' in line or 'build myexe.exe:' in line: self.assertIn('liblib1.a liblib2.a', line) @@ -7852,7 +7843,7 @@ class LinuxlikeTests(BasePlatformTests): rpathre = re.compile(r'-rpath,.*/subprojects/sub1.*-rpath,.*/subprojects/sub2') else: rpathre = re.compile(r'-rpath,\$\$ORIGIN/subprojects/sub1:\$\$ORIGIN/subprojects/sub2') - with open(os.path.join(self.builddir, 'build.ninja')) as bfile: + with open(os.path.join(self.builddir, 'build.ninja'), encoding='utf-8') as bfile: for line in bfile: if '-rpath' in line: self.assertRegex(line, rpathre) @@ -7865,7 +7856,7 @@ class LinuxlikeTests(BasePlatformTests): ''' testdir = os.path.join(self.src_root, 'test cases', 'native', '9 override with exe') self.init(testdir) - with open(os.path.join(self.builddir, 'build.ninja')) as bfile: + with open(os.path.join(self.builddir, 'build.ninja'), encoding='utf-8') as bfile: for line in bfile: if 'main1.c:' in line or 'main2.c:' in line: self.assertIn('| subprojects/sub/foobar', line) @@ -8206,7 +8197,7 @@ class LinuxlikeTests(BasePlatformTests): patch_filename = foo-patch.tar.xz patch_hash = {} """.format(source_filename, source_hash, patch_filename, patch_hash)) - with open(wrap_filename, 'w') as f: + with open(wrap_filename, 'w', encoding='utf-8') as f: f.write(wrap) self.init(testdir) self.build() @@ -8234,7 +8225,7 @@ class LinuxlikeTests(BasePlatformTests): # the 2nd dependency() to lookup on system. self.new_builddir() with tempfile.TemporaryDirectory() as d: - with open(os.path.join(d, 'meson.build'), 'w') as f: + with open(os.path.join(d, 'meson.build'), 'w', encoding='utf-8') as f: f.write(textwrap.dedent('''\ project('test') dependency('notfound', fallback: 'broken', required: false) @@ -8245,10 +8236,10 @@ class LinuxlikeTests(BasePlatformTests): def test_as_link_whole(self): testdir = os.path.join(self.unit_test_dir, '77 as link whole') self.init(testdir) - with open(os.path.join(self.privatedir, 'bar1.pc')) as f: + with open(os.path.join(self.privatedir, 'bar1.pc'), encoding='utf-8') as f: content = f.read() self.assertIn('-lfoo', content) - with open(os.path.join(self.privatedir, 'bar2.pc')) as f: + with open(os.path.join(self.privatedir, 'bar2.pc'), encoding='utf-8') as f: content = f.read() self.assertNotIn('-lfoo', content) @@ -8848,7 +8839,7 @@ class NativeFileTests(BasePlatformTests): """ filename = os.path.join(self.builddir, f'generated{self.current_config}.config') self.current_config += 1 - with open(filename, 'wt') as f: + with open(filename, 'wt', encoding='utf-8') as f: for section, entries in values.items(): f.write(f'[{section}]\n') for k, v in entries.items(): @@ -8870,7 +8861,7 @@ class NativeFileTests(BasePlatformTests): else: chbang = '#!/usr/bin/env python3' - with open(filename, 'wt') as f: + with open(filename, 'wt', encoding='utf-8') as f: f.write(textwrap.dedent('''\ {} import argparse @@ -8908,7 +8899,7 @@ class NativeFileTests(BasePlatformTests): # invoke python files itself, so instead we generate a .bat file, which # invokes our python wrapper batfile = os.path.join(self.builddir, f'binary_wrapper{self.current_wrapper}.bat') - with open(batfile, 'wt') as f: + with open(batfile, 'wt', encoding='utf-8') as f: f.write(fr'@{sys.executable} {filename} %*') return batfile @@ -8942,7 +8933,7 @@ class NativeFileTests(BasePlatformTests): wrapper = self.helper_create_binary_wrapper('bash', d, version='12345') def filler(): - with open(fifo, 'w') as f: + with open(fifo, 'w', encoding='utf-8') as f: f.write('[binaries]\n') f.write(f"bash = '{wrapper}'\n") @@ -9427,7 +9418,7 @@ class CrossFileTests(BasePlatformTests): testdir = os.path.join(self.unit_test_dir, '71 cross test passed') with tempfile.TemporaryDirectory() as d: p = Path(d) / 'crossfile' - with p.open('wt') as f: + with p.open('wt', encoding='utf-8') as f: f.write(self._cross_file_generator(needs_exe_wrapper=True)) self.init(testdir, extra_args=['--cross-file=' + str(p)]) out = self.run_target('test') @@ -9437,7 +9428,7 @@ class CrossFileTests(BasePlatformTests): testdir = os.path.join(self.unit_test_dir, '71 cross test passed') with tempfile.TemporaryDirectory() as d: p = Path(d) / 'crossfile' - with p.open('wt') as f: + with p.open('wt', encoding='utf-8') as f: f.write(self._cross_file_generator(needs_exe_wrapper=False)) self.init(testdir, extra_args=['--cross-file=' + str(p)]) out = self.run_target('test') @@ -9447,11 +9438,11 @@ class CrossFileTests(BasePlatformTests): testdir = os.path.join(self.unit_test_dir, '71 cross test passed') with tempfile.TemporaryDirectory() as d: s = Path(d) / 'wrapper.py' - with s.open('wt') as f: + with s.open('wt', encoding='utf-8') as f: f.write(self._stub_exe_wrapper()) s.chmod(0o774) p = Path(d) / 'crossfile' - with p.open('wt') as f: + with p.open('wt', encoding='utf-8') as f: f.write(self._cross_file_generator( needs_exe_wrapper=True, exe_wrapper=[str(s)])) @@ -9464,7 +9455,7 @@ class CrossFileTests(BasePlatformTests): testdir = os.path.join(self.unit_test_dir, '71 cross test passed') with tempfile.TemporaryDirectory() as d: p = Path(d) / 'crossfile' - with p.open('wt') as f: + with p.open('wt', encoding='utf-8') as f: f.write(self._cross_file_generator(needs_exe_wrapper=True)) self.init(testdir, extra_args=['--cross-file=' + str(p)]) @@ -9522,7 +9513,7 @@ class CrossFileTests(BasePlatformTests): """ filename = os.path.join(self.builddir, f'generated{self.current_config}.config') self.current_config += 1 - with open(filename, 'wt') as f: + with open(filename, 'wt', encoding='utf-8') as f: for section, entries in values.items(): f.write(f'[{section}]\n') for k, v in entries.items(): @@ -9996,7 +9987,7 @@ class SubprojectsCommandTests(BasePlatformTests): def _create_project(self, path, project_name='dummy'): os.makedirs(str(path), exist_ok=True) - with open(str(path / 'meson.build'), 'w') as f: + with open(str(path / 'meson.build'), 'w', encoding='utf-8') as f: f.write(f"project('{project_name}')") def _git(self, cmd, workdir): @@ -10058,7 +10049,7 @@ class SubprojectsCommandTests(BasePlatformTests): def _wrap_create_git(self, name, revision='master'): path = self.root_dir / name - with open(str((self.subprojects_dir / name).with_suffix('.wrap')), 'w') as f: + with open(str((self.subprojects_dir / name).with_suffix('.wrap')), 'w', encoding='utf-8') as f: f.write(textwrap.dedent( ''' [wrap-git] @@ -10068,7 +10059,7 @@ class SubprojectsCommandTests(BasePlatformTests): def _wrap_create_file(self, name, tarball='dummy.tar.gz'): path = self.root_dir / tarball - with open(str((self.subprojects_dir / name).with_suffix('.wrap')), 'w') as f: + with open(str((self.subprojects_dir / name).with_suffix('.wrap')), 'w', encoding='utf-8') as f: f.write(textwrap.dedent( f''' [wrap-file] diff --git a/tools/ac_converter.py b/tools/ac_converter.py index 6c72f8d..c3b954c 100755 --- a/tools/ac_converter.py +++ b/tools/ac_converter.py @@ -371,7 +371,7 @@ if len(sys.argv) != 2: print(help_message.format(sys.argv[0])) sys.exit(0) -with open(sys.argv[1]) as f: +with open(sys.argv[1], encoding='utf-8') as f: for line in f: line = line.strip() arr = line.split() diff --git a/tools/boost_names.py b/tools/boost_names.py index 89926ec..b716ccb 100755 --- a/tools/boost_names.py +++ b/tools/boost_names.py @@ -78,7 +78,7 @@ class BoostModule(): def get_boost_version() -> T.Optional[str]: - raw = jamroot.read_text() + raw = jamroot.read_text(encoding='utf-8') m = re.search(r'BOOST_VERSION\s*:\s*([0-9\.]+)\s*;', raw) if m: return m.group(1) @@ -91,7 +91,7 @@ def get_libraries(jamfile: Path) -> T.List[BoostLibrary]: # - compiler flags libs: T.List[BoostLibrary] = [] - raw = jamfile.read_text() + raw = jamfile.read_text(encoding='utf-8') raw = re.sub(r'#.*\n', '\n', raw) # Remove comments raw = re.sub(r'\s+', ' ', raw) # Force single space raw = re.sub(r'}', ';', raw) # Cheat code blocks by converting } to ; @@ -185,7 +185,7 @@ def process_lib_dir(ldir: Path) -> T.List[BoostModule]: libs = get_libraries(bjam_file) # Extract metadata - data = json.loads(meta_file.read_text()) + data = json.loads(meta_file.read_text(encoding='utf-8')) if not isinstance(data, list): data = [data] diff --git a/tools/cmake2meson.py b/tools/cmake2meson.py index 4185b5a..a12d9cf 100755 --- a/tools/cmake2meson.py +++ b/tools/cmake2meson.py @@ -280,13 +280,13 @@ class Converter: subdir = self.cmake_root cfile = Path(subdir).expanduser() / 'CMakeLists.txt' try: - with cfile.open() as f: + with cfile.open(encoding='utf-8') as f: cmakecode = f.read() except FileNotFoundError: print('\nWarning: No CMakeLists.txt in', subdir, '\n', file=sys.stderr) return p = Parser(cmakecode) - with (subdir / 'meson.build').open('w') as outfile: + with (subdir / 'meson.build').open('w', encoding='utf-8') as outfile: for t in p.parse(): if t.name == 'add_subdirectory': # print('\nRecursing to subdir', @@ -300,7 +300,7 @@ class Converter: def write_options(self) -> None: filename = self.cmake_root / 'meson_options.txt' - with filename.open('w') as optfile: + with filename.open('w', encoding='utf-8') as optfile: for o in self.options: (optname, description, default) = o if default is None: diff --git a/tools/dircondenser.py b/tools/dircondenser.py index 9d642d2..2a726df 100755 --- a/tools/dircondenser.py +++ b/tools/dircondenser.py @@ -54,11 +54,11 @@ def get_entries() -> T.List[T.Tuple[int, str]]: return entries def replace_source(sourcefile: str, replacements: T.List[T.Tuple[str, str]]) -> None: - with open(sourcefile) as f: + with open(sourcefile, encoding='utf-8') as f: contents = f.read() for old_name, new_name in replacements: contents = contents.replace(old_name, new_name) - with open(sourcefile, 'w') as f: + with open(sourcefile, 'w', encoding='utf-8') as f: f.write(contents) def condense(dirname: str) -> None: diff --git a/tools/gen_data.py b/tools/gen_data.py index 9affc66..b1c62e0 100755 --- a/tools/gen_data.py +++ b/tools/gen_data.py @@ -107,7 +107,7 @@ def main() -> int: def write_once(self, path: Path) -> None: if not path.exists(): - path.write_text(self.data) + path.write_text(self.data, encoding='utf-8') def write_to_private(self, env: 'Environment') -> Path: out_file = Path(env.scratch_dir) / 'data' / self.path.name @@ -133,7 +133,7 @@ def main() -> int: ''') print(f'Updating {out_file}') - out_file.write_text(data) + out_file.write_text(data, encoding='utf-8') return 0 if __name__ == '__main__': diff --git a/tools/regenerate_docs.py b/tools/regenerate_docs.py index f5ce77b..2029314 100755 --- a/tools/regenerate_docs.py +++ b/tools/regenerate_docs.py @@ -114,13 +114,13 @@ def generate_hotdoc_includes(root_dir: Path, output_dir: Path) -> None: for cmd, parsed in cmd_data.items(): for typ in parsed.keys(): - with open(output_dir / (cmd+'_'+typ+'.inc'), 'w') as f: + with open(output_dir / (cmd+'_'+typ+'.inc'), 'w', encoding='utf-8') as f: f.write(parsed[typ]) def generate_wrapdb_table(output_dir: Path) -> None: url = urlopen('https://wrapdb.mesonbuild.com/v2/releases.json') releases = json.loads(url.read().decode()) - with open(output_dir / 'wrapdb-table.md', 'w') as f: + with open(output_dir / 'wrapdb-table.md', 'w', encoding='utf-8') as f: f.write('| Project | Versions | Provided dependencies | Provided programs |\n') f.write('| ------- | -------- | --------------------- | ----------------- |\n') for name, info in releases.items(): @@ -147,7 +147,7 @@ def regenerate_docs(output_dir: PathLike, generate_wrapdb_table(output_dir) if dummy_output_file: - with open(output_dir/dummy_output_file, 'w') as f: + with open(output_dir/dummy_output_file, 'w', encoding='utf-8') as f: f.write('dummy file for custom_target output') if __name__ == '__main__': diff --git a/tools/run_with_cov.py b/tools/run_with_cov.py index 17fb300..3f78efc 100755 --- a/tools/run_with_cov.py +++ b/tools/run_with_cov.py @@ -28,9 +28,9 @@ from mesonbuild import mesonlib def generate_coveragerc() -> Path: i_file = (root_path / 'data' / '.coveragerc.in') o_file = (root_path / '.coveragerc') - raw = i_file.read_text() + raw = i_file.read_text(encoding='utf-8') raw = raw.replace('@ROOT@', root_path.as_posix()) - o_file.write_text(raw) + o_file.write_text(raw, encoding='utf-8') return o_file def main() -> int: |