8000 Added new module, matplotlib.subprocess_fixed, as a replacement for s… · ivanov/matplotlib@0755aa6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0755aa6

Browse files
committed
Added new module, matplotlib.subprocess_fixed, as a replacement for subprocess.
cbook: Moved check_output to subprocess_fixed. backend_pgf: Import check_output from subprocess_fixed instead of cbook.
1 parent 4703f2f commit 0755aa6

File tree

3 files changed

+65
-44
lines changed

3 files changed

+65
-44
lines changed

lib/matplotlib/backends/backend_pgf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from matplotlib import font_manager
2222
from matplotlib.ft2font import FT2Font
2323
from matplotlib.cbook import is_string_like, is_writable_file_like
24-
from matplotlib.cbook import check_output
24+
from matplotlib.subprocess_fixed import check_output
2525

2626

2727
###############################################################################

lib/matplotlib/cbook.py

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import locale
1818
import os
1919
import re
20-
import subprocess
2120
import sys
2221
import threading
2322
import time
@@ -1795,45 +1794,3 @@ def get_instancemethod(self):
17951794
else:
17961795
def _putmask(a, mask, values):
17971796
return np.copyto(a, values, where=mask)
1798-
1799-
1800-
def _check_output(*popenargs, **kwargs):
1801-
r"""Run command with arguments and return its output as a byte
1802-
string.
1803-
1804-
If the exit code was non-zero it raises a CalledProcessError. The
1805-
CalledProcessError object will have the return code in the
1806-
returncode
1807-
attribute and output in the output attribute.
1808-
1809-
The arguments are the same as for the Popen constructor. Example::
1810-
1811-
>>> check_output(["ls", "-l", "/dev/null"])
1812-
'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n'
1813-
1814-
The stdout argument is not allowed as it is used internally.
1815-
To capture standard error in the result, use stderr=STDOUT.::
1816-
1817-
>>> check_output(["/bin/sh", "-c",
1818-
... "ls -l non_existent_file ; exit 0"],
1819-
... stderr=STDOUT)
1820-
'ls: non_existent_file: No such file or directory\n'
1821-
"""
1822-
if 'stdout' in kwargs:
1823-
raise ValueError('stdout argument not allowed, it will be overridden.')
1824-
process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs)
1825-
output, unused_err = process.communicate()
1826-
retcode = process.poll()
1827-
if retcode:
1828-
cmd = kwargs.get("args")
1829-
if cmd is None:
1830-
cmd = popenargs[0]
1831-
raise subprocess.CalledProcessError(retcode, cmd, output=output)
1832-
return output
1833-
1834-
1835-
# python2.7's subprocess provides a check_output method
1836-
if hasattr(subprocess, 'check_output'):
1837-
check_output = subprocess.check_output
1838-
else:
1839-
check_output = _check_output

lib/matplotlib/subprocess_fixed.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
"""
2+
A replacement wrapper around the subprocess module, with a number of
3+
work-arounds:
4+
- Provides the check_output function (which subprocess only provides from Python
5+
2.7 onwards).
6+
7+
Instead of importing subprocess, other modules should use this as follows:
8+
9+
import subprocess_fixed as subprocess
10+
11+
This module is safe to import from anywhere within matplotlib.
12+
"""
13+
14+
from __future__ import print_function
15+
16+
import subprocess
17+
18+
__all__ = ['Popen', 'PIPE', 'STDOUT', 'check_output']
19+
20+
Popen = subprocess.Popen
21+
PIPE = subprocess.PIPE
22+
STDOUT = subprocess.STDOUT
23+
24+
25+
def _check_output(*popenargs, **kwargs):
26+
r"""Run command with arguments and return its output as a byte
27+
string.
28+
29+
If the exit code was non-zero it raises a CalledProcessError. The
30+
CalledProcessError object will have the return code in the
31+
returncode
32+
attribute and output in the output attribute.
33+
34+
The arguments are the same as for the Popen constructor. Example::
35+
36+
>>> check_output(["ls", "-l", "/dev/null"])
37+
'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n'
38+
39+
The stdout argument is not allowed as it is used internally.
40+
To capture standard error in the result, use stderr=STDOUT.::
41+
42+
>>> check_output(["/bin/sh", "-c",
43+
... "ls -l non_existent_file ; exit 0"],
44+
... stderr=STDOUT)
45+
'ls: non_existent_file: No such file or directory\n'
46+
"""
47+
if 'stdout' in kwargs:
48+
raise ValueError('stdout argument not allowed, it will be overridden.')
49+
process = Popen(stdout=PIPE, *popenargs, **kwargs)
50+
output, unused_err = process.communicate()
51+
retcode = process.poll()
52+
if retcode:
53+
cmd = kwargs.get("args")
54+
if cmd is None:
55+
cmd = popenargs[0]
56+
raise subprocess.CalledProcessError(retcode, cmd, output=output)
57+
return output
58+
59+
60+
# python2.7's subprocess provides a check_output method
61+
if hasattr(subprocess, 'check_output'):
62+
check_output = subprocess.check_output
63+
else:
64+
check_output = _check_output

0 commit comments

Comments
 (0)
0