Skip to content

Commit

Permalink
fix: match behaviour between PSFile / PSInMemory parser
Browse files Browse the repository at this point in the history
  • Loading branch information
dhdaines committed Sep 20, 2024
1 parent 4c7d494 commit 6e9d73f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
23 changes: 17 additions & 6 deletions pdfminer/psparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,15 +399,26 @@ def _parse_literal(self) -> bytes:
def _parse_literal_hex(self) -> bytes:
"""State for escaped hex characters in literal names"""
# Consume a hex digit only if we can ... consume a hex digit
if len(self.hex) >= 2: # it actually can't exceed 2
self._curtoken += bytes((int(self.hex, 16),))
self._parse1 = self._parse_literal
return b"/"
c = self.fp.read(1)
if c and c in HEX and len(self.hex) < 2:
if c and c in HEX:
self.hex += c
else:
if c:
if c: # not EOF, but not hex either
log.warning("Invalid hex digit %r in literal", c)
self.fp.seek(-1, io.SEEK_CUR)
if self.hex:
self._curtoken += bytes((int(self.hex, 16),))
self._parse1 = self._parse_literal
# Add the intervening junk, just in case
try:
tok = LIT(self._curtoken.decode("utf-8"))
except UnicodeDecodeError:
tok = LIT(self._curtoken)
self._add_token(tok)
self._curtokenpos = self.tell() - 1 - len(self.hex)
self._add_token(KWD(b"#" + self.hex))
self._parse1 = self._parse_main
return c

def _parse_number(self) -> bytes:
Expand Down Expand Up @@ -597,7 +608,7 @@ def _parse_hexstring(self) -> bytes:
| (?P<number> [-+]? (?: \d*\.\d+ | \d+ ) )
| (?P<keyword> [A-Za-z] [^#/%\[\]()<>{}\s]*)
| (?P<startstr> \([^()\\]*)
| (?P<hexstr> <[A-Fa-f\d\s]+>)
| (?P<hexstr> <[A-Fa-f\d\s]*>)
| (?P<startdict> <<)
| (?P<enddict> >>)
| (?P<other> .)
Expand Down
8 changes: 5 additions & 3 deletions tests/test_pdfminer_psparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ class TestPSFileParser:
(23, LIT("a")),
(25, LIT("BCD")),
(30, LIT("Some_Name")),
(41, LIT("foo_xbaa")),
(41, LIT("foo_")),
(48, KWD(b"#")),
(49, KWD(b"xbaa")),
(54, 0),
(56, 1),
(59, -2),
Expand Down Expand Up @@ -91,7 +93,7 @@ class TestPSFileParser:
(23, LIT("a")),
(25, LIT("BCD")),
(30, LIT("Some_Name")),
(41, LIT("foo_xbaa")),
(41, LIT("foo_")),
(54, 0),
(56, 1),
(59, -2),
Expand Down Expand Up @@ -136,7 +138,7 @@ class MyParser(PSStackParser):
def flush(self):
self.add_results(*self.popall())

parser = MyParser(BytesIO(s))
parser = MyParser(s)
r = []
try:
while True:
Expand Down

0 comments on commit 6e9d73f

Please sign in to comment.