Skip to content

Commit a958cb2

Browse files
Using next() for Python 3 compatibility
1 parent 911c808 commit a958cb2

File tree

7 files changed

+143
-143
lines changed

7 files changed

+143
-143
lines changed

scrapy/core/engine.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def _next_request(self, spider):
108108

109109
if slot.start_requests and not self._needs_backout(spider):
110110
try:
111-
request = slot.start_requests.next()
111+
request = next(slot.start_requests)
112112
except StopIteration:
113113
slot.start_requests = None
114114
except Exception, exc:

scrapy/tests/test_utils_iterators.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def test_xmliter_namespaces(self):
6161
response = XmlResponse(url='http://mydummycompany.com', body=body)
6262
my_iter = self.xmliter(response, 'item')
6363

64-
node = my_iter.next()
64+
node = next(my_iter)
6565
node.register_namespace('g', 'http://base.google.com/ns/1.0')
6666
self.assertEqual(node.xpath('title/text()').extract(), ['Item 1'])
6767
self.assertEqual(node.xpath('description/text()').extract(), ['This is item 1'])
@@ -77,10 +77,10 @@ def test_xmliter_exception(self):
7777
body = u"""<?xml version="1.0" encoding="UTF-8"?><products><product>one</product><product>two</product></products>"""
7878

7979
iter = self.xmliter(body, 'product')
80-
iter.next()
81-
iter.next()
80+
next(iter)
81+
next(iter)
8282

83-
self.assertRaises(StopIteration, iter.next)
83+
self.assertRaises(StopIteration, next, iter)
8484

8585
def test_xmliter_encoding(self):
8686
body = '<?xml version="1.0" encoding="ISO-8859-9"?>\n<xml>\n <item>Some Turkish Characters \xd6\xc7\xde\xdd\xd0\xdc \xfc\xf0\xfd\xfe\xe7\xf6</item>\n</xml>\n\n'
@@ -122,9 +122,9 @@ def test_xmliter_iterate_namespace(self):
122122
self.assertEqual(len(list(no_namespace_iter)), 0)
123123

124124
namespace_iter = self.xmliter(response, 'image_link', 'http://base.google.com/ns/1.0')
125-
node = namespace_iter.next()
125+
node = next(namespace_iter)
126126
self.assertEqual(node.xpath('text()').extract(), ['http://www.mydummycompany.com/images/item1.jpg'])
127-
node = namespace_iter.next()
127+
node = next(namespace_iter)
128128
self.assertEqual(node.xpath('text()').extract(), ['http://www.mydummycompany.com/images/item2.jpg'])
129129

130130

@@ -205,12 +205,12 @@ def test_csviter_exception(self):
205205

206206
response = TextResponse(url="http://example.com/", body=body)
207207
iter = csviter(response)
208-
iter.next()
209-
iter.next()
210-
iter.next()
211-
iter.next()
208+
next(iter)
209+
next(iter)
210+
next(iter)
211+
next(iter)
212212

213-
self.assertRaises(StopIteration, iter.next)
213+
self.assertRaises(StopIteration, next, iter)
214214

215215
def test_csviter_encoding(self):
216216
body1 = get_testdata('feeds', 'feed-sample4.csv')

scrapy/tests/test_utils_python.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class Obj:
121121
def test_weakkeycache(self):
122122
class _Weakme(object): pass
123123
_values = count()
124-
wk = WeakKeyCache(lambda k: _values.next())
124+
wk = WeakKeyCache(lambda k: next(_values))
125125
k = _Weakme()
126126
v = wk[k]
127127
self.assertEqual(v, wk[k])

scrapy/utils/defer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def iter_errback(iterable, errback, *a, **kw):
9393
it = iter(iterable)
9494
while 1:
9595
try:
96-
yield it.next()
96+
yield next(it)
9797
except StopIteration:
9898
break
9999
except:

