warn if Link objects are instantiated with unicode urls

This commit is contained in:
Pablo Hoffman 2012-05-16 13:12:25 -03:00
parent 30b6c77ce5
commit b4f368c37e
2 changed files with 13 additions and 0 deletions

View File

@ -11,6 +11,11 @@ class Link(object):
__slots__ = ['url', 'text', 'fragment', 'nofollow']
def __init__(self, url, text='', fragment='', nofollow=False):
if isinstance(url, unicode):
import warnings
warnings.warn("Do not instantiate Link objects with unicode urls. " \
"Assuming utf-8 encoding (which could be wrong)")
url = url.encode('utf-8')
self.url = url
self.text = text
self.fragment = fragment

View File

@ -1,4 +1,5 @@
import unittest
import warnings
from scrapy.link import Link
@ -41,3 +42,10 @@ class LinkTest(unittest.TestCase):
l1 = Link("http://www.example.com", text="test", fragment='something', nofollow=True)
l2 = eval(repr(l1))
self._assert_same_links(l1, l2)
def test_unicode_url(self):
with warnings.catch_warnings(record=True) as w:
l = Link(u"http://www.example.com/\xa3")
assert isinstance(l.url, str)
assert l.url == 'http://www.example.com/\xc2\xa3'
assert len(w) == 1, "warning not issued"