From 3fc499bca18454b9f432b9b0106cab662bfeb549 Mon Sep 17 00:00:00 2001 From: Ammar Askar Date: Wed, 6 Sep 2017 02:41:30 -0400 Subject: bpo-31178: Avoid concatenating bytes with str in subprocess error (#3066) Avoid concatenating bytes with str in the typically rare subprocess error path (exec failed). Includes a mock based unittest to exercise the codepath. --- Lib/subprocess.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'Lib/subprocess.py') diff --git a/Lib/subprocess.py b/Lib/subprocess.py index bafb501fcf..25e5698493 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -1313,15 +1313,18 @@ class Popen(object): try: exception_name, hex_errno, err_msg = ( errpipe_data.split(b':', 2)) + # The encoding here should match the encoding + # written in by the subprocess implementations + # like _posixsubprocess + err_msg = err_msg.decode() except ValueError: exception_name = b'SubprocessError' hex_errno = b'0' - err_msg = (b'Bad exception data from child: ' + - repr(errpipe_data)) + err_msg = 'Bad exception data from child: {!r}'.format( + bytes(errpipe_data)) child_exception_type = getattr( builtins, exception_name.decode('ascii'), SubprocessError) - err_msg = err_msg.decode(errors="surrogatepass") if issubclass(child_exception_type, OSError) and hex_errno: errno_num = int(hex_errno, 16) child_exec_never_called = (err_msg == "noexec") -- cgit v1.2.1