File tree Expand file tree Collapse file tree 1 file changed +16
-0
lines changed Expand file tree Collapse file tree 1 file changed +16
-0
lines changed Original file line number Diff line number Diff line change @@ -312,8 +312,10 @@ def __init__(
312312 __name__ ,
313313 )
314314
315+ self ._we_opened = False
315316 if isinstance (stream , (str , Path )):
316317 stream = FileIO (stream , "rb" )
318+ self ._we_opened = True
317319 weakref .finalize (self , stream .close )
318320
319321 self .read (stream )
@@ -349,6 +351,20 @@ def close(self) -> None:
349351 """Close the underlying file handle"""
350352 self .stream .close ()
351353
354+ def __enter__ (self ) -> "PdfReader" :
355+ """Use PdfReader as context manager"""
356+ return self
357+
358+ def __exit__ (
359+ self ,
360+ exc_type : Optional [Type [BaseException ]],
361+ exc : Optional [BaseException ],
362+ traceback : Optional [TracebackType ],
363+ ) -> None :
364+ """Write data to the fileobj."""
365+ if self ._we_opened :
366+ self .close ()
367+
352368 @property
353369 def root_object (self ) -> DictionaryObject :
354370 """Provide access to "/Root". standardized with PdfWriter."""
You can’t perform that action at this time.
0 commit comments