|
17 | 17 | import locale |
18 | 18 | import os |
19 | 19 | import re |
20 | | -import subprocess |
21 | 20 | import sys |
22 | 21 | import threading |
23 | 22 | import time |
@@ -1212,19 +1211,34 @@ def restrict_dict(d, keys): |
1212 | 1211 |
|
1213 | 1212 | def report_memory(i=0): # argument may go away |
1214 | 1213 | 'return the memory consumed by process' |
1215 | | - from subprocess import Popen, PIPE |
| 1214 | + from matplotlib.compat.subprocess import Popen, PIPE |
1216 | 1215 | pid = os.getpid() |
1217 | 1216 | if sys.platform == 'sunos5': |
1218 | | - a2 = Popen('ps -p %d -o osz' % pid, shell=True, |
1219 | | - stdout=PIPE).stdout.readlines() |
| 1217 | + try: |
| 1218 | + a2 = Popen('ps -p %d -o osz' % pid, shell=True, |
| 1219 | + stdout=PIPE).stdout.readlines() |
| 1220 | + except OSError: |
| 1221 | + raise NotImplementedError( |
| 1222 | + "report_memory works on Sun OS only if " |
| 1223 | + "the 'ps' program is found") |
1220 | 1224 | mem = int(a2[-1].strip()) |
1221 | 1225 | elif sys.platform.startswith('linux'): |
1222 | | - a2 = Popen('ps -p %d -o rss,sz' % pid, shell=True, |
1223 | | - stdout=PIPE).stdout.readlines() |
| 1226 | + try: |
| 1227 | + a2 = Popen('ps -p %d -o rss,sz' % pid, shell=True, |
| 1228 | + stdout=PIPE).stdout.readlines() |
| 1229 | + except OSError: |
| 1230 | + raise NotImplementedError( |
| 1231 | + "report_memory works on Linux only if " |
| 1232 | + "the 'ps' program is found") |
1224 | 1233 | mem = int(a2[1].split()[1]) |
1225 | 1234 | elif sys.platform.startswith('darwin'): |
1226 | | - a2 = Popen('ps -p %d -o rss,vsz' % pid, shell=True, |
1227 | | - stdout=PIPE).stdout.readlines() |
| 1235 | + try: |
| 1236 | + a2 = Popen('ps -p %d -o rss,vsz' % pid, shell=True, |
| 1237 | + stdout=PIPE).stdout.readlines() |
| 1238 | + except OSError: |
| 1239 | + raise NotImplementedError( |
| 1240 | + "report_memory works on Mac OS only if " |
| 1241 | + "the 'ps' program is found") |
1228 | 1242 | mem = int(a2[1].split()[0]) |
1229 | 1243 | elif sys.platform.startswith('win'): |
1230 | 1244 | try: |
@@ -1795,45 +1809,3 @@ def get_instancemethod(self): |
1795 | 1809 | else: |
1796 | 1810 | def _putmask(a, mask, values): |
1797 | 1811 | 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 |
0 commit comments