From 450b7b37ef92fa529d07c9d348aaae4a5c036f9f Mon Sep 17 00:00:00 2001 From: Todd Leonhardt Date: Mon, 30 Jan 2017 22:10:27 -0500 Subject: A more complete fix for the subprocess.check_call() blocking issue addressed in PR #25. --- cmd2.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/cmd2.py b/cmd2.py index 08fab0bd..38cf7bb1 100755 --- a/cmd2.py +++ b/cmd2.py @@ -208,6 +208,7 @@ to be installed on operating system. %s""" if sys.platform == "win32": + # Running on Windows try: import win32clipboard @@ -234,11 +235,15 @@ if sys.platform == "win32": write_to_paste_buffer = get_paste_buffer elif sys.platform == 'darwin': + # Running on Mac OS X can_clip = False try: + # Warning: subprocess.call() and subprocess.check_call() should never be called with stdout=PIPE or stderr=PIPE + # because the child process will block if it generates enough output to a pipe to fill up the OS pipe buffer. + # Starting with Python 3.5 there is a newer, safer API based on the run() function. + # test for pbcopy - AFAIK, should always be installed on MacOS - subprocess.check_call('pbcopy -help', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, - stderr=subprocess.PIPE) + subprocess.check_call('pbcopy -help', shell=True, stdin=subprocess.PIPE, stdout=None, stderr=None) can_clip = True except (subprocess.CalledProcessError, OSError, IOError): pass @@ -261,10 +266,10 @@ elif sys.platform == 'darwin': write_to_paste_buffer = get_paste_buffer else: + # Running on Linux can_clip = False try: - subprocess.check_call('xclip -o -sel clip', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, - stderr=subprocess.PIPE) + subprocess.check_call('xclip -o -sel clip', shell=True, stdin=subprocess.PIPE, stdout=None, stderr=None) can_clip = True except AttributeError: # check_call not defined, Python < 2.5 try: -- cgit v1.2.1