From 18519a0862ee75e8e9875e16bff2f96b856dbcd8 Mon Sep 17 00:00:00 2001 From: Marco Sirabella Date: Wed, 20 Mar 2024 17:08:30 -0700 Subject: [PATCH] TST: Use buffer instead of opening file many times See #2520, basically this was the last failing (only on windows) test because if the pdfreaders are implicitly opening file streams that don't get closed until they get garbage collected the .unlinks() create file lock errors. --- tests/test_writer.py | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/tests/test_writer.py b/tests/test_writer.py index 84fcd51106..15aa1abbeb 100644 --- a/tests/test_writer.py +++ b/tests/test_writer.py @@ -1118,7 +1118,7 @@ def test_append_multiple(): @pytest.mark.samples() -def test_set_page_label(pdf_file_path): +def test_set_page_label(): src = RESOURCE_ROOT / "GeoBase_NHNC1_Data_Model_UML_EN.pdf" # File without labels reader = PdfReader(src) @@ -1154,8 +1154,9 @@ def test_set_page_label(pdf_file_path): writer.set_page_label(11, 11, "/r") writer.set_page_label(12, 13, "/R") writer.set_page_label(17, 18, "/R") - writer.write(pdf_file_path) - assert PdfReader(pdf_file_path).page_labels == expected + _, buf = writer.write(BytesIO()) + buf.seek(0) + assert PdfReader(buf).page_labels == expected writer = PdfWriter() # Same labels, different set order writer.clone_document_from_reader(reader) @@ -1165,8 +1166,9 @@ def test_set_page_label(pdf_file_path): writer.set_page_label(0, 1, "/r") writer.set_page_label(12, 13, "/R") writer.set_page_label(11, 11, "/r") - writer.write(pdf_file_path) - assert PdfReader(pdf_file_path).page_labels == expected + _, buf = writer.write(BytesIO()) + buf.seek(0) + assert PdfReader(buf).page_labels == expected # Tests labels assigned only in the middle # Tests label assigned to a range already containing labled ranges @@ -1176,8 +1178,9 @@ def test_set_page_label(pdf_file_path): writer.set_page_label(3, 4, "/a") writer.set_page_label(5, 5, "/A") writer.set_page_label(2, 6, "/r") - writer.write(pdf_file_path) - assert PdfReader(pdf_file_path).page_labels[: len(expected)] == expected + _, buf = writer.write(BytesIO()) + buf.seek(0) + assert PdfReader(buf).page_labels[: len(expected)] == expected # Tests labels assigned inside a previously existing range expected = ["1", "2", "i", "a", "b", "A", "1", "1", "2"] @@ -1187,8 +1190,9 @@ def test_set_page_label(pdf_file_path): writer.set_page_label(2, 6, "/r") writer.set_page_label(3, 4, "/a") writer.set_page_label(5, 5, "/A") - writer.write(pdf_file_path) - assert PdfReader(pdf_file_path).page_labels[: len(expected)] == expected + _, buf = writer.write(BytesIO()) + buf.seek(0) + assert PdfReader(buf).page_labels[: len(expected)] == expected # Tests invalid user input writer = PdfWriter() @@ -1212,7 +1216,6 @@ def test_set_page_label(pdf_file_path): ): writer.set_page_label(0, 5, "/r", start=-1) - pdf_file_path.unlink() src = ( SAMPLE_ROOT / "009-pdflatex-geotopo/GeoTopo.pdf" @@ -1224,18 +1227,18 @@ def test_set_page_label(pdf_file_path): writer = PdfWriter() writer.clone_document_from_reader(reader) writer.set_page_label(2, 3, "/A") - writer.write(pdf_file_path) - assert PdfReader(pdf_file_path).page_labels[: len(expected)] == expected + _, buf = writer.write(BytesIO()) + buf.seek(0) + assert PdfReader(buf).page_labels[: len(expected)] == expected # Tests replacing existing lables expected = ["A", "B", "1", "1", "2"] writer = PdfWriter() writer.clone_document_from_reader(reader) writer.set_page_label(0, 1, "/A") - writer.write(pdf_file_path) - assert PdfReader(pdf_file_path).page_labels[: len(expected)] == expected - - pdf_file_path.unlink() + _, buf = writer.write(BytesIO()) + buf.seek(0) + assert PdfReader(buf).page_labels[: len(expected)] == expected # Tests prefix and start. src = RESOURCE_ROOT / "issue-604.pdf" # File without page labels @@ -1250,7 +1253,7 @@ def test_set_page_label(pdf_file_path): writer.set_page_label(11, 21, "/D", prefix="PAP-") writer.set_page_label(22, 30, "/D", prefix="FOLL-") writer.set_page_label(31, 39, "/D", prefix="HURT-") - writer.write(pdf_file_path) + writer.write(BytesIO()) @pytest.mark.enable_socket() @@ -1452,7 +1455,6 @@ def test_named_dest_page_number(): def test_update_form_fields(tmp_path): - write_data_here = tmp_path / "out.pdf" writer = PdfWriter(clone_from=RESOURCE_ROOT / "FormTestFromOo.pdf") writer.update_page_form_field_values( writer.pages[0], @@ -1476,8 +1478,8 @@ def test_update_form_fields(tmp_path): auto_regenerate=False, ) - writer.write(write_data_here) - reader = PdfReader(write_data_here) + _, buf = writer.write(BytesIO()) + reader = PdfReader(buf) flds = reader.get_fields() assert flds["CheckBox1"]["/V"] == "/Yes" assert flds["CheckBox1"].indirect_reference.get_object()["/AS"] == "/Yes" @@ -1499,8 +1501,6 @@ def test_update_form_fields(tmp_path): 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"]) - Path(write_data_here).unlink() - @pytest.mark.enable_socket() def test_iss1862():