Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Do N SVG->PNG conversions with inkscape before shutting down the inks…

…cape server and starting a new one to prevent memory leaks in Inkscape from becoming problematic.
  • Loading branch information...
commit 9086ef1a1c86e9f96d4d01893bfc13ac73596be8 1 parent a074823
@mdboom mdboom authored
Showing with 29 additions and 16 deletions.
  1. +29 −16 lib/matplotlib/testing/compare.py
View
45 lib/matplotlib/testing/compare.py
@@ -112,8 +112,34 @@ def convert(*args):
converter['eps'] = make_external_conversion_command(cmd)
if matplotlib.checkdep_inkscape() is not None:
- def make_svg_converter():
- def read_to_end(buf):
+ class SVGConverter:
+ def __init__(self):
+ self._count = 0
+ self._process = None
+
+ def get_process(self):
+ # Since Inkscape can leak a little memory, we run X
+ # conversions and then shut it down and start up a new
+ # Inkscape.
+ if self._count == 0:
+ if self._process is not None:
+ self._process.communicate('quit\n')
+ self._process = subprocess.Popen(['inkscape', '-z', '--shell'],
+ stdin=subprocess.PIPE,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.STDOUT)
+ self.read_to_end(self._process.stdout)
+ self._count = 10
+ self._count -= 1
+ return self._process
+
+ def __call__(self, old, new):
+ process = self.get_process()
+ process.stdin.write('%s --export-png=%s\n' % (old, new))
+ process.stdin.flush()
+ self.read_to_end(process.stdout)
+
+ def read_to_end(self, buf):
ret = ''
lastchar = ''
while True:
@@ -124,20 +150,7 @@ def read_to_end(buf):
lastchar = char
return ret
- p = subprocess.Popen(['inkscape', '--shell'],
- stdin=subprocess.PIPE,
- stdout=subprocess.PIPE,
- stderr=subprocess.STDOUT)
- read_to_end(p.stdout)
-
- def convert_svg(old, new):
- p.stdin.write('%s --export-png=%s\n' % (old, new))
- p.stdin.flush()
- read_to_end(p.stdout)
-
- return convert_svg
-
- converter['svg'] = make_svg_converter()
+ converter['svg'] = SVGConverter()
def comparable_formats():
'''Returns the list of file formats that compare_images can compare
Please sign in to comment.
Something went wrong with that request. Please try again.