Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c6de097

Browse files
committed
2to3: Apply next fixer.
The next builtin has been available since Python 2.6 and allows `it.next()` to be replaced by `next(it)`. In Python 3 the `next` method is gone entirely, replaced entirely by the `__next__` method. The next fixer changes all the `it.next()` calls to the new form and renames the `next` methods to `__next__`. In order to keep Numpy code backwards compatible with Python 2, a `next` method was readded to all the Numpy iterators after the fixer was run so they all contain both methods. The presence of the appropriate method could have been made version dependent, but that looked unduly complicated. Closes numpy#3072.
1 parent 6c47259 commit c6de097

7 files changed

Lines changed: 44 additions & 29 deletions

File tree

doc/sphinxext/numpydoc/comment_eater.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def new_noncomment(self, start_lineno, end_lineno):
106106

107107
def new_comment(self, string, start, end, line):
108108
""" Possibly add a new comment.
109-
109+
110110
Only adds a new comment if this comment is the only thing on the line.
111111
Otherwise, it extends the noncomment block.
112112
"""

numpy/core/tests/test_nditer.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
from __future__ import division, absolute_import, print_function
22

3+
import sys, warnings
4+
35
import numpy as np
46
from numpy import array, arange, nditer, all
57
from numpy.compat import asbytes
68
from numpy.testing import *
7-
import sys, warnings
89

9-
import warnings
10+
1011

1112
def iter_multi_index(i):
1213
ret = []
@@ -1210,7 +1211,8 @@ def test_iter_copy():
12101211
assert_equal([x[()] for x in i], [x[()] for x in j])
12111212

12121213
i.iterrange = (2,18)
1213-
i.next(); i.next()
1214+
next(i)
1215+
next(i)
12141216
j = i.copy()
12151217
assert_equal([x[()] for x in i], [x[()] for x in j])
12161218

@@ -2528,14 +2530,14 @@ def test_0d_iter():
25282530
# Basic test for iteration of 0-d arrays:
25292531
i = nditer([2, 3], ['multi_index'], [['readonly']]*2)
25302532
assert_equal(i.ndim, 0)
2531-
assert_equal(i.next(), (2, 3))
2533+
assert_equal(next(i), (2, 3))
25322534
assert_equal(i.multi_index, ())
25332535
assert_equal(i.iterindex, 0)
2534-
assert_raises(StopIteration, i.next)
2536+
assert_raises(StopIteration, next, i)
25352537
# test reset:
25362538
i.reset()
2537-
assert_equal(i.next(), (2, 3))
2538-
assert_raises(StopIteration, i.next)
2539+
assert_equal(next(i), (2, 3))
2540+
assert_raises(StopIteration, next, i)
25392541

25402542
# test forcing to 0-d
25412543
i = nditer(np.arange(5), ['multi_index'], [['readonly']], op_axes=[()])
@@ -2548,7 +2550,7 @@ def test_0d_iter():
25482550
a = np.array(0.5, dtype='f4')
25492551
i = nditer(a, ['buffered','refs_ok'], ['readonly'],
25502552
casting='unsafe', op_dtypes=sdt)
2551-
vals = i.next()
2553+
vals = next(i)
25522554
assert_equal(vals['a'], 0.5)
25532555
assert_equal(vals['b'], 0)
25542556
assert_equal(vals['c'], [[(0.5)]*3]*2)

numpy/lib/index_tricks.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ class ndenumerate(object):
487487
def __init__(self, arr):
488488
self.iter = asarray(arr).flat
489489

490-
def next(self):
490+
def __next__(self):
491491
"""
492492
Standard iterator method, returns the index tuple and array value.
493493
@@ -499,11 +499,14 @@ def next(self):
499499
The array element of the current iteration.
500500
501501
"""
502-
return self.iter.coords, self.iter.next()
502+
return self.iter.coords, next(self.iter)
503503

504504
def __iter__(self):
505505
return self
506506

