mirror of https://github.com/scrapy/scrapy.git
warn if Link objects are instantiated with unicode urls
This commit is contained in:
parent
30b6c77ce5
commit
b4f368c37e
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
Loading…
Reference in New Issue