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

This commit is contained in:
Adrián Chaves 2024-02-21 14:27:42 +01:00
parent 3562618f67
commit 82981fb8a2
3 changed files with 9 additions and 4 deletions

View File

@ -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.

View File

@ -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

View File

@ -1732,6 +1732,7 @@ class FeedExportTest(FeedExportTestBase):
def store(self, file):
Storage.store_file = file
Storage.file_was_closed = file.closed
file.close()
settings = {
@ -1747,6 +1748,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):