Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e3cc885

Browse files
committed
Inline MovieWriter._frame_sink.
It plays two fairly different roles in MovieWriter (it's the stdin of the long-standing ffmpeg-like subprocess) and FileMovieWriter (it's the ever-changing handle of the frame currently being saved), with different life-cycles and no reuse between the subclasses, so it's easier to just inline it at the call site (and skip a comment about how FileMovieWriter._frame_sink gets closed in grab_frame, hinting at the tight-coupling between the two of them). Also, no need to close the process stdin after calling communicate() -- communicate() does it for us.
1 parent 7c813db commit e3cc885

File tree

1 file changed

+10
-27
lines changed

1 file changed

+10
-27
lines changed

lib/matplotlib/animation.py

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -344,21 +344,16 @@ def grab_frame(self, **savefig_kwargs):
344344
# All frames must have the same size to save the movie correctly.
345345
self.fig.set_size_inches(self._w, self._h)
346346
# Save the figure data to the sink, using the frame format and dpi.
347-
self.fig.savefig(self._frame_sink(), format=self.frame_format,
347+
self.fig.savefig(self._proc.stdin, format=self.frame_format,
348348
dpi=self.dpi, **savefig_kwargs)
349349

350-
def _frame_sink(self):
351-
"""Return the place to which frames should be written."""
352-
return self._proc.stdin
353-
354350
def _args(self):
355351
"""Assemble list of encoder-specific command-line arguments."""
356352
return NotImplementedError("args needs to be implemented by subclass.")
357353

358354
def cleanup(self):
359355
"""Clean-up and collect the process used to write the movie file."""
360356
out, err = self._proc.communicate()
361-
self._frame_sink().close()
362357
# Use the encoding/errors that universal_newlines would use.
363358
out = TextIOWrapper(BytesIO(out)).read()
364359
err = TextIOWrapper(BytesIO(err)).read()
@@ -474,30 +469,18 @@ def _base_temp_name(self):
474469
# for extension and the prefix.
475470
return self.fname_format_str % (self.temp_prefix, self.frame_format)
476471

477-
def _frame_sink(self):
478-
# Creates a filename for saving using the basename and the current
479-
# counter.
480-
path = Path(self._base_temp_name() % self._frame_counter)
481-
482-
# Save the filename so we can delete it later if necessary
483-
self._temp_paths.append(path)
484-
_log.debug('FileMovieWriter.frame_sink: saving frame %d to path=%s',
485-
self._frame_counter, path)
486-
self._frame_counter += 1 # Ensures each created name is 'unique'
487-
488-
# This file returned here will be closed once it's used by savefig()
489-
# because it will no longer be referenced and will be gc-ed.
490-
return open(path, 'wb')
491-
492472
def grab_frame(self, **savefig_kwargs):
493473
# docstring inherited
494474
# Overloaded to explicitly close temp file.
495-
_log.debug('MovieWriter.grab_frame: Grabbing frame.')
496-
# Tell the figure to save its data to the sink, using the
497-
# frame format and dpi.
498-
with self._frame_sink() as myframesink:
499-
self.fig.savefig(myframesink, format=self.frame_format,
500-
dpi=self.dpi, **savefig_kwargs)
475+
# Creates a filename for saving using basename and counter.
476+
path = Path(self._base_temp_name() % self._frame_counter)
477+
self._temp_paths.append(path) # Record the filename for later cleanup.
478+
self._frame_counter += 1 # Ensures each created name is unique.
479+
_log.debug('FileMovieWriter.grab_frame: Grabbing frame %d to path=%s',
480+
self._frame_counter, path)
481+
with open(path, 'wb') as sink: # Save figure to the sink.
482+
self.fig.savefig(sink, format=self.frame_format, dpi=self.dpi,
483+
**savefig_kwargs)
501484

502485
def finish(self):
503486
# Call run here now that all frame grabbing is done. All temp files

0 commit comments

Comments
 (0)