Skip to content

Commit

Permalink
ROB: warn-only in readStringFromStream (#837)
Browse files Browse the repository at this point in the history
An unexpected escape string was raising a PdfReadError before.
Now, only a warning is issued.

Closes #360
Closes #794 : Passing the strict parameter looks like a good idea,
              but there is also the pdf parameter. Sadly, it is
              None for that issue.
  • Loading branch information
MartinThoma authored Apr 28, 2022
1 parent 5c7d6e8 commit 5e86977
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
6 changes: 3 additions & 3 deletions PyPDF2/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,9 @@ def readStringFromStream(stream):
# line break was escaped:
tok = b_("")
else:
raise PdfReadError(
r"Unexpected escaped string: {}".format(tok.decode("utf8"))
)
msg = r"Unexpected escaped string: {}".format(tok.decode("utf8"))
# if.strict: PdfReadError(msg)
logger.warning(msg)
txt += tok
return createStringObject(txt)

Expand Down
13 changes: 12 additions & 1 deletion Tests/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ def test_readStringFromStream_not_in_escapedict_no_digit():
stream = BytesIO(b"x\\y")
with pytest.raises(PdfReadError) as exc:
readStringFromStream(stream)
assert exc.value.args[0] == "Unexpected escaped string: y"
assert exc.value.args[0] == "Stream has ended unexpectedly"
# "Unexpected escaped string: y"


def test_readStringFromStream_multichar_eol():
Expand Down Expand Up @@ -216,6 +217,11 @@ def test_DictionaryObject_key_is_no_pdfobject():
assert exc.value.args[0] == "key must be PdfObject"


def test_DictionaryObject_xmp_meta():
do = DictionaryObject({NameObject("/S"): NameObject("/GoTo")})
assert do.xmpMetadata is None


def test_DictionaryObject_value_is_no_pdfobject():
do = DictionaryObject({NameObject("/S"): NameObject("/GoTo")})
with pytest.raises(ValueError) as exc:
Expand All @@ -237,6 +243,11 @@ def test_DictionaryObject_setdefault_value_is_no_pdfobject():
assert exc.value.args[0] == "value must be PdfObject"


def test_DictionaryObject_setdefault_value():
do = DictionaryObject({NameObject("/S"): NameObject("/GoTo")})
do.setdefault(NameObject("/S"), NameObject("/GoTo"))


def test_DictionaryObject_read_from_stream():
stream = BytesIO(b"<< /S /GoTo >>")
pdf = None
Expand Down

0 comments on commit 5e86977

Please sign in to comment.