Scrapy, a fast high-level web crawling & scraping framework for Python.
remove selector support for LxmlDocument DOM cache and add deprecation warning for _root argument
Elias Dorneles01d948f0fd79
diff --git a/scrapy/selector/unified.py b/scrapy/selector/unified.pyindex 9e0716671..db2a8510a 100644--- a/scrapy/selector/unified.py+++ b/scrapy/selector/unified.py@@ -2,13 +2,13 @@XPath selectors based on lxml"""+import warnings+from parsel import Selector as ParselSelector, SelectorListfrom scrapy.utils.trackref import object_reffrom scrapy.utils.python import to_bytesfrom scrapy.http import HtmlResponse, XmlResponsefrom scrapy.utils.decorators import deprecated-from parsel import Selector as ParselSelector, SelectorList-from parsel.unified import _ctgroup-from .lxmldocument import LxmlDocument+from scrapy.exceptions import ScrapyDeprecationWarning__all__ = ['Selector', 'SelectorList']@@ -33,22 +33,21 @@ class Selector(ParselSelector, object_ref):__slots__ = ['response']- def __init__(self, response=None, text=None, type=None, root=None, **kwargs):+ def __init__(self, response=None, text=None, type=None, root=None, _root=None, **kwargs):st = _st(response, type or self._default_type)- # supporting legacy _root argument- root = kwargs.get('_root', root)-- self._parser = _ctgroup[st]['_parser']+ if root is None and _root is not None:+ warnings.warn("Argument `_root` is deprecated, use `root` instead",+ ScrapyDeprecationWarning, stacklevel=2)+ root = _rootif text is not None:response = _response_from_text(text, st)if response is not None:- root = LxmlDocument(response, self._parser)+ text = response.body_as_unicode()self.response = response- text = response.body_as_unicode() if response else Nonesuper(Selector, self).__init__(text=text, type=st, root=root, **kwargs)# Deprecated api@@ -59,4 +58,3 @@ class Selector(ParselSelector, object_ref):@deprecated(use_instead='.extract()')def extract_unquoted(self):return self.extract()-diff --git a/tests/test_selector.py b/tests/test_selector.pyindex 02f82298c..dd27ae7da 100644--- a/tests/test_selector.py+++ b/tests/test_selector.py@@ -7,6 +7,8 @@ from scrapy.exceptions import ScrapyDeprecationWarningfrom scrapy.http import TextResponse, HtmlResponse, XmlResponsefrom scrapy.selector import Selectorfrom scrapy.selector.lxmlsel import XmlXPathSelector, HtmlXPathSelector, XPathSelector+from lxml import etree+from tests import mockclass SelectorTestCase(unittest.TestCase):@@ -37,6 +39,15 @@ class SelectorTestCase(unittest.TestCase):self.assertEqual([x.extract() for x in sel.xpath("concat(//input[@name='a']/@value, //input[@name='b']/@value)")],[u'12'])+ @mock.patch('scrapy.selector.unified.warnings')+ def test_deprecated_root_argument(self, warnings):+ root = etree.fromstring(u'<html/>')+ sel = self.sscls(_root=root)+ self.assertEqual(root, sel._root)+ warnings.warn.assert_called_once_with(+ 'Argument `_root` is deprecated, use `root` instead',+ mock.ANY, stacklevel=2)+def test_representation_slice(self):body = u"<p><input name='{}' value='\xa9'/></p>".format(50 * 'b')response = TextResponse(url="http://example.com", body=body, encoding='utf8')