diff --git a/PyPDF2/pdf.py b/PyPDF2/pdf.py index 1bcd63eda3..449eeb78f8 100644 --- a/PyPDF2/pdf.py +++ b/PyPDF2/pdf.py @@ -606,7 +606,7 @@ def _sweepIndirectReferences(self, externMap, data): self._sweepIndirectReferences(externMap, realdata) return data else: - if data.pdf.stream.closed: + if hasattr(data.pdf, "stream") and data.pdf.stream.closed: raise ValueError("I/O operation on closed file: {}".format(data.pdf.stream.name)) newobj = externMap.get(data.pdf, {}).get(data.generation, {}).get(data.idnum, None) if newobj is None: diff --git a/Tests/test_writer.py b/Tests/test_writer.py index 7e478ca27a..22d35a6dad 100644 --- a/Tests/test_writer.py +++ b/Tests/test_writer.py @@ -340,3 +340,13 @@ def test_io_streams(): writer = PdfFileWriter() with BytesIO() as output_stream: writer.write(output_stream) + + +def test_regression_issue670(): + filepath = os.path.join(RESOURCE_ROOT, "crazyones.pdf") + reader = PdfFileReader(filepath, strict=False, overwriteWarnings=False) + for _ in range(2): + pdf_writer = PdfFileWriter() + pdf_writer.addPage(reader.getPage(0)) + with open("dont_commit_issue670.pdf", "wb") as f_pdf: + pdf_writer.write(f_pdf)