mirror of https://github.com/scrapy/scrapy.git
fix PythonItemExporter for non-string types
This commit is contained in:
parent
7d24df3738
commit
f1d971a5c0
|
|
@ -273,10 +273,10 @@ class PythonItemExporter(BaseItemExporter):
|
|||
return dict(self._serialize_dict(value))
|
||||
if is_listlike(value):
|
||||
return [self._serialize_value(v) for v in value]
|
||||
if self.binary:
|
||||
return to_bytes(value, encoding=self.encoding)
|
||||
else:
|
||||
return to_unicode(value, encoding=self.encoding)
|
||||
encode_func = to_bytes if self.binary else to_unicode
|
||||
if isinstance(value, (six.text_type, bytes)):
|
||||
return encode_func(value, encoding=self.encoding)
|
||||
return value
|
||||
|
||||
def _serialize_dict(self, value):
|
||||
for key, val in six.iteritems(value):
|
||||
|
|
|
|||
|
|
@ -134,6 +134,19 @@ class PythonItemExporterTest(BaseItemExporterTest):
|
|||
expected = {b'name': b'John\xc2\xa3', b'age': b'22'}
|
||||
self.assertEqual(expected, exporter.export_item(value))
|
||||
|
||||
def test_other_python_types_item(self):
|
||||
from datetime import datetime
|
||||
now = datetime.now()
|
||||
item = {
|
||||
'boolean': False,
|
||||
'number': 22,
|
||||
'time': now,
|
||||
'float': 3.14,
|
||||
}
|
||||
ie = self._get_exporter()
|
||||
exported = ie.export_item(item)
|
||||
self.assertEqual(exported, item)
|
||||
|
||||
|
||||
class PprintItemExporterTest(BaseItemExporterTest):
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue