mirror of https://github.com/scrapy/scrapy.git
remove selector support for LxmlDocument DOM cache and add deprecation warning for _root argument
This commit is contained in:
parent
3a572e2f3b
commit
01d948f0fd
|
|
@ -2,13 +2,13 @@
|
|||
XPath selectors based on lxml
|
||||
"""
|
||||
|
||||
import warnings
|
||||
from parsel import Selector as ParselSelector, SelectorList
|
||||
from scrapy.utils.trackref import object_ref
|
||||
from scrapy.utils.python import to_bytes
|
||||
from scrapy.http import HtmlResponse, XmlResponse
|
||||
from 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 = _root
|
||||
|
||||
if 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 None
|
||||
super(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()
|
||||
|
||||
|
|
|
|||
|
|
@ -7,6 +7,8 @@ from scrapy.exceptions import ScrapyDeprecationWarning
|
|||
from scrapy.http import TextResponse, HtmlResponse, XmlResponse
|
||||
from scrapy.selector import Selector
|
||||
from scrapy.selector.lxmlsel import XmlXPathSelector, HtmlXPathSelector, XPathSelector
|
||||
from lxml import etree
|
||||
from tests import mock
|
||||
|
||||
|
||||
class 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')
|
||||
|
|
|
|||
Loading…
Reference in New Issue