From 126f6becb197fd2845c67da7e8333daff08f3b50 Mon Sep 17 00:00:00 2001 From: Martin Thoma Date: Sun, 8 Oct 2023 11:52:28 +0200 Subject: [PATCH] TST: Regression test against non-deterministic accidental object reuse (#2244) Full credit to sjoerdjob for this contribution via #1995 See #1788 Co-authored-by: Sjoerd Job Postmus --- tests/test_writer.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/test_writer.py b/tests/test_writer.py index 358fdb1cf..00d3e513f 100644 --- a/tests/test_writer.py +++ b/tests/test_writer.py @@ -1859,3 +1859,31 @@ def test_object_contains_indirect_reference_to_self(): outpage = writer.add_blank_page(width, height) outpage.merge_page(reader.pages[6]) writer.append(reader) + + +def test_merging_many_temporary_files(): + def create_number_pdf(n) -> BytesIO: + from fpdf import FPDF + + pdf = FPDF() + pdf.add_page() + pdf.set_font("helvetica", "B", 16) + pdf.cell(40, 10, str(n)) + byte_string = pdf.output() + return BytesIO(byte_string) + + writer = PdfWriter() + for n in range(100): + reader = PdfReader(create_number_pdf(n)) + for page in reader.pages: + # Should only be one page. + writer.add_page(page) + + out = BytesIO() + writer.write(out) + + out.seek(0) + reader = PdfReader(out) + for n, page in enumerate(reader.pages): + text = page.extract_text() + assert text == str(n)