Skip to content

Commit

Permalink
Do not close the underlying file from compression plugins (#6239)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gallaecio committed Feb 21, 2024
1 parent ee11895 commit 6fc7827
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
8 changes: 7 additions & 1 deletion docs/topics/feed-exports.rst
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,13 @@ Each plugin is a class that must implement the following methods:

.. method:: close(self)

Close the target file object.
Clean up the plugin.

For example, you might want to close a file wrapper that you might have
used to compress data written into the file received in the ``__init__``
method.

.. warning:: Do not close the file from the ``__init__`` method.

To pass a parameter to your plugin, use :ref:`feed options <feed-options>`. You
can then access those parameters from the ``__init__`` method of your plugin.
Expand Down
3 changes: 0 additions & 3 deletions scrapy/extensions/postprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ def write(self, data: bytes) -> int:

def close(self) -> None:
self.gzipfile.close()
self.file.close()


class Bz2Plugin:
Expand All @@ -69,7 +68,6 @@ def write(self, data: bytes) -> int:

def close(self) -> None:
self.bz2file.close()
self.file.close()


class LZMAPlugin:
Expand Down Expand Up @@ -111,7 +109,6 @@ def write(self, data: bytes) -> int:

def close(self) -> None:
self.lzmafile.close()
self.file.close()


# io.IOBase is subclassed here, so that exporters can use the PostProcessingManager
Expand Down
2 changes: 2 additions & 0 deletions tests/test_feedexport.py
Original file line number Diff line number Diff line change
Expand Up @@ -1731,6 +1731,7 @@ def open(self, spider):

def store(self, file):
Storage.store_file = file
Storage.file_was_closed = file.closed
file.close()

settings = {
Expand All @@ -1746,6 +1747,7 @@ def store(self, file):
}
yield self.exported_no_data(settings)
self.assertIs(Storage.open_file, Storage.store_file)
self.assertFalse(Storage.file_was_closed)


class FeedPostProcessedExportsTest(FeedExportTestBase):
Expand Down

0 comments on commit 6fc7827

Please sign in to comment.