18
18
_Backend , FigureCanvasBase , FigureManagerBase , GraphicsContextBase ,
19
19
RendererBase )
20
20
from matplotlib .backends .backend_mixed import MixedModeRenderer
21
- from matplotlib .cbook import is_writable_file_like
22
21
from matplotlib .path import Path
23
22
from matplotlib .figure import Figure
24
23
from matplotlib ._pylab_helpers import Gcf
@@ -385,8 +384,8 @@ def __init__(self, figure, fh, dummy=False):
385
384
Matplotlib figure to initialize height, width and dpi from.
386
385
fh : file-like
387
386
File handle for the output of the drawing commands.
388
-
389
387
"""
388
+
390
389
RendererBase .__init__ (self )
391
390
self .dpi = figure .dpi
392
391
self .fh = fh
@@ -842,16 +841,10 @@ def print_pgf(self, fname_or_fh, *args, **kwargs):
842
841
if kwargs .get ("dryrun" , False ):
843
842
self ._print_pgf_to_fh (None , * args , ** kwargs )
844
843
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 )
855
848
856
849
def _print_pdf_to_fh (self , fh , * args , ** kwargs ):
857
850
w , h = self .figure .get_figwidth (), self .figure .get_figheight ()
@@ -896,21 +889,12 @@ def _print_pdf_to_fh(self, fh, *args, **kwargs):
896
889
TmpDirCleaner .add (tmpdir )
897
890
898
891
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."""
902
893
if kwargs .get ("dryrun" , False ):
903
894
self ._print_pgf_to_fh (None , * args , ** kwargs )
904
895
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 )
914
898
915
899
def _print_png_to_fh (self , fh , * args , ** kwargs ):
916
900
converter = make_pdf_to_png_converter ()
@@ -933,20 +917,12 @@ def _print_png_to_fh(self, fh, *args, **kwargs):
933
917
TmpDirCleaner .add (tmpdir )
934
918
935
919
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."""
939
921
if kwargs .get ("dryrun" , False ):
940
922
self ._print_pgf_to_fh (None , * args , ** kwargs )
941
923
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 )
950
926
951
927
def get_renderer (self ):
952
928
return RendererPgf (self .figure , None , dummy = True )
0 commit comments