diff --git a/mesonbuild/mesonmain.py b/mesonbuild/mesonmain.py index 0fbc717f8b28..fe7dd39447be 100644 --- a/mesonbuild/mesonmain.py +++ b/mesonbuild/mesonmain.py @@ -217,7 +217,7 @@ def run_script_command(script_name, script_args): return 1 def ensure_stdout_accepts_unicode(): - if sys.stdout.encoding and not sys.stdout.encoding.upper().startswith('UTF-'): + if sys.stdout is not None and sys.stdout.encoding and not sys.stdout.encoding.upper().startswith('UTF-'): sys.stdout.reconfigure(errors='surrogateescape') def set_meson_command(mainfile): diff --git a/mesonbuild/mlog.py b/mesonbuild/mlog.py index a8b0185371d1..3a4e4b7fb414 100644 --- a/mesonbuild/mlog.py +++ b/mesonbuild/mlog.py @@ -49,6 +49,9 @@ def _windows_ansi() -> bool: return bool(kernel.SetConsoleMode(stdout, mode.value | 0x4) or os.environ.get('ANSICON')) def colorize_console() -> bool: + if sys.stdout is None: + return False + _colorize_console: bool = getattr(sys.stdout, 'colorize_console', None) if _colorize_console is not None: return _colorize_console diff --git a/mesonbuild/utils/universal.py b/mesonbuild/utils/universal.py index 952d6f7b1588..fe16acecfa1e 100644 --- a/mesonbuild/utils/universal.py +++ b/mesonbuild/utils/universal.py @@ -1512,7 +1512,7 @@ def Popen_safe(args: T.List[str], write: T.Optional[str] = None, stdin = subprocess.PIPE try: - if not sys.stdout.encoding or encoding.upper() != 'UTF-8': + if (sys.stdout is not None and not sys.stdout.encoding) or encoding.upper() != 'UTF-8': p, o, e = Popen_safe_legacy(args, write=write, stdin=stdin, stdout=stdout, stderr=stderr, **kwargs) else: p = subprocess.Popen(args, universal_newlines=True, encoding=encoding, close_fds=False, @@ -1542,7 +1542,7 @@ def Popen_safe_legacy(args: T.List[str], write: T.Optional[str] = None, input_ = write.encode('utf-8') o, e = p.communicate(input_) if o is not None: - if sys.stdout.encoding is not None: + if sys.stdout is not None and sys.stdout.encoding is not None: o = o.decode(encoding=sys.stdout.encoding, errors='replace').replace('\r\n', '\n') else: o = o.decode(errors='replace').replace('\r\n', '\n')