Skip to content

Commit 8c542f3

Browse files
authored
BUG: Add delimiters to NameObject.renumber_table (#3286)
1 parent a1c3366 commit 8c542f3

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

pypdf/generic/_base.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -780,11 +780,7 @@ class NameObject(str, PdfObject): # noqa: SLOT000
780780
delimiter_pattern = re.compile(rb"\s+|[\(\)<>\[\]{}/%]")
781781
surfix = b"/"
782782
renumber_table: ClassVar[Dict[str, bytes]] = {
783-
"#": b"#23",
784-
"(": b"#28",
785-
")": b"#29",
786-
"/": b"#2F",
787-
"%": b"#25",
783+
**{chr(i): f"#{i:02X}".encode() for i in b"#()<>[]{}/%"},
788784
**{chr(i): f"#{i:02X}".encode() for i in range(33)},
789785
}
790786

tests/test_generic.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,18 @@ def test_name_object(caplog):
243243
assert bytes(b.getbuffer()) == b"/DIJMAC+Arial#20Black#231"
244244
assert caplog.text == ""
245245

246+
caplog.clear()
246247
b = BytesIO()
247248
NameObject("/你好世界 (%)").write_to_stream(b)
248249
assert bytes(b.getbuffer()) == b"/#E4#BD#A0#E5#A5#BD#E4#B8#96#E7#95#8C#20#28#25#29"
249250
assert caplog.text == ""
250251

252+
caplog.clear()
253+
b = BytesIO()
254+
NameObject("/{foo}<bar>(baz)[qux]#/%").write_to_stream(b)
255+
assert bytes(b.getbuffer()) == b"/#7Bfoo#7D#3Cbar#3E#28baz#29#5Bqux#5D#23#2F#25"
256+
assert caplog.text == ""
257+
251258

252259
def test_destination_fit_r():
253260
d = Destination(

0 commit comments

Comments
 (0)