From d12cc0ac80b4e4521874856429aa14ab00a2a73a Mon Sep 17 00:00:00 2001 From: IvanHrasko Date: Wed, 8 Jul 2015 12:25:03 +0200 Subject: [PATCH] ihrasko: open stdout and stderr in text mode and avoid type errors in Python 3 during tests --- test/test.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/test/test.py b/test/test.py index 0258c3a..1399995 100644 --- a/test/test.py +++ b/test/test.py @@ -46,9 +46,22 @@ def fetch_url(src, dst): def exe(command): + """ + Executes command and returns string representations of stdout and stderr captured from the console. + When universal_newlines=True stdout and stderr are opened in text mode. + Otherwise, they are opened in binary mode. In that case captured stdout and stderr + are not strings and Python 3 throws type error when compared against strings later in tests. + + Note: + This feature is only available if Python is built with universal newline support (the default). + Also, the newlines attribute of the file objects stdout, stdin and stderr are not updated by the + communicate() method. + See https://docs.python.org/2/library/subprocess.html + """ stdout, stderr = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE, - stderr=subprocess.PIPE).communicate() + stderr=subprocess.PIPE, + universal_newlines=True).communicate() return stdout, stderr # ------------------------------------------------------------------------------