Scrapy, a fast high-level web crawling & scraping framework for Python.
Test stream detaching in CsvItemExporter
Adrián Chaves2f2bcb006d34
diff --git a/scrapy/exporters.py b/scrapy/exporters.pyindex 243ec4fe1..42105690c 100644--- a/scrapy/exporters.py+++ b/scrapy/exporters.py@@ -248,10 +248,7 @@ class CsvItemExporter(BaseItemExporter):self.csv_writer.writerow(values)def finish_exporting(self):- # Detaching stream in order to avoid file closing.- # The file will be closed with slot.storage.store- # https://github.com/scrapy/scrapy/issues/5043- self.stream.detach()+ self.stream.detach() # Avoid closing the wrapped file.def _build_row(self, values):for s in values:diff --git a/tests/test_exporters.py b/tests/test_exporters.pyindex 69ac928c3..bec8d2267 100644--- a/tests/test_exporters.py+++ b/tests/test_exporters.py@@ -85,6 +85,10 @@ class BaseItemExporterTest(unittest.TestCase):if self.ie.__class__ is not BaseItemExporter:raiseself.ie.finish_exporting()+ # Delete the item exporter object, so that if it causes the output+ # file handle be closed, which should not be the case, follow-up+ # interactions with the output file handle will surface the issue.+ del self.ieself._check_output()def test_export_item(self):@@ -230,6 +234,7 @@ class PickleItemExporterTest(BaseItemExporterTest):ie.export_item(i1)ie.export_item(i2)ie.finish_exporting()+ del ie # See the first “del self.ie” in this file for context.f.seek(0)self.assertEqual(self.item_class(**pickle.load(f)), i1)self.assertEqual(self.item_class(**pickle.load(f)), i2)@@ -241,6 +246,7 @@ class PickleItemExporterTest(BaseItemExporterTest):ie.start_exporting()ie.export_item(item)ie.finish_exporting()+ del ie # See the first “del self.ie” in this file for context.self.assertEqual(pickle.loads(fp.getvalue()), item)@@ -267,6 +273,7 @@ class MarshalItemExporterTest(BaseItemExporterTest):ie.start_exporting()ie.export_item(item)ie.finish_exporting()+ del ie # See the first “del self.ie” in this file for context.fp.seek(0)self.assertEqual(marshal.load(fp), item)@@ -299,6 +306,7 @@ class CsvItemExporterTest(BaseItemExporterTest):ie.start_exporting()ie.export_item(item)ie.finish_exporting()+ del ie # See the first “del self.ie” in this file for context.self.assertCsvEqual(fp.getvalue(), expected)def test_header_export_all(self):@@ -330,6 +338,7 @@ class CsvItemExporterTest(BaseItemExporterTest):ie.export_item(item)ie.export_item(item)ie.finish_exporting()+ del ie # See the first “del self.ie” in this file for context.self.assertCsvEqual(output.getvalue(),b'age,name\r\n22,John\xc2\xa3\r\n22,John\xc2\xa3\r\n')@@ -414,6 +423,7 @@ class XmlItemExporterTest(BaseItemExporterTest):ie.start_exporting()ie.export_item(item)ie.finish_exporting()+ del ie # See the first “del self.ie” in this file for context.self.assertXmlEquivalent(fp.getvalue(), expected_value)def _check_output(self):@@ -520,6 +530,7 @@ class JsonLinesItemExporterTest(BaseItemExporterTest):self.ie.start_exporting()self.ie.export_item(i3)self.ie.finish_exporting()+ del self.ie # See the first “del self.ie” in this file for context.exported = json.loads(to_unicode(self.output.getvalue()))self.assertEqual(exported, self._expected_nested)@@ -534,6 +545,7 @@ class JsonLinesItemExporterTest(BaseItemExporterTest):self.ie.start_exporting()self.ie.export_item(item)self.ie.finish_exporting()+ del self.ie # See the first “del self.ie” in this file for context.exported = json.loads(to_unicode(self.output.getvalue()))item['time'] = str(item['time'])self.assertEqual(exported, item)@@ -561,6 +573,7 @@ class JsonItemExporterTest(JsonLinesItemExporterTest):self.ie.export_item(item)self.ie.export_item(item)self.ie.finish_exporting()+ del self.ie # See the first “del self.ie” in this file for context.exported = json.loads(to_unicode(self.output.getvalue()))self.assertEqual(exported, [ItemAdapter(item).asdict(), ItemAdapter(item).asdict()])@@ -577,6 +590,7 @@ class JsonItemExporterTest(JsonLinesItemExporterTest):self.ie.start_exporting()self.ie.export_item(i3)self.ie.finish_exporting()+ del self.ie # See the first “del self.ie” in this file for context.exported = json.loads(to_unicode(self.output.getvalue()))expected = {'name': 'Jesus', 'age': {'name': 'Maria', 'age': ItemAdapter(i1).asdict()}}self.assertEqual(exported, [expected])@@ -588,6 +602,7 @@ class JsonItemExporterTest(JsonLinesItemExporterTest):self.ie.start_exporting()self.ie.export_item(i3)self.ie.finish_exporting()+ del self.ie # See the first “del self.ie” in this file for context.exported = json.loads(to_unicode(self.output.getvalue()))expected = {'name': 'Jesus', 'age': {'name': 'Maria', 'age': i1}}self.assertEqual(exported, [expected])@@ -597,6 +612,7 @@ class JsonItemExporterTest(JsonLinesItemExporterTest):self.ie.start_exporting()self.ie.export_item(item)self.ie.finish_exporting()+ del self.ie # See the first “del self.ie” in this file for context.exported = json.loads(to_unicode(self.output.getvalue()))item['time'] = str(item['time'])self.assertEqual(exported, [item])