added some unittests to make sure certain objects are using __slots__ and are also weak-referenceable

This commit is contained in:
Pablo Hoffman 2009-08-18 20:40:04 -03:00
parent 240e06813d
commit e947e1d45b
10 changed files with 49 additions and 4 deletions

View File

@ -22,6 +22,8 @@ def _unicode_to_str(string, encoding):
class FormRequest(Request):
__slots__ = ()
def __init__(self, *args, **kwargs):
formdata = kwargs.pop('formdata', None)
Request.__init__(self, *args, **kwargs)

View File

@ -12,6 +12,8 @@ from scrapy.utils.python import memoizemethod
class HtmlResponse(TextResponse):
__slots__ = ()
_template = r'''%s\s*=\s*["']?\s*%s\s*["']?'''
_httpequiv_re = _template % ('http-equiv', 'Content-Type')

View File

@ -12,6 +12,8 @@ from scrapy.utils.python import memoizemethod
class XmlResponse(TextResponse):
__slots__ = ()
_template = r'''%s\s*=\s*["']?\s*%s\s*["']?'''
_encoding_re = _template % ('encoding', r'(?P<charset>[\w-]+)')
XMLDECL_RE = re.compile(r'<\?xml\s.*?%s' % _encoding_re, re.I)

View File

@ -10,7 +10,7 @@ class Link(object):
At the moment, it contains just the url and link text.
"""
__slots__ = 'url', 'text'
__slots__ = ['url', 'text']
def __init__(self, url, text=''):
self.url = url

View File

@ -1,4 +1,5 @@
import unittest
import weakref
import copy
from scrapy.http import Headers
@ -103,3 +104,8 @@ class HeadersTest(unittest.TestCase):
self.assertEqual(h1.getlist('header1'), ['value1'])
self.assertEqual(h1.getlist('header2'), ['value2', 'value3'])
def test_slots(self):
"""Check that classes are using slots and are weak-referenceable"""
x = Headers({})
assert not hasattr(x, '__dict__'), "%s does not use __slots__" % \
x.__class__.__name__

View File

@ -1,5 +1,6 @@
import unittest
import cgi
import weakref
from cStringIO import StringIO
from urlparse import urlparse
@ -166,6 +167,14 @@ class RequestTest(unittest.TestCase):
self.assertEqual(r4.meta, {})
assert r4.dont_filter is False
def test_weakref_slots(self):
"""Check that classes are using slots and are weak-referenceable"""
for cls in [Request, FormRequest]:
x = cls('http://www.example.com')
weakref.ref(x)
assert not hasattr(x, '__dict__'), "%s does not use __slots__" % \
x.__class__.__name__
class FormRequestTest(unittest.TestCase):
@ -299,6 +308,5 @@ class XmlRpcRequestTest(unittest.TestCase):
self.assertEqual(r1.body, r2.body)
if __name__ == "__main__":
unittest.main()

View File

@ -1,4 +1,6 @@
import unittest
import weakref
from scrapy.http import Response, TextResponse, HtmlResponse, XmlResponse, Headers
class ResponseTest(unittest.TestCase):
@ -201,6 +203,14 @@ class ResponseTest(unittest.TestCase):
self._assert_response_values(r5, 'iso-8859-1', body)
self._assert_response_values(r6, 'utf-8', body2)
def test_weakref_slots(self):
"""Check that classes are using slots and are weak-referenceable"""
for cls in [Response, TextResponse, XmlResponse, HtmlResponse]:
x = cls('http://www.example.com')
weakref.ref(x)
assert not hasattr(x, '__dict__'), "%s does not use __slots__" % \
x.__class__.__name__
if __name__ == "__main__":
unittest.main()

View File

@ -1,10 +1,12 @@
import re
import unittest
import weakref
import libxml2
from scrapy.http import TextResponse, HtmlResponse, XmlResponse
from scrapy.xpath.selector import XmlXPathSelector, HtmlXPathSelector
from scrapy.xpath.selector import XmlXPathSelector, HtmlXPathSelector, \
XPathSelector
from scrapy.xpath.document import Libxml2Document
from scrapy.utils.test import libxml2debug
@ -239,6 +241,15 @@ class XPathSelectorTestCase(unittest.TestCase):
u'\n ',
u'\n pff\n'])
@libxml2debug
def test_weakref_slots(self):
"""Check that classes are using slots and are weak-referenceable"""
for cls in [XPathSelector, HtmlXPathSelector, XmlXPathSelector]:
x = cls()
weakref.ref(x)
assert not hasattr(x, '__dict__'), "%s does not use __slots__" % \
x.__class__.__name__
class Libxml2DocumentTest(unittest.TestCase):
@libxml2debug

View File

@ -160,6 +160,9 @@ class SiteNode(object):
class CaselessDict(dict):
__slots__ = ()
def __init__(self, seq=None):
super(CaselessDict, self).__init__()
if seq:

View File

@ -140,9 +140,10 @@ class XPathSelectorList(list):
class XmlXPathSelector(XPathSelector):
"""XPathSelector for XML content"""
__slots__ = ()
_get_libxml2_doc = staticmethod(xmlDoc_from_xml)
class HtmlXPathSelector(XPathSelector):
"""XPathSelector for HTML content"""
__slots__ = ()
_get_libxml2_doc = staticmethod(xmlDoc_from_html)