|
82 | 82 | except ImportError:
|
83 | 83 | pass
|
84 | 84 |
|
| 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 | + |
85 | 91 | __version__ = '0.7.6'
|
86 | 92 |
|
87 | 93 | # 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
|
539 | 545 | # Used when piping command output to a shell command
|
540 | 546 | self.pipe_proc = None
|
541 | 547 |
|
| 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 | + |
542 | 553 | # ----- Methods related to presenting output to the user -----
|
543 | 554 |
|
544 | 555 | @property
|
@@ -575,7 +586,7 @@ def poutput(self, msg, end='\n'):
|
575 | 586 | self.stdout.write(msg_str)
|
576 | 587 | if not msg_str.endswith(end):
|
577 | 588 | self.stdout.write(end)
|
578 |
| - except BrokenPipeError: |
| 589 | + except BROKEN_PIPE_ERROR: |
579 | 590 | # This occurs if a command's output is being piped to another process and that process closes before the
|
580 | 591 | # command is finished. We intentionally don't print a warning message here since we know that stdout
|
581 | 592 | # 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):
|
805 | 816 |
|
806 | 817 | # Open each side of the pipe and set stdout accordingly
|
807 | 818 | # 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) |
809 | 820 | # 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) |
811 | 822 |
|
812 | 823 | # We want Popen to raise an exception if it fails to open the process. Thus we don't set shell to True.
|
813 | 824 | try:
|
@@ -852,7 +863,7 @@ def _restore_output(self, statement):
|
852 | 863 | try:
|
853 | 864 | # Close the file or pipe that stdout was redirected to
|
854 | 865 | self.stdout.close()
|
855 |
| - except BrokenPipeError: |
| 866 | + except BROKEN_PIPE_ERROR: |
856 | 867 | pass
|
857 | 868 | finally:
|
858 | 869 | # Restore self.stdout
|
@@ -2352,7 +2363,7 @@ def __nonzero__(self):
|
2352 | 2363 | if __name__ == '__main__':
|
2353 | 2364 | # If run as the main application, simply start a bare-bones cmd2 application with only built-in functionality.
|
2354 | 2365 |
|
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 |
2356 | 2367 | # debugging of your application via introspection on self.
|
2357 | 2368 | app = Cmd(use_ipython=False)
|
2358 | 2369 | app.cmdloop()
|
0 commit comments