From 6c2cdf229dd62a957d7e27533725a9cc013db22e Mon Sep 17 00:00:00 2001 From: Todd Leonhardt Date: Mon, 22 Jan 2018 00:33:26 -0500 Subject: redirect_stderr wasn't added to contextlib until Python 3.5 So it turns out that we need contextlib2 for Python 3.4 and earlier. --- cmd2.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'cmd2.py') diff --git a/cmd2.py b/cmd2.py index c397bb7c..e77f4557 100755 --- a/cmd2.py +++ b/cmd2.py @@ -73,6 +73,12 @@ try: except ImportError: import subprocess +# Python 3.4 and earlier require contextlib2 for temporarily redirecting stderr and stdout +if sys.version_info < (3, 5): + from contextlib2 import redirect_stdout, redirect_stderr +else: + from contextlib import redirect_stdout, redirect_stderr + # Detect whether IPython is installed to determine if the built-in "ipy" command should be included ipython_available = True try: @@ -92,12 +98,8 @@ except ImportError: # BrokenPipeError is only in Python 3. Use IOError for Python 2. if six.PY3: BROKEN_PIPE_ERROR = BrokenPipeError - - # redirect_stdout and redirect_stderr weren't added to contextlib until Python 3.4 - from contextlib import redirect_stdout, redirect_stderr else: BROKEN_PIPE_ERROR = IOError - from contextlib2 import redirect_stdout, redirect_stderr # On some systems, pyperclip will import gtk for its clipboard functionality. # The following code is a workaround for gtk interfering with printing from a background -- cgit v1.2.1