From b830e95e15db6822934a3138cdf9c46bd303cd40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Gra=C3=B1a?= Date: Wed, 4 Apr 2012 16:17:18 -0300 Subject: [PATCH] do not fail handling unicode xpaths in libxml2 backed selectors --- scrapy/selector/libxml2sel.py | 1 + scrapy/tests/test_selector.py | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/scrapy/selector/libxml2sel.py b/scrapy/selector/libxml2sel.py index 3784902e0..9342c9639 100644 --- a/scrapy/selector/libxml2sel.py +++ b/scrapy/selector/libxml2sel.py @@ -37,6 +37,7 @@ class XPathSelector(object_ref): def select(self, xpath): if hasattr(self.xmlNode, 'xpathEval'): self.doc.xpathContext.setContextNode(self.xmlNode) + xpath = unicode_to_str(xpath, 'utf-8') try: xpath_result = self.doc.xpathContext.xpathEval(xpath) except libxml2.xpathError: diff --git a/scrapy/tests/test_selector.py b/scrapy/tests/test_selector.py index 5ee0c6625..a0ae3d2c8 100644 --- a/scrapy/tests/test_selector.py +++ b/scrapy/tests/test_selector.py @@ -43,6 +43,12 @@ class XPathSelectorTestCase(unittest.TestCase): self.assertEqual([x.extract() for x in xpath.select("concat(//input[@name='a']/@value, //input[@name='b']/@value)")], [u'12']) + def test_selector_unicode_query(self): + body = u"

" + response = TextResponse(url="http://example.com", body=body, encoding='utf8') + xpath = self.hxs_cls(response) + self.assertEqual(xpath.select(u'//input[@name="\xa9"]/@value').extract(), [u'1']) + @libxml2debug def test_selector_same_type(self): """Test XPathSelector returning the same type in x() method"""