Skip to content

Commit 0786520

Browse files
committed
fixup! PI: Don't load entire file into memory when passed file name
1 parent 9ccce80 commit 0786520

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

pypdf/_reader.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ def __init__(
314314

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

319320
self.read(stream)

tests/test_writer.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,6 +1211,9 @@ def test_set_page_label(pdf_file_path):
12111211
ValueError, match="if given, start must be equal or greater than one"
12121212
):
12131213
writer.set_page_label(0, 5, "/r", start=-1)
1214+
del writer
1215+
import gc
1216+
gc.collect()
12141217

12151218
pdf_file_path.unlink()
12161219

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

12381244
pdf_file_path.unlink()
12391245

@@ -1477,6 +1483,7 @@ def test_update_form_fields(tmp_path):
14771483
)
14781484

14791485
writer.write(write_data_here)
1486+
del writer
14801487
reader = PdfReader(write_data_here)
14811488
flds = reader.get_fields()
14821489
assert flds["CheckBox1"]["/V"] == "/Yes"
@@ -1498,6 +1505,9 @@ def test_update_form_fields(tmp_path):
14981505
assert all(x in flds["CheckBox1"]["/_States_"] for x in ["/Off", "/Yes"])
14991506
assert all(x in flds["RadioGroup1"]["/_States_"] for x in ["/1", "/2", "/3"])
15001507
assert all(x in flds["Liste1"]["/_States_"] for x in ["Liste1", "Liste2", "Liste3"])
1508+
del reader, flds
1509+
import gc
1510+
gc.collect()
15011511

15021512
Path(write_data_here).unlink()
15031513

0 commit comments

Comments
 (0)