summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2018-01-22 00:33:26 -0500
committerTodd Leonhardt <todd.leonhardt@gmail.com>2018-01-22 00:33:26 -0500
commit6c2cdf229dd62a957d7e27533725a9cc013db22e (patch)
treeb426400b79068713c75dc281112d7c80da16e328
parentf28c10a50535f753419bd2120ac6cb0bea9f56e2 (diff)
downloadcmd2-git-6c2cdf229dd62a957d7e27533725a9cc013db22e.tar.gz
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.
-rwxr-xr-xcmd2.py10
-rwxr-xr-xsetup.py8
2 files changed, 12 insertions, 6 deletions
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
diff --git a/setup.py b/setup.py
index aea5ab92..58f8e4cd 100755
--- a/setup.py
+++ b/setup.py
@@ -67,9 +67,13 @@ INSTALL_REQUIRES = ['pyparsing >= 2.0.1', 'pyperclip', 'six']
if sys.platform.startswith('win'):
INSTALL_REQUIRES += ['pyreadline']
-# Python 2.7 also requires contextlib2 for temporarily redirecting stdout and stderr and subprocess32
+# Python 3.4 and earlier require contextlib2 for temporarily redirecting stderr and stdout
+if sys.version_info < (3, 5):
+ INSTALL_REQUIRES += ['contextlib2']
+
+# Python 2.7 also requires subprocess32
if sys.version_info < (3, 0):
- INSTALL_REQUIRES += ['contextlib2', 'subprocess32']
+ INSTALL_REQUIRES += ['subprocess32']
# unittest.mock was added in Python 3.3. mock is a backport of unittest.mock to all versions of Python
TESTS_REQUIRE = ['mock', 'pytest']