Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STY: Increase readability slightly #2986

Merged
merged 10 commits into from
Dec 4, 2024
20 changes: 10 additions & 10 deletions pypdf/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@
and obj.indirect_reference.pdf == self # type: ignore
):
return obj.indirect_reference # type: ignore
# check for /Contents in Pages (/Contents in annotation are strings)
# check for /Contents in Pages (/Contents in annotations are strings)
if isinstance(obj, DictionaryObject) and isinstance(
obj.get(PG.CONTENTS, None), (ArrayObject, DictionaryObject)
):
Expand All @@ -434,7 +434,7 @@
if isinstance(indirect_reference, int):
obj = self._objects[indirect_reference - 1]
elif indirect_reference.pdf != self:
raise ValueError("pdf must be self")
raise ValueError("PDF must be self")
else:
obj = self._objects[indirect_reference.idnum - 1]
assert obj is not None # clarification for mypy
Expand Down Expand Up @@ -498,13 +498,13 @@
else:
cast(ArrayObject, node[PA.KIDS]).append(page.indirect_reference)
self.flattened_pages.append(page)
cpt = 1000
recurse = 0
while not is_null_or_none(node):
node = cast(DictionaryObject, node.get_object())
node[NameObject(PA.COUNT)] = NumberObject(cast(int, node[PA.COUNT]) + 1)
node = node.get(PA.PARENT, None)
cpt -= 1
if cpt < 0:
recurse += 1
if recurse > 1000:
raise PyPdfError("Too many recursive calls!")
return page

Expand Down Expand Up @@ -955,8 +955,8 @@
else:
font_full_rev = {v: bytes((k,)) for k, v in font_encoding.items()}
font_encoding_rev = {v: bytes((k,)) for k, v in font_encoding.items()}
for kk, v in font_map.items():
font_full_rev[v] = font_encoding_rev.get(kk, kk)
for key, value in font_map.items():
font_full_rev[value] = font_encoding_rev.get(key, key)

Check warning on line 959 in pypdf/_writer.py

View check run for this annotation

Codecov / codecov/patch

pypdf/_writer.py#L959

Added line #L959 was not covered by tests
else:
logger_warning(f"Font dictionary for {font_name} not found.", __name__)
font_full_rev = {}
Expand Down Expand Up @@ -1055,10 +1055,10 @@

"""
if CatalogDictionary.ACRO_FORM not in self._root_object:
raise PyPdfError("No /AcroForm dictionary in PdfWriter Object")
raise PyPdfError("No /AcroForm dictionary in PDF of PdfWriter Object")
af = cast(DictionaryObject, self._root_object[CatalogDictionary.ACRO_FORM])
if InteractiveFormDictEntries.Fields not in af:
raise PyPdfError("No /Fields dictionary in Pdf in PdfWriter Object")
raise PyPdfError("No /Fields dictionary in PDF of PdfWriter Object")
if isinstance(auto_regenerate, bool):
self.set_need_appearances_writer(auto_regenerate)
# Iterate through pages, update field values
Expand Down Expand Up @@ -1366,7 +1366,7 @@
self._reader.stream.seek(0)
stream.write(self._reader.stream.read(-1))
if len(self.list_objects_in_increment()) > 0:
self._write_increment(stream) # writes objs, Xref stream and startx
self._write_increment(stream) # writes objs, xref stream and startxref
else:
object_positions, free_objects = self._write_pdf_structure(stream)
xref_location = self._write_xref_table(
Expand Down
6 changes: 3 additions & 3 deletions tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ def test_add_named_destination(pdf_file_path):
assert writer.get_object(root[1].idnum) == writer.get_object(root[1])
with pytest.raises(ValueError) as exc:
writer.get_object(reader.pages[0].indirect_reference)
assert exc.value.args[0] == "pdf must be self"
assert exc.value.args[0] == "PDF must be self"

# write "output" to pypdf-output.pdf
with open(pdf_file_path, "wb") as output_stream:
Expand Down Expand Up @@ -1773,7 +1773,7 @@ def test_missing_fields(pdf_file_path):
writer.update_page_form_field_values(
writer.pages[0], {"foo": "some filled in text"}, flags=1
)
assert exc.value.args[0] == "No /AcroForm dictionary in PdfWriter Object"
assert exc.value.args[0] == "No /AcroForm dictionary in PDF of PdfWriter Object"

writer = PdfWriter()
writer.append(reader, [0])
Expand All @@ -1782,7 +1782,7 @@ def test_missing_fields(pdf_file_path):
writer.update_page_form_field_values(
writer.pages[0], {"foo": "some filled in text"}, flags=1
)
assert exc.value.args[0] == "No /Fields dictionary in Pdf in PdfWriter Object"
assert exc.value.args[0] == "No /Fields dictionary in PDF of PdfWriter Object"


def test_missing_info():
Expand Down
Loading