Skip to content

Commit

Permalink
MAINT: Let PdfMerger._create_stream raise NotImplemented (#1251)
Browse files Browse the repository at this point in the history
... if arg is none of str/Path/stream/PdfReader
  • Loading branch information
MartinThoma authored Aug 20, 2022
1 parent 7f0a6b0 commit 0983fe4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
7 changes: 6 additions & 1 deletion PyPDF2/_merger.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,12 @@ def _create_stream(
stream = BytesIO(filecontent)
my_file = True
else:
stream = fileobj
raise NotImplementedError(
"PdfMerger.merge requires an object that PdfReader can parse. "
"Typically, that is a Path or a string representing a Path, "
"a file object, or an object implementing .seek and .read. "
"Passing a PdfReader directly works as well."
)
return stream, my_file, encryption_obj

@deprecate_bookmark(bookmark="outline_item", import_bookmarks="import_outline")
Expand Down
8 changes: 8 additions & 0 deletions tests/test_merger.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ def merger_operate(merger):
merger.append(pdf_path, pages=PyPDF2.pagerange.PageRange(slice(0, 0)))
merger.append(pdf_forms)
merger.merge(0, pdf_path, import_outline=False)
with pytest.raises(NotImplementedError) as exc:
with open(pdf_path, "rb") as fp:
data = fp.read()
merger.append(data)
assert exc.value.args[0].startswith(
"PdfMerger.merge requires an object that PdfReader can parse. "
"Typically, that is a Path"
)

# Merging an encrypted file
reader = PyPDF2.PdfReader(pdf_pw)
Expand Down

0 comments on commit 0983fe4

Please sign in to comment.