Skip to content

Commit 91991e0

Browse files
committed
PY port scrapy.utils.reqser
1 parent 95e6bd2 commit 91991e0

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

scrapy/utils/reqser.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
"""
22
Helper functions for serializing (and deserializing) requests.
33
"""
4+
import six
45

56
from scrapy.http import Request
7+
from scrapy.utils.python import to_unicode, to_native_str
8+
69

710
def request_to_dict(request, spider=None):
811
"""Convert Request object to a dict.
@@ -17,7 +20,7 @@ def request_to_dict(request, spider=None):
1720
if callable(eb):
1821
eb = _find_method(spider, eb)
1922
d = {
20-
'url': request.url.decode('ascii'), # urls should be safe (safe_string_url)
23+
'url': to_unicode(request.url), # urls should be safe (safe_string_url)
2124
'callback': cb,
2225
'errback': eb,
2326
'method': request.method,
@@ -45,7 +48,7 @@ def request_from_dict(d, spider=None):
4548
if eb and spider:
4649
eb = _get_method(spider, eb)
4750
return Request(
48-
url=d['url'].encode('ascii'),
51+
url=to_native_str(d['url']),
4952
callback=cb,
5053
errback=eb,
5154
method=d['method'],
@@ -59,10 +62,16 @@ def request_from_dict(d, spider=None):
5962

6063

6164
def _find_method(obj, func):
62-
if obj and hasattr(func, 'im_self') and func.im_self is obj:
63-
return func.im_func.__name__
64-
else:
65-
raise ValueError("Function %s is not a method of: %s" % (func, obj))
65+
if obj:
66+
try:
67+
func_self = six.get_method_self(func)
68+
except AttributeError: # func has no __self__
69+
pass
70+
else:
71+
if func_self is obj:
72+
return six.get_method_function(func).__name__
73+
raise ValueError("Function %s is not a method of: %s" % (func, obj))
74+
6675

6776
def _get_method(obj, name):
6877
name = str(name)

tests/py3-ignores.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ tests/test_spider.py
3939
tests/test_stats.py
4040
tests/test_utils_iterators.py
4141
tests/test_utils_log.py
42-
tests/test_utils_reqser.py
4342
tests/test_utils_template.py
4443
tests/test_webclient.py
4544

tests/test_utils_reqser.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
# -*- coding: utf-8 -*-
12
import unittest
23

34
from scrapy.http import Request
45
from scrapy.spiders import Spider
56
from scrapy.utils.reqser import request_to_dict, request_from_dict
67

8+
79
class RequestSerializationTest(unittest.TestCase):
810

911
def setUp(self):
@@ -20,18 +22,18 @@ def test_all_attributes(self):
2022
method="POST",
2123
body="some body",
2224
headers={'content-encoding': 'text/html; charset=latin-1'},
23-
cookies={'currency': 'usd'},
25+
cookies={'currency': u'руб'},
2426
encoding='latin-1',
2527
priority=20,
2628
meta={'a': 'b'})
2729
self._assert_serializes_ok(r)
2830

2931
def test_latin1_body(self):
30-
r = Request("http://www.example.com/", body="\xa3")
32+
r = Request("http://www.example.com/", body=b"\xa3")
3133
self._assert_serializes_ok(r)
3234

3335
def test_utf8_body(self):
34-
r = Request("http://www.example.com/", body="\xc2\xa3")
36+
r = Request("http://www.example.com/", body=b"\xc2\xa3")
3537
self._assert_serializes_ok(r)
3638

3739
def _assert_serializes_ok(self, request, spider=None):
@@ -53,8 +55,8 @@ def _assert_same_request(self, r1, r2):
5355
self.assertEqual(r1.dont_filter, r2.dont_filter)
5456

5557
def test_callback_serialization(self):
56-
r = Request("http://www.example.com/", callback=self.spider.parse_item, \
57-
errback=self.spider.handle_error)
58+
r = Request("http://www.example.com/", callback=self.spider.parse_item,
59+
errback=self.spider.handle_error)
5860
self._assert_serializes_ok(r, spider=self.spider)
5961

6062
def test_unserializable_callback1(self):
@@ -69,7 +71,9 @@ def test_unserializable_callback2(self):
6971

7072
class TestSpider(Spider):
7173
name = 'test'
74+
7275
def parse_item(self, response):
7376
pass
77+
7478
def handle_error(self, failure):
7579
pass

0 commit comments

Comments
 (0)