@@ -2669,18 +2669,19 @@ class PdfPages:
26692669 In reality `PdfPages` is a thin wrapper around `PdfFile`, in order to avoid
26702670 confusion when using `~.pyplot.savefig` and forgetting the format argument.
26712671 """
2672- __slots__ = ('_file' , 'keep_empty' )
26732672
2674- def __init__ (self , filename , keep_empty = True , metadata = None ):
2673+ _UNSET = object ()
2674+
2675+ def __init__ (self , filename , keep_empty = _UNSET , metadata = None ):
26752676 """
26762677 Create a new PdfPages object.
26772678
26782679 Parameters
26792680 ----------
26802681 filename : str or path-like or file-like
2681- Plots using `PdfPages.savefig` will be written to a file at this
2682- location. The file is opened at once and any older file with the
2683- same name is overwritten .
2682+ Plots using `PdfPages.savefig` will be written to a file at this location.
2683+ The file is opened when a figure is saved for the first time (overwriting
2684+ any older file with the same name) .
26842685
26852686 keep_empty : bool, optional
26862687 If set to False, then empty pdf files will be deleted automatically
@@ -2696,34 +2697,50 @@ def __init__(self, filename, keep_empty=True, metadata=None):
26962697 'Trapped'. Values have been predefined for 'Creator', 'Producer'
26972698 and 'CreationDate'. They can be removed by setting them to `None`.
26982699 """
2699- self ._file = PdfFile (filename , metadata = metadata )
2700- self .keep_empty = keep_empty
2700+ self ._filename = filename
2701+ self ._metadata = metadata
2702+ self ._file = None
2703+ if keep_empty and keep_empty is not self ._UNSET :
2704+ _api .warn_deprecated ("3.8" , message = (
2705+ "Keeping empty pdf files is deprecated since %(since)s and support "
2706+ "will be removed %(removal)s." ))
2707+ self ._keep_empty = keep_empty
2708+
2709+ keep_empty = _api .deprecate_privatize_attribute ("3.8" )
27012710
27022711 def __enter__ (self ):
27032712 return self
27042713
27052714 def __exit__ (self , exc_type , exc_val , exc_tb ):
27062715 self .close ()
27072716
2717+ def _ensure_file (self ):
2718+ if self ._file is None :
2719+ self ._file = PdfFile (self ._filename , metadata = self ._metadata ) # init.
2720+ return self ._file
2721+
27082722 def close (self ):
27092723 """
27102724 Finalize this object, making the underlying file a complete
27112725 PDF file.
27122726 """
2713- self ._file .finalize ()
2714- self ._file .close ()
2715- if (self .get_pagecount () == 0 and not self .keep_empty and
2716- not self ._file .passed_in_file_object ):
2717- os .remove (self ._file .fh .name )
2718- self ._file = None
2727+ if self ._file is not None :
2728+ self ._file .finalize ()
2729+ self ._file .close ()
2730+ self ._file = None
2731+ elif self ._keep_empty : # True *or* UNSET.
2732+ _api .warn_deprecated ("3.8" , message = (
2733+ "Keeping empty pdf files is deprecated since %(since)s and support "
2734+ "will be removed %(removal)s." ))
2735+ PdfFile (self ._filename , metadata = self ._metadata ) # touch the file.
27192736
27202737 def infodict (self ):
27212738 """
27222739 Return a modifiable information dictionary object
27232740 (see PDF reference section 10.2.1 'Document Information
27242741 Dictionary').
27252742 """
2726- return self ._file .infoDict
2743+ return self ._ensure_file () .infoDict
27272744
27282745 def savefig (self , figure = None , ** kwargs ):
27292746 """
@@ -2750,7 +2767,7 @@ def savefig(self, figure=None, **kwargs):
27502767
27512768 def get_pagecount (self ):
27522769 """Return the current number of pages in the multipage pdf file."""
2753- return len (self ._file .pageList )
2770+ return len (self ._ensure_file () .pageList )
27542771
27552772 def attach_note (self , text , positionRect = [- 100 , - 100 , 0 , 0 ]):
27562773 """
@@ -2759,7 +2776,7 @@ def attach_note(self, text, positionRect=[-100, -100, 0, 0]):
27592776 page. It is outside the page per default to make sure it is
27602777 invisible on printouts.
27612778 """
2762- self ._file .newTextnote (text , positionRect )
2779+ self ._ensure_file () .newTextnote (text , positionRect )
27632780
27642781
27652782class FigureCanvasPdf (FigureCanvasBase ):
@@ -2778,7 +2795,7 @@ def print_pdf(self, filename, *,
27782795 self .figure .dpi = 72 # there are 72 pdf points to an inch
27792796 width , height = self .figure .get_size_inches ()
27802797 if isinstance (filename , PdfPages ):
2781- file = filename ._file
2798+ file = filename ._ensure_file ()
27822799 else :
27832800 file = PdfFile (filename , metadata = metadata )
27842801 try :
0 commit comments