Skip to content

Commit

Permalink
Implement feedback from CR
Browse files Browse the repository at this point in the history
  • Loading branch information
tibor-reiss committed May 23, 2024
1 parent de037e0 commit 5f0bce4
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pypdf/_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,11 @@ def __init__(
"It may not be read correctly.",
__name__,
)
self._stream_opened = False
if isinstance(stream, (str, Path)):
with open(stream, "rb") as fh:
stream = BytesIO(fh.read())
self._stream_opened = True
self.read(stream)
self.stream = stream

Expand Down Expand Up @@ -170,7 +172,12 @@ def __exit__(
exc_val: Optional[BaseException],
exc_tb: Optional[TracebackType],
) -> None:
self.stream.close()
self.close()

def close(self) -> None:
"""Close the stream if opened in __init__ and clear memory."""
if self._stream_opened:
self.stream.close()
self.flattened_pages = []
self.resolved_objects = {}
self.trailer = DictionaryObject()
Expand Down

0 comments on commit 5f0bce4

Please sign in to comment.