From d505aa138a2ab0adb85fe249fec7b551c84ceed1 Mon Sep 17 00:00:00 2001 From: Jack O'Connor Date: Tue, 4 Nov 2014 09:06:56 -0800 Subject: [PATCH] unify newlines in subprocess output Summary: Because all asyncio subprocess output is read in binary mode, we don't get universal newlines for free. But it's the right thing to do, because we do all our printing with strings in text mode, which translates "\n" back into the platform-appropriate line separator. So for example, "\r\n" in a string on Windows will become "\r\r\n" when it gets printed. This function ensures that all newlines are represented as "\n" internally, which solves that problem and also helps our tests work on Windows. Right now we only handle Windows, but we can expand this if there's ever another newline style we have to support. Reviewers: sean Differential Revision: https://phabricator.buildinspace.com/D114 --- peru/async.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/peru/async.py b/peru/async.py index 016c610..254043e 100644 --- a/peru/async.py +++ b/peru/async.py @@ -74,8 +74,9 @@ def create_subprocess_with_handle(command, display_handle, *, shell=False, cwd, if not outputbytes: break outputstr = decoder.decode(outputbytes) - display_handle.write(outputstr) - output_copy.write(outputstr) + outputstr_unified = _unify_newlines(outputstr) + display_handle.write(outputstr_unified) + output_copy.write(outputstr_unified) returncode = yield from proc.wait() @@ -88,3 +89,17 @@ def create_subprocess_with_handle(command, display_handle, *, shell=False, cwd, assert not decoder.buffer, 'decoder nonempty: ' + repr(decoder.buffer) return output_copy.getvalue() + + +def _unify_newlines(s): + r'''Because all asyncio subprocess output is read in binary mode, we don't + get universal newlines for free. But it's the right thing to do, because we + do all our printing with strings in text mode, which translates "\n" back + into the platform-appropriate line separator. So for example, "\r\n" in a + string on Windows will become "\r\r\n" when it gets printed. This function + ensures that all newlines are represented as "\n" internally, which solves + that problem and also helps our tests work on Windows. Right now we only + handle Windows, but we can expand this if there's ever another newline + style we have to support.''' + + return s.replace('\r\n', '\n')