From af47fee4f3c0ea59106e5cb6137263a94177d773 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Thu, 5 Dec 2019 20:27:43 -0500 Subject: Enabled line buffering when redirecting output to a file --- cmd2/cmd2.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'cmd2/cmd2.py') diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 753fb5ab..127a8bac 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -1874,22 +1874,26 @@ class Cmd(cmd.Cmd): self.perror("Cannot redirect to paste buffer; missing 'pyperclip' and/or pyperclip dependencies") redir_error = True + # Redirecting to a file elif statement.output_to: - # going to a file - mode = 'w' - # statement.output can only contain - # REDIRECTION_APPEND or REDIRECTION_OUTPUT + # statement.output can only contain REDIRECTION_APPEND or REDIRECTION_OUTPUT if statement.output == constants.REDIRECTION_APPEND: mode = 'a' + else: + mode = 'w' + mode += 't' + try: - new_stdout = open(utils.strip_quotes(statement.output_to), mode) + # Use line buffering + new_stdout = open(utils.strip_quotes(statement.output_to), mode=mode, buffering=1) saved_state.redirecting = True sys.stdout = self.stdout = new_stdout except OSError as ex: self.pexcept('Failed to redirect because - {}'.format(ex)) redir_error = True + + # Redirecting to a paste buffer else: - # going to a paste buffer new_stdout = tempfile.TemporaryFile(mode="w+") saved_state.redirecting = True sys.stdout = self.stdout = new_stdout -- cgit v1.2.1 From 8862e278cf30b9bb09e19c5e13024cf6f6d70825 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Fri, 6 Dec 2019 12:28:05 -0500 Subject: Removed redundant line of code --- cmd2/cmd2.py | 1 - 1 file changed, 1 deletion(-) (limited to 'cmd2/cmd2.py') diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 127a8bac..bd581919 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -1881,7 +1881,6 @@ class Cmd(cmd.Cmd): mode = 'a' else: mode = 'w' - mode += 't' try: # Use line buffering -- cgit v1.2.1