Skip to content

Commit 40b2007

Browse files
committed
Move check_output implementation to future.backports.misc
1 parent a8af55c commit 40b2007

File tree

4 files changed

+22
-28
lines changed

4 files changed

+22
-28
lines changed

future/backports/misc.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"""
1010

1111
from math import ceil as oldceil
12+
import subprocess
1213

1314
from future.utils import iteritems, PY26
1415

@@ -488,3 +489,17 @@ def __and__(self, other):
488489
OrderedDict = _OrderedDict
489490
Counter = _Counter
490491

492+
493+
# For Python 2.6 compatibility: see http://stackoverflow.com/questions/4814970/
494+
def check_output(*popenargs, **kwargs):
495+
if 'stdout' in kwargs:
496+
raise ValueError('stdout argument not allowed, it will be overridden.')
497+
process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs)
498+
output, unused_err = process.communicate()
499+
retcode = process.poll()
500+
if retcode:
501+
cmd = kwargs.get("args")
502+
if cmd is None:
503+
cmd = popenargs[0]
504+
raise subprocess.CalledProcessError(retcode, cmd)
505+
return output

future/moves/subprocess.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,4 @@
88
from commands import getoutput, getstatusoutput
99

1010
if PY26:
11-
def check_output(*popenargs, **kwargs):
12-
if 'stdout' in kwargs:
13-
raise ValueError('stdout argument not allowed, it will be overridden.')
14-
process = Popen(stdout=PIPE, *popenargs, **kwargs)
15-
output, unused_err = process.communicate()
16-
retcode = process.poll()
17-
if retcode:
18-
cmd = kwargs.get("args")
19-
if cmd is None:
20-
cmd = popenargs[0]
21-
raise CalledProcessError(retcode, cmd)
22-
return output
11+
from future.backports.misc import check_output

future/standard_library/__init__.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@
197197
('base64', 'decodebytes','base64', 'decodestring'),
198198
('subprocess', 'getoutput', 'commands', 'getoutput'),
199199
('subprocess', 'getstatusoutput', 'commands', 'getstatusoutput'),
200+
('subprocess', 'check_output', 'future.backports.misc', 'check_output'),
200201
('math', 'ceil', 'future.backports.misc', 'ceil'),
201202
('collections', 'OrderedDict', 'future.backports.misc', 'OrderedDict'),
202203
('collections', 'Counter', 'future.backports.misc', 'Counter'),
@@ -297,11 +298,6 @@ def _find_and_load_module(self, name, path=None):
297298
flog.debug('What to do here?')
298299

299300
name = bits[0]
300-
# We no longer use the fake module six.moves:
301-
# if name == 'moves':
302-
# # imp.find_module doesn't find this fake module
303-
# from future.utils.six import moves
304-
# return moves
305301
module_info = imp.find_module(name, path)
306302
return imp.load_module(name, *module_info)
307303

@@ -316,7 +312,7 @@ class hooks(object):
316312
>>> from future import standard_library
317313
>>> with standard_library.hooks():
318314
... import http.client
319-
>>> import requests # incompatible with ``future``'s standard library hooks
315+
>>> import requests
320316
321317
For this to work, http.client will be scrubbed from sys.modules after the
322318
'with' block. That way the modules imported in the 'with' block will

future/tests/base.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import tempfile
33
import unittest
44
import sys
5-
import subprocess
65
import re
76
import warnings
87
import io
@@ -13,12 +12,7 @@
1312
import unittest2 as unittest
1413

1514
from future.utils import bind_method, PY26, PY3, PY2
16-
17-
18-
# For Python 2.6 compatibility: see http://stackoverflow.com/questions/4814970/
19-
if "check_output" not in dir(subprocess): # duck punch it in!
20-
from future.moves.subprocess import check_output
21-
subprocess.check_output = check_output
15+
from future.moves.subprocess import check_output, STDOUT
2216

2317

2418
def reformat_code(code):
@@ -282,15 +276,15 @@ def _futurize_test_script(self, filename='mytestscript.py', stages=(1, 2),
282276
params.append('--conservative')
283277
# No extra params needed
284278

285-
output = subprocess.check_output([sys.executable, script] + params +
279+
output = check_output([sys.executable, script] + params +
286280
['-w', self.tempdir + filename],
287-
stderr=subprocess.STDOUT)
281+
stderr=STDOUT)
288282
return output
289283

290284
def _run_test_script(self, filename='mytestscript.py',
291285
interpreter=sys.executable):
292286
env = {'PYTHONPATH': os.getcwd()}
293-
return subprocess.check_output([interpreter, self.tempdir + filename],
287+
return check_output([interpreter, self.tempdir + filename],
294288
env=env)
295289

296290

0 commit comments

Comments
 (0)