diff --git a/PyPDF2/_merger.py b/PyPDF2/_merger.py index 37955b030..467ef8124 100644 --- a/PyPDF2/_merger.py +++ b/PyPDF2/_merger.py @@ -134,6 +134,9 @@ def merge( # Create a new PdfReader instance using the stream # (either file or BytesIO or StringIO) created above reader = PdfReader(stream, strict=self.strict) # type: ignore[arg-type] + + # Keep track of our input files so we can close them later + self.inputs.append((stream, reader, my_file)) if decryption_key is not None: reader._decryption_key = decryption_key @@ -183,9 +186,6 @@ def merge( # Slice to insert the pages at the specified position self.pages[position:position] = srcpages - # Keep track of our input files so we can close them later - self.inputs.append((stream, reader, my_file)) - def _create_stream( self, fileobj: Union[StrByteType, PdfReader] ) -> Tuple[IOBase, bool, Optional[bytes]]: diff --git a/tests/test_merger.py b/tests/test_merger.py index 6bc776b46..e8cb7999f 100644 --- a/tests/test_merger.py +++ b/tests/test_merger.py @@ -1,6 +1,8 @@ import os import sys +import pytest + import PyPDF2 from PyPDF2.generic import Destination @@ -24,6 +26,7 @@ def test_merge(): merger.append(outline) merger.append(pdf_path, pages=PyPDF2.pagerange.PageRange(slice(0, 0))) merger.append(pdf_forms) + merger.merge(0, pdf_path, import_bookmarks=False) # Merging an encrypted file reader = PyPDF2.PdfReader(pdf_pw) @@ -61,6 +64,13 @@ def test_merge(): merger.add_bookmark( "The FitBV fit", 0, bookmark, (255, 0, 15), True, True, "/FitBV", 10 ) + + found_bm = merger.find_bookmark("nothing here") + assert found_bm is None + + found_bm = merger.find_bookmark("foo") + assert found_bm == [9] + merger.add_metadata({"author": "Martin Thoma"}) merger.add_named_destination("title", 0) merger.set_page_layout("/SinglePage") @@ -92,3 +102,56 @@ def test_merge(): # Clean up os.remove(tmp_path) + + +def test_merge_page_exception(): + merger = PyPDF2.PdfMerger() + pdf_path = os.path.join(RESOURCE_ROOT, "crazyones.pdf") + with pytest.raises(TypeError) as exc: + merger.merge(0, pdf_path, pages="a:b") + assert exc.value.args[0] == '"pages" must be a tuple of (start, stop[, step])' + merger.close() + + +def test_merge_page_tuple(): + merger = PyPDF2.PdfMerger() + pdf_path = os.path.join(RESOURCE_ROOT, "crazyones.pdf") + merger.merge(0, pdf_path, pages=(0, 1)) + merger.close() + + +def test_merge_write_closed_fh(): + merger = PyPDF2.PdfMerger() + pdf_path = os.path.join(RESOURCE_ROOT, "crazyones.pdf") + merger.append(pdf_path) + + err_closed = "close() was called and thus the writer cannot be used anymore" + + merger.close() + with pytest.raises(RuntimeError) as exc: + merger.write("stream.pdf") + assert exc.value.args[0] == err_closed + + with pytest.raises(RuntimeError) as exc: + merger.add_metadata({"author": "Martin Thoma"}) + assert exc.value.args[0] == err_closed + + with pytest.raises(RuntimeError) as exc: + merger.set_page_layout("/SinglePage") + assert exc.value.args[0] == err_closed + + with pytest.raises(RuntimeError) as exc: + merger.set_page_mode("/UseNone") + assert exc.value.args[0] == err_closed + + with pytest.raises(RuntimeError) as exc: + merger._write_bookmarks() + assert exc.value.args[0] == err_closed + + with pytest.raises(RuntimeError) as exc: + merger.add_bookmark("A bookmark", 0) + assert exc.value.args[0] == err_closed + + with pytest.raises(RuntimeError) as exc: + merger._write_dests() + assert exc.value.args[0] == err_closed