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

Skip to content

Commit 33bc9af

Browse files
committed
Simplify lookup of animation external commands.
... by using shutil.which instead of trying to run the command. Also, we don't need to explicitly call ImageMagickBase._init_from_registry because the writers.register decorator will do that for us (via the call to isAvailable).
1 parent 9ec4b95 commit 33bc9af

File tree

1 file changed

+12
-32
lines changed

1 file changed

+12
-32
lines changed

lib/matplotlib/animation.py

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import os
2626
from pathlib import Path
2727
import platform
28+
import shutil
2829
import subprocess
2930
import sys
3031
from tempfile import TemporaryDirectory
@@ -408,27 +409,9 @@ def bin_path(cls):
408409
@classmethod
409410
def isAvailable(cls):
410411
'''
411-
Check to see if a MovieWriter subclass is actually available by
412-
running the commandline tool.
412+
Check to see if a MovieWriter subclass is actually available.
413413
'''
414-
bin_path = cls.bin_path()
415-
if not bin_path:
416-
return False
417-
try:
418-
p = subprocess.Popen(
419-
bin_path,
420-
shell=False,
421-
stdout=subprocess.PIPE,
422-
stderr=subprocess.PIPE,
423-
creationflags=subprocess_creation_flags)
424-
return cls._handle_subprocess(p)
425-
except OSError:
426-
return False
427-
428-
@classmethod
429-
def _handle_subprocess(cls, process):
430-
process.communicate()
431-
return True
414+
return shutil.which(cls.bin_path()) is not None
432415

433416

434417
class FileMovieWriter(MovieWriter):
@@ -633,13 +616,14 @@ def output_args(self):
633616
return args + ['-y', self.outfile]
634617

635618
@classmethod
636-
def _handle_subprocess(cls, process):
637-
_, err = process.communicate()
638-
# Ubuntu 12.04 ships a broken ffmpeg binary which we shouldn't use
639-
# NOTE : when removed, remove the same method in AVConvBase.
640-
if 'Libav' in err.decode():
641-
return False
642-
return True
619+
def isAvailable(cls):
620+
return (
621+
super().isAvailable()
622+
# Ubuntu 12.04 ships a broken ffmpeg binary which we shouldn't use.
623+
# NOTE: when removed, remove the same method in AVConvBase.
624+
and b'LibAv' not in subprocess.run(
625+
[cls.bin_path()], creationflags=subprocess_creation_flags,
626+
stdout=subprocess.DEVNULL, stderr=subprocess.PIPE).stderr)
643627

644628

645629
# Combine FFMpeg options with pipe-based writing
@@ -697,9 +681,7 @@ class AVConvBase(FFMpegBase):
697681
args_key = 'animation.avconv_args'
698682

699683
# NOTE : should be removed when the same method is removed in FFMpegBase.
700-
@classmethod
701-
def _handle_subprocess(cls, process):
702-
return MovieWriter._handle_subprocess(process)
684+
isAvailable = classmethod(MovieWriter.isAvailable.__func__)
703685

704686

705687
# Combine AVConv options with pipe-based writing
@@ -772,8 +754,6 @@ def isAvailable(cls):
772754
cls._init_from_registry()
773755
return super().isAvailable()
774756

775-
ImageMagickBase._init_from_registry()
776-
777757

778758
# Note: the base classes need to be in that order to get
779759
# isAvailable() from ImageMagickBase called and not the

0 commit comments

Comments
 (0)