provide __next__() and use native next() to support py2/py3 in one source. refs #1350.

This commit is contained in:
Takayuki Shimizukawa
2014-04-29 21:54:25 +09:00
parent 49944cfa52
commit 91b9d75ac8
8 changed files with 33 additions and 29 deletions

View File

@@ -56,9 +56,9 @@ UTF8StreamReader = codecs.lookup('utf-8')[2]
def read_inventory_v1(f, uri, join):
f = UTF8StreamReader(f)
invdata = {}
line = f.next()
line = next(f)
projname = line.rstrip()[11:]
line = f.next()
line = next(f)
version = line.rstrip()[11:]
for line in f:
name, type, location = line.rstrip().split(None, 2)

View File

@@ -190,7 +190,7 @@ class GoogleDocstring(object):
line = self._line_iter.peek()
while(not self._is_section_break()
and (not line or self._is_indented(line, indent))):
lines.append(self._line_iter.next())
lines.append(next(self._line_iter))
line = self._line_iter.peek()
return lines
@@ -199,19 +199,19 @@ class GoogleDocstring(object):
while (self._line_iter.has_next()
and self._line_iter.peek()
and not self._is_section_header()):
lines.append(self._line_iter.next())
lines.append(next(self._line_iter))
return lines
def _consume_empty(self):
lines = []
line = self._line_iter.peek()
while self._line_iter.has_next() and not line:
lines.append(self._line_iter.next())
lines.append(next(self._line_iter))
line = self._line_iter.peek()
return lines
def _consume_field(self, parse_type=True, prefer_type=False):
line = self._line_iter.next()
line = next(self._line_iter)
match = None
_name, _type, _desc = line.strip(), '', ''
@@ -268,7 +268,7 @@ class GoogleDocstring(object):
return []
def _consume_section_header(self):
section = self._line_iter.next()
section = next(self._line_iter)
stripped_section = section.strip(':')
if stripped_section.lower() in self._sections:
section = stripped_section
@@ -278,7 +278,7 @@ class GoogleDocstring(object):
self._consume_empty()
lines = []
while not self._is_section_break():
lines.append(self._line_iter.next())
lines.append(next(self._line_iter))
return lines + self._consume_empty()
def _dedent(self, lines, full=False):
@@ -708,7 +708,7 @@ class NumpyDocstring(GoogleDocstring):
name, obj, options)
def _consume_field(self, parse_type=True, prefer_type=False):
line = self._line_iter.next()
line = next(self._line_iter)
if parse_type:
_name, _, _type = line.partition(':')
else:
@@ -725,10 +725,10 @@ class NumpyDocstring(GoogleDocstring):
return self._consume_fields(prefer_type=True)
def _consume_section_header(self):
section = self._line_iter.next()
section = next(self._line_iter)
if not _directive_regex.match(section):
# Consume the header underline
self._line_iter.next()
next(self._line_iter)
return section
def _is_section_break(self):

View File

@@ -76,7 +76,7 @@ class peek_iter(object):
n = 1
try:
while len(self._cache) < n:
self._cache.append(self._iterable.next())
self._cache.append(next(self._iterable))
except StopIteration:
while len(self._cache) < n:
self._cache.append(self.sentinel)
@@ -239,7 +239,7 @@ class modify_iter(peek_iter):
n = 1
try:
while len(self._cache) < n:
self._cache.append(self.modifier(self._iterable.next()))
self._cache.append(self.modifier(next(self._iterable)))
except StopIteration:
while len(self._cache) < n:
self._cache.append(self.sentinel)

View File

@@ -324,9 +324,9 @@ class ParserGenerator(object):
return value
def gettoken(self):
tup = self.generator.next()
tup = next(self.generator)
while tup[0] in (tokenize.COMMENT, tokenize.NL):
tup = self.generator.next()
tup = next(self.generator)
self.type, self.value, self.begin, self.end, self.line = tup
#print token.tok_name[self.type], repr(self.value)

View File

