Skip to content

Commit 6c2cdf2

Browse files
committed
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.
1 parent f28c10a commit 6c2cdf2

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

cmd2.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@
7373
except ImportError:
7474
import subprocess
7575

76+
# Python 3.4 and earlier require contextlib2 for temporarily redirecting stderr and stdout
77+
if sys.version_info < (3, 5):
78+
from contextlib2 import redirect_stdout, redirect_stderr
79+
else:
80+
from contextlib import redirect_stdout, redirect_stderr
81+
7682
# Detect whether IPython is installed to determine if the built-in "ipy" command should be included
7783
ipython_available = True
7884
try:
@@ -92,12 +98,8 @@
9298
# BrokenPipeError is only in Python 3. Use IOError for Python 2.
9399
if six.PY3:
94100
BROKEN_PIPE_ERROR = BrokenPipeError
95-
96-
# redirect_stdout and redirect_stderr weren't added to contextlib until Python 3.4
97-
from contextlib import redirect_stdout, redirect_stderr
98101
else:
99102
BROKEN_PIPE_ERROR = IOError
100-
from contextlib2 import redirect_stdout, redirect_stderr
101103

102104
# On some systems, pyperclip will import gtk for its clipboard functionality.
103105
# The following code is a workaround for gtk interfering with printing from a background

setup.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,13 @@
6767
if sys.platform.startswith('win'):
6868
INSTALL_REQUIRES += ['pyreadline']
6969

70-
# Python 2.7 also requires contextlib2 for temporarily redirecting stdout and stderr and subprocess32
70+
# Python 3.4 and earlier require contextlib2 for temporarily redirecting stderr and stdout
71+
if sys.version_info < (3, 5):
72+
INSTALL_REQUIRES += ['contextlib2']
73+
74+
# Python 2.7 also requires subprocess32
7175
if sys.version_info < (3, 0):
72-
INSTALL_REQUIRES += ['contextlib2', 'subprocess32']
76+
INSTALL_REQUIRES += ['subprocess32']
7377

7478
# unittest.mock was added in Python 3.3. mock is a backport of unittest.mock to all versions of Python
7579
TESTS_REQUIRE = ['mock', 'pytest']

0 commit comments

Comments
 (0)