507+
next = __next__
508+
509+
507510
class ndindex(object):
508511
"""
509512
An N-dimensional iterator object to index arrays.
@@ -532,7 +535,7 @@ class ndindex(object):
532535
(2, 0, 0)
533536
(2, 1, 0)
534537
535-
"""
538+
"""
536539
def __init__(self, *shape):
537540
if len(shape) == 1 and isinstance(shape[0], tuple):
538541
shape = shape[0]
@@ -541,16 +544,16 @@ def __init__(self, *shape):
541544

542545
def __iter__(self):
543546
return self
544-
547+
545548
def ndincr(self):
546549
"""
547550
Increment the multi-dimensional index by one.
548551
549552
This method is for backward compatibility only: do not use.
550553
"""
551-
self.next()
554+
next(self)
552555

553-
def next(self):
556+
def __next__(self):
554557
"""
555558
Standard iterator method, updates the index and returns the index tuple.
556559
@@ -560,9 +563,11 @@ def next(self):
560563
Returns a tuple containing the indices of the current iteration.
561564
562565
"""
563-
self._it.next()
566+
next(self._it)
564567
return self._it.multi_index
565568

569+
next = __next__
570+
566571

567572
# You can do all this with slice() plus a few special objects,
568573
# but there's a lot to remember. This version is simpler because

numpy/lib/npyio.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -785,14 +785,14 @@ def split_line(line):
785785

786786
# Skip the first `skiprows` lines
787787
for i in range(skiprows):
788-
fh.next()
788+
next(fh)
789789

790790
# Read until we find a line with some values, and use
791791
# it to estimate the number of columns, N.
792792
first_vals = None
793793
try:
794794
while not first_vals:
795-
first_line = fh.next()
795+
first_line = next(fh)
796796
first_vals = split_line(first_line)
797797
except StopIteration:
798798
# End of lines reached
@@ -1344,13 +1344,13 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None,
13441344
skip_header = skiprows
13451345
# Skip the first `skip_header` rows
13461346
for i in range(skip_header):
1347-
fhd.next()
1347+
next(fhd)
13481348

13491349
# Keep on until we find the first valid values
13501350
first_values = None
13511351
try:
13521352
while not first_values:
1353-
first_line = fhd.next()
1353+
first_line = next(fhd)
13541354
if names is True:
13551355
if comments in first_line:
13561356
first_line = asbytes('').join(first_line.split(comments)[1:])

numpy/linalg/lapack_lite/fortran.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,19 @@ def __init__(self, iterable):
3636
object.__init__(self)
3737
self.iterable = iter(iterable)
3838
self.lineno = 0
39+
3940
def __iter__(self):
4041
return self
41-
def next(self):
42+
43+
def __next__(self):
4244
self.lineno += 1
43-
line = self.iterable.next()
45+
line = next(self.iterable)
4446
line = line.rstrip()
4547
return line
4648

49+
next = __next__
50+
51+
4752
class PushbackIterator(object):
4853
"""PushbackIterator(iterable)
4954
@@ -59,15 +64,18 @@ def __init__(self, iterable):
5964
def __iter__(self):
6065
return self
6166

62-
def next(self):
67+
def __next__(self):
6368
if self.buffer:
6469
return self.buffer.pop()
6570
else:
66-
return self.iterable.next()
71+
return next(self.iterable)
6772

6873
def pushback(self, item):
6974
self.buffer.append(item)
7075

76+
next = __next__
77+
78+
7179
def fortranSourceLines(fo):
7280
"""Return an iterator over statement lines of a Fortran source file.
7381

numpy/ma/core.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2540,7 +2540,7 @@ def __setitem__(self, index, value):
25402540
if self.maskiter is not None:
25412541
self.maskiter[index] = getmaskarray(value)
25422542

2543-
def next(self):
2543+
def __next__(self):
25442544
"""
25452545
Return the next value, or raise StopIteration.
25462546
@@ -2562,12 +2562,12 @@ def next(self):
25622562
StopIteration
25632563
25642564
"""
2565-
d = self.dataiter.next()
2566-
if self.maskiter is not None and self.maskiter.next():
2565+
d = next(self.dataiter)
2566+
if self.maskiter is not None and next(self.maskiter):
25672567
d = masked
25682568
return d
25692569

2570-
2570+
next = __next__
25712571

25722572

25732573
class MaskedArray(ndarray):

tools/py3tool.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
'metaclass',
7373
'methodattrs',
7474
'ne',
75-
# 'next',
75+
'next',
7676
'nonzero',
7777
'numliterals',
7878
'operator',

0 commit comments

Comments
 (0)