Skip to content

Commit 13c9927

Browse files
committed
coverage+
1 parent cfe8358 commit 13c9927

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

pypdf/generic/_base.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,8 +530,11 @@ def __new__(cls, value: Any) -> "TextStringObject":
530530
o.autodetect_pdfdocencoding = False
531531
o.utf16_bom = b""
532532
if value.startswith(("\xfe\xff", "\xff\xfe")):
533+
assert org is not None # for mypy
534+
o = str.__new__(cls, org.decode("utf-16"))
535+
o._original_bytes = org
533536
o.autodetect_utf16 = True
534-
o.utf16_bom = value[:2].encode("charmap")
537+
o.utf16_bom = org[:2]
535538
else:
536539
try:
537540
encode_pdfdocencoding(o)

tests/test_generic.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,16 @@ def test_create_string_object_utf16_bom():
11311131
result.get_encoded_bytes()
11321132
== b"\xff\xfeP\x00a\x00p\x00e\x00r\x00P\x00o\x00r\x00t\x00 \x001\x004\x00\x00\x00"
11331133
)
1134+
result = TextStringObject(
1135+
b"\xff\xfeP\x00a\x00p\x00e\x00r\x00P\x00o\x00r\x00t\x00 \x001\x004\x00\x00\x00"
1136+
)
1137+
assert result == "PaperPort 14\x00"
1138+
assert result.autodetect_utf16 is True
1139+
assert result.utf16_bom == b"\xff\xfe"
1140+
assert (
1141+
result.get_encoded_bytes()
1142+
== b"\xff\xfeP\x00a\x00p\x00e\x00r\x00P\x00o\x00r\x00t\x00 \x001\x004\x00\x00\x00"
1143+
)
11341144

11351145
# utf16-be without bom
11361146
result = TextStringObject("ÿ")

0 commit comments

Comments
 (0)