Scrapy, a fast high-level web crawling & scraping framework for Python.
add test for PythonItemExporter binary mode
Elias Dornelese938752973b4
diff --git a/scrapy/exporters.py b/scrapy/exporters.pyindex 8d7ffbc71..118df34a7 100644--- a/scrapy/exporters.py+++ b/scrapy/exporters.py@@ -272,7 +272,11 @@ class PythonItemExporter(BaseItemExporter):def _serialize_dict(self, value):for key, val in six.iteritems(value):+ key = to_bytes(key) if self.binary else keyyield key, self._serialize_value(val)def export_item(self, item):- return dict(self._get_serialized_fields(item))+ result = dict(self._get_serialized_fields(item))+ if self.binary:+ result = dict(self._serialize_dict(result))+ return resultdiff --git a/tests/test_exporters.py b/tests/test_exporters.pyindex 39e996062..9e57745dc 100644--- a/tests/test_exporters.py+++ b/tests/test_exporters.py@@ -2,6 +2,7 @@ from __future__ import absolute_importimport reimport jsonimport unittest+import warningsfrom io import BytesIOfrom six.moves import cPickle as pickle@@ -115,6 +116,12 @@ class PythonItemExporterTest(BaseItemExporterTest):self.assertEqual(type(exported['age'][0]), dict)self.assertEqual(type(exported['age'][0]['age'][0]), dict)+ def test_export_binary(self):+ exporter = PythonItemExporter(binary=True)+ value = TestItem(name=u'John\xa3', age=u'22')+ expected = {b'name': b'John\xc2\xa3', b'age': b'22'}+ self.assertEqual(expected, exporter.export_item(value))+class PprintItemExporterTest(BaseItemExporterTest):