Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

Do not close the underlying file from compression plugins (#6239)

Adrián Chaves6fc78270427c
diff --git a/docs/topics/feed-exports.rst b/docs/topics/feed-exports.rst
index f64bbac06..922b765db 100644
--- a/docs/topics/feed-exports.rst
+++ b/docs/topics/feed-exports.rst
@@ -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.
diff --git a/scrapy/extensions/postprocessing.py b/scrapy/extensions/postprocessing.py
index 79e3b1656..17969c5b0 100644
--- a/scrapy/extensions/postprocessing.py
+++ b/scrapy/extensions/postprocessing.py
@@ -42,7 +42,6 @@ class GzipPlugin:
def close(self) -> None:
self.gzipfile.close()
- self.file.close()
class Bz2Plugin:
@@ -69,7 +68,6 @@ class Bz2Plugin:
def close(self) -> None:
self.bz2file.close()
- self.file.close()
class LZMAPlugin:
@@ -111,7 +109,6 @@ class LZMAPlugin:
def close(self) -> None:
self.lzmafile.close()
- self.file.close()
# io.IOBase is subclassed here, so that exporters can use the PostProcessingManager
diff --git a/tests/test_feedexport.py b/tests/test_feedexport.py
index 277555608..d7560b5ff 100644
--- a/tests/test_feedexport.py
+++ b/tests/test_feedexport.py
@@ -1731,6 +1731,7 @@ class FeedExportTest(FeedExportTestBase):
def store(self, file):
Storage.store_file = file
+ Storage.file_was_closed = file.closed
file.close()
settings = {
@@ -1746,6 +1747,7 @@ class FeedExportTest(FeedExportTestBase):
}
yield self.exported_no_data(settings)
self.assertIs(Storage.open_file, Storage.store_file)
+ self.assertFalse(Storage.file_was_closed)
class FeedPostProcessedExportsTest(FeedExportTestBase):