1818 _Backend , FigureCanvasBase , FigureManagerBase , GraphicsContextBase ,
1919 RendererBase )
2020from matplotlib .backends .backend_mixed import MixedModeRenderer
21- from matplotlib .cbook import is_writable_file_like
2221from matplotlib .path import Path
2322from matplotlib .figure import Figure
2423from matplotlib ._pylab_helpers import Gcf
@@ -385,8 +384,8 @@ def __init__(self, figure, fh, dummy=False):
385384 Matplotlib figure to initialize height, width and dpi from.
386385 fh : file-like
387386 File handle for the output of the drawing commands.
388-
389387 """
388+
390389 RendererBase .__init__ (self )
391390 self .dpi = figure .dpi
392391 self .fh = fh
@@ -842,16 +841,10 @@ def print_pgf(self, fname_or_fh, *args, **kwargs):
842841 if kwargs .get ("dryrun" , False ):
843842 self ._print_pgf_to_fh (None , * args , ** kwargs )
844843 return
845-
846- # figure out where the pgf is to be written to
847- if isinstance (fname_or_fh , str ):
848- with open (fname_or_fh , "w" , encoding = "utf-8" ) as fh :
849- self ._print_pgf_to_fh (fh , * args , ** kwargs )
850- elif is_writable_file_like (fname_or_fh ):
851- fh = codecs .getwriter ("utf-8" )(fname_or_fh )
852- self ._print_pgf_to_fh (fh , * args , ** kwargs )
853- else :
854- raise ValueError ("filename must be a path" )
844+ with cbook .open_file_cm (fname_or_fh , "w" , encoding = "utf-8" ) as file :
845+ if not cbook .file_requires_unicode (file ):
846+ file = codecs .getwriter ("utf-8" )(file )
847+ self ._print_pgf_to_fh (file , * args , ** kwargs )
855848
856849 def _print_pdf_to_fh (self , fh , * args , ** kwargs ):
857850 w , h = self .figure .get_figwidth (), self .figure .get_figheight ()
@@ -896,21 +889,12 @@ def _print_pdf_to_fh(self, fh, *args, **kwargs):
896889 TmpDirCleaner .add (tmpdir )
897890
898891 def print_pdf (self , fname_or_fh , * args , ** kwargs ):
899- """
900- Use LaTeX to compile a Pgf generated figure to PDF.
901- """
892+ """Use LaTeX to compile a Pgf generated figure to PDF."""
902893 if kwargs .get ("dryrun" , False ):
903894 self ._print_pgf_to_fh (None , * args , ** kwargs )
904895 return
905-
906- # figure out where the pdf is to be written to
907- if isinstance (fname_or_fh , str ):
908- with open (fname_or_fh , "wb" ) as fh :
909- self ._print_pdf_to_fh (fh , * args , ** kwargs )
910- elif is_writable_file_like (fname_or_fh ):
911- self ._print_pdf_to_fh (fname_or_fh , * args , ** kwargs )
912- else :
913- raise ValueError ("filename must be a path or a file-like object" )
896+ with cbook .open_file_cm (fname_or_fh , "wb" ) as file :
897+ self ._print_pdf_to_fh (file , * args , ** kwargs )
914898
915899 def _print_png_to_fh (self , fh , * args , ** kwargs ):
916900 converter = make_pdf_to_png_converter ()
@@ -933,20 +917,12 @@ def _print_png_to_fh(self, fh, *args, **kwargs):
933917 TmpDirCleaner .add (tmpdir )
934918
935919 def print_png (self , fname_or_fh , * args , ** kwargs ):
936- """
937- Use LaTeX to compile a pgf figure to pdf and convert it to png.
938- """
920+ """Use LaTeX to compile a pgf figure to pdf and convert it to png."""
939921 if kwargs .get ("dryrun" , False ):
940922 self ._print_pgf_to_fh (None , * args , ** kwargs )
941923 return
942-
943- if isinstance (fname_or_fh , str ):
944- with open (fname_or_fh , "wb" ) as fh :
945- self ._print_png_to_fh (fh , * args , ** kwargs )
946- elif is_writable_file_like (fname_or_fh ):
947- self ._print_png_to_fh (fname_or_fh , * args , ** kwargs )
948- else :
949- raise ValueError ("filename must be a path or a file-like object" )
924+ with cbook .open_file_cm (fname_or_fh , "wb" ) as file :
925+ self ._print_png_to_fh (file , * args , ** kwargs )
950926
951927 def get_renderer (self ):
952928 return RendererPgf (self .figure , None , dummy = True )
0 commit comments