From 3e396b3782813d36d46195564cd0e111422bcaf5 Mon Sep 17 00:00:00 2001
From: Daniel Mensinger <daniel@mensinger-ka.de>
Date: Tue, 22 Jun 2021 22:59:16 +0200
Subject: fix: Always explicitly set encoding for text files (fixes #8263)

---
 mesonbuild/compilers/compilers.py    | 4 ++--
 mesonbuild/compilers/cs.py           | 2 +-
 mesonbuild/compilers/cuda.py         | 2 +-
 mesonbuild/compilers/d.py            | 2 +-
 mesonbuild/compilers/fortran.py      | 2 +-
 mesonbuild/compilers/java.py         | 2 +-
 mesonbuild/compilers/mixins/clike.py | 2 +-
 mesonbuild/compilers/rust.py         | 2 +-
 mesonbuild/compilers/swift.py        | 2 +-
 9 files changed, 10 insertions(+), 10 deletions(-)

(limited to 'mesonbuild/compilers')

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)
-- 
cgit v1.1