From bd677835874aa4f74d115644d60526098d5104be Mon Sep 17 00:00:00 2001 From: "Jens H. Nielsen" Date: Fri, 11 Jul 2014 17:52:56 -0500 Subject: [PATCH 1/7] Rewrite checks to use communicate. This ensures that file handles are cloesed --- lib/matplotlib/__init__.py | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py index b787b5a9a057..d11b9d1c9817 100644 --- a/lib/matplotlib/__init__.py +++ b/lib/matplotlib/__init__.py @@ -343,8 +343,9 @@ def checkdep_dvipng(): try: s = subprocess.Popen(['dvipng','-version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) - line = s.stdout.readlines()[1] - v = line.split()[-1].decode('ascii') + stdout, stderr = s.communicate() + line = stdout.decode('ascii').split('\n')[1] + v = line.split()[-1] return v except (IndexError, ValueError, OSError): return None @@ -371,7 +372,8 @@ def checkdep_tex(): try: s = subprocess.Popen(['tex','-version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) - line = s.stdout.readlines()[0].decode('ascii') + stdout, stderr = s.communicate() + line = stdout.decode('ascii').split('\n')[0] pattern = '3\.1\d+' match = re.search(pattern, line) v = match.group(0) @@ -383,9 +385,10 @@ def checkdep_pdftops(): try: s = subprocess.Popen(['pdftops','-v'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) - for line in s.stderr: - if b'version' in line: - v = line.split()[-1].decode('ascii') + stdout, stderr = s.communicate() + if b'version' in stderr: + line = stderr.decode('ascii').split('\n')[0] + v = line.split()[-1] return v except (IndexError, ValueError, UnboundLocalError, OSError): return None @@ -394,10 +397,9 @@ def checkdep_inkscape(): try: s = subprocess.Popen(['inkscape','-V'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) - for line in s.stdout: - if b'Inkscape' in line: - v = line.split()[1].decode('ascii') - break + stdout, stderr = s.communicate() + if b'Inkscape' in stdout: + v = stdout.split()[1].decode('ascii') return v except (IndexError, ValueError, UnboundLocalError, OSError): return None @@ -406,10 +408,10 @@ def checkdep_xmllint(): try: s = subprocess.Popen(['xmllint','--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) - for line in s.stderr: - if b'version' in line: - v = line.split()[-1].decode('ascii') - break + stdout, stderr = s.communicate() + if b'version' in stderr: + line = stderr.decode('ascii').split('\n')[0] + v = line.split()[-1] return v except (IndexError, ValueError, UnboundLocalError, OSError): return None From 04536ea788e965f77189948cafdd4273698c60c4 Mon Sep 17 00:00:00 2001 From: "Jens H. Nielsen" Date: Sat, 12 Jul 2014 09:38:18 -0500 Subject: [PATCH 2/7] Silence some more cloesed file warnings --- lib/matplotlib/backends/backend_ps.py | 9 +++++---- lib/matplotlib/tests/test_backend_ps.py | 4 +++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/matplotlib/backends/backend_ps.py b/lib/matplotlib/backends/backend_ps.py index 066cb00e1092..eaebf15bf4ae 100644 --- a/lib/matplotlib/backends/backend_ps.py +++ b/lib/matplotlib/backends/backend_ps.py @@ -90,12 +90,13 @@ def gs_version(self): pass from matplotlib.compat.subprocess import Popen, PIPE - pipe = Popen(self.gs_exe + " --version", - shell=True, stdout=PIPE).stdout + s = Popen(self.gs_exe + " --version", + shell=True, stdout=PIPE) + pipe, stderr = s.communicate() if six.PY3: - ver = pipe.read().decode('ascii') + ver = pipe.decode('ascii') else: - ver = pipe.read() + ver = pipe gs_version = tuple(map(int, ver.strip().split("."))) self._cached["gs_version"] = gs_version diff --git a/lib/matplotlib/tests/test_backend_ps.py b/lib/matplotlib/tests/test_backend_ps.py index 1b6aac6e37e5..fc82b6a53d55 100644 --- a/lib/matplotlib/tests/test_backend_ps.py +++ b/lib/matplotlib/tests/test_backend_ps.py @@ -40,6 +40,8 @@ def _test_savefig_to_stringio(format='ps'): assert values[0] == values[1] assert values[1] == values[2].replace(b'\r\n', b'\n') + for buffer in buffers: + buffer.close() @cleanup @@ -80,4 +82,4 @@ def test_savefig_to_stringio_with_usetex_eps(): if __name__ == '__main__': import nose - nose.runmodule(argv=['-s', '--with-doctest'], exit=False) + nose.runmodule(argv=['-s', '--with-doctest', '-verbose'], exit=False) From 78e7531f2ff662b505f73ec6caba95b129535a4c Mon Sep 17 00:00:00 2001 From: "Jens H. Nielsen" Date: Sat, 12 Jul 2014 10:11:49 -0500 Subject: [PATCH 3/7] Ensure that file is closed in texmanager --- lib/matplotlib/texmanager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/texmanager.py b/lib/matplotlib/texmanager.py index 51758fe6851e..240155b489c7 100644 --- a/lib/matplotlib/texmanager.py +++ b/lib/matplotlib/texmanager.py @@ -70,10 +70,10 @@ def dvipng_hack_alpha(): try: p = Popen(['dvipng', '-version'], stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=(sys.platform != 'win32')) + stdin, stderr = p.communicate() except OSError: mpl.verbose.report('No dvipng was found', 'helpful') return False - stdin, stdout = p.stdin, p.stdout for line in stdout: if line.startswith(b'dvipng '): version = line.split()[-1] From dfcbe975675050a84e7a5bfd0b015154e52a0e52 Mon Sep 17 00:00:00 2001 From: "Jens H. Nielsen" Date: Sat, 12 Jul 2014 10:18:55 -0500 Subject: [PATCH 4/7] Remove leftover debug --- lib/matplotlib/tests/test_backend_ps.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_backend_ps.py b/lib/matplotlib/tests/test_backend_ps.py index fc82b6a53d55..93aca46e28c1 100644 --- a/lib/matplotlib/tests/test_backend_ps.py +++ b/lib/matplotlib/tests/test_backend_ps.py @@ -82,4 +82,4 @@ def test_savefig_to_stringio_with_usetex_eps(): if __name__ == '__main__': import nose - nose.runmodule(argv=['-s', '--with-doctest', '-verbose'], exit=False) + nose.runmodule(argv=['-s', '--with-doctest'], exit=False) From cd3b7750268902e878375e2721cff3ee8be5e9a0 Mon Sep 17 00:00:00 2001 From: "Jens H. Nielsen" Date: Sat, 12 Jul 2014 14:12:31 -0500 Subject: [PATCH 5/7] Correct stdout in dvipng_hack --- lib/matplotlib/texmanager.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/texmanager.py b/lib/matplotlib/texmanager.py index 240155b489c7..8ff931a0d410 100644 --- a/lib/matplotlib/texmanager.py +++ b/lib/matplotlib/texmanager.py @@ -70,16 +70,16 @@ def dvipng_hack_alpha(): try: p = Popen(['dvipng', '-version'], stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=(sys.platform != 'win32')) - stdin, stderr = p.communicate() + stdout, stderr = p.communicate() except OSError: mpl.verbose.report('No dvipng was found', 'helpful') return False - for line in stdout: - if line.startswith(b'dvipng '): + lines = stdout.decode('ascii').split('\n') + for line in lines: + if line.startswith('dvipng '): version = line.split()[-1] mpl.verbose.report('Found dvipng version %s' % version, 'helpful') - version = version.decode('ascii') version = distutils.version.LooseVersion(version) return version < distutils.version.LooseVersion('1.6') mpl.verbose.report('Unexpected response from dvipng -version', 'helpful') From 5728b56dae83919f9ce48ca48e9eff5825fe7297 Mon Sep 17 00:00:00 2001 From: "Jens H. Nielsen" Date: Sun, 13 Jul 2014 09:13:55 -0500 Subject: [PATCH 6/7] Change checks to more closly match the original --- lib/matplotlib/__init__.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py index d11b9d1c9817..220ae10f74c8 100644 --- a/lib/matplotlib/__init__.py +++ b/lib/matplotlib/__init__.py @@ -386,9 +386,10 @@ def checkdep_pdftops(): s = subprocess.Popen(['pdftops','-v'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = s.communicate() - if b'version' in stderr: - line = stderr.decode('ascii').split('\n')[0] - v = line.split()[-1] + lines = stderr.decode('ascii').split('\n') + for line in lines: + if 'version' in line: + v = line.split()[-1] return v except (IndexError, ValueError, UnboundLocalError, OSError): return None @@ -398,8 +399,10 @@ def checkdep_inkscape(): s = subprocess.Popen(['inkscape','-V'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = s.communicate() - if b'Inkscape' in stdout: - v = stdout.split()[1].decode('ascii') + lines = stdout.decode('ascii').split('\n') + for line in lines: + if 'Inkscape' in line: + v = line.split()[1] return v except (IndexError, ValueError, UnboundLocalError, OSError): return None @@ -409,9 +412,10 @@ def checkdep_xmllint(): s = subprocess.Popen(['xmllint','--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = s.communicate() - if b'version' in stderr: - line = stderr.decode('ascii').split('\n')[0] - v = line.split()[-1] + lines = stderr.decode('ascii').split('\n') + for line in lines: + if 'version' in line: + v = line.split()[-1] return v except (IndexError, ValueError, UnboundLocalError, OSError): return None From 461264eb5255eac57506069914a4c57cb0cbb67b Mon Sep 17 00:00:00 2001 From: "Jens H. Nielsen" Date: Sun, 13 Jul 2014 09:16:57 -0500 Subject: [PATCH 7/7] Add breaks to match original --- lib/matplotlib/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py index 220ae10f74c8..25d7e04f3103 100644 --- a/lib/matplotlib/__init__.py +++ b/lib/matplotlib/__init__.py @@ -403,6 +403,7 @@ def checkdep_inkscape(): for line in lines: if 'Inkscape' in line: v = line.split()[1] + break return v except (IndexError, ValueError, UnboundLocalError, OSError): return None @@ -416,6 +417,7 @@ def checkdep_xmllint(): for line in lines: if 'version' in line: v = line.split()[-1] + break return v except (IndexError, ValueError, UnboundLocalError, OSError): return None