@@ -421,11 +421,13 @@ class PeekableIterator(object):
def __iter__(self):
return self
def next(self):
def __next__(self):
"""Return the next item from the iterator."""
if self.remaining:
return self.remaining.popleft()
return self._iterator.next()
return next(self._iterator)
next = __next__ # Python 2 compatibility
def push(self, item):
"""Push the `item` on the internal stack, it will be returned on the
@@ -435,6 +437,6 @@ class PeekableIterator(object):
def peek(self):
"""Return the next item without changing the state of the iterator."""
item = self.next()
item = next(self)
self.push(item)
return item

View File

@@ -35,9 +35,9 @@ class BooleanParser(Parser):
node = nodes.Const(None, lineno=token.lineno)
else:
node = nodes.Name(token.value, 'load', lineno=token.lineno)
self.stream.next()
next(self.stream)
elif token.type == 'lparen':
self.stream.next()
next(self.stream)
node = self.parse_expression()
self.stream.expect('rparen')
else:

View File

@@ -1187,7 +1187,7 @@ class _IterParseIterator(object):
append((event, None))
parser.EndNamespaceDeclHandler = handler
def next(self):
def __next__(self):
while 1:
try:
item = self._events[self._index]
@@ -1208,6 +1208,8 @@ class _IterParseIterator(object):
self._index = self._index + 1
return item
next = __next__ # Python 2 compatibility
def __iter__(self):
return self

View File

@@ -29,7 +29,7 @@ class BaseIteratorsTest(TestCase):
self.assertTrueTwice(it.has_next)
self.assertEqualTwice(expected, it.peek)
self.assertTrueTwice(it.has_next)
self.assertEqual(expected, it.next())
self.assertEqual(expected, next(it))
if is_last:
self.assertFalseTwice(it.has_next)
self.assertRaisesTwice(StopIteration, it.next)
@@ -217,7 +217,7 @@ class PeekIterTest(BaseIteratorsTest):
self.assertTrueTwice(it.has_next)
self.assertEqualTwice(['1', '2', '3', it.sentinel], it.peek, 4)
self.assertTrueTwice(it.has_next)
self.assertEqual('1', it.next())
self.assertEqual('1', next(it))
self.assertTrueTwice(it.has_next)
self.assertEqualTwice(['2', '3'], it.peek, 2)
self.assertTrueTwice(it.has_next)
@@ -237,7 +237,7 @@ class PeekIterTest(BaseIteratorsTest):
it = peek_iter(a)
self.assertTrueTwice(it.has_next)
self.assertEqualTwice('1', it.peek)
self.assertEqual('1', it.next())
self.assertEqual('1', next(it))
self.assertFalseTwice(it.has_next)
self.assertEqualTwice(it.sentinel, it.peek)
self.assertFalseTwice(it.has_next)
@@ -246,10 +246,10 @@ class PeekIterTest(BaseIteratorsTest):
it = peek_iter(a)
self.assertTrueTwice(it.has_next)
self.assertEqualTwice('1', it.peek)
self.assertEqual('1', it.next())
self.assertEqual('1', next(it))
self.assertTrueTwice(it.has_next)
self.assertEqualTwice('2', it.peek)
self.assertEqual('2', it.next())
self.assertEqual('2', next(it))
self.assertFalseTwice(it.has_next)
self.assertEqualTwice(it.sentinel, it.peek)
self.assertFalseTwice(it.has_next)
@@ -265,7 +265,7 @@ class PeekIterTest(BaseIteratorsTest):
it = peek_iter(a)
self.assertTrueTwice(it.has_next)
self.assertEqualTwice(['1'], it.peek, 1)
self.assertEqual('1', it.next())
self.assertEqual('1', next(it))
self.assertFalseTwice(it.has_next)
self.assertEqualTwice([it.sentinel], it.peek, 1)
self.assertFalseTwice(it.has_next)
@@ -274,10 +274,10 @@ class PeekIterTest(BaseIteratorsTest):
it = peek_iter(a)
self.assertTrueTwice(it.has_next)
self.assertEqualTwice(['1'], it.peek, 1)
self.assertEqual('1', it.next())
self.assertEqual('1', next(it))
self.assertTrueTwice(it.has_next)
self.assertEqualTwice(['2'], it.peek, 1)
self.assertEqual('2', it.next())
self.assertEqual('2', next(it))
self.assertFalseTwice(it.has_next)
self.assertEqualTwice([it.sentinel], it.peek, 1)
self.assertFalseTwice(it.has_next)