Scrapy, a fast high-level web crawling & scraping framework for Python.
make libxml2 optional now that lxml is the default
Daniel Grañaf530b0b3eb5b
diff --git a/scrapy/__init__.py b/scrapy/__init__.pyindex 76f704f7a..2e40a5b3c 100644--- a/scrapy/__init__.py+++ b/scrapy/__init__.py@@ -33,3 +33,10 @@ except ImportError:passelse:optional_features.add('boto')++try:+ import libxml2+except ImportError:+ pass+else:+ optional_features.add('libxml2')diff --git a/scrapy/selector/libxml2document.py b/scrapy/selector/libxml2document.pyindex a1b903772..070dd0c58 100644--- a/scrapy/selector/libxml2document.py+++ b/scrapy/selector/libxml2document.py@@ -4,16 +4,18 @@ garbage collection to libxml2 documents (xmlDoc)."""import weakref-import libxml2from scrapy.utils.trackref import object_ref+from scrapy import optional_features-xml_parser_options = libxml2.XML_PARSE_RECOVER + \- libxml2.XML_PARSE_NOERROR + \- libxml2.XML_PARSE_NOWARNING+if 'libxml2' in optional_features:+ import libxml2+ xml_parser_options = libxml2.XML_PARSE_RECOVER + \+ libxml2.XML_PARSE_NOERROR + \+ libxml2.XML_PARSE_NOWARNING-html_parser_options = libxml2.HTML_PARSE_RECOVER + \- libxml2.HTML_PARSE_NOERROR + \- libxml2.HTML_PARSE_NOWARNING+ html_parser_options = libxml2.HTML_PARSE_RECOVER + \+ libxml2.HTML_PARSE_NOERROR + \+ libxml2.HTML_PARSE_NOWARNING_UTF8_ENCODINGS = set(('utf-8', 'UTF-8', 'utf8', 'UTF8'))diff --git a/scrapy/selector/libxml2sel.py b/scrapy/selector/libxml2sel.pyindex 7bd3b8a38..da546ade2 100644--- a/scrapy/selector/libxml2sel.py+++ b/scrapy/selector/libxml2sel.py@@ -2,7 +2,9 @@XPath selectors based on libxml2"""-import libxml2+from scrapy import optional_features+if 'libxml2' in optional_features:+ import libxml2from scrapy.http import TextResponsefrom scrapy.utils.python import unicode_to_strdiff --git a/scrapy/tests/test_libxml2.py b/scrapy/tests/test_libxml2.pyindex 3f3249440..82f2ea0c9 100644--- a/scrapy/tests/test_libxml2.py+++ b/scrapy/tests/test_libxml2.py@@ -1,13 +1,12 @@from twisted.trial import unittestfrom scrapy.utils.test import libxml2debug+from scrapy import optional_features+class Libxml2Test(unittest.TestCase):- try:- import libxml2- except ImportError, e:- skip = str(e)+ skip = 'libxml2' not in optional_features@libxml2debugdef test_libxml2_bug_2_6_27(self):diff --git a/scrapy/tests/test_selector_libxml2.py b/scrapy/tests/test_selector_libxml2.pyindex e447a6297..5dabd2d01 100644--- a/scrapy/tests/test_selector_libxml2.py+++ b/scrapy/tests/test_selector_libxml2.py@@ -2,7 +2,9 @@Selectors tests, specific for libxml2 backend"""-import unittest+from twisted.trial import unittest+from scrapy import optional_features+from scrapy.http import TextResponse, HtmlResponse, XmlResponsefrom scrapy.selector.libxml2sel import XmlXPathSelector, HtmlXPathSelector, \@@ -18,6 +20,8 @@ class Libxml2XPathSelectorTestCase(test_selector.XPathSelectorTestCase):hxs_cls = HtmlXPathSelectorxxs_cls = XmlXPathSelector+ skip = 'libxml2' not in optional_features+@libxml2debugdef test_null_bytes(self):hxs = HtmlXPathSelector(text='<root>la\x00la</root>')@@ -62,6 +66,8 @@ class Libxml2XPathSelectorTestCase(test_selector.XPathSelectorTestCase):class Libxml2DocumentTest(unittest.TestCase):+ skip = 'libxml2' not in optional_features+@libxml2debugdef test_response_libxml2_caching(self):r1 = HtmlResponse('http://www.example.com', body='<html><head></head><body></body></html>')