From b650ccda72102bf8581420cfa31f6c6eab67f65d Mon Sep 17 00:00:00 2001 From: Ulf Magnusson Date: Tue, 24 Jul 2018 19:50:15 +0200 Subject: Use universal newlines mode in $(shell) implementation This prevents e.g. stray \r's in command output on Windows after stripping trailing newlines. --- kconfiglib.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'kconfiglib.py') diff --git a/kconfiglib.py b/kconfiglib.py index e5db25c..18baa83 100644 --- a/kconfiglib.py +++ b/kconfiglib.py @@ -5639,14 +5639,26 @@ def _error_if_fn(kconf, args): return "" def _shell_fn(kconf, args): - stdout, stderr = subprocess.Popen( - args[1], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE - ).communicate() + # Use universal newlines mode to prevent e.g. stray \r's in command output + # on Windows - if not _IS_PY2: + if _IS_PY2: + # No decoding on Python 2 + stdout, stderr = subprocess.Popen( + args[1], shell=True, + stdout=subprocess.PIPE, stderr=subprocess.PIPE, + universal_newlines=True + ).communicate() + + else: + # Passing universal_newlines=True and/or 'encoding' on Python 3 turns + # on decoding of the output (bytes -> str), which might fail try: - stdout = stdout.decode(kconf._encoding) - stderr = stderr.decode(kconf._encoding) + stdout, stderr = subprocess.Popen( + args[1], shell=True, + stdout=subprocess.PIPE, stderr=subprocess.PIPE, + universal_newlines=True, encoding=kconf._encoding + ).communicate() except UnicodeDecodeError as e: _decoding_error(e, kconf._filename, kconf._linenr) -- cgit v1.2.3