Skip to content

Commit

Permalink
fixup! PI: Don't load entire file into memory when passed file name
Browse files Browse the repository at this point in the history
  • Loading branch information
mjsir911 committed Mar 15, 2024
1 parent 9ccce80 commit 0786520
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions pypdf/_reader.py
Expand Up @@ -314,6 +314,7 @@ def __init__(

if isinstance(stream, (str, Path)):
stream = open(stream, "rb") # noqa: SIM115
# Wish I could just close stream in __del__ but that fails a test very strangely
weakref.finalize(self, stream.close)

self.read(stream)
Expand Down
10 changes: 10 additions & 0 deletions tests/test_writer.py
Expand Up @@ -1211,6 +1211,9 @@ def test_set_page_label(pdf_file_path):
ValueError, match="if given, start must be equal or greater than one"
):
writer.set_page_label(0, 5, "/r", start=-1)
del writer
import gc
gc.collect()

pdf_file_path.unlink()

Expand All @@ -1234,6 +1237,9 @@ def test_set_page_label(pdf_file_path):
writer.set_page_label(0, 1, "/A")
writer.write(pdf_file_path)
assert PdfReader(pdf_file_path).page_labels[: len(expected)] == expected
import gc
gc.collect()
del reader

pdf_file_path.unlink()

Expand Down Expand Up @@ -1477,6 +1483,7 @@ def test_update_form_fields(tmp_path):
)

writer.write(write_data_here)
del writer
reader = PdfReader(write_data_here)
flds = reader.get_fields()
assert flds["CheckBox1"]["/V"] == "/Yes"
Expand All @@ -1498,6 +1505,9 @@ def test_update_form_fields(tmp_path):
assert all(x in flds["CheckBox1"]["/_States_"] for x in ["/Off", "/Yes"])
assert all(x in flds["RadioGroup1"]["/_States_"] for x in ["/1", "/2", "/3"])
assert all(x in flds["Liste1"]["/_States_"] for x in ["Liste1", "Liste2", "Liste3"])
del reader, flds
import gc
gc.collect()

Path(write_data_here).unlink()

Expand Down

0 comments on commit 0786520

Please sign in to comment.