diff --git a/pypdf/_writer.py b/pypdf/_writer.py index 4ca5bec3e..c4406ac91 100644 --- a/pypdf/_writer.py +++ b/pypdf/_writer.py @@ -1435,24 +1435,24 @@ def write(self, stream: Union[Path, StrByteType]) -> Tuple[bool, IO[Any]]: def list_objects_in_increment(self) -> List[IndirectObject]: """ - For debugging/analysis. - Provides the list of new/modified objects that will be written + For analysis or debugging. + Provides the list of new or modified objects that will be written in the increment. Deleted objects will not be freed but will become orphans. Returns: - List of (new / modified) IndirectObjects + List of new or modified IndirectObjects """ + original_hash_count = len(self._original_hash) return [ - cast(IndirectObject, self._objects[i]).indirect_reference - for i in range(len(self._objects)) + cast(IndirectObject, obj).indirect_reference + for i, obj in enumerate(self._objects) if ( - self._objects[i] is not None + obj is not None and ( - i >= len(self._original_hash) - or cast(PdfObject, self._objects[i]).hash_bin() - != self._original_hash[i] + i >= original_hash_count + or obj.hash_bin() != self._original_hash[i] ) ) ] @@ -1462,11 +1462,11 @@ def _write_increment(self, stream: StreamType) -> None: object_blocks = [] current_start = -1 current_stop = -2 + original_hash_count = len(self._original_hash) for i, obj in enumerate(self._objects): - if self._objects[i] is not None and ( - i >= len(self._original_hash) - or cast(PdfObject, self._objects[i]).hash_bin() - != self._original_hash[i] + if obj is not None and ( + i >= original_hash_count + or obj.hash_bin() != self._original_hash[i] ): idnum = i + 1 assert isinstance(obj, PdfObject) # mypy