Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAINT: Let PdfMerger._create_stream raise NotImplemented if arg is none of str/Path/stream/PdfReader #1251

Merged
merged 4 commits into from
Aug 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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