]> granicus.if.org Git - python/commitdiff
PEP 3114: rename .next() to .__next__() and add next() builtin.
authorGeorg Brandl <georg@python.org>
Sat, 21 Apr 2007 15:47:16 +0000 (15:47 +0000)
committerGeorg Brandl <georg@python.org>
Sat, 21 Apr 2007 15:47:16 +0000 (15:47 +0000)
83 files changed:
Doc/api/newtypes.tex
Doc/howto/functional.rst
Doc/lib/libcollections.tex
Doc/lib/libcsv.tex
Doc/lib/libdis.tex
Doc/lib/libexcs.tex
Doc/lib/libfuncs.tex
Doc/lib/libitertools.tex
Doc/lib/libstdtypes.tex
Doc/lib/sqlite3/executemany_1.py
Doc/ref/ref3.tex
Doc/ref/ref5.tex
Doc/ref/ref6.tex
Doc/tut/glossary.tex
Doc/tut/tut.tex
Lib/StringIO.py
Lib/UserDict.py
Lib/codecs.py
Lib/contextlib.py
Lib/csv.py
Lib/difflib.py
Lib/email/feedparser.py
Lib/fileinput.py
Lib/gzip.py
Lib/heapq.py
Lib/hotshot/log.py
Lib/httplib.py
Lib/inspect.py
Lib/io.py
Lib/mailbox.py
Lib/pickle.py
Lib/pstats.py
Lib/pyclbr.py
Lib/shelve.py
Lib/shlex.py
Lib/socket.py
Lib/sqlite3/test/dbapi.py
Lib/tarfile.py
Lib/tempfile.py
Lib/test/list_tests.py
Lib/test/mapping_tests.py
Lib/test/seq_tests.py
Lib/test/test_StringIO.py
Lib/test/test_bsddb.py
Lib/test/test_builtin.py
Lib/test/test_csv.py
Lib/test/test_deque.py
Lib/test/test_dict.py
Lib/test/test_difflib.py
Lib/test/test_enumerate.py
Lib/test/test_extcall.py
Lib/test/test_file.py
Lib/test/test_fileinput.py
Lib/test/test_generators.py
Lib/test/test_genexps.py
Lib/test/test_grammar.py
Lib/test/test_heapq.py
Lib/test/test_iter.py
Lib/test/test_iterlen.py
Lib/test/test_itertools.py
Lib/test/test_mailbox.py
Lib/test/test_re.py
Lib/test/test_set.py
Lib/test/test_str.py
Lib/test/test_tempfile.py
Lib/test/test_tokenize.py
Lib/test/test_unicode.py
Lib/test/test_userlist.py
Lib/test/test_wsgiref.py
Lib/tokenize.py
Lib/types.py
Lib/urllib.py
Lib/wsgiref/util.py
Lib/wsgiref/validate.py
Lib/xml/dom/pulldom.py
Lib/xml/etree/ElementTree.py
Misc/Vim/vim_syntax.py
Misc/cheatsheet
Modules/itertoolsmodule.c
Objects/exceptions.c
Objects/typeobject.c
Python/bltinmodule.c
Tools/scripts/cleanfuture.py

index e5c5aaced2fb02ff161fa5b3cfd2700f47faa599..af1aed3d9f4f0ed23ecc0dcc99d0a62bf767a545 100644 (file)
@@ -1071,7 +1071,7 @@ The next two fields only exist if the
   iterator, or raises \exception{StopIteration} when the iterator is
   exhausted.  Its presence normally signals that the instances of this
   type are iterators (although classic instances always have this
-  function, even if they don't define a \method{next()} method).
+  function, even if they don't define a \method{__next__()} method).
 
   Iterator types should also define the \member{tp_iter} function, and
   that function should return the iterator instance itself (not a new
index 124dd01bbc60b33581985c2e27323593279703e2..98d1094784dda004c02ad5ce97ca262fd4c87912 100644 (file)
@@ -199,11 +199,13 @@ foundation for writing functional-style programs: iterators.
 
 An iterator is an object representing a stream of data; this object
 returns the data one element at a time.  A Python iterator must
-support a method called ``next()`` that takes no arguments and always
+support a method called ``__next__()`` that takes no arguments and always
 returns the next element of the stream.  If there are no more elements
-in the stream, ``next()`` must raise the ``StopIteration`` exception.
+in the stream, ``__next__()`` must raise the ``StopIteration`` exception.
 Iterators don't have to be finite, though; it's perfectly reasonable
 to write an iterator that produces an infinite stream of data.
+The built-in ``next()`` function is normally used to call the iterator's
+``__next__()`` method.
 
 The built-in ``iter()`` function takes an arbitrary object and tries
 to return an iterator that will return the object's contents or
@@ -218,13 +220,13 @@ You can experiment with the iteration interface manually::
     >>> it = iter(L)
     >>> print it
     <iterator object at 0x8116870>
-    >>> it.next()
+    >>> next(it)
     1
-    >>> it.next()
+    >>> next(it)
     2
-    >>> it.next()
+    >>> next(it)
     3
-    >>> it.next()
+    >>> next(it)
     Traceback (most recent call last):
       File "<stdin>", line 1, in ?
     StopIteration
@@ -271,7 +273,7 @@ won't return either.
 Note that you can only go forward in an iterator; there's no way to
 get the previous element, reset the iterator, or make a copy of it.
 Iterator objects can optionally provide these additional capabilities,
-but the iterator protocol only specifies the ``next()`` method.
+but the iterator protocol only specifies the ``__next__()`` method.
 Functions may therefore consume all of the iterator's output, and if
 you need to do something different with the same stream, you'll have
 to create a new iterator.
@@ -485,7 +487,7 @@ outputs the value of ``i``, similar to a ``return``
 statement.  The big difference between ``yield`` and a
 ``return`` statement is that on reaching a ``yield`` the
 generator's state of execution is suspended and local variables are
-preserved.  On the next call to the generator's ``.next()`` method,
+preserved.  On the next call ``next(generator)``,
 the function will resume executing.  
 
 Here's a sample usage of the ``generate_ints()`` generator::
@@ -493,13 +495,13 @@ Here's a sample usage of the ``generate_ints()`` generator::
     >>> gen = generate_ints(3)
     >>> gen
     <generator object at 0x8117f90>
-    >>> gen.next()
+    >>> next(gen)
     0
-    >>> gen.next()
+    >>> next(gen)
     1
-    >>> gen.next()
+    >>> next(gen)
     2
-    >>> gen.next()
+    >>> next(gen)
     Traceback (most recent call last):
       File "stdin", line 1, in ?
       File "stdin", line 2, in generate_ints
@@ -521,7 +523,7 @@ You could achieve the effect of generators manually by writing your
 own class and storing all the local variables of the generator as
 instance variables.  For example, returning a list of integers could
 be done by setting ``self.count`` to 0, and having the
-``next()`` method increment ``self.count`` and return it.
+``__next__()`` method increment ``self.count`` and return it.
 However, for a moderately complicated generator, writing a
 corresponding class can be much messier.
 
@@ -583,7 +585,7 @@ use parentheses when there's an operation, as in ``val = (yield i)
 Values are sent into a generator by calling its
 ``send(value)`` method.  This method resumes the 
 generator's code and the ``yield`` expression returns the specified
-value.  If the regular ``next()`` method is called, the
+value.  If the regular ``__next__()`` method is called, the
 ``yield`` returns ``None``.
 
 Here's a simple counter that increments by 1 and allows changing the
@@ -604,18 +606,18 @@ value of the internal counter.
 And here's an example of changing the counter:
 
     >>> it = counter(10)
-    >>> print it.next()
+    >>> print next(it)
     0
-    >>> print it.next()
+    >>> print next(it)
     1
     >>> print it.send(8)
     8
-    >>> print it.next()
+    >>> print next(it)
     9
-    >>> print it.next()
+    >>> print next(it)
     Traceback (most recent call last):
       File ``t.py'', line 15, in ?
-        print it.next()
+        print next(it)
     StopIteration
 
 Because ``yield`` will often be returning ``None``, you
index a763e31bfecaff497b3d931e98c965cd58eee76d..5a07a2d8d9ac40511358792926fdf4c8c1eb95b2 100644 (file)
@@ -174,7 +174,7 @@ def roundrobin(*iterables):
     while pending:
         task = pending.popleft()
         try:
-            yield task.next()
+            yield next(task)
         except StopIteration:
             continue
         pending.append(task)
@@ -315,12 +315,12 @@ letter.
 
 The function \function{int()} which always returns zero is just a special
 case of constant functions.  A faster and more flexible way to create
-constant functions is to use \function{itertools.repeat()} which can supply
+constant functions is to use a lambda function which can supply
 any constant value (not just zero):
 
 \begin{verbatim}
 >>> def constant_factory(value):
-...     return itertools.repeat(value).next
+...     return lambda: value
 >>> d = defaultdict(constant_factory('<missing>'))
 >>> d.update(name='John', action='ran')
 >>> '%(name)s %(action)s to %(object)s' % d
index b87bc9d2588aa84a11e7da746819e944dabdb4fb..54fc8db5fe1ab6de73e8d39e92c853050e3c9fd3 100644 (file)
@@ -487,8 +487,8 @@ class UTF8Recoder:
     def __iter__(self):
         return self
 
-    def next(self):
-        return self.reader.next().encode("utf-8")
+    def __next__(self):
+        return next(self.reader).encode("utf-8")
 
 class UnicodeReader:
     """
@@ -500,8 +500,8 @@ class UnicodeReader:
         f = UTF8Recoder(f, encoding)
         self.reader = csv.reader(f, dialect=dialect, **kwds)
 
-    def next(self):
-        row = self.reader.next()
+    def __next__(self):
+        row = next(self.reader)
         return [unicode(s, "utf-8") for s in row]
 
     def __iter__(self):
index 4b0d63be2cb6a041056c52373dc968f32ba72353..2d78d9a776c1ee3f7676afd25a1d0abcdd306438 100644 (file)
@@ -529,10 +529,10 @@ Set byte code counter to \var{target}.
 \end{opcodedesc}
 
 \begin{opcodedesc}{FOR_ITER}{delta}
-\code{TOS} is an iterator.  Call its \method{next()} method.  If this
-yields a new value, push it on the stack (leaving the iterator below
-it).  If the iterator indicates it is exhausted  \code{TOS} is
-popped, and the byte code counter is incremented by \var{delta}.
+  \code{TOS} is an iterator.  Call its \method{__next__()} method.  If this
+  yields a new value, push it on the stack (leaving the iterator below it).  If
+  the iterator indicates it is exhausted \code{TOS} is popped, and the byte code
+  counter is incremented by \var{delta}.
 \end{opcodedesc}
 
 %\begin{opcodedesc}{FOR_LOOP}{delta}
index 02e99a79831a80333fd8410c7c58c6ab5c706718..d119ed98931235a341b5016b2c4b4284bf738209 100644 (file)
@@ -265,8 +265,8 @@ Raised when an \keyword{assert} statement fails.
 \end{excdesc}
 
 \begin{excdesc}{StopIteration}
-  Raised by an iterator's \method{next()} method to signal that there
-  are no further values.
+  Raised by builtin \function{next()} and an iterator's \method{__next__()}
+  method to signal that there are no further values.
   This is derived from \exception{Exception} rather than
   \exception{StandardError}, since this is not considered an error in
   its normal application.
index b1d2983a624c582202d4f4fa3b336a0048fca081..b488ce4be93ae10022a7c1050783689c147e5d30 100644 (file)
@@ -342,14 +342,12 @@ class C:
 \end{funcdesc}
 
 \begin{funcdesc}{enumerate}{iterable}
-  Return an enumerate object. \var{iterable} must be a sequence, an
-  iterator, or some other object which supports iteration.  The
-  \method{next()} method of the iterator returned by
-  \function{enumerate()} returns a tuple containing a count (from
-  zero) and the corresponding value obtained from iterating over
-  \var{iterable}.  \function{enumerate()} is useful for obtaining an
-  indexed series: \code{(0, seq[0])}, \code{(1, seq[1])}, \code{(2,
-  seq[2])}, \ldots.
+  Return an enumerate object. \var{iterable} must be a sequence, an iterator, or
+  some other object which supports iteration.  The \method{__next__()} method of
+  the iterator returned by \function{enumerate()} returns a tuple containing a
+  count (from zero) and the corresponding value obtained from iterating over
+  \var{iterable}.  \function{enumerate()} is useful for obtaining an indexed
+  series: \code{(0, seq[0])}, \code{(1, seq[1])}, \code{(2, seq[2])}, \ldots.
   \versionadded{2.3}
 \end{funcdesc}
 
@@ -615,7 +613,7 @@ class C:
   support either of those protocols, \exception{TypeError} is raised.
   If the second argument, \var{sentinel}, is given, then \var{o} must
   be a callable object.  The iterator created in this case will call
-  \var{o} with no arguments for each call to its \method{next()}
+  \var{o} with no arguments for each call to its \method{__next__()}
   method; if the value returned is equal to \var{sentinel},
   \exception{StopIteration} will be raised, otherwise the value will
   be returned.
@@ -695,6 +693,12 @@ class C:
   \versionchanged[Added support for the optional \var{key} argument]{2.5}
 \end{funcdesc}
 
+\begin{funcdesc}{next}{iterator\optional{, default}}
+  Retrieve the next item from the \var{iterable} by calling its
+  \method{__next__()} method.  If \var{default} is given, it is returned if the
+  iterator is exhausted, otherwise \exception{StopIteration} is raised.
+\end{funcdesc}
+
 \begin{funcdesc}{object}{}
   Return a new featureless object.  \class{object} is a base
   for all new style classes.  It has the methods that are common
index ac6028b31a747321a35e19ec66b378d4fb926a3c..a2f37d737db0aa7e869865152fab9f177c168d92 100644 (file)
@@ -164,16 +164,16 @@ by functions or loops that truncate the stream.
             self.tgtkey = self.currkey = self.currvalue = xrange(0)
         def __iter__(self):
             return self
-        def next(self):
+        def __next__(self):
             while self.currkey == self.tgtkey:
-                self.currvalue = self.it.next() # Exit on StopIteration
+                self.currvalue = next(self.it) # Exit on StopIteration
                 self.currkey = self.keyfunc(self.currvalue)
             self.tgtkey = self.currkey
             return (self.currkey, self._grouper(self.tgtkey))
         def _grouper(self, tgtkey):
             while self.currkey == tgtkey:
                 yield self.currvalue
-                self.currvalue = self.it.next() # Exit on StopIteration
+                self.currvalue = next(self.it) # Exit on StopIteration
                 self.currkey = self.keyfunc(self.currvalue)
   \end{verbatim}
   \versionadded{2.4}
@@ -227,7 +227,7 @@ by functions or loops that truncate the stream.
      def imap(function, *iterables):
          iterables = map(iter, iterables)
          while True:
-             args = [i.next() for i in iterables]
+             args = [next(i) for i in iterables]
              if function is None:
                  yield tuple(args)
              else:
@@ -253,11 +253,11 @@ by functions or loops that truncate the stream.
      def islice(iterable, *args):
          s = slice(*args)
          it = iter(xrange(s.start or 0, s.stop or sys.maxint, s.step or 1))
-         nexti = it.next()
+         nexti = next(it)
          for i, element in enumerate(iterable):
              if i == nexti:
                  yield element
-                 nexti = it.next()          
+                 nexti = next(it)
   \end{verbatim}
 
   If \var{start} is \code{None}, then iteration starts at zero.
@@ -276,7 +276,7 @@ by functions or loops that truncate the stream.
      def izip(*iterables):
          iterables = map(iter, iterables)
          while iterables:
-             result = [it.next() for it in iterables]
+             result = [next(it) for it in iterables]
              yield tuple(result)
   \end{verbatim}
 
@@ -297,7 +297,7 @@ by functions or loops that truncate the stream.
   from each iterator in-turn, but the process ends when one of the iterators
   terminates.  This leaves the last fetched values in limbo (they cannot be
   returned in a final, incomplete tuple and they are cannot be pushed back
-  into the iterator for retrieval with \code{it.next()}).  In general,
+  into the iterator for retrieval with \code{next(it)}).  In general,
   \function{izip()} should only be used with unequal length inputs when you
   don't care about trailing, unmatched values from the longer iterables.
 \end{funcdesc}
@@ -360,7 +360,7 @@ by functions or loops that truncate the stream.
      def starmap(function, iterable):
          iterable = iter(iterable)
          while True:
-             yield function(*iterable.next())
+             yield function(*next(iterable))
   \end{verbatim}
 \end{funcdesc}
 
@@ -393,7 +393,7 @@ by functions or loops that truncate the stream.
                      item = data.pop(i)
                  yield item
          it = iter(iterable)
-         return (gen(it.next), gen(it.next))
+         return (gen(it.__next__), gen(it.__next__))
   \end{verbatim}
 
   Note, once \function{tee()} has made a split, the original \var{iterable}
@@ -556,10 +556,7 @@ def repeatfunc(func, times=None, *args):
 def pairwise(iterable):
     "s -> (s0,s1), (s1,s2), (s2, s3), ..."
     a, b = tee(iterable)
-    try:
-        b.next()
-    except StopIteration:
-        pass
+    next(b, None)
     return izip(a, b)
 
 def grouper(n, iterable, padvalue=None):
index f3ce92ae84dd8025fb2069e3d4faa19993cebe41..d7b8858c64466cd03755d8ea7ef2865c20f434b6 100644 (file)
@@ -388,18 +388,17 @@ general and specific sequence types, dictionaries, and other more
 specialized forms.  The specific types are not important beyond their
 implementation of the iterator protocol.
 
-The intention of the protocol is that once an iterator's
-\method{next()} method raises \exception{StopIteration}, it will
-continue to do so on subsequent calls.  Implementations that
-do not obey this property are deemed broken.  (This constraint
-was added in Python 2.3; in Python 2.2, various iterators are
-broken according to this rule.)
+The intention of the protocol is that once an iterator's \method{__next__()}
+method raises \exception{StopIteration}, it will continue to do so on subsequent
+calls.  Implementations that do not obey this property are deemed broken.  (This
+constraint was added in Python 2.3; in Python 2.2, various iterators are broken
+according to this rule.)
 
-Python's generators provide a convenient way to implement the
-iterator protocol.  If a container object's \method{__iter__()}
-method is implemented as a generator, it will automatically
-return an iterator object (technically, a generator object)
-supplying the \method{__iter__()} and \method{next()} methods.
+Python's generators provide a convenient way to implement the iterator protocol.
+If a container object's \method{__iter__()} method is implemented as a
+generator, it will automatically return an iterator object (technically, a
+generator object) supplying the \method{__iter__()} and \method{__next__()}
+methods.
 
 
 \section{Sequence Types ---
@@ -1587,17 +1586,17 @@ finally:
   with a real file, this method should \emph{not} be implemented.}
 \end{methoddesc}
 
-\begin{methoddesc}[file]{next}{}
+\begin{methoddesc}[file]{__next__}{}
 A file object is its own iterator, for example \code{iter(\var{f})} returns
 \var{f} (unless \var{f} is closed).  When a file is used as an
 iterator, typically in a \keyword{for} loop (for example,
-\code{for line in f: print line}), the \method{next()} method is
+\code{for line in f: print line}), the \method{__next__()} method is
 called repeatedly.  This method returns the next input line, or raises
 \exception{StopIteration} when \EOF{} is hit.  In order to make a
 \keyword{for} loop the most efficient way of looping over the lines of
-a file (a very common operation), the \method{next()} method uses a
+a file (a very common operation), the \method{__next__()} method uses a
 hidden read-ahead buffer.  As a consequence of using a read-ahead
-buffer, combining \method{next()} with other file methods (like
+buffer, combining \method{__next__()} with other file methods (like
 \method{readline()}) does not work right.  However, using
 \method{seek()} to reposition the file to an absolute position will
 flush the read-ahead buffer.
index 24357c52da71155627deecae12ef34b85d6c25d0..2dc72cd2c5fef4637d9df1a1f708ed7f5bae74a0 100644 (file)
@@ -7,7 +7,7 @@ class IterChars:
     def __iter__(self):
         return self
 
-    def next(self):
+    def __next__(self):
         if self.count > ord('z'):
             raise StopIteration
         self.count += 1
index a63ae3af3b460f41e740c064e4a2232cb4a3357e..40b2ebd64c56c4f8ada94b3c666ecefbab636234 100644 (file)
@@ -633,7 +633,7 @@ A function or method which uses the \keyword{yield} statement (see
 section~\ref{yield}, ``The \keyword{yield} statement'') is called a
 \dfn{generator function}.  Such a function, when called, always
 returns an iterator object which can be used to execute the body of
-the function:  calling the iterator's \method{next()} method will
+the function:  calling the iterator's \method{__next__()} method will
 cause the function to execute until it provides a value using the
 \keyword{yield} statement.  When the function executes a
 \keyword{return} statement or falls off the end, a
index 6aa6de10638a9f580482643e82ccf769b4bb1aaa..0b4e97821b180f883284f0154cc72352a4cedb77 100644 (file)
@@ -222,7 +222,7 @@ evaluating the expression to yield a value that is reached the
 innermost block for each iteration.
 
 Variables used in the generator expression are evaluated lazily
-when the \method{next()} method is called for generator object
+when the \method{__next__()} method is called for generator object
 (in the same fashion as normal generators). However, the leftmost
 \keyword{for} clause is immediately evaluated so that error produced
 by it can be seen before any other possible error in the code that
index 4c7487b6dfa0d9d95c9f21db67b0614d074870b1..e92a63d64b360757832eb26cb26b8dffd1f364b7 100644 (file)
@@ -418,19 +418,18 @@ Using a \keyword{yield} statement in a function definition is
 sufficient to cause that definition to create a generator function
 instead of a normal function.
 
-When a generator function is called, it returns an iterator known as a
-generator iterator, or more commonly, a generator.  The body of the
-generator function is executed by calling the generator's
-\method{next()} method repeatedly until it raises an exception.
-
-When a \keyword{yield} statement is executed, the state of the
-generator is frozen and the value of \grammartoken{expression_list} is
-returned to \method{next()}'s caller.  By ``frozen'' we mean that all
-local state is retained, including the current bindings of local
-variables, the instruction pointer, and the internal evaluation stack:
-enough information is saved so that the next time \method{next()} is
-invoked, the function can proceed exactly as if the \keyword{yield}
-statement were just another external call.
+When a generator function is called, it returns an iterator known as a generator
+iterator, or more commonly, a generator.  The body of the generator function is
+executed by calling the generator's \method{__next__()} method repeatedly until
+it raises an exception.
+
+When a \keyword{yield} statement is executed, the state of the generator is
+frozen and the value of \grammartoken{expression_list} is returned to
+\method{__next__()}'s caller.  By ``frozen'' we mean that all local state is
+retained, including the current bindings of local variables, the instruction
+pointer, and the internal evaluation stack: enough information is saved so that
+the next time \method{__next__()} is invoked, the function can proceed exactly
+as if the \keyword{yield} statement were just another external call.
 
 As of Python version 2.5, the \keyword{yield} statement is now
 allowed in the \keyword{try} clause of a \keyword{try} ...\ 
index 738e12d1f2fbdd892889600642d189495d0fd07b..a19416b69b16bb698ec77aae6d2059664992a71a 100644 (file)
@@ -117,7 +117,7 @@ contain one or more {}\keyword{for} or \keyword{while} loops that
 \keyword{yield} elements back to the caller.  The function execution is
 stopped at the {}\keyword{yield} keyword (returning the result) and is
 resumed there when the next element is requested by calling the
-\method{next()} method of the returned iterator.
+\method{__next__()} method of the returned iterator.
 
 \index{generator expression}
 \item[generator expression]
@@ -207,10 +207,10 @@ hold the iterator for the duration of the loop.  See also
 \index{iterator}
 \item[iterator]
 An object representing a stream of data.  Repeated calls to the
-iterator's \method{next()} method return successive items in the
+iterator's \method{__next__()} method return successive items in the
 stream.  When no more data is available a \exception{StopIteration}
 exception is raised instead.  At this point, the iterator object is
-exhausted and any further calls to its \method{next()} method just
+exhausted and any further calls to its \method{__next__()} method just
 raise \exception{StopIteration} again.  Iterators are required to have
 an \method{__iter__()} method that returns the iterator object
 itself so every iterator is also iterable and may be used in most
index a0b75c6189ee78cf6b9fb321d72437b32732764d..4abd0bd76cbfa8d256045d234fca6dd6c8c848b5 100644 (file)
@@ -4488,34 +4488,35 @@ This style of access is clear, concise, and convenient.  The use of iterators
 pervades and unifies Python.  Behind the scenes, the \keyword{for}
 statement calls \function{iter()} on the container object.  The
 function returns an iterator object that defines the method
-\method{next()} which accesses elements in the container one at a
-time.  When there are no more elements, \method{next()} raises a
+\method{__next__()} which accesses elements in the container one at a
+time.  When there are no more elements, \method{__next__()} raises a
 \exception{StopIteration} exception which tells the \keyword{for} loop
-to terminate.  This example shows how it all works:
+to terminate.  You can call the \method{__next__()} method using the
+\function{next()} builtin; this example shows how it all works:
 
 \begin{verbatim}
 >>> s = 'abc'
 >>> it = iter(s)
 >>> it
 <iterator object at 0x00A1DB50>
->>> it.next()
+>>> next(it)
 'a'
->>> it.next()
+>>> next(it)
 'b'
->>> it.next()
+>>> next(it)
 'c'
->>> it.next()
+>>> next(it)
 
 Traceback (most recent call last):
   File "<stdin>", line 1, in ?
-    it.next()
+    next(it)
 StopIteration
 \end{verbatim}
 
 Having seen the mechanics behind the iterator protocol, it is easy to add
 iterator behavior to your classes.  Define a \method{__iter__()} method
-which returns an object with a \method{next()} method.  If the class defines
-\method{next()}, then \method{__iter__()} can just return \code{self}:
+which returns an object with a \method{__next__()} method.  If the class defines
+\method{__next__()}, then \method{__iter__()} can just return \code{self}:
 
 \begin{verbatim}
 class Reverse:
@@ -4525,7 +4526,7 @@ class Reverse:
         self.index = len(data)
     def __iter__(self):
         return self
-    def next(self):
+    def __next__(self):
         if self.index == 0:
             raise StopIteration
         self.index = self.index - 1
@@ -4545,7 +4546,7 @@ s
 
 Generators are a simple and powerful tool for creating iterators.  They are
 written like regular functions but use the \keyword{yield} statement whenever
-they want to return data.  Each time \method{next()} is called, the
+they want to return data.  Each time \function{next()} is called on it, the
 generator resumes where it left-off (it remembers all the data values and
 which statement was last executed).  An example shows that generators can
 be trivially easy to create:
@@ -4566,7 +4567,7 @@ g
 
 Anything that can be done with generators can also be done with class based
 iterators as described in the previous section.  What makes generators so
-compact is that the \method{__iter__()} and \method{next()} methods are
+compact is that the \method{__iter__()} and \method{__next__()} methods are
 created automatically.
 
 Another key feature is that the local variables and execution state
index 189d368f01ef305700df63ab8f859d936dd9b8d4..815bce6b16f23a391075d9f8569f443ae25a8e41 100644 (file)
@@ -64,10 +64,10 @@ class StringIO:
     def __iter__(self):
         return self
 
-    def next(self):
+    def __next__(self):
         """A file object is its own iterator, for example iter(f) returns f
         (unless f is closed). When a file is used as an iterator, typically
-        in a for loop (for example, for line in f: print line), the next()
+        in a for loop (for example, for line in f: print line), the __next__()
         method is called repeatedly. This method returns the next input line,
         or raises StopIteration when EOF is hit.
         """
index 91508d8201e68c0876cab6c82262687d89339de0..bcee543bab7b6cd141563d984db54e3d228f9d3d 100644 (file)
@@ -139,7 +139,7 @@ class DictMixin:
         return value
     def popitem(self):
         try:
-            k, v = self.iteritems().next()
+            k, v = next(self.iteritems())
         except StopIteration:
             raise KeyError, 'container is empty'
         del self[k]
index 185ad4262239d9d0f64c089383f814bbba10bab3..d340725d7b666dac264089b26f785215476720bb 100644 (file)
@@ -600,7 +600,7 @@ class StreamReader(Codec):
         self.reset()
         self.stream.seek(offset, whence)
 
-    def next(self):
+    def __next__(self):
 
         """ Return the next decoded line from the input stream."""
         line = self.readline()
@@ -669,10 +669,10 @@ class StreamReaderWriter:
 
         return self.reader.readlines(sizehint)
 
-    def next(self):
+    def __next__(self):
 
         """ Return the next decoded line from the input stream."""
-        return self.reader.next()
+        return next(self.reader)
 
     def __iter__(self):
         return self
@@ -782,10 +782,10 @@ class StreamRecoder:
         data, bytesencoded = self.encode(data, self.errors)
         return data.splitlines(1)
 
-    def next(self):
+    def __next__(self):
 
         """ Return the next decoded line from the input stream."""
-        data = self.reader.next()
+        data = next(self.reader)
         data, bytesencoded = self.encode(data, self.errors)
         return data
 
index 731bf8f86818b3a24e37dda0a262755aca25ca10..6605bea23789f527eb01cb11a42e0af460c77051 100644 (file)
@@ -12,14 +12,14 @@ class GeneratorContextManager(object):
 
     def __enter__(self):
         try:
-            return self.gen.next()
+            return next(self.gen)
         except StopIteration:
             raise RuntimeError("generator didn't yield")
 
     def __exit__(self, type, value, traceback):
         if type is None:
             try:
-                self.gen.next()
+                next(self.gen)
             except StopIteration:
                 return
             else:
index 92e466603202452e459a2f79fbe1ecd2e53e5b4d..45570f74c03d5362b3a90754ed6baa9cf2b538fb 100644 (file)
@@ -79,17 +79,17 @@ class DictReader:
     def __iter__(self):
         return self
 
-    def next(self):
-        row = self.reader.next()
+    def __next__(self):
+        row = next(self.reader)
         if self.fieldnames is None:
             self.fieldnames = row
-            row = self.reader.next()
+            row = next(self.reader)
 
         # unlike the basic reader, we prefer not to return blanks,
         # because we will typically wind up with a dict full of None
         # values
         while row == []:
-            row = self.reader.next()
+            row = next(self.reader)
         d = dict(zip(self.fieldnames, row))
         lf = len(self.fieldnames)
         lr = len(row)
@@ -351,7 +351,7 @@ class Sniffer:
 
         rdr = reader(StringIO(sample), self.sniff(sample))
 
-        header = rdr.next() # assume first row is header
+        header = next(rdr) # assume first row is header
 
         columns = len(header)
         columnTypes = {}
index 2dca7495d519e1ac1108992b03dc2b2e18d39e1d..2a057d91c30ab7f236582fd1ab51a2f3ecfb216d 100644 (file)
@@ -1430,7 +1430,7 @@ def _mdiff(fromlines, tolines, context=None, linejunk=None,
             # so we can do some very readable comparisons.
             while len(lines) < 4:
                 try:
-                    lines.append(diff_lines_iterator.next())
+                    lines.append(next(diff_lines_iterator))
                 except StopIteration:
                     lines.append('X')
             s = ''.join([line[0] for line in lines])
@@ -1517,7 +1517,7 @@ def _mdiff(fromlines, tolines, context=None, linejunk=None,
         while True:
             # Collecting lines of text until we have a from/to pair
             while (len(fromlines)==0 or len(tolines)==0):
-                from_line, to_line, found_diff =line_iterator.next()
+                from_line, to_line, found_diff = next(line_iterator)
                 if from_line is not None:
                     fromlines.append((from_line,found_diff))
                 if to_line is not None:
@@ -1532,7 +1532,7 @@ def _mdiff(fromlines, tolines, context=None, linejunk=None,
     line_pair_iterator = _line_pair_iterator()
     if context is None:
         while True:
-            yield line_pair_iterator.next()
+            yield next(line_pair_iterator)
     # Handle case where user wants context differencing.  We must do some
     # storage of lines until we know for sure that they are to be yielded.
     else:
@@ -1545,7 +1545,7 @@ def _mdiff(fromlines, tolines, context=None, linejunk=None,
             index, contextLines = 0, [None]*(context)
             found_diff = False
             while(found_diff is False):
-                from_line, to_line, found_diff = line_pair_iterator.next()
+                from_line, to_line, found_diff = next(line_pair_iterator)
                 i = index % context
                 contextLines[i] = (from_line, to_line, found_diff)
                 index += 1
@@ -1565,7 +1565,7 @@ def _mdiff(fromlines, tolines, context=None, linejunk=None,
             # Now yield the context lines after the change
             lines_to_write = context-1
             while(lines_to_write):
-                from_line, to_line, found_diff = line_pair_iterator.next()
+                from_line, to_line, found_diff = next(line_pair_iterator)
                 # If another change within the context, extend the context
                 if found_diff:
                     lines_to_write = context-1
index afb02b32b24a2b75f2038f1bdf19c0f6af6b8253..9ed8e4d38554f253f6acb62af3ebdff155bdb6bd 100644 (file)
@@ -122,7 +122,7 @@ class BufferedSubFile(object):
     def __iter__(self):
         return self
 
-    def next(self):
+    def __next__(self):
         line = self.readline()
         if line == '':
             raise StopIteration
@@ -138,7 +138,7 @@ class FeedParser:
         self._factory = _factory
         self._input = BufferedSubFile()
         self._msgstack = []
-        self._parse = self._parsegen().next
+        self._parse = self._parsegen().__next__
         self._cur = None
         self._last = None
         self._headersonly = False
index f6913eb8cbd31e675e7993ecc56e8189c8e997e2..b8b98706a3572660f0574bb4dad6278aac607db4 100644 (file)
@@ -240,7 +240,7 @@ class FileInput:
     def __iter__(self):
         return self
 
-    def next(self):
+    def __next__(self):
         try:
             line = self._buffer[self._bufindex]
         except IndexError:
@@ -259,7 +259,7 @@ class FileInput:
         if i != self._lineno:
             raise RuntimeError, "accessing lines out of order"
         try:
-            return self.next()
+            return self.__next__()
         except StopIteration:
             raise IndexError, "end of input reached"
 
index 4ff4883ce709c233b4703ce98488da854bdffae2..fd72b9eb2a7a1dc7027f93112e8ffdb703891d76 100644 (file)
@@ -453,7 +453,7 @@ class GzipFile:
     def __iter__(self):
         return self
 
-    def next(self):
+    def __next__(self):
         line = self.readline()
         if line:
             return line
index 01680651fc21201864f141ec59a6d7efeb3275e8..6ee26d17f9d7230ee8fcceaf6702fa14ff27a6a5 100644 (file)
@@ -325,7 +325,7 @@ def merge(*iterables):
     h_append = h.append
     for itnum, it in enumerate(map(iter, iterables)):
         try:
-            next = it.next
+            next = it.__next__
             h_append([next(), itnum, next])
         except _StopIteration:
             pass
index 059142eec1d8a16e617352bd651c8d3b172dccb1..880b25c5ed0194f25674598359552e8b54934085 100644 (file)
@@ -29,7 +29,7 @@ class LogReader:
         self._funcmap = {}
 
         self._reader = _hotshot.logreader(logfn)
-        self._nextitem = self._reader.next
+        self._nextitem = self._reader.__next__
         self._info = self._reader.info
         if 'current-directory' in self._info:
             self.cwd = self._info['current-directory']
@@ -93,7 +93,7 @@ class LogReader:
     # same bound method can be used as the __getitem__() method -- this
     # avoids using an additional method call which kills the performance.
 
-    def next(self, index=0):
+    def __next__(self, index=0):
         while 1:
             # This call may raise StopIteration:
             what, tdelta, fileno, lineno = self._nextitem()
index 8876aadd8fa017b12dbd9ee1866b8ec4a6778b36..89d5392252155c104f1d73a7c399e9493ef99206 100644 (file)
@@ -1098,7 +1098,7 @@ class SSLFile(SharedSocketClient):
     def __iter__(self):
         return self
 
-    def next(self):
+    def __next__(self):
         line = self.readline()
         if not line:
             raise StopIteration
index d4cfc0714a65333be756f1efd82facc60452cdac..0be0419719b8bcb6bd62560095bf99d222efc5ac 100644 (file)
@@ -603,7 +603,7 @@ def getblock(lines):
     """Extract the block of code at the top of the given list of lines."""
     blockfinder = BlockFinder()
     try:
-        tokenize.tokenize(iter(lines).next, blockfinder.tokeneater)
+        tokenize.tokenize(iter(lines).__next__, blockfinder.tokeneater)
     except (EndOfBlock, IndentationError):
         pass
     return lines[:blockfinder.last]
index 6bda7e52f7aece49899cede1701f07bdcbe97b00..2b85da72cf73b779ba837e05cf66aea9ea877088 100644 (file)
--- a/Lib/io.py
+++ b/Lib/io.py
@@ -904,7 +904,7 @@ class TextIOBase(IOBase):
         """
         return self
 
-    def next(self) -> str:
+    def __next__(self) -> str:
         """Same as readline() except raises StopIteration on immediate EOF."""
         line = self.readline()
         if not line:
@@ -1125,7 +1125,7 @@ class TextIOWrapper(TextIOBase):
             self._pending = res[n:]
             return self._simplify(res[:n])
 
-    def next(self) -> str:
+    def __next__(self) -> str:
         self._telling = False
         line = self.readline()
         if not line:
index fde71327b980d502f0d058165c061349172394ce..9642d83c960e8242aa2b11090dcf9ebaf3d6fc81 100755 (executable)
@@ -480,7 +480,7 @@ class Maildir(Mailbox):
             self._onetime_keys = iter(self.keys())
         while True:
             try:
-                return self[self._onetime_keys.next()]
+                return self[next(self._onetime_keys)]
             except StopIteration:
                 return None
             except KeyError:
index 8e772e77049fe35b0ccb8ee92b5e707c80d0d332..2b01b02ed24942e40f9c6e2bc2fa0bb0cb047769 100644 (file)
@@ -640,7 +640,7 @@ class Pickler:
             tmp = []
             for i in r:
                 try:
-                    x = items.next()
+                    x = next(items)
                     tmp.append(x)
                 except StopIteration:
                     items = None
@@ -688,7 +688,7 @@ class Pickler:
             tmp = []
             for i in r:
                 try:
-                    tmp.append(items.next())
+                    tmp.append(next(items))
                 except StopIteration:
                     items = None
                     break
index 87038d9c2169d1406d97eca37cf14789ec5bb3c0..59c84fe98340c27423e7f7e656d28bb1cd162c71 100644 (file)
@@ -396,7 +396,7 @@ class Stats:
         subheader = False
         for cc, nc, tt, ct, callers in self.stats.values():
             if callers:
-                value = iter(callers.values()).next()
+                value = next(iter(callers.values()))
                 subheader = isinstance(value, tuple)
                 break
         if subheader:
index fdbfbd46ccf6748d3a76e0d88cae56be3f09a9d1..c402a0994a8b1f912cc702e9a56db57a7c0ae7bb 100644 (file)
@@ -161,7 +161,7 @@ def _readmodule(module, path, inpackage=None):
                 # close previous nested classes and defs
                 while stack and stack[-1][1] >= thisindent:
                     del stack[-1]
-                tokentype, meth_name, start, end, line = g.next()
+                tokentype, meth_name, start, end, line = next(g)
                 if tokentype != NAME:
                     continue # Syntax error
                 if stack:
@@ -179,11 +179,11 @@ def _readmodule(module, path, inpackage=None):
                 # close previous nested classes and defs
                 while stack and stack[-1][1] >= thisindent:
                     del stack[-1]
-                tokentype, class_name, start, end, line = g.next()
+                tokentype, class_name, start, end, line = next(g)
                 if tokentype != NAME:
                     continue # Syntax error
                 # parse what follows the class name
-                tokentype, token, start, end, line = g.next()
+                tokentype, token, start, end, line = next(g)
                 inherit = None
                 if token == '(':
                     names = [] # List of superclasses
@@ -191,7 +191,7 @@ def _readmodule(module, path, inpackage=None):
                     level = 1
                     super = [] # Tokens making up current superclass
                     while True:
-                        tokentype, token, start, end, line = g.next()
+                        tokentype, token, start, end, line = next(g)
                         if token in (')', ',') and level == 1:
                             n = "".join(super)
                             if n in dict:
@@ -287,7 +287,7 @@ def _getnamelist(g):
             name2 = None
         names.append((name, name2))
         while token != "," and "\n" not in token:
-            tokentype, token, start, end, line = g.next()
+            tokentype, token, start, end, line = next(g)
         if token != ",":
             break
     return names
@@ -297,15 +297,15 @@ def _getname(g):
     # name is the dotted name, or None if there was no dotted name,
     # and token is the next input token.
     parts = []
-    tokentype, token, start, end, line = g.next()
+    tokentype, token, start, end, line = next(g)
     if tokentype != NAME and token != '*':
         return (None, token)
     parts.append(token)
     while True:
-        tokentype, token, start, end, line = g.next()
+        tokentype, token, start, end, line = next(g)
         if token != '.':
             break
-        tokentype, token, start, end, line = g.next()
+        tokentype, token, start, end, line = next(g)
         if tokentype != NAME:
             break
         parts.append(token)
index d86718e141c5f45df251c2e9ba8e82a25823630a..697ae4f0f9f955f62fd306abd2e674b110cef90b 100644 (file)
@@ -174,7 +174,7 @@ class BsdDbShelf(Shelf):
         return (key, Unpickler(f).load())
 
     def next(self):
-        (key, value) = self.dict.next()
+        (key, value) = next(self.dict)
         f = StringIO(value)
         return (key, Unpickler(f).load())
 
index d81e99ec84d2587c1a41d48e7bd1a2587a04505a..520b637bcb79954924389ca66805b719dba0afb8 100644 (file)
@@ -265,7 +265,7 @@ class shlex:
     def __iter__(self):
         return self
 
-    def next(self):
+    def __next__(self):
         token = self.get_token()
         if token == self.eof:
             raise StopIteration
index 2222600721c3a754487ad24488fa5d4da83634cd..3fe6ec5c531fd8e5e9ec8441f9f16437573370ad 100644 (file)
@@ -409,7 +409,7 @@ class _fileobject(object):
     def __iter__(self):
         return self
 
-    def next(self):
+    def __next__(self):
         line = self.readline()
         if not line:
             raise StopIteration
index 287848a438e70d17a9cc0031cb23a1829dd446a1..5a97acfb2cac198bae39c1b8e7db754ca384d622 100644 (file)
@@ -286,7 +286,7 @@ class CursorTests(unittest.TestCase):
             def __init__(self):
                 self.value = 5
 
-            def next(self):
+            def __next__(self):
                 if self.value == 10:
                     raise StopIteration
                 else:
index 146bbb717b5013504436bc2a108218efdb2e2d5b..963127c2b385dce5bac9897e6bdd1fc3bf44586f 100644 (file)
@@ -2046,7 +2046,7 @@ class TarIter:
         """Return iterator object.
         """
         return self
-    def next(self):
+    def __next__(self):
         """Return the next item using TarFile's next() method.
            When all members have been read, set TarFile as _loaded.
         """
index f4f10581bd6471e6e4accba7961ffee5c2a32500..0ebf6b4fc5b29a769b39cd1118bbbcff0eb0518e 100644 (file)
@@ -124,7 +124,7 @@ class _RandomNameSequence:
     def __iter__(self):
         return self
 
-    def next(self):
+    def __next__(self):
         m = self.mutex
         c = self.characters
         choose = self.rng.choice
@@ -191,7 +191,7 @@ def _get_default_tempdir():
             dir = _os.path.normcase(_os.path.abspath(dir))
         # Try only a few names per directory.
         for seq in xrange(100):
-            name = namer.next()
+            name = next(namer)
             filename = _os.path.join(dir, name)
             try:
                 fd = _os.open(filename, flags, 0600)
@@ -230,7 +230,7 @@ def _mkstemp_inner(dir, pre, suf, flags):
     names = _get_candidate_names()
 
     for seq in xrange(TMP_MAX):
-        name = names.next()
+        name = next(names)
         file = _os.path.join(dir, pre + name + suf)
         try:
             fd = _os.open(file, flags, 0600)
@@ -322,7 +322,7 @@ def mkdtemp(suffix="", prefix=template, dir=None):
     names = _get_candidate_names()
 
     for seq in xrange(TMP_MAX):
-        name = names.next()
+        name = next(names)
         file = _os.path.join(dir, prefix + name + suffix)
         try:
             _os.mkdir(file, 0700)
@@ -357,7 +357,7 @@ def mktemp(suffix="", prefix=template, dir=None):
 
     names = _get_candidate_names()
     for seq in xrange(TMP_MAX):
-        name = names.next()
+        name = next(names)
         file = _os.path.join(dir, prefix + name + suffix)
         if not _exists(file):
             return file
index a6180811b19167aee0b19c83930fc1e258d7107f..ad1052365ebaa29d43e1425484bbccc190b1273c 100644 (file)
@@ -77,7 +77,7 @@ class CommonTest(seq_tests.CommonTest):
         a = self.type2test(range(20))
         r = reversed(a)
         self.assertEqual(list(r), self.type2test(range(19, -1, -1)))
-        self.assertRaises(StopIteration, r.next)
+        self.assertRaises(StopIteration, next, r)
         self.assertEqual(list(reversed(self.type2test())),
                          self.type2test())
 
index 77d66b206296fc2e922f38f5b3c02ea187b1111b..d595cc5ec4e9add6bbf11c0d6dfe5bea5638168c 100644 (file)
@@ -69,7 +69,7 @@ class BasicTestMappingProtocol(unittest.TestCase):
         if not d: self.fail("Full mapping must compare to True")
         # keys(), items(), iterkeys() ...
         def check_iterandlist(iter, lst, ref):
-            self.assert_(hasattr(iter, 'next'))
+            self.assert_(hasattr(iter, '__next__'))
             self.assert_(hasattr(iter, '__iter__'))
             x = list(iter)
             self.assert_(set(x)==set(lst)==set(ref))
@@ -81,8 +81,8 @@ class BasicTestMappingProtocol(unittest.TestCase):
         check_iterandlist(iter(d.items()), list(d.items()),
                           self.reference.items())
         #get
-        key, value = iter(d.items()).next()
-        knownkey, knownvalue = iter(self.other.items()).next()
+        key, value = next(iter(d.items()))
+        knownkey, knownvalue = next(iter(self.other.items()))
         self.assertEqual(d.get(key, knownvalue), value)
         self.assertEqual(d.get(knownkey, knownvalue), knownvalue)
         self.failIf(knownkey in d)
@@ -107,8 +107,8 @@ class BasicTestMappingProtocol(unittest.TestCase):
         self.assertEqual(dict(p), self.reference)
         d = self._full_mapping(self.reference)
         #setdefault
-        key, value = iter(d.items()).next()
-        knownkey, knownvalue = iter(self.other.items()).next()
+        key, value = next(iter(d.items()))
+        knownkey, knownvalue = next(iter(self.other.items()))
         self.assertEqual(d.setdefault(key, knownvalue), value)
         self.assertEqual(d[key], value)
         self.assertEqual(d.setdefault(knownkey, knownvalue), knownvalue)
@@ -225,7 +225,7 @@ class BasicTestMappingProtocol(unittest.TestCase):
                         self.i = 1
                     def __iter__(self):
                         return self
-                    def next(self):
+                    def __next__(self):
                         if self.i:
                             self.i = 0
                             return 'a'
@@ -242,7 +242,7 @@ class BasicTestMappingProtocol(unittest.TestCase):
                         self.i = ord('a')
                     def __iter__(self):
                         return self
-                    def next(self):
+                    def __next__(self):
                         if self.i <= ord('z'):
                             rtn = chr(self.i)
                             self.i += 1
@@ -257,7 +257,7 @@ class BasicTestMappingProtocol(unittest.TestCase):
         class badseq(object):
             def __iter__(self):
                 return self
-            def next(self):
+            def __next__(self):
                 raise Exc()
 
         self.assertRaises(Exc, d.update, badseq())
@@ -456,7 +456,7 @@ class TestMappingProtocol(BasicTestMappingProtocol):
         class BadSeq(object):
             def __iter__(self):
                 return self
-            def next(self):
+            def __next__(self):
                 raise Exc()
 
         self.assertRaises(Exc, self.type2test.fromkeys, BadSeq())
index 0dfe7e4aab8ebbaf70d8da9e469bc05de31e9f4e..d4e72e146e7328806c38feee1050420904576b62 100644 (file)
@@ -26,7 +26,7 @@ class IterFunc:
         self.i = 0
     def __iter__(self):
         return self
-    def next(self):
+    def __next__(self):
         if self.i >= len(self.seqn): raise StopIteration
         v = self.seqn[self.i]
         self.i += 1
@@ -46,14 +46,14 @@ class IterNextOnly:
     def __init__(self, seqn):
         self.seqn = seqn
         self.i = 0
-    def next(self):
+    def __next__(self):
         if self.i >= len(self.seqn): raise StopIteration
         v = self.seqn[self.i]
         self.i += 1
         return v
 
 class IterNoNext:
-    'Iterator missing next()'
+    'Iterator missing __next__()'
     def __init__(self, seqn):
         self.seqn = seqn
         self.i = 0
@@ -67,7 +67,7 @@ class IterGenExc:
         self.i = 0
     def __iter__(self):
         return self
-    def next(self):
+    def __next__(self):
         3 // 0
 
 class IterFuncStop:
@@ -76,7 +76,7 @@ class IterFuncStop:
         pass
     def __iter__(self):
         return self
-    def next(self):
+    def __next__(self):
         raise StopIteration
 
 from itertools import chain, imap
@@ -296,7 +296,7 @@ class CommonTest(unittest.TestCase):
         class T(self.type2test):
             def __getitem__(self, key):
                 return str(key) + '!!!'
-        self.assertEqual(iter(T((1,2))).next(), 1)
+        self.assertEqual(next(iter(T((1,2)))), 1)
 
     def test_repeat(self):
         for m in xrange(4):
index 9f79b02e16c8812f0780d8e43e52d03b218b2d1c..83cd76c0d12e3914a850a675a2862987e274e600 100644 (file)
@@ -89,14 +89,14 @@ class TestGenericStringIO(unittest.TestCase):
         eq(iter(self._fp), self._fp)
         # Does this object support the iteration protocol?
         unless(hasattr(self._fp, '__iter__'))
-        unless(hasattr(self._fp, 'next'))
+        unless(hasattr(self._fp, '__next__'))
         i = 0
         for line in self._fp:
             eq(line, self._line + '\n')
             i += 1
         eq(i, 5)
         self._fp.close()
-        self.assertRaises(ValueError, self._fp.next)
+        self.assertRaises(ValueError, next, self._fp)
 
 class TestStringIO(TestGenericStringIO):
     MODULE = StringIO
index 3a62f9c1c7f6314f25897ee73b8f06842eaf7a0e..876a1000dd3b5b6a4c1b0126801cffc7574307c7 100755 (executable)
@@ -72,7 +72,7 @@ class TestBSDDB(unittest.TestCase):
         di = iter(self.d)
         while 1:
             try:
-                key = di.next()
+                key = next(di)
                 self.d[key] = 'modified '+key
             except StopIteration:
                 break
@@ -83,7 +83,7 @@ class TestBSDDB(unittest.TestCase):
         fi = iter(self.f)
         while 1:
             try:
-                key = fi.next()
+                key = next(fi)
                 self.f[key] = 'modified '+key
             except StopIteration:
                 break
@@ -97,7 +97,7 @@ class TestBSDDB(unittest.TestCase):
         di = iter(self.d.items())
         while 1:
             try:
-                k, v = di.next()
+                k, v = next(di)
                 self.d[k] = 'modified '+v
             except StopIteration:
                 break
@@ -108,7 +108,7 @@ class TestBSDDB(unittest.TestCase):
         fi = iter(self.f.items())
         while 1:
             try:
-                k, v = fi.next()
+                k, v = next(fi)
                 self.f[k] = 'modified '+v
             except StopIteration:
                 break
@@ -160,13 +160,13 @@ class TestBSDDB(unittest.TestCase):
         if hasattr(self.f, 'iteritems'):
             if debug: print("D")
             i = iter(self.f.items())
-            k,v = i.next()
+            k,v = next(i)
             if debug: print("E")
             self.f[k] = "please don't deadlock"
             if debug: print("F")
             while 1:
                 try:
-                    k,v = i.next()
+                    k,v = next(i)
                 except StopIteration:
                     break
             if debug: print("F2")
@@ -176,7 +176,7 @@ class TestBSDDB(unittest.TestCase):
             while i:
                 try:
                     if debug: print("H")
-                    k = i.next()
+                    k = next(i)
                     if debug: print("I")
                     self.f[k] = "deadlocks-r-us"
                     if debug: print("J")
@@ -201,7 +201,7 @@ class TestBSDDB(unittest.TestCase):
         i = iter(self.f.iteritems())
         nc2 = len(self.f._cursor_refs)
         # use the iterator (should run to the first yield, creating the cursor)
-        k, v = i.next()
+        k, v = next(i)
         nc3 = len(self.f._cursor_refs)
         # destroy the iterator; this should cause the weakref callback
         # to remove the cursor object from self.f._cursor_refs
index 05e80e45802e8692c4635d7eb8a689796ced28ba..500516cefb8a2d0da74941d49b4d67ccc4a55fab 100644 (file)
@@ -907,9 +907,9 @@ class BuiltinTest(unittest.TestCase):
             lists.append(unicode("12"))
         for l in lists:
             i = iter(l)
-            self.assertEqual(i.next(), '1')
-            self.assertEqual(i.next(), '2')
-            self.assertRaises(StopIteration, i.next)
+            self.assertEqual(next(i), '1')
+            self.assertEqual(next(i), '2')
+            self.assertRaises(StopIteration, next, i)
 
     def test_isinstance(self):
         class C:
@@ -1305,6 +1305,33 @@ class BuiltinTest(unittest.TestCase):
         self.assertEqual(min(data, key=f),
                          sorted(data, key=f)[0])
 
+    def test_next(self):
+        it = iter(range(2))
+        self.assertEqual(next(it), 0)
+        self.assertEqual(next(it), 1)
+        self.assertRaises(StopIteration, next, it)
+        self.assertRaises(StopIteration, next, it)
+        self.assertEquals(next(it, 42), 42)
+
+        class Iter(object):
+            def __iter__(self):
+                return self
+            def __next__(self):
+                raise StopIteration
+
+        it = iter(Iter())
+        self.assertEquals(next(it, 42), 42)
+        self.assertRaises(StopIteration, next, it)
+
+        def gen():
+            yield 1
+            return
+
+        it = gen()
+        self.assertEquals(next(it), 1)
+        self.assertRaises(StopIteration, next, it)
+        self.assertEquals(next(it, 42), 42)
+
     def test_oct(self):
         self.assertEqual(oct(100), '0144')
         self.assertEqual(oct(100), '0144')
index 2cdc8074449cbc3070916d61b66076f1c5151099..980f3fc8550125321e845871414dc51643359674 100644 (file)
@@ -271,13 +271,13 @@ class Test_Csv(unittest.TestCase):
     def test_read_linenum(self):
         r = csv.reader(['line,1', 'line,2', 'line,3'])
         self.assertEqual(r.line_num, 0)
-        r.next()
+        next(r)
         self.assertEqual(r.line_num, 1)
-        r.next()
+        next(r)
         self.assertEqual(r.line_num, 2)
-        r.next()
+        next(r)
         self.assertEqual(r.line_num, 3)
-        self.assertRaises(StopIteration, r.next)
+        self.assertRaises(StopIteration, next, r)
         self.assertEqual(r.line_num, 3)
 
 class TestDialectRegistry(unittest.TestCase):
@@ -338,9 +338,9 @@ class TestDialectRegistry(unittest.TestCase):
         try:
             fileobj.write("abc def\nc1ccccc1 benzene\n")
             fileobj.seek(0)
-            rdr = csv.reader(fileobj, dialect=space())
-            self.assertEqual(rdr.next(), ["abc", "def"])
-            self.assertEqual(rdr.next(), ["c1ccccc1", "benzene"])
+            reader = csv.reader(fileobj, dialect=space())
+            self.assertEqual(next(reader), ["abc", "def"])
+            self.assertEqual(next(reader), ["c1ccccc1", "benzene"])
         finally:
             fileobj.close()
             os.unlink(name)
@@ -593,7 +593,7 @@ class TestDictFields(unittest.TestCase):
             fileobj.seek(0)
             reader = csv.DictReader(fileobj,
                                     fieldnames=["f1", "f2", "f3"])
-            self.assertEqual(reader.next(), {"f1": '1', "f2": '2', "f3": 'abc'})
+            self.assertEqual(next(reader), {"f1": '1', "f2": '2', "f3": 'abc'})
         finally:
             fileobj.close()
             os.unlink(name)
@@ -605,7 +605,7 @@ class TestDictFields(unittest.TestCase):
             fileobj.write("f1,f2,f3\r\n1,2,abc\r\n")
             fileobj.seek(0)
             reader = csv.DictReader(fileobj)
-            self.assertEqual(reader.next(), {"f1": '1', "f2": '2', "f3": 'abc'})
+            self.assertEqual(next(reader), {"f1": '1', "f2": '2', "f3": 'abc'})
         finally:
             fileobj.close()
             os.unlink(name)
@@ -618,7 +618,7 @@ class TestDictFields(unittest.TestCase):
             fileobj.seek(0)
             reader = csv.DictReader(fileobj,
                                     fieldnames=["f1", "f2"])
-            self.assertEqual(reader.next(), {"f1": '1', "f2": '2',
+            self.assertEqual(next(reader), {"f1": '1', "f2": '2',
                                              None: ["abc", "4", "5", "6"]})
         finally:
             fileobj.close()
@@ -632,7 +632,7 @@ class TestDictFields(unittest.TestCase):
             fileobj.seek(0)
             reader = csv.DictReader(fileobj,
                                     fieldnames=["f1", "f2"], restkey="_rest")
-            self.assertEqual(reader.next(), {"f1": '1', "f2": '2',
+            self.assertEqual(next(reader), {"f1": '1', "f2": '2',
                                              "_rest": ["abc", "4", "5", "6"]})
         finally:
             fileobj.close()
@@ -645,7 +645,7 @@ class TestDictFields(unittest.TestCase):
             fileobj.write("f1,f2\r\n1,2,abc,4,5,6\r\n")
             fileobj.seek(0)
             reader = csv.DictReader(fileobj, restkey="_rest")
-            self.assertEqual(reader.next(), {"f1": '1', "f2": '2',
+            self.assertEqual(next(reader), {"f1": '1', "f2": '2',
                                              "_rest": ["abc", "4", "5", "6"]})
         finally:
             fileobj.close()
@@ -660,9 +660,9 @@ class TestDictFields(unittest.TestCase):
             reader = csv.DictReader(fileobj,
                                     fieldnames="1 2 3 4 5 6".split(),
                                     restval="DEFAULT")
-            self.assertEqual(reader.next(), {"1": '1', "2": '2', "3": 'abc',
+            self.assertEqual(next(reader), {"1": '1', "2": '2', "3": 'abc',
                                              "4": '4', "5": '5', "6": '6'})
-            self.assertEqual(reader.next(), {"1": '1', "2": '2', "3": 'abc',
+            self.assertEqual(next(reader), {"1": '1', "2": '2', "3": 'abc',
                                              "4": 'DEFAULT', "5": 'DEFAULT',
                                              "6": 'DEFAULT'})
         finally:
@@ -678,7 +678,7 @@ class TestDictFields(unittest.TestCase):
 
         reader = csv.DictReader(sample,
                                 fieldnames="i1 float i2 s1 s2".split())
-        self.assertEqual(reader.next(), {"i1": '2147483648',
+        self.assertEqual(next(reader), {"i1": '2147483648',
                                          "float": '43.0e12',
                                          "i2": '17',
                                          "s1": 'abc',
@@ -688,16 +688,16 @@ class TestDictFields(unittest.TestCase):
         reader = csv.DictReader(["1,2,abc,4,5,6\r\n","\r\n",
                                  "1,2,abc,4,5,6\r\n"],
                                 fieldnames="1 2 3 4 5 6".split())
-        self.assertEqual(reader.next(), {"1": '1', "2": '2', "3": 'abc',
+        self.assertEqual(next(reader), {"1": '1', "2": '2', "3": 'abc',
                                          "4": '4', "5": '5', "6": '6'})
-        self.assertEqual(reader.next(), {"1": '1', "2": '2', "3": 'abc',
+        self.assertEqual(next(reader), {"1": '1', "2": '2', "3": 'abc',
                                          "4": '4', "5": '5', "6": '6'})
 
     def test_read_semi_sep(self):
         reader = csv.DictReader(["1;2;abc;4;5;6\r\n"],
                                 fieldnames="1 2 3 4 5 6".split(),
                                 delimiter=';')
-        self.assertEqual(reader.next(), {"1": '1', "2": '2', "3": 'abc',
+        self.assertEqual(next(reader), {"1": '1', "2": '2', "3": 'abc',
                                          "4": '4', "5": '5', "6": '6'})
 
 class TestArrayWrites(unittest.TestCase):
index 28099045a611fe0dabc30d848768e6ff60019119..7df31b7035d9c1820007f2d9e5be56c623e7d601 100644 (file)
@@ -394,13 +394,13 @@ class TestVariousIteratorArgs(unittest.TestCase):
         d = deque('abcdefg')
         it = iter(d)
         d.pop()
-        self.assertRaises(RuntimeError, it.next)
+        self.assertRaises(RuntimeError, next, it)
 
     def test_runtime_error_on_empty_deque(self):
         d = deque()
         it = iter(d)
         d.append(10)
-        self.assertRaises(RuntimeError, it.next)
+        self.assertRaises(RuntimeError, next, it)
 
 class Deque(deque):
     pass
@@ -567,7 +567,7 @@ deque(['a', 'b', 'd', 'e', 'f'])
 ...     while pending:
 ...         task = pending.popleft()
 ...         try:
-...             yield task.next()
+...             yield next(task)
 ...         except StopIteration:
 ...             continue
 ...         pending.append(task)
index 679181fc259e7a9fed2f6103eb923b4d76f915c9..d8dfd7e413564fd45fcefbf55ba74993e133d0f4 100644 (file)
@@ -144,7 +144,7 @@ class DictTest(unittest.TestCase):
                         self.i = 1
                     def __iter__(self):
                         return self
-                    def next(self):
+                    def __next__(self):
                         if self.i:
                             self.i = 0
                             return 'a'
@@ -161,7 +161,7 @@ class DictTest(unittest.TestCase):
                         self.i = ord('a')
                     def __iter__(self):
                         return self
-                    def next(self):
+                    def __next__(self):
                         if self.i <= ord('z'):
                             rtn = chr(self.i)
                             self.i += 1
@@ -175,7 +175,7 @@ class DictTest(unittest.TestCase):
         class badseq(object):
             def __iter__(self):
                 return self
-            def next(self):
+            def __next__(self):
                 raise Exc()
 
         self.assertRaises(Exc, {}.update, badseq())
@@ -225,7 +225,7 @@ class DictTest(unittest.TestCase):
         class BadSeq(object):
             def __iter__(self):
                 return self
-            def next(self):
+            def __next__(self):
                 raise Exc()
 
         self.assertRaises(Exc, dict.fromkeys, BadSeq())
index 83fad1548ded7abd1e5204d1f4ac62d50b7488ba..b5168dd7094fd1629705f162e897ec3bad369755 100644 (file)
@@ -16,9 +16,9 @@ class TestSFbugs(unittest.TestCase):
     def test_comparing_empty_lists(self):
         # Check fix for bug #979794
         group_gen = difflib.SequenceMatcher(None, [], []).get_grouped_opcodes()
-        self.assertRaises(StopIteration, group_gen.next)
+        self.assertRaises(StopIteration, next, group_gen)
         diff_gen = difflib.unified_diff([], [])
-        self.assertRaises(StopIteration, diff_gen.next)
+        self.assertRaises(StopIteration, next, diff_gen)
 
 patch914575_from1 = """
    1. Beautiful is beTTer than ugly.
index 62900704606af8ebd60bcdba1cba500f9c44e695..af7512d215b587207d387aee8f5fa02d54ead5b0 100644 (file)
@@ -17,7 +17,7 @@ class I:
         self.i = 0
     def __iter__(self):
         return self
-    def next(self):
+    def __next__(self):
         if self.i >= len(self.seqn): raise StopIteration
         v = self.seqn[self.i]
         self.i += 1
@@ -37,7 +37,7 @@ class X:
     def __init__(self, seqn):
         self.seqn = seqn
         self.i = 0
-    def next(self):
+    def __next__(self):
         if self.i >= len(self.seqn): raise StopIteration
         v = self.seqn[self.i]
         self.i += 1
@@ -50,11 +50,11 @@ class E:
         self.i = 0
     def __iter__(self):
         return self
-    def next(self):
+    def __next__(self):
         3 // 0
 
 class N:
-    'Iterator missing next()'
+    'Iterator missing __next__()'
     def __init__(self, seqn):
         self.seqn = seqn
         self.i = 0
@@ -76,17 +76,17 @@ class EnumerateTestCase(unittest.TestCase):
     def test_getitemseqn(self):
         self.assertEqual(list(self.enum(G(self.seq))), self.res)
         e = self.enum(G(''))
-        self.assertRaises(StopIteration, e.next)
+        self.assertRaises(StopIteration, next, e)
 
     def test_iteratorseqn(self):
         self.assertEqual(list(self.enum(I(self.seq))), self.res)
         e = self.enum(I(''))
-        self.assertRaises(StopIteration, e.next)
+        self.assertRaises(StopIteration, next, e)
 
     def test_iteratorgenerator(self):
         self.assertEqual(list(self.enum(Ig(self.seq))), self.res)
         e = self.enum(Ig(''))
-        self.assertRaises(StopIteration, e.next)
+        self.assertRaises(StopIteration, next, e)
 
     def test_noniterable(self):
         self.assertRaises(TypeError, self.enum, X(self.seq))
index 453f464a9909459d28f44b48e632c9ec31263c19..552583201c77135cfe46d5babab2a771da81383a 100644 (file)
@@ -103,7 +103,7 @@ class Nothing:
         self.c = 0
     def __iter__(self):
         return self
-    def next(self):
+    def __next__(self):
         if self.c == 4:
             raise StopIteration
         c = self.c
index c4bd610d3eccc709152b393a68834c8b5938c447..f682f8950abc7320d69ad70c566f1c149c7b0ee3 100644 (file)
@@ -95,7 +95,7 @@ class AutoFileTests(unittest.TestCase):
         self.assert_(f.closed)
 
     def testMethods(self):
-        methods = ['fileno', 'flush', 'isatty', 'next', 'read', 'readinto',
+        methods = ['fileno', 'flush', 'isatty', '__next__', 'read', 'readinto',
                    'readline', 'readlines', 'seek', 'tell', 'truncate',
                    'write', '__iter__']
         if sys.platform.startswith('atheos'):
@@ -248,7 +248,7 @@ class OtherFileTests(unittest.TestCase):
             # Test for appropriate errors mixing read* and iteration
             for methodname, args in methods:
                 f = open(TESTFN, 'rb')
-                if f.next() != filler:
+                if next(f) != filler:
                     self.fail, "Broken testfile"
                 meth = getattr(f, methodname)
                 try:
@@ -269,7 +269,7 @@ class OtherFileTests(unittest.TestCase):
             # between 4 and 16384 (inclusive).
             f = open(TESTFN, 'rb')
             for i in range(nchunks):
-                f.next()
+                next(f)
             testline = testlines.pop(0)
             try:
                 line = f.readline()
index b7f84c686af29a598e1b2d91fa8420d6a5c3d996..17ca944d71049f1203e777b36fdb44a55305d962 100644 (file)
@@ -179,7 +179,7 @@ try:
     t2 = writeTmp(2, ["C\nD"])
     fi = FileInput(files=(t1, t2))
     verify(fi.fileno() == -1)
-    line = fi.next()
+    line = next(fi)
     verify(fi.fileno() != -1)
     fi.nextfile()
     verify(fi.fileno() == -1)
index 12276be0d92bfec2e9c730e62d7fbab9b466f96d..08d354a13c480a18d11b6a864c10169351c13bf0 100644 (file)
@@ -10,14 +10,14 @@ Let's try a simple generator:
     1
     2
     >>> g = f()
-    >>> g.next()
+    >>> next(g)
     1
-    >>> g.next()
+    >>> next(g)
     2
 
 "Falling off the end" stops the generator:
 
-    >>> g.next()
+    >>> next(g)
     Traceback (most recent call last):
       File "<stdin>", line 1, in ?
       File "<stdin>", line 2, in g
@@ -31,14 +31,14 @@ Let's try a simple generator:
     ...     yield 2 # never reached
     ...
     >>> g = f()
-    >>> g.next()
+    >>> next(g)
     1
-    >>> g.next()
+    >>> next(g)
     Traceback (most recent call last):
       File "<stdin>", line 1, in ?
       File "<stdin>", line 3, in f
     StopIteration
-    >>> g.next() # once stopped, can't be resumed
+    >>> next(g) # once stopped, can't be resumed
     Traceback (most recent call last):
       File "<stdin>", line 1, in ?
     StopIteration
@@ -51,13 +51,13 @@ Let's try a simple generator:
     ...     yield 2 # never reached
     ...
     >>> g = f()
-    >>> g.next()
+    >>> next(g)
     1
-    >>> g.next()
+    >>> next(g)
     Traceback (most recent call last):
       File "<stdin>", line 1, in ?
     StopIteration
-    >>> g.next()
+    >>> next(g)
     Traceback (most recent call last):
       File "<stdin>", line 1, in ?
     StopIteration
@@ -105,7 +105,7 @@ Generators always return to the most recent caller:
 
     >>> def creator():
     ...     r = yrange(5)
-    ...     print("creator", r.next())
+    ...     print("creator", next(r))
     ...     return r
     ...
     >>> def caller():
@@ -141,10 +141,10 @@ Specification:  Yield
     running:
 
     >>> def g():
-    ...     i = me.next()
+    ...     i = next(me)
     ...     yield i
     >>> me = g()
-    >>> me.next()
+    >>> next(me)
     Traceback (most recent call last):
      ...
       File "<string>", line 2, in g
@@ -185,13 +185,13 @@ Specification: Generators and Exception Propagation
     ...     yield f()  # the zero division exception propagates
     ...     yield 42   # and we'll never get here
     >>> k = g()
-    >>> k.next()
+    >>> next(k)
     Traceback (most recent call last):
       File "<stdin>", line 1, in ?
       File "<stdin>", line 2, in g
       File "<stdin>", line 2, in f
     ZeroDivisionError: integer division or modulo by zero
-    >>> k.next()  # and the generator cannot be resumed
+    >>> next(k)  # and the generator cannot be resumed
     Traceback (most recent call last):
       File "<stdin>", line 1, in ?
     StopIteration
@@ -382,9 +382,9 @@ From the Iterators list, about the types of these things.
 >>> type(i)
 <type 'generator'>
 >>> [s for s in dir(i) if not s.startswith('_')]
-['close', 'gi_frame', 'gi_running', 'next', 'send', 'throw']
->>> print(i.next.__doc__)
-x.next() -> the next value, or raise StopIteration
+['close', 'gi_frame', 'gi_running', 'send', 'throw']
+>>> print(i.__next__.__doc__)
+x.__next__() <==> next(x)
 >>> iter(i) is i
 True
 >>> import types
@@ -406,7 +406,7 @@ AttributeError: readonly attribute
 >>> me = g()
 >>> me.gi_running
 0
->>> me.next()
+>>> next(me)
 1
 >>> me.gi_running
 0
@@ -429,7 +429,7 @@ Subject: Re: PEP 255: Simple Generators
 ...             yield x
 ...
 ...     def find(self):
-...         return self.generator.next()
+...         return next(self.generator)
 ...
 ...     def union(self, parent):
 ...         if self.parent:
@@ -493,7 +493,7 @@ fun_tests = """
 Build up to a recursive Sieve of Eratosthenes generator.
 
 >>> def firstn(g, n):
-...     return [g.next() for i in range(n)]
+...     return [next(g) for i in range(n)]
 
 >>> def intsfrom(i):
 ...     while 1:
@@ -512,7 +512,7 @@ Build up to a recursive Sieve of Eratosthenes generator.
 [1, 2, 4, 5, 7, 8]
 
 >>> def sieve(ints):
-...     prime = ints.next()
+...     prime = next(ints)
 ...     yield prime
 ...     not_divisible_by_prime = exclude_multiples(prime, ints)
 ...     for p in sieve(not_divisible_by_prime):
@@ -536,19 +536,19 @@ Try writing it without generators, and correctly, and without generating
 [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
 
 >>> def merge(g, h):
-...     ng = g.next()
-...     nh = h.next()
+...     ng = next(g)
+...     nh = next(h)
 ...     while 1:
 ...         if ng < nh:
 ...             yield ng
-...             ng = g.next()
+...             ng = next(g)
 ...         elif ng > nh:
 ...             yield nh
-...             nh = h.next()
+...             nh = next(h)
 ...         else:
 ...             yield ng
-...             ng = g.next()
-...             nh = h.next()
+...             ng = next(g)
+...             nh = next(h)
 
 The following works, but is doing a whale of a lot of redundant work --
 it's not clear how to get the internal uses of m235 to share a single
@@ -589,7 +589,7 @@ arguments are iterable -- a LazyList is the same as a generator to times().
 >>> class LazyList:
 ...     def __init__(self, g):
 ...         self.sofar = []
-...         self.fetch = g.next
+...         self.fetch = g.__next__
 ...
 ...     def __getitem__(self, i):
 ...         sofar, fetch = self.sofar, self.fetch
@@ -626,10 +626,10 @@ Ye olde Fibonacci generator, LazyList style.
 ...
 ...     def sum(g, h):
 ...         while 1:
-...             yield g.next() + h.next()
+...             yield next(g) + next(h)
 ...
 ...     def tail(g):
-...         g.next()    # throw first away
+...         next(g)    # throw first away
 ...         for x in g:
 ...             yield x
 ...
@@ -705,12 +705,12 @@ Ye olde Fibonacci generator, tee style.
 ...
 ...     def _isum(g, h):
 ...         while 1:
-...             yield g.next() + h.next()
+...             yield next(g) + next(h)
 ...
 ...     def _fib():
 ...         yield 1
 ...         yield 2
-...         fibTail.next() # throw first away
+...         next(fibTail) # throw first away
 ...         for res in _isum(fibHead, fibTail):
 ...             yield res
 ...
@@ -890,13 +890,13 @@ This one caused a crash (see SF bug 567538):
 ...             yield i
 ...
 >>> g = f()
->>> print(g.next())
+>>> print(next(g))
 0
->>> print(g.next())
+>>> print(next(g))
 1
->>> print(g.next())
+>>> print(next(g))
 2
->>> print(g.next())
+>>> print(next(g))
 Traceback (most recent call last):
 StopIteration
 """
@@ -1013,7 +1013,7 @@ def flat_conjoin(gs):  # rename to conjoin to run tests with this instead
         # Descend.
         try:
             while i < n:
-                it = iters[i] = gs[i]().next
+                it = iters[i] = gs[i]().__next__
                 values[i] = it()
                 i += 1
         except _StopIteration:
@@ -1463,7 +1463,7 @@ Sending a value into a started generator:
 ...     print((yield 1))
 ...     yield 2
 >>> g = f()
->>> g.next()
+>>> next(g)
 1
 >>> g.send(42)
 42
@@ -1506,7 +1506,7 @@ A yield expression with augmented assignment.
 ...         seq.append(count)
 >>> seq = []
 >>> c = coroutine(seq)
->>> c.next()
+>>> next(c)
 >>> print(seq)
 []
 >>> c.send(10)
@@ -1558,7 +1558,7 @@ Now check some throw() conditions:
 ...             print("caught ValueError (%s)" % (v))
 >>> import sys
 >>> g = f()
->>> g.next()
+>>> next(g)
 
 >>> g.throw(ValueError) # type only
 caught ValueError ()
@@ -1642,7 +1642,7 @@ Now let's try closing a generator:
 ...         print("exiting")
 
 >>> g = f()
->>> g.next()
+>>> next(g)
 >>> g.close()
 exiting
 >>> g.close()  # should be no-op now
@@ -1652,7 +1652,7 @@ exiting
 >>> def f(): yield      # an even simpler generator
 >>> f().close()         # close before opening
 >>> g = f()
->>> g.next()
+>>> next(g)
 >>> g.close()           # close normally
 
 And finalization:
@@ -1663,7 +1663,7 @@ And finalization:
 ...         print("exiting")
 
 >>> g = f()
->>> g.next()
+>>> next(g)
 >>> del g
 exiting
 
@@ -1675,7 +1675,7 @@ Now let's try some ill-behaved generators:
 ...     except GeneratorExit:
 ...         yield "foo!"
 >>> g = f()
->>> g.next()
+>>> next(g)
 >>> g.close()
 Traceback (most recent call last):
   ...
@@ -1688,7 +1688,7 @@ Our ill-behaved code should be invoked during GC:
 >>> import sys, StringIO
 >>> old, sys.stderr = sys.stderr, StringIO.StringIO()
 >>> g = f()
->>> g.next()
+>>> next(g)
 >>> del g
 >>> sys.stderr.getvalue().startswith(
 ...     "Exception RuntimeError: 'generator ignored GeneratorExit' in "
@@ -1704,7 +1704,7 @@ And errors thrown during closing should propagate:
 ...     except GeneratorExit:
 ...         raise TypeError("fie!")
 >>> g = f()
->>> g.next()
+>>> next(g)
 >>> g.close()
 Traceback (most recent call last):
   ...
@@ -1760,7 +1760,7 @@ would trigger if it starts being uncleanable again.
 ...     class gen:
 ...         def __iter__(self):
 ...             return self
-...         def next(self):
+...         def __next__(self):
 ...             return self.item
 ...     g = gen()
 ...     head, tail = itertools.tee(g)
@@ -1771,7 +1771,7 @@ would trigger if it starts being uncleanable again.
 Make sure to also test the involvement of the tee-internal teedataobject,
 which stores returned items.
 
->>> item = it.next()
+>>> item = next(it)
 
 
 
index 1b246729002016b81722cce828a917bd40fd4842..cafca5723483b456fd94a10d38355fbf1087cee9 100644 (file)
@@ -34,24 +34,24 @@ Test first class
 Test direct calls to next()
 
     >>> g = (i*i for i in range(3))
-    >>> g.next()
+    >>> next(g)
     0
-    >>> g.next()
+    >>> next(g)
     1
-    >>> g.next()
+    >>> next(g)
     4
-    >>> g.next()
+    >>> next(g)
     Traceback (most recent call last):
       File "<pyshell#21>", line 1, in -toplevel-
-        g.next()
+        next(g)
     StopIteration
 
 Does it stay stopped?
 
-    >>> g.next()
+    >>> next(g)
     Traceback (most recent call last):
       File "<pyshell#21>", line 1, in -toplevel-
-        g.next()
+        next(g)
     StopIteration
     >>> list(g)
     []
@@ -157,7 +157,7 @@ Generators always return to the most recent caller:
 
     >>> def creator():
     ...     r = yrange(5)
-    ...     print("creator", r.next())
+    ...     print("creator", next(r))
     ...     return r
     >>> def caller():
     ...     r = creator()
@@ -181,32 +181,32 @@ Generators can call other generators:
 
 Verify that a gen exp cannot be resumed while it is actively running:
 
-    >>> g = (me.next() for i in xrange(10))
+    >>> g = (next(me) for i in xrange(10))
     >>> me = g
-    >>> me.next()
+    >>> next(me)
     Traceback (most recent call last):
       File "<pyshell#30>", line 1, in -toplevel-
-        me.next()
+        next(me)
       File "<pyshell#28>", line 1, in <generator expression>
-        g = (me.next() for i in xrange(10))
+        g = (next(me) for i in xrange(10))
     ValueError: generator already executing
 
 Verify exception propagation
 
     >>> g = (10 // i for i in (5, 0, 2))
-    >>> g.next()
+    >>> next(g)
     2
-    >>> g.next()
+    >>> next(g)
     Traceback (most recent call last):
       File "<pyshell#37>", line 1, in -toplevel-
-        g.next()
+        next(g)
       File "<pyshell#35>", line 1, in <generator expression>
         g = (10 // i for i in (5, 0, 2))
     ZeroDivisionError: integer division or modulo by zero
-    >>> g.next()
+    >>> next(g)
     Traceback (most recent call last):
       File "<pyshell#38>", line 1, in -toplevel-
-        g.next()
+        next(g)
     StopIteration
 
 Make sure that None is a valid return value
@@ -217,12 +217,12 @@ Make sure that None is a valid return value
 Check that generator attributes are present
 
     >>> g = (i*i for i in range(3))
-    >>> expected = set(['gi_frame', 'gi_running', 'next'])
+    >>> expected = set(['gi_frame', 'gi_running'])
     >>> set(attr for attr in dir(g) if not attr.startswith('__')) >= expected
     True
 
-    >>> print(g.next.__doc__)
-    x.next() -> the next value, or raise StopIteration
+    >>> print(g.__next__.__doc__)
+    x.__next__() <==> next(x)
     >>> import types
     >>> isinstance(g, types.GeneratorType)
     True
@@ -238,7 +238,7 @@ Verify that the running flag is set properly
     >>> me = g
     >>> me.gi_running
     0
-    >>> me.next()
+    >>> next(me)
     1
     >>> me.gi_running
     0
index 2c1b6ca9419520c0eb04f63e443c766fe87621e0..bd04735dab018df9e24151da367d55ec349bb856 100644 (file)
@@ -785,9 +785,9 @@ class GrammarTests(unittest.TestCase):
     def testGenexps(self):
         # generator expression tests
         g = ([x for x in range(10)] for x in range(1))
-        self.assertEqual(g.next(), [x for x in range(10)])
+        self.assertEqual(next(g), [x for x in range(10)])
         try:
-            g.next()
+            next(g)
             self.fail('should produce StopIteration exception')
         except StopIteration:
             pass
@@ -795,7 +795,7 @@ class GrammarTests(unittest.TestCase):
         a = 1
         try:
             g = (a for d in a)
-            g.next()
+            next(g)
             self.fail('should produce TypeError')
         except TypeError:
             pass
index 156c835151a679f83af1f3aaf283afc660fc9b7d..dbbfcb0f6d02041aafdb31b98f0b3ecf8b8d0043 100644 (file)
@@ -180,7 +180,7 @@ class I:
         self.i = 0
     def __iter__(self):
         return self
-    def next(self):
+    def __next__(self):
         if self.i >= len(self.seqn): raise StopIteration
         v = self.seqn[self.i]
         self.i += 1
@@ -200,14 +200,14 @@ class X:
     def __init__(self, seqn):
         self.seqn = seqn
         self.i = 0
-    def next(self):
+    def __next__(self):
         if self.i >= len(self.seqn): raise StopIteration
         v = self.seqn[self.i]
         self.i += 1
         return v
 
 class N:
-    'Iterator missing next()'
+    'Iterator missing __next__()'
     def __init__(self, seqn):
         self.seqn = seqn
         self.i = 0
@@ -221,7 +221,7 @@ class E:
         self.i = 0
     def __iter__(self):
         return self
-    def next(self):
+    def __next__(self):
         3 // 0
 
 class S:
@@ -230,7 +230,7 @@ class S:
         pass
     def __iter__(self):
         return self
-    def next(self):
+    def __next__(self):
         raise StopIteration
 
 from itertools import chain, imap
index 820911da35ab0397c4d66291465f15aca20e1d61..65e143d10b042f86b3cf2db3ed27c8f560468eec 100644 (file)
@@ -22,7 +22,7 @@ class BasicIterClass:
     def __init__(self, n):
         self.n = n
         self.i = 0
-    def next(self):
+    def __next__(self):
         res = self.i
         if res >= self.n:
             raise StopIteration
@@ -53,7 +53,7 @@ class TestCase(unittest.TestCase):
         res = []
         while 1:
             try:
-                val = it.next()
+                val = next(it)
             except StopIteration:
                 break
             res.append(val)
@@ -342,7 +342,7 @@ class TestCase(unittest.TestCase):
                         self.i = 0
                     def __iter__(self):
                         return self
-                    def next(self):
+                    def __next__(self):
                         i = self.i
                         self.i = i + 1
                         if i < len(self.vals):
@@ -447,7 +447,7 @@ class TestCase(unittest.TestCase):
             def __iter__(self):
                 return self
 
-            def next(self):
+            def __next__(self):
                 i = self.i
                 self.i = i+1
                 return i
@@ -514,12 +514,12 @@ class TestCase(unittest.TestCase):
             def __iter__(self):
                 return self
 
-            def next(self):
+            def __next__(self):
                 i = self.i
                 self.i = i+1
                 if i == 2:
                     return unicode("fooled you!")
-                return self.it.next()
+                return next(self.it)
 
         f = open(TESTFN, "w")
         try:
@@ -682,7 +682,7 @@ class TestCase(unittest.TestCase):
                     self.finish = finish
                     self.i = self.start
 
-                def next(self):
+                def __next__(self):
                     if self.i >= self.finish:
                         raise StopIteration
                     result = str(self.i) + '\n'
index 8ff1d6bb17182a03c4e9cfd924a3f398b4fdf1a6..28f25676e226e1198ca8b6e965e1cfe531403b81 100644 (file)
@@ -10,7 +10,7 @@ The desired invariant is:  len(it)==len(list(it)).
 A complication is that an iterable and iterator can be the same object. To
 maintain the invariant, an iterator needs to dynamically update its length.
 For instance, an iterable such as xrange(10) always reports its length as ten,
-but it=iter(xrange(10)) starts at ten, and then goes to nine after it.next().
+but it=iter(xrange(10)) starts at ten, and then goes to nine after next(it).
 Having this capability means that map() can ignore the distinction between
 map(func, iterable) and map(func, iter(iterable)).
 
@@ -67,9 +67,9 @@ class TestInvariantWithoutMutations(unittest.TestCase):
         it = self.it
         for i in reversed(xrange(1, n+1)):
             self.assertEqual(len(it), i)
-            it.next()
+            next(it)
         self.assertEqual(len(it), 0)
-        self.assertRaises(StopIteration, it.next)
+        self.assertRaises(StopIteration, next, it)
         self.assertEqual(len(it), 0)
 
 class TestTemporarilyImmutable(TestInvariantWithoutMutations):
@@ -80,10 +80,10 @@ class TestTemporarilyImmutable(TestInvariantWithoutMutations):
 
         it = self.it
         self.assertEqual(len(it), n)
-        it.next()
+        next(it)
         self.assertEqual(len(it), n-1)
         self.mutate()
-        self.assertRaises(RuntimeError, it.next)
+        self.assertRaises(RuntimeError, next, it)
         self.assertEqual(len(it), 0)
 
 ## ------- Concrete Type Tests -------
@@ -166,8 +166,8 @@ class TestList(TestInvariantWithoutMutations):
     def test_mutation(self):
         d = range(n)
         it = iter(d)
-        it.next()
-        it.next()
+        next(it)
+        next(it)
         self.assertEqual(len(it), n-2)
         d.append(n)
         self.assertEqual(len(it), n-1)  # grow with append
@@ -185,8 +185,8 @@ class TestListReversed(TestInvariantWithoutMutations):
     def test_mutation(self):
         d = range(n)
         it = reversed(d)
-        it.next()
-        it.next()
+        next(it)
+        next(it)
         self.assertEqual(len(it), n-2)
         d.append(n)
         self.assertEqual(len(it), n-2)  # ignore append
@@ -204,8 +204,8 @@ class TestSeqIter(TestInvariantWithoutMutations):
     def test_mutation(self):
         d = UserList(range(n))
         it = iter(d)
-        it.next()
-        it.next()
+        next(it)
+        next(it)
         self.assertEqual(len(it), n-2)
         d.append(n)
         self.assertEqual(len(it), n-1)  # grow with append
@@ -223,8 +223,8 @@ class TestSeqIterReversed(TestInvariantWithoutMutations):
     def test_mutation(self):
         d = UserList(range(n))
         it = reversed(d)
-        it.next()
-        it.next()
+        next(it)
+        next(it)
         self.assertEqual(len(it), n-2)
         d.append(n)
         self.assertEqual(len(it), n-2)  # ignore append
index bbebbd9558ac2d7c599ef0bf677c22fb75fa5c59..d74b9d5c489c2a9c65696f5b670376ef506ad5a6 100644 (file)
@@ -34,7 +34,7 @@ class StopNow:
     'Class emulating an empty iterable.'
     def __iter__(self):
         return self
-    def next(self):
+    def __next__(self):
         raise StopIteration
 
 def take(n, seq):
@@ -58,12 +58,12 @@ class TestBasicOps(unittest.TestCase):
         self.assertRaises(OverflowError, list, islice(count(sys.maxint-5), 10))
         c = count(3)
         self.assertEqual(repr(c), 'count(3)')
-        c.next()
+        next(c)
         self.assertEqual(repr(c), 'count(4)')
         c = count(-9)
         self.assertEqual(repr(c), 'count(-9)')
-        c.next()
-        self.assertEqual(c.next(), -8)
+        next(c)
+        self.assertEqual(next(c), -8)
 
     def test_cycle(self):
         self.assertEqual(take(10, cycle('abc')), list('abcabcabca'))
@@ -121,7 +121,7 @@ class TestBasicOps(unittest.TestCase):
         r = sorted([(len(list(g)) , k) for k, g in groupby(sorted(s))], reverse=True)[:3]
         self.assertEqual(r, [(5, 'a'), (2, 'r'), (2, 'b')])
 
-        # iter.next failure
+        # iter.__next__ failure
         class ExpectedError(Exception):
             pass
         def delayed_raise(n=0):
@@ -131,9 +131,9 @@ class TestBasicOps(unittest.TestCase):
         def gulp(iterable, keyp=None, func=list):
             return [func(g) for k, g in groupby(iterable, keyp)]
 
-        # iter.next failure on outer object
+        # iter.__next__ failure on outer object
         self.assertRaises(ExpectedError, gulp, delayed_raise(0))
-        # iter.next failure on inner object
+        # iter.__next__ failure on inner object
         self.assertRaises(ExpectedError, gulp, delayed_raise(1))
 
         # __cmp__ failure
@@ -169,7 +169,7 @@ class TestBasicOps(unittest.TestCase):
         self.assertRaises(TypeError, ifilter, lambda x:x)
         self.assertRaises(TypeError, ifilter, lambda x:x, range(6), 7)
         self.assertRaises(TypeError, ifilter, isEven, 3)
-        self.assertRaises(TypeError, ifilter(range(6), range(6)).next)
+        self.assertRaises(TypeError, next, ifilter(range(6), range(6)))
 
     def test_ifilterfalse(self):
         self.assertEqual(list(ifilterfalse(isEven, range(6))), [1,3,5])
@@ -179,7 +179,7 @@ class TestBasicOps(unittest.TestCase):
         self.assertRaises(TypeError, ifilterfalse, lambda x:x)
         self.assertRaises(TypeError, ifilterfalse, lambda x:x, range(6), 7)
         self.assertRaises(TypeError, ifilterfalse, isEven, 3)
-        self.assertRaises(TypeError, ifilterfalse(range(6), range(6)).next)
+        self.assertRaises(TypeError, next, ifilterfalse(range(6), range(6)))
 
     def test_izip(self):
         # XXX This is rather silly now that builtin zip() calls izip()...
@@ -276,9 +276,9 @@ class TestBasicOps(unittest.TestCase):
         self.assertEqual(list(imap(operator.pow, [])), [])
         self.assertRaises(TypeError, imap)
         self.assertRaises(TypeError, imap, operator.neg)
-        self.assertRaises(TypeError, imap(10, range(5)).next)
-        self.assertRaises(ValueError, imap(errfunc, [4], [5]).next)
-        self.assertRaises(TypeError, imap(onearg, [4], [5]).next)
+        self.assertRaises(TypeError, next, imap(10, range(5)))
+        self.assertRaises(ValueError, next, imap(errfunc, [4], [5]))
+        self.assertRaises(TypeError, next, imap(onearg, [4], [5]))
 
     def test_starmap(self):
         self.assertEqual(list(starmap(operator.pow, zip(range(3), range(1,7)))),
@@ -289,9 +289,9 @@ class TestBasicOps(unittest.TestCase):
         self.assertRaises(TypeError, list, starmap(operator.pow, [[4,5]]))
         self.assertRaises(TypeError, starmap)
         self.assertRaises(TypeError, starmap, operator.pow, [(4,5)], 'extra')
-        self.assertRaises(TypeError, starmap(10, [(4,5)]).next)
-        self.assertRaises(ValueError, starmap(errfunc, [(4,5)]).next)
-        self.assertRaises(TypeError, starmap(onearg, [(4,5)]).next)
+        self.assertRaises(TypeError, next, starmap(10, [(4,5)]))
+        self.assertRaises(ValueError, next, starmap(errfunc, [(4,5)]))
+        self.assertRaises(TypeError, next, starmap(onearg, [(4,5)]))
 
     def test_islice(self):
         for args in [          # islice(args) should agree with range(args)
@@ -344,11 +344,11 @@ class TestBasicOps(unittest.TestCase):
         self.assertRaises(TypeError, takewhile)
         self.assertRaises(TypeError, takewhile, operator.pow)
         self.assertRaises(TypeError, takewhile, operator.pow, [(4,5)], 'extra')
-        self.assertRaises(TypeError, takewhile(10, [(4,5)]).next)
-        self.assertRaises(ValueError, takewhile(errfunc, [(4,5)]).next)
+        self.assertRaises(TypeError, next, takewhile(10, [(4,5)]))
+        self.assertRaises(ValueError, next, takewhile(errfunc, [(4,5)]))
         t = takewhile(bool, [1, 1, 1, 0, 0, 0])
         self.assertEqual(list(t), [1, 1, 1])
-        self.assertRaises(StopIteration, t.next)
+        self.assertRaises(StopIteration, next, t)
 
     def test_dropwhile(self):
         data = [1, 3, 5, 20, 2, 4, 6, 8]
@@ -358,8 +358,8 @@ class TestBasicOps(unittest.TestCase):
         self.assertRaises(TypeError, dropwhile)
         self.assertRaises(TypeError, dropwhile, operator.pow)
         self.assertRaises(TypeError, dropwhile, operator.pow, [(4,5)], 'extra')
-        self.assertRaises(TypeError, dropwhile(10, [(4,5)]).next)
-        self.assertRaises(ValueError, dropwhile(errfunc, [(4,5)]).next)
+        self.assertRaises(TypeError, next, dropwhile(10, [(4,5)]))
+        self.assertRaises(ValueError, next, dropwhile(errfunc, [(4,5)]))
 
     def test_tee(self):
         n = 200
@@ -380,13 +380,13 @@ class TestBasicOps(unittest.TestCase):
 
         a, b = tee(irange(n)) # test dealloc of leading iterator
         for i in xrange(100):
-            self.assertEqual(a.next(), i)
+            self.assertEqual(next(a), i)
         del a
         self.assertEqual(list(b), range(n))
 
         a, b = tee(irange(n)) # test dealloc of trailing iterator
         for i in xrange(100):
-            self.assertEqual(a.next(), i)
+            self.assertEqual(next(a), i)
         del b
         self.assertEqual(list(a), range(100, n))
 
@@ -396,7 +396,7 @@ class TestBasicOps(unittest.TestCase):
             lists = ([], [])
             its = tee(irange(n))
             for i in order:
-                value = its[i].next()
+                value = next(its[i])
                 lists[i].append(value)
             self.assertEqual(lists[0], range(n))
             self.assertEqual(lists[1], range(n))
@@ -415,9 +415,9 @@ class TestBasicOps(unittest.TestCase):
         # test long-lagged and multi-way split
         a, b, c = tee(xrange(2000), 3)
         for i in xrange(100):
-            self.assertEqual(a.next(), i)
+            self.assertEqual(next(a), i)
         self.assertEqual(list(b), range(2000))
-        self.assertEqual([c.next(), c.next()], range(2))
+        self.assertEqual([next(c), next(c)], range(2))
         self.assertEqual(list(a), range(100,2000))
         self.assertEqual(list(c), range(2,2000))
 
@@ -451,33 +451,33 @@ class TestBasicOps(unittest.TestCase):
         self.assertRaises(ReferenceError, getattr, p, '__class__')
 
     def test_StopIteration(self):
-        self.assertRaises(StopIteration, izip().next)
+        self.assertRaises(StopIteration, next, izip())
 
         for f in (chain, cycle, izip, groupby):
-            self.assertRaises(StopIteration, f([]).next)
-            self.assertRaises(StopIteration, f(StopNow()).next)
+            self.assertRaises(StopIteration, next, f([]))
+            self.assertRaises(StopIteration, next, f(StopNow()))
 
-        self.assertRaises(StopIteration, islice([], None).next)
-        self.assertRaises(StopIteration, islice(StopNow(), None).next)
+        self.assertRaises(StopIteration, next, islice([], None))
+        self.assertRaises(StopIteration, next, islice(StopNow(), None))
 
         p, q = tee([])
-        self.assertRaises(StopIteration, p.next)
-        self.assertRaises(StopIteration, q.next)
+        self.assertRaises(StopIteration, next, p)
+        self.assertRaises(StopIteration, next, q)
         p, q = tee(StopNow())
-        self.assertRaises(StopIteration, p.next)
-        self.assertRaises(StopIteration, q.next)
+        self.assertRaises(StopIteration, next, p)
+        self.assertRaises(StopIteration, next, q)
 
-        self.assertRaises(StopIteration, repeat(None, 0).next)
+        self.assertRaises(StopIteration, next, repeat(None, 0))
 
         for f in (ifilter, ifilterfalse, imap, takewhile, dropwhile, starmap):
-            self.assertRaises(StopIteration, f(lambda x:x, []).next)
-            self.assertRaises(StopIteration, f(lambda x:x, StopNow()).next)
+            self.assertRaises(StopIteration, next, f(lambda x:x, []))
+            self.assertRaises(StopIteration, next, f(lambda x:x, StopNow()))
 
 class TestGC(unittest.TestCase):
 
     def makecycle(self, iterator, container):
         container.append(iterator)
-        iterator.next()
+        next(iterator)
         del container, iterator
 
     def test_chain(self):
@@ -547,7 +547,7 @@ class I:
         self.i = 0
     def __iter__(self):
         return self
-    def next(self):
+    def __next__(self):
         if self.i >= len(self.seqn): raise StopIteration
         v = self.seqn[self.i]
         self.i += 1
@@ -567,14 +567,14 @@ class X:
     def __init__(self, seqn):
         self.seqn = seqn
         self.i = 0
-    def next(self):
+    def __next__(self):
         if self.i >= len(self.seqn): raise StopIteration
         v = self.seqn[self.i]
         self.i += 1
         return v
 
 class N:
-    'Iterator missing next()'
+    'Iterator missing __next__()'
     def __init__(self, seqn):
         self.seqn = seqn
         self.i = 0
@@ -588,7 +588,7 @@ class E:
         self.i = 0
     def __iter__(self):
         return self
-    def next(self):
+    def __next__(self):
         3 // 0
 
 class S:
@@ -597,7 +597,7 @@ class S:
         pass
     def __iter__(self):
         return self
-    def next(self):
+    def __next__(self):
         raise StopIteration
 
 def L(seqn):
@@ -748,13 +748,13 @@ class RegressionTests(unittest.TestCase):
             def g(value, first=[1]):
                 if first:
                     del first[:]
-                    f(z.next())
+                    f(next(z))
                 return value
             items = list(tuple2)
             items[1:1] = list(tuple1)
             gen = imap(g, items)
             z = izip(*[gen]*len(tuple1))
-            z.next()
+            next(z)
 
         def f(t):
             global T
@@ -930,7 +930,7 @@ Samuele
 ...     "s -> (s0,s1), (s1,s2), (s2, s3), ..."
 ...     a, b = tee(iterable)
 ...     try:
-...         b.next()
+...         next(b)
 ...     except StopIteration:
 ...         pass
 ...     return izip(a, b)
index 803edd589dc8328ff766e19291ae4dd734d3a4f2..8650cef710404f1fae3608a7092aed90de533b31 100644 (file)
@@ -1518,11 +1518,11 @@ class TestProxyFileBase(TestBase):
         # Iterate by line
         proxy.seek(0)
         iterator = iter(proxy)
-        self.assert_(iterator.next() == 'foo' + os.linesep)
-        self.assert_(iterator.next() == 'bar' + os.linesep)
-        self.assert_(iterator.next() == 'fred' + os.linesep)
-        self.assert_(iterator.next() == 'bob')
-        self.assertRaises(StopIteration, lambda: iterator.next())
+        self.assert_(next(iterator) == 'foo' + os.linesep)
+        self.assert_(next(iterator) == 'bar' + os.linesep)
+        self.assert_(next(iterator) == 'fred' + os.linesep)
+        self.assert_(next(iterator) == 'bob')
+        self.assertRaises(StopIteration, next, iterator)
 
     def _test_seek_and_tell(self, proxy):
         # Seek and use tell to check position
index bb97433f4012032d3dcdeff333d27755ac4d2f18..bca2ecfd501829affce9f9db25116af1e0a1ee34 100644 (file)
@@ -586,8 +586,8 @@ class ReTests(unittest.TestCase):
 
     def test_bug_581080(self):
         iter = re.finditer(r"\s", "a b")
-        self.assertEqual(iter.next().span(), (1,2))
-        self.assertRaises(StopIteration, iter.next)
+        self.assertEqual(next(iter).span(), (1,2))
+        self.assertRaises(StopIteration, next, iter)
 
         scanner = re.compile(r"\s").scanner("a b")
         self.assertEqual(scanner.search().span(), (1, 2))
@@ -595,9 +595,9 @@ class ReTests(unittest.TestCase):
 
     def test_bug_817234(self):
         iter = re.finditer(r".*", "asdf")
-        self.assertEqual(iter.next().span(), (0, 4))
-        self.assertEqual(iter.next().span(), (4, 4))
-        self.assertRaises(StopIteration, iter.next)
+        self.assertEqual(next(iter).span(), (0, 4))
+        self.assertEqual(next(iter).span(), (4, 4))
+        self.assertRaises(StopIteration, next, iter)
 
 
 def run_re_tests():
index 8a43109f31d40cfb61495717030bee458b3bd75e..997e17f7897b9302d184f683cc268a947b3941f0 100644 (file)
@@ -1382,7 +1382,7 @@ class I:
         self.i = 0
     def __iter__(self):
         return self
-    def next(self):
+    def __next__(self):
         if self.i >= len(self.seqn): raise StopIteration
         v = self.seqn[self.i]
         self.i += 1
@@ -1402,14 +1402,14 @@ class X:
     def __init__(self, seqn):
         self.seqn = seqn
         self.i = 0
-    def next(self):
+    def __next__(self):
         if self.i >= len(self.seqn): raise StopIteration
         v = self.seqn[self.i]
         self.i += 1
         return v
 
 class N:
-    'Iterator missing next()'
+    'Iterator missing __next__()'
     def __init__(self, seqn):
         self.seqn = seqn
         self.i = 0
@@ -1423,7 +1423,7 @@ class E:
         self.i = 0
     def __iter__(self):
         return self
-    def next(self):
+    def __next__(self):
         3 // 0
 
 class S:
@@ -1432,7 +1432,7 @@ class S:
         pass
     def __iter__(self):
         return self
-    def next(self):
+    def __next__(self):
         raise StopIteration
 
 from itertools import chain, imap
index 226a16809ecd76644e728ce27fbc5cf73c1090cc..0067bdbcfe74892548c12fd50cb0cb30e4a05500 100644 (file)
@@ -22,10 +22,10 @@ class StrTest(
     def test_iterators(self):
         # Make sure str objects have an __iter__ method
         it = "abc".__iter__()
-        self.assertEqual(it.next(), "a")
-        self.assertEqual(it.next(), "b")
-        self.assertEqual(it.next(), "c")
-        self.assertRaises(StopIteration, it.next)
+        self.assertEqual(next(it), "a")
+        self.assertEqual(next(it), "b")
+        self.assertEqual(next(it), "c")
+        self.assertRaises(StopIteration, next, it)
 
     def test_conversion(self):
         # Make sure __str__() behaves properly
index 2047a6364ef72d5c9d7238c533bff1f24ef5222a..a398d3740a7edf402e21e808905920a242e4375b 100644 (file)
@@ -102,7 +102,7 @@ class test__RandomNameSequence(TC):
 
     def test_get_six_char_str(self):
         # _RandomNameSequence returns a six-character string
-        s = self.r.next()
+        s = next(self.r)
         self.nameCheck(s, '', '', '')
 
     def test_many(self):
@@ -111,7 +111,7 @@ class test__RandomNameSequence(TC):
         dict = {}
         r = self.r
         for i in xrange(TEST_FILES):
-            s = r.next()
+            s = next(r)
             self.nameCheck(s, '', '', '')
             self.failIf(s in dict)
             dict[s] = 1
index d6c37775f0e4452bbabfed658361ade3a9fa9cea..0880f0f5033c893e052d1e6b5d839507856a6e3f 100644 (file)
@@ -103,7 +103,7 @@ def test_roundtrip(f):
 
     t1 = [tok[:2] for tok in fulltok]
     newtext = untokenize(t1)
-    readline = iter(newtext.splitlines(1)).next
+    readline = iter(newtext.splitlines(1)).__next__
     t2 = [tok[:2] for tok in generate_tokens(readline)]
     if t1 != t2:
         raise TestFailed("untokenize() roundtrip failed for %r" % f)
@@ -224,7 +224,7 @@ def test_rarrow():
     This function exists solely to test the tokenization of the RARROW
     operator.
 
-    >>> tokenize(iter(['->']).next)   #doctest: +NORMALIZE_WHITESPACE
+    >>> tokenize(iter(['->']).__next__)   #doctest: +NORMALIZE_WHITESPACE
     1,0-1,2:\tOP\t'->'
     2,0-2,0:\tENDMARKER\t''
     """
index b789fb0ba093b58a83e6c8bb82c97726ee76855e..cb7586c2e941d5480a9df5ab4cc800f0173767ee 100644 (file)
@@ -99,10 +99,10 @@ class UnicodeTest(
     def test_iterators(self):
         # Make sure unicode objects have an __iter__ method
         it = u"\u1111\u2222\u3333".__iter__()
-        self.assertEqual(it.next(), u"\u1111")
-        self.assertEqual(it.next(), u"\u2222")
-        self.assertEqual(it.next(), u"\u3333")
-        self.assertRaises(StopIteration, it.next)
+        self.assertEqual(next(it), u"\u1111")
+        self.assertEqual(next(it), u"\u2222")
+        self.assertEqual(next(it), u"\u3333")
+        self.assertRaises(StopIteration, next, it)
 
     def test_count(self):
         string_tests.CommonTest.test_count(self)
index 8c7ef2efe2b467e5d3daff957f17831e2053042e..e9fbd2de138ffa308b313c02ce25015521c06de6 100644 (file)
@@ -51,7 +51,7 @@ class UserListTest(list_tests.CommonTest):
         class T(self.type2test):
             def __getitem__(self, key):
                 return str(key) + '!!!'
-        self.assertEqual(iter(T((1,2))).next(), "0!!!")
+        self.assertEqual(next(iter(T((1,2)))), "0!!!")
 
 def test_main():
     test_support.run_unittest(UserListTest)
index f33c30dda4837207c1314522dcae6adce23652d1..d77e861dd6df08388f92eca3dedce111f86ec5f9 100755 (executable)
@@ -108,13 +108,13 @@ def compare_generic_iter(make_it,match):
         it = make_it()
         if not iter(it) is it: raise AssertionError
         for item in match:
-            if not it.next()==item: raise AssertionError
+            if not next(it) == item: raise AssertionError
         try:
-            it.next()
+            next(it)
         except StopIteration:
             pass
         else:
-            raise AssertionError("Too many items from .next()",it)
+            raise AssertionError("Too many items from .__next__()", it)
 
 
 
index 8dc4c53e916b3e208b38b2366a2d9333a9feb084..1a72d6fc59a32d72b5c2160d890ef6570d40cd35 100644 (file)
@@ -228,7 +228,7 @@ def untokenize(iterable):
         # Output text will tokenize the back to the input
         t1 = [tok[:2] for tok in generate_tokens(f.readline)]
         newcode = untokenize(t1)
-        readline = iter(newcode.splitlines(1)).next
+        readline = iter(newcode.splitlines(1)).__next__
         t2 = [tok[:2] for tokin generate_tokens(readline)]
         assert t1 == t2
     """
@@ -242,7 +242,7 @@ def generate_tokens(readline):
     readline() method of built-in file objects. Each call to the function
     should return one line of input as a string.  Alternately, readline
     can be a callable function terminating with StopIteration:
-        readline = open(myfile).next    # Example of alternate readline
+        readline = open(myfile).__next__    # Example of alternate readline
 
     The generator produces 5-tuples with these members: the token type; the
     token string; a 2-tuple (srow, scol) of ints specifying the row and
index 9811646fc996c1e2314754d8f77787b1fdf04252..22d00f7784d5f441cb45f8291956c7951f4e854b 100644 (file)
@@ -7,7 +7,7 @@ import sys
 # Iterators in Python aren't a matter of type but of protocol.  A large
 # and changing number of builtin types implement *some* flavor of
 # iterator.  Don't check the type!  Use hasattr to check for both
-# "__iter__" and "next" attributes instead.
+# "__iter__" and "__next__" attributes instead.
 
 NoneType = type(None)
 TypeType = type
index 60d2a413bfabc32aece4a5b03fa41ca4f96c832b..4e24a8f051b613922f1b69d2f4ab15b90dc15ec7 100644 (file)
@@ -902,8 +902,8 @@ class addbase:
             self.fileno = lambda: None
         if hasattr(self.fp, "__iter__"):
             self.__iter__ = self.fp.__iter__
-            if hasattr(self.fp, "next"):
-                self.next = self.fp.next
+            if hasattr(self.fp, "__next__"):
+                self.__next__ = self.fp.__next__
 
     def __repr__(self):
         return '<%s at %r whose fp = %r>' % (self.__class__.__name__,
index 450a32fbce5647fa142a23a7f4f1924abfed8cf1..5b44eda16dffe90020fd3ba5174b8dea66c83cb6 100644 (file)
@@ -26,7 +26,7 @@ class FileWrapper:
     def __iter__(self):
         return self
 
-    def next(self):
+    def __next__(self):
         data = self.filelike.read(self.blksize)
         if data:
             return data
index 43784f9e660da51875a641d0e394bb9840b16cc8..09b0d950e2dfcd5d54ec928ae4a38f553b4bc2d0 100644 (file)
@@ -98,7 +98,7 @@ Some of the things this checks:
   - That it is not a string (it should be a list of a single string; a
     string will work, but perform horribly).
 
-  - That .next() returns a string
+  - That .__next__() returns a string
 
   - That the iterator is not iterated over until start_response has
     been called (that can signal either a server or application
@@ -265,10 +265,10 @@ class IteratorWrapper:
     def __iter__(self):
         return self
 
-    def next(self):
+    def __next__(self):
         assert_(not self.closed,
             "Iterator read after closed")
-        v = self.iterator.next()
+        v = next(self.iterator)
         if self.check_start_response is not None:
             assert_(self.check_start_response,
                 "The application returns and we started iterating over its body, but start_response has not yet been called")
index f0245c49c39acb2aacdb8e65a42b76d72cad50ed..fe9a3a2196e0f4d3f1fc21881952b5aa0b7e1ad7 100644 (file)
@@ -228,7 +228,7 @@ class DOMEventStream:
             return rc
         raise IndexError
 
-    def next(self):
+    def __next__(self):
         rc = self.getEvent()
         if rc:
             return rc
index 19bb74772b56a597ff24c97122ad44b79bb87e5e..694d6c4a9091629d017232635dba75660cc82df7 100644 (file)
@@ -893,7 +893,7 @@ class iterparse:
                     append((event, None))
                 parser.EndNamespaceDeclHandler = handler
 
-    def next(self):
+    def __next__(self):
         while 1:
             try:
                 item = self._events[self._index]
@@ -923,7 +923,7 @@ class iterparse:
             return self
     except NameError:
         def __getitem__(self, index):
-            return self.next()
+            return self.__next__()
 
 ##
 # Parses an XML document from a string constant.  This function can
index 55dd27702d6aa8cd4ce8a1a0496617c8551bb43a..c43d226161627a644a9370a7ba9247f8cca1265a 100644 (file)
@@ -139,7 +139,7 @@ def fill_stmt(iterable, fill_len):
             overflow = None
         while total_len < fill_len:
             try:
-                new_item = it.next()
+                new_item = next(it)
                 buffer_.append(new_item)
                 total_len += len(new_item) + 1
             except StopIteration:
@@ -188,7 +188,7 @@ def main(file_path):
                                             FILL - len(prefix) - len(indent))
                         try:
                             while True:
-                                print>>FILE, indent + prefix + stmt_iter.next()
+                                print>>FILE, indent + prefix + next(stmt_iter)
                         except StopIteration:
                             print>>FILE, ''
                     else:
index 0b4b9417e1b021d5c14f589ba5549b8c4943999b..0e34b15fd3ec11d10b3801caaa21754acbc72096 100644 (file)
@@ -721,7 +721,7 @@ File Exceptions
     return [result] -- Exits from function (or method) and returns result (use a tuple to
                        return more than one value). If no result given, then returns None.
     yield result    -- Freezes the execution frame of a generator and returns the result
-                       to the iterator's .next() method.  Upon the next call to next(),
+                       to the iterator's .__next__() method.  Upon the next call to __next__(),
                        resumes execution at the frozen point with all of the local variables
                        still intact.
 
@@ -1058,7 +1058,7 @@ Exception>
     SystemExit
          On 'sys.exit()'
     StopIteration
-         Signal the end from iterator.next()
+         Signal the end from iterator.__next__()
     StandardError
                  Base class for all built-in exceptions; derived from Exception
     root class.
index 8fe484a770a09fb38f2265e6777e19170991bf2f..1b8f51c5ac62e861164b9b069c4ff490bf37204a 100644 (file)
@@ -1701,7 +1701,7 @@ chain_next(chainobject *lz)
 PyDoc_STRVAR(chain_doc,
 "chain(*iterables) --> chain object\n\
 \n\
-Return a chain object whose .next() method returns elements from the\n\
+Return a chain object whose .__next__() method returns elements from the\n\
 first iterable until it is exhausted, then elements from the next\n\
 iterable, until all of the iterables are exhausted.");
 
@@ -2090,7 +2090,7 @@ count_repr(countobject *lz)
 PyDoc_STRVAR(count_doc,
 "count([firstval]) --> count object\n\
 \n\
-Return a count object whose .next() method returns consecutive\n\
+Return a count object whose .__next__() method returns consecutive\n\
 integers starting from zero or, if specified, from firstval.");
 
 static PyTypeObject count_type = {
@@ -2272,8 +2272,8 @@ izip_next(izipobject *lz)
 PyDoc_STRVAR(izip_doc,
 "izip(iter1 [,iter2 [...]]) --> izip object\n\
 \n\
-Return a izip object whose .next() method returns a tuple where\n\
-the i-th element comes from the i-th iterable argument.  The .next()\n\
+Return a izip object whose .__next__() method returns a tuple where\n\
+the i-th element comes from the i-th iterable argument.  The .__next__()\n\
 method continues until the shortest iterable in the argument sequence\n\
 is exhausted and then it raises StopIteration.  Works like the zip()\n\
 function but consumes less memory by returning an iterator instead of\n\
@@ -2648,8 +2648,8 @@ izip_longest_next(iziplongestobject *lz)
 PyDoc_STRVAR(izip_longest_doc,
 "izip_longest(iter1 [,iter2 [...]], [fillvalue=None]) --> izip_longest object\n\
 \n\
-Return an izip_longest object whose .next() method returns a tuple where\n\
-the i-th element comes from the i-th iterable argument.  The .next()\n\
+Return an izip_longest object whose .__next__() method returns a tuple where\n\
+the i-th element comes from the i-th iterable argument.  The .__next__()\n\
 method continues until the longest iterable in the argument sequence\n\
 is exhausted and then it raises StopIteration.  When the shorter iterables\n\
 are exhausted, the fillvalue is substituted in their place.  The fillvalue\n\
index be3302ba7cdc735b51927e00006cf160eaa26b61..6832fd96ab62528f49d6be13f2eba8064f2b36ec 100644 (file)
@@ -369,7 +369,7 @@ SimpleExtendsException(PyExc_StandardError, TypeError,
  *    StopIteration extends Exception
  */
 SimpleExtendsException(PyExc_Exception, StopIteration,
-                       "Signal the end from iterator.next().");
+                       "Signal the end from iterator.__next__().");
 
 
 /*
index 1ccd97b7b2903489f44908857b141eab57f4f00d..c6091dfcbc1d3ba5c2f3ac610b60f6cb533ec66a 100644 (file)
@@ -4636,7 +4636,7 @@ static PyObject *
 slot_tp_iternext(PyObject *self)
 {
        static PyObject *next_str;
-       return call_method(self, "next", &next_str, "()");
+       return call_method(self, "__next__", &next_str, "()");
 }
 
 static PyObject *
@@ -5031,8 +5031,8 @@ static slotdef slotdefs[] = {
               "x.__ge__(y) <==> x>=y"),
        TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc,
               "x.__iter__() <==> iter(x)"),
-       TPSLOT("next", tp_iternext, slot_tp_iternext, wrap_next,
-              "x.next() -> the next value, or raise StopIteration"),
+       TPSLOT("__next__", tp_iternext, slot_tp_iternext, wrap_next,
+              "x.__next__() <==> next(x)"),
        TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get,
               "descr.__get__(obj[, type]) -> value"),
        TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set,
index 20746fa4bfca67055c139b01dbe84e076d0bec41..8ce0e4859af2cf58623301bdb684d00d527ec75d 100644 (file)
@@ -1136,6 +1136,46 @@ sequences have the same length.  If the function is None, return a list of\n\
 the items of the sequence (or a list of tuples if more than one sequence).");
 
 
+static PyObject *
+builtin_next(PyObject *self, PyObject *args)
+{
+       PyObject *it, *res;
+       PyObject *def = NULL;
+
+       if (!PyArg_UnpackTuple(args, "next", 1, 2, &it, &def))
+               return NULL;
+       if (!PyIter_Check(it)) {
+               PyErr_Format(PyExc_TypeError,
+                       "%.200s object is not an iterator", it->ob_type->tp_name);
+               return NULL;
+       }
+       
+       res = (*it->ob_type->tp_iternext)(it);
+       if (res == NULL) {
+               if (def) {
+                       if (PyErr_Occurred() &&
+                           !PyErr_ExceptionMatches(PyExc_StopIteration))
+                               return NULL;
+                       PyErr_Clear();
+                       Py_INCREF(def);
+                       return def;
+               } else if (PyErr_Occurred()) {
+                       return NULL;
+               } else {
+                       PyErr_SetNone(PyExc_StopIteration);
+                       return NULL;
+               }
+       }
+       return res;
+}
+
+PyDoc_STRVAR(next_doc,
+"next(iterator[, default])\n\
+\n\
+Return the next item from the iterator. If default is given and the iterator\n\
+is exhausted, it is returned instead of raising StopIteration.");
+
+
 static PyObject *
 builtin_setattr(PyObject *self, PyObject *args)
 {
@@ -2252,6 +2292,7 @@ static PyMethodDef builtin_methods[] = {
        {"map",         builtin_map,        METH_VARARGS, map_doc},
        {"max",         (PyCFunction)builtin_max,        METH_VARARGS | METH_KEYWORDS, max_doc},
        {"min",         (PyCFunction)builtin_min,        METH_VARARGS | METH_KEYWORDS, min_doc},
+       {"next",        (PyCFunction)builtin_next,       METH_VARARGS, next_doc},
        {"oct",         builtin_oct,        METH_O, oct_doc},
        {"open",        (PyCFunction)builtin_open,       METH_VARARGS | METH_KEYWORDS, open_doc},
        {"ord",         builtin_ord,        METH_O, ord_doc},
index 0e90cd3d22e01f4102164668eedc511855f468ef..6ee93e6ebd2778065f26ba2f33c336c1e1955703 100644 (file)
@@ -162,7 +162,7 @@ class FutureFinder:
         OP = tokenize.OP
 
         changed = self.changed
-        get = tokenize.generate_tokens(self.getline).next
+        get = tokenize.generate_tokens(self.getline).__next__
         type, token, (srow, scol), (erow, ecol), line = get()
 
         # Chew up initial comments and blank lines (if any).