Skip to content

Commit

Permalink
Always return a str from literal_name (#978)
Browse files Browse the repository at this point in the history
  • Loading branch information
pietermarsman authored Jul 8, 2024
1 parent 3daf9a4 commit 8386822
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Fixed

- `TypeError` when PDF object reference cannot be parsed as int ([#972](https://github.com/pdfminer/pdfminer.six/pull/972))])
- `TypeError` when PDF literal cannot be converted to str ([#978](https://github.com/pdfminer/pdfminer.six/pull/978))

### Removed

Expand Down
2 changes: 1 addition & 1 deletion pdfminer/pdfinterp.py
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ def do_EI(self, obj: PDFStackT) -> None:

def do_Do(self, xobjid_arg: PDFStackT) -> None:
"""Invoke named XObject"""
xobjid = cast(str, literal_name(xobjid_arg))
xobjid = literal_name(xobjid_arg)
try:
xobj = stream_value(self.xobjmap[xobjid])
except KeyError:
Expand Down
27 changes: 13 additions & 14 deletions pdfminer/psparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,23 +117,21 @@ def intern(self, name: PSLiteral.NameType) -> _SymbolT:
KEYWORD_DICT_END = KWD(b">>")


def literal_name(x: object) -> Any:
if not isinstance(x, PSLiteral):
def literal_name(x: Any) -> str:
if isinstance(x, PSLiteral):
if isinstance(x.name, str):
return x.name
try:
return str(x.name, "utf-8")
except UnicodeDecodeError:
return str(x.name)
else:
if settings.STRICT:
raise PSTypeError(f"Literal required: {x!r}")
else:
name = x
else:
name = x.name
if not isinstance(name, str):
try:
name = str(name, "utf-8")
except Exception:
pass
return name
return str(x)


def keyword_name(x: object) -> Any:
def keyword_name(x: Any) -> Any:
if not isinstance(x, PSKeyword):
if settings.STRICT:
raise PSTypeError("Keyword required: %r" % x)
Expand Down Expand Up @@ -523,12 +521,13 @@ def nexttoken(self) -> Tuple[int, PSBaseParserToken]:


# Stack slots may by occupied by any of:
# * the name of a literal
# * the PSBaseParserToken types
# * list (via KEYWORD_ARRAY)
# * dict (via KEYWORD_DICT)
# * subclass-specific extensions (e.g. PDFStream, PDFObjRef) via ExtraT
ExtraT = TypeVar("ExtraT")
PSStackType = Union[float, bool, PSLiteral, bytes, List, Dict, ExtraT]
PSStackType = Union[str, float, bool, PSLiteral, bytes, List, Dict, ExtraT]
PSStackEntry = Tuple[int, PSStackType[ExtraT]]


Expand Down

0 comments on commit 8386822

Please sign in to comment.