From de3e451564a07f5ceedc910946b5640417e495c0 Mon Sep 17 00:00:00 2001 From: Mikhail Korobov Date: Wed, 21 Aug 2013 06:05:40 +0600 Subject: [PATCH] fix XmlItemExporter in Python 2.7.4 and 2.7.5 --- scrapy/contrib/exporter/__init__.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/scrapy/contrib/exporter/__init__.py b/scrapy/contrib/exporter/__init__.py index ab0e9a94f..f7b25ccc1 100644 --- a/scrapy/contrib/exporter/__init__.py +++ b/scrapy/contrib/exporter/__init__.py @@ -3,6 +3,7 @@ Item Exporters are used to export/serialize items into different formats. """ import csv +import sys import pprint import marshal import json @@ -141,9 +142,23 @@ class XmlItemExporter(BaseItemExporter): for value in serialized_value: self._export_xml_field('value', value) else: - self.xg.characters(serialized_value) + self._xg_characters(serialized_value) self.xg.endElement(name) + # Workaround for http://bugs.python.org/issue17606 + # Before Python 2.7.4 xml.sax.saxutils required bytes; + # since 2.7.4 it requires unicode. The bug is likely to be + # fixed in 2.7.6, but 2.7.6 will still support unicode, + # and Python 3.x will require unicode, so ">= 2.7.4" should be fine. + if sys.version_info[:3] >= (2, 7, 4): + def _xg_characters(self, serialized_value): + if not isinstance(serialized_value, unicode): + serialized_value = serialized_value.decode(self.encoding) + return self.xg.characters(serialized_value) + else: + def _xg_characters(self, serialized_value): + return self.xg.characters(serialized_value) + class CsvItemExporter(BaseItemExporter):