diff --git a/scrapy/__init__.py b/scrapy/__init__.py index 76f704f7a..2e40a5b3c 100644 --- a/scrapy/__init__.py +++ b/scrapy/__init__.py @@ -33,3 +33,10 @@ except ImportError: pass else: 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.py index 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 libxml2 from 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.py index 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 libxml2 from scrapy.http import TextResponse from scrapy.utils.python import unicode_to_str diff --git a/scrapy/tests/test_libxml2.py b/scrapy/tests/test_libxml2.py index 3f3249440..82f2ea0c9 100644 --- a/scrapy/tests/test_libxml2.py +++ b/scrapy/tests/test_libxml2.py @@ -1,13 +1,12 @@ from twisted.trial import unittest from 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 @libxml2debug def test_libxml2_bug_2_6_27(self): diff --git a/scrapy/tests/test_selector_libxml2.py b/scrapy/tests/test_selector_libxml2.py index 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, XmlResponse from scrapy.selector.libxml2sel import XmlXPathSelector, HtmlXPathSelector, \ @@ -18,6 +20,8 @@ class Libxml2XPathSelectorTestCase(test_selector.XPathSelectorTestCase): hxs_cls = HtmlXPathSelector xxs_cls = XmlXPathSelector + skip = 'libxml2' not in optional_features + @libxml2debug def test_null_bytes(self): hxs = HtmlXPathSelector(text='la\x00la') @@ -62,6 +66,8 @@ class Libxml2XPathSelectorTestCase(test_selector.XPathSelectorTestCase): class Libxml2DocumentTest(unittest.TestCase): + skip = 'libxml2' not in optional_features + @libxml2debug def test_response_libxml2_caching(self): r1 = HtmlResponse('http://www.example.com', body='')