scrapy/utils/iterators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def csviter(obj, delimiter=None, headers=None, encoding=None):
4747
"""
4848
encoding = obj.encoding if isinstance(obj, TextResponse) else encoding or 'utf-8'
4949
def _getrow(csv_r):
50-
return [str_to_unicode(field, encoding) for field in csv_r.next()]
50+
return [str_to_unicode(field, encoding) for field in next(csv_r)]
5151

5252
lines = StringIO(body_or_str(obj, unicode=False))
5353
if delimiter:

scrapy/xlib/ordereddict.py

Lines changed: 127 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,127 +1,127 @@
1-
# Copyright (c) 2009 Raymond Hettinger
2-
#
3-
# Permission is hereby granted, free of charge, to any person
4-
# obtaining a copy of this software and associated documentation files
5-
# (the "Software"), to deal in the Software without restriction,
6-
# including without limitation the rights to use, copy, modify, merge,
7-
# publish, distribute, sublicense, and/or sell copies of the Software,
8-
# and to permit persons to whom the Software is furnished to do so,
9-
# subject to the following conditions:
10-
#
11-
# The above copyright notice and this permission notice shall be
12-
# included in all copies or substantial portions of the Software.
13-
#
14-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15-
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16-
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17-
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
18-
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19-
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20-
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21-
# OTHER DEALINGS IN THE SOFTWARE.
22-
23-
from UserDict import DictMixin
24-
25-
class OrderedDict(dict, DictMixin):
26-
27-
def __init__(self, *args, **kwds):
28-
if len(args) > 1:
29-
raise TypeError('expected at most 1 arguments, got %d' % len(args))
30-
try:
31-
self.__end
32-
except AttributeError:
33-
self.clear()
34-
self.update(*args, **kwds)
35-
36-
def clear(self):
37-
self.__end = end = []
38-
end += [None, end, end] # sentinel node for doubly linked list
39-
self.__map = {} # key --> [key, prev, next]
40-
dict.clear(self)
41-
42-
def __setitem__(self, key, value):
43-
if key not in self:
44-
end = self.__end
45-
curr = end[1]
46-
curr[2] = end[1] = self.__map[key] = [key, curr, end]
47-
dict.__setitem__(self, key, value)
48-
49-
def __delitem__(self, key):
50-
dict.__delitem__(self, key)
51-
key, prev, next = self.__map.pop(key)
52-
prev[2] = next
53-
next[1] = prev
54-
55-
def __iter__(self):
56-
end = self.__end
57-
curr = end[2]
58-
while curr is not end:
59-
yield curr[0]
60-
curr = curr[2]
61-
62-
def __reversed__(self):
63-
end = self.__end
64-
curr = end[1]
65-
while curr is not end:
66-
yield curr[0]
67-
curr = curr[1]
68-
69-
def popitem(self, last=True):
70-
if not self:
71-
raise KeyError('dictionary is empty')
72-
if last:
73-
key = reversed(self).next()
74-
else:
75-
key = iter(self).next()
76-
value = self.pop(key)
77-
return key, value
78-
79-
def __reduce__(self):
80-
items = [[k, self[k]] for k in self]
81-
tmp = self.__map, self.__end
82-
del self.__map, self.__end
83-
inst_dict = vars(self).copy()
84-
self.__map, self.__end = tmp
85-
if inst_dict:
86-
return (self.__class__, (items,), inst_dict)
87-
return self.__class__, (items,)
88-
89-
def keys(self):
90-
return list(self)
91-
92-
setdefault = DictMixin.setdefault
93-
update = DictMixin.update
94-
pop = DictMixin.pop
95-
values = DictMixin.values
96-
items = DictMixin.items
97-
iterkeys = DictMixin.iterkeys
98-
itervalues = DictMixin.itervalues
99-
iteritems = DictMixin.iteritems
100-
101-
def __repr__(self):
102-
if not self:
103-
return '%s()' % (self.__class__.__name__,)
104-
return '%s(%r)' % (self.__class__.__name__, self.items())
105-
106-
def copy(self):
107-
return self.__class__(self)
108-
109-
@classmethod
110-
def fromkeys(cls, iterable, value=None):
111-
d = cls()
112-
for key in iterable:
113-
d[key] = value
114-
return d
115-
116-
def __eq__(self, other):
117-
if isinstance(other, OrderedDict):
118-
if len(self) != len(other):
119-
return False
120-
for p, q in zip(self.items(), other.items()):
121-
if p != q:
122-
return False
123-
return True
124-
return dict.__eq__(self, other)
125-
126-
def __ne__(self, other):
127-
return not self == other
1+
# Copyright (c) 2009 Raymond Hettinger
2+
#
3+
# Permission is hereby granted, free of charge, to any person
4+
# obtaining a copy of this software and associated documentation files
5+
# (the "Software"), to deal in the Software without restriction,
6+
# including without limitation the rights to use, copy, modify, merge,
7+
# publish, distribute, sublicense, and/or sell copies of the Software,
8+
# and to permit persons to whom the Software is furnished to do so,
9+
# subject to the following conditions:
10+
#
11+
# The above copyright notice and this permission notice shall be
12+
# included in all copies or substantial portions of the Software.
13+
#
14+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16+
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
18+
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19+
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20+
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21+
# OTHER DEALINGS IN THE SOFTWARE.
22+
23+
from UserDict import DictMixin
24+
25+
class OrderedDict(dict, DictMixin):
26+
27+
def __init__(self, *args, **kwds):
28+
if len(args) > 1:
29+
raise TypeError('expected at most 1 arguments, got %d' % len(args))
30+
try:
31+
self.__end
32+
except AttributeError:
33+
self.clear()
34+
self.update(*args, **kwds)
35+
36+
def clear(self):
37+
self.__end = end = []
38+
end += [None, end, end] # sentinel node for doubly linked list
39+
self.__map = {} # key --> [key, prev, next]
40+
dict.clear(self)
41+
42+
def __setitem__(self, key, value):
43+
if key not in self:
44+
end = self.__end
45+
curr = end[1]
46+
curr[2] = end[1] = self.__map[key] = [key, curr, end]
47+
dict.__setitem__(self, key, value)
48+
49+
def __delitem__(self, key):
50+
dict.__delitem__(self, key)
51+
key, prev, next = self.__map.pop(key)
52+
prev[2] = next
53+
next[1] = prev
54+
55+
def __iter__(self):
56+
end = self.__end
57+
curr = end[2]
58+
while curr is not end:
59+
yield curr[0]
60+
curr = curr[2]
61+
62+
def __reversed__(self):
63+
end = self.__end
64+
curr = end[1]
65+
while curr is not end:
66+
yield curr[0]
67+
curr = curr[1]
68+
69+
def popitem(self, last=True):
70+
if not self:
71+
raise KeyError('dictionary is empty')
72+
if last:
73+
key = next(reversed(self))
74+
else:
75+
key = next(iter(self))
76+
value = self.pop(key)
77+
return key, value
78+
79+
def __reduce__(self):
80+
items = [[k, self[k]] for k in self]
81+
tmp = self.__map, self.__end
82+
del self.__map, self.__end
83+
inst_dict = vars(self).copy()
84+
self.__map, self.__end = tmp
85+
if inst_dict:
86+
return (self.__class__, (items,), inst_dict)
87+
return self.__class__, (items,)
88+
89+
def keys(self):
90+
return list(self)
91+
92+
setdefault = DictMixin.setdefault
93+
update = DictMixin.update
94+
pop = DictMixin.pop
95+
values = DictMixin.values
96+
items = DictMixin.items
97+
iterkeys = DictMixin.iterkeys
98+
itervalues = DictMixin.itervalues
99+
iteritems = DictMixin.iteritems
100+
101+
def __repr__(self):
102+
if not self:
103+
return '%s()' % (self.__class__.__name__,)
104+
return '%s(%r)' % (self.__class__.__name__, self.items())
105+
106+
def copy(self):
107+
return self.__class__(self)
108+
109+
@classmethod
110+
def fromkeys(cls, iterable, value=None):
111+
d = cls()
112+
for key in iterable:
113+
d[key] = value
114+
return d
115+
116+
def __eq__(self, other):
117+
if isinstance(other, OrderedDict):
118+
if len(self) != len(other):
119+
return False
120+
for p, q in zip(self.items(), other.items()):
121+
if p != q:
122+
return False
123+
return True
124+
return dict.__eq__(self, other)
125+
126+
def __ne__(self, other):
127+
return not self == other

scrapy/xlib/tx/endpoints.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ def _tokenize(description):
810810
current = ''
811811
ops = nextOps[n]
812812
elif n == '\\':
813-
current += description.next()
813+
current += next(description)
814814
else:
815815
current += n
816816
yield _STRING, current

0 commit comments

Comments
 (0)