Skip to content

Commit 9086ef1

Browse files
committed
Do N SVG->PNG conversions with inkscape before shutting down the inkscape server and starting a new one to prevent memory leaks in Inkscape from becoming problematic.
1 parent a074823 commit 9086ef1

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

lib/matplotlib/testing/compare.py

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,34 @@ def convert(*args):
112112
converter['eps'] = make_external_conversion_command(cmd)
113113

114114
if matplotlib.checkdep_inkscape() is not None:
115-
def make_svg_converter():
116-
def read_to_end(buf):
115+
class SVGConverter:
116+
def __init__(self):
117+
self._count = 0
118+
self._process = None
119+
120+
def get_process(self):
121+
# Since Inkscape can leak a little memory, we run X
122+
# conversions and then shut it down and start up a new
123+
# Inkscape.
124+
if self._count == 0:
125+
if self._process is not None:
126+
self._process.communicate('quit\n')
127+
self._process = subprocess.Popen(['inkscape', '-z', '--shell'],
128+
stdin=subprocess.PIPE,
129+
stdout=subprocess.PIPE,
130+
stderr=subprocess.STDOUT)
131+
self.read_to_end(self._process.stdout)
132+
self._count = 10
133+
self._count -= 1
134+
return self._process
135+
136+
def __call__(self, old, new):
137+
process = self.get_process()
138+
process.stdin.write('%s --export-png=%s\n' % (old, new))
139+
process.stdin.flush()
140+
self.read_to_end(process.stdout)
141+
142+
def read_to_end(self, buf):
117143
ret = ''
118144
lastchar = ''
119145
while True:
@@ -124,20 +150,7 @@ def read_to_end(buf):
124150
lastchar = char
125151
return ret
126152

127-
p = subprocess.Popen(['inkscape', '--shell'],
128-
stdin=subprocess.PIPE,
129-
stdout=subprocess.PIPE,
130-
stderr=subprocess.STDOUT)
131-
read_to_end(p.stdout)
132-
133-
def convert_svg(old, new):
134-
p.stdin.write('%s --export-png=%s\n' % (old, new))
135-
p.stdin.flush()
136-
read_to_end(p.stdout)
137-
138-
return convert_svg
139-
140-
converter['svg'] = make_svg_converter()
153+
converter['svg'] = SVGConverter()
141154

142155
def comparable_formats():
143156
'''Returns the list of file formats that compare_images can compare

0 commit comments

Comments
 (0)