Skip to content

Commit

Permalink
fix stdout assumptions
Browse files Browse the repository at this point in the history
stdout, just like stderr, may be None which would cause a heap of problems
see 02733bc
  • Loading branch information
Jan200101 committed Feb 2, 2024
1 parent 6fcf863 commit 93f1bb0
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
2 changes: 1 addition & 1 deletion mesonbuild/mesonmain.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 3 additions & 0 deletions mesonbuild/mlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions mesonbuild/utils/universal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit 93f1bb0

Please sign in to comment.