-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Use tempfile.mkdtemp for animation.FileMovieWriter. #3536
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Currently, `animation.FileMovieWriter` (e.g. `FFMpegFileWriter`) creates temporaries in the current folder, without even checking whether the file existed before. This is obviously risky (e.g., create a file named `_tmp0000000.png` in the current folder before running `moviewriter.py` from the docs examples and it will be overwritten. Moveover, temporaries are not cleaned up if an exception is raised (e.g., KeyboardInterrupt). This is relatively easy to fix using `tempfile.mkdtemp`, although I had to modify `finish` so that it doesn't call `cleanup` by itself (obviously, this can be fixed by keeping a `cleaned_up` attribute but it feels like a minor API change is preferable; moreover the context-manager API stays the same). Only basic, manual testing done.
Thanks. I would also suggest using the streaming writing, it is much faster and avoids these problems. |
I seem to remember that there were some issues with temporary files on windows @dopplershift probably remembers better than me. |
@@ -193,7 +199,6 @@ def _run(self): | |||
|
|||
def finish(self): | |||
'Finish any processing for writing the movie.' | |||
self.cleanup() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this even valid? Doesn't there have to at least be a "pass" statement? Plus, I don't like the idea of a function that is documented to finish something actually not do anything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is valid(!) Moreover it is not that easy to write code with the proper behavior without making this change (because in case of failure you want to call cleanup
without calling finish
).
In my opinion the finish
and cleanup
methods should be private anyways. I can also rename the new ones _finish
and _cleanup
, have the saving
contextmanager just call the private methods, and deprecate the wrappers finish
and cleanup
which keep the previous behavior... What do you think?
re: Using streaming writing: it doesn't support |
Sorry to derail the issue a bit, but why do you need I suspect that (with out really knowing the problem) you would be better off a) sizing the figure properly and b) using PS how is ffmpeg re-assembling non-uniformily shaped files later? I assume it is just re-sampling to what ever size it wants? |
I am not using |
I like the feature, thanks for writing it up. I have no problems outside the aforementioned isinstance() checks. Regarding @jenshnielsen I don't remember anything about temp files on windows, but I'm not a windows guy. |
Ok I just remember that someone had some issues with the animations at Scipy which I think was on windows but I can't remember any details so I am fine with these changes apart from your comments. |
The issue at Scipy was
8000
that the image-magick convert.exe executable On Thu, Sep 18, 2014 at 4:04 PM, Jens Hedegaard Nielsen <
|
The old setup, finish and cleanup methods interact poorly with the context-manager approach anyways so the saving context-manager now calls _setup, _finish and _cleanup. The saving context-manager now supports keyword-arguments, e.g. clear_temp. Do not overload the _temp_names attribute anymore. Minor layout changes.
The new commit takes into account most suggestions. It deprecates the old non-context-manager API though, in order to keep things reasonably simple. |
I don't have time to see why at the moment, but it seems that this PR is causing test failures. |
Should be fixed now. |
I'll let this settle for a bit, but I'm good with as this stands. |
Can you add text to the deprecation warnings to the effect 'deprecated in 1.5 and will be removed in 1.6'? |
I am also in favor of using |
I'm all for super but I had a PR some time ago where I was discouraged from using it... is there any "official" policy about it? |
I think I was the one doing the discouraging. The issue (iircc) there was that only a sub-set of the classes in the classes family were getting, here there are few enough classes and little enough overriding that superifying everything isn't so bad. |
68c8c9e
to
6ffb7bc
Compare
@@ -316,7 +351,8 @@ def _frame_sink(self): | |||
fname = self._base_temp_name() % self._frame_counter | |||
|
|||
# Save the filename so we can delete it later if necessary | |||
self._temp_names.append(fname) | |||
if isinstance(self._temp_names, list): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you do this test as self._temp_names is not None
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feel free to do so. See below.
I have lost interest in this issue as I ended up just rewriting my own wrapper to ffmpeg for my use case. Feel free to close it, amend it or merge it. |
@anntzer Any interest in revisiting this PR? |
I don't really care anymore, as I have my own wrapper now: https://gist.github.com/anntzer/e0186a16b392b4abf3e1 Note that this directly takes |
bump on this. |
Honestly I think the whole thing should just be rewritten to pipe the RGBA bytes to ffmpeg's (or whatever process is used) stdin. Feel free to close this, I won't revisit it (plus, #5454 made rebasing not so trivial). |
That is what http://matplotlib.org/api/animation_api.html#matplotlib.animation.FFMpegWriter and friends do. I think the file-base version exist either for back-compatibility or some use case where writing to stdin does not work. |
Fair enough. Still not enough to convince me to fix the rebase :-) |
Closing this as the rebase has become pretty nasty due to other changes to |
Currently,
animation.FileMovieWriter
(e.g.FFMpegFileWriter
)creates temporaries in the current folder, without even checking
whether the file existed before. This is obviously risky (e.g., create
a file named
_tmp0000000.png
in the current folder before runningmoviewriter.py
from the docs examples and it will be overwritten.Moveover, temporaries are not cleaned up if an exception is raised
(e.g., KeyboardInterrupt).
This is relatively easy to fix using
tempfile.mkdtemp
, although Ihad to modify
finish
so that it doesn't callcleanup
by itself(obviously, this can be fixed by keeping a
cleaned_up
attributebut it feels like a minor API change is preferable; moreover the
context-manager API stays the same).
Only basic, manual testing done.