Skip to content

Commit b319900

Browse files
authored
Merge pull request #203 from python-cmd2/buffer_size
Pipe Buffer Size
2 parents 256e069 + c5f9ca9 commit b319900

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

cmd2.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@
8282
except ImportError:
8383
pass
8484

85+
# BrokenPipeError is only in Python 3. Use IOError for Python 2.
86+
if six.PY3:
87+
BROKEN_PIPE_ERROR = BrokenPipeError
88+
else:
89+
BROKEN_PIPE_ERROR = IOError
90+
8591
__version__ = '0.7.6'
8692

8793
# Pyparsing enablePackrat() can greatly speed up parsing, but problems have been seen in Python 3 in the past
@@ -539,6 +545,11 @@ def __init__(self, completekey='tab', stdin=None, stdout=None, use_ipython=False
539545
# Used when piping command output to a shell command
540546
self.pipe_proc = None
541547

548+
# Size of the stdin/stdout buffers used when piping command output to a shell command via self.stdout.
549+
# This should be increased in cases where large amounts of data are expected to be piped to the shell
550+
# to prevent blocking when writing to self.stdout.
551+
self._pipe_buffer_size = io.DEFAULT_BUFFER_SIZE
552+
542553
# ----- Methods related to presenting output to the user -----
543554

544555
@property
@@ -575,7 +586,7 @@ def poutput(self, msg, end='\n'):
575586
self.stdout.write(msg_str)
576587
if not msg_str.endswith(end):
577588
self.stdout.write(end)
578-
except BrokenPipeError:
589+
except BROKEN_PIPE_ERROR:
579590
# This occurs if a command's output is being piped to another process and that process closes before the
580591
# command is finished. We intentionally don't print a warning message here since we know that stdout
581592
# will be restored by the _restore_output() method. If you would like your application to print a
@@ -805,9 +816,9 @@ def _redirect_output(self, statement):
805816

806817
# Open each side of the pipe and set stdout accordingly
807818
# noinspection PyTypeChecker
808-
self.stdout = io.open(write_fd, write_mode)
819+
self.stdout = io.open(write_fd, write_mode, buffering=self._pipe_buffer_size)
809820
# noinspection PyTypeChecker
810-
subproc_stdin = io.open(read_fd, read_mode)
821+
subproc_stdin = io.open(read_fd, read_mode, buffering=self._pipe_buffer_size)
811822

812823
# We want Popen to raise an exception if it fails to open the process. Thus we don't set shell to True.
813824
try:
@@ -852,7 +863,7 @@ def _restore_output(self, statement):
852863
try:
853864
# Close the file or pipe that stdout was redirected to
854865
self.stdout.close()
855-
except BrokenPipeError:
866+
except BROKEN_PIPE_ERROR:
856867
pass
857868
finally:
858869
# Restore self.stdout
@@ -2352,7 +2363,7 @@ def __nonzero__(self):
23522363
if __name__ == '__main__':
23532364
# If run as the main application, simply start a bare-bones cmd2 application with only built-in functionality.
23542365

2355-
# Set "use_iptyhon" to True to include the ipy command if IPython is installed, which supports advanced interactive
2366+
# Set "use_ipython" to True to include the ipy command if IPython is installed, which supports advanced interactive
23562367
# debugging of your application via introspection on self.
23572368
app = Cmd(use_ipython=False)
23582369
app.cmdloop()

0 commit comments

Comments
 (0)