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

BUG: encode_pdfdocencoding() always returns bytes #2440

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pypdf/generic/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,4 +650,4 @@ def encode_pdfdocencoding(unicode_string: str) -> bytes:
raise UnicodeEncodeError(
"pdfdocencoding", c, -1, -1, "does not exist in translation table"
)
return retval
return bytes(retval)
11 changes: 11 additions & 0 deletions tests/test_generic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test the pypdf.generic module."""

from io import BytesIO
from pathlib import Path
from unittest.mock import patch
Expand Down Expand Up @@ -273,6 +274,16 @@ def test_encode_pdfdocencoding_keyerror():
assert exc.value.args[0] == "pdfdocencoding"


@pytest.mark.parametrize("test_input", ["", "data"])
def test_encode_pdfdocencoding_returns_bytes(test_input):
"""
Test that encode_pdfdocencoding() always returns bytes because bytearray
is duck type compatible with bytes in mypy
"""
out = encode_pdfdocencoding(test_input)
assert isinstance(out, bytes)


def test_read_object_comment_exception():
stream = BytesIO(b"% foobar")
pdf = None
Expand Down
Loading