Skip to content

Commit

Permalink
[py3] Renamed next to __next__ in iterators.
Browse files Browse the repository at this point in the history
See PEP 3114. `next` is retained as an alias for Python 2.
  • Loading branch information
aaugustin committed Aug 9, 2012
1 parent 96a6912 commit 5c09c59
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 8 deletions.
4 changes: 3 additions & 1 deletion django/core/serializers/base.py
Expand Up @@ -136,10 +136,12 @@ def __init__(self, stream_or_string, **options):
def __iter__(self):
return self

def next(self):
def __next__(self):
"""Iteration iterface -- return the next item in the stream"""
raise NotImplementedError

next = __next__ # Python 2 compatibility

class DeserializedObject(object):
"""
A deserialized model.
Expand Down
4 changes: 3 additions & 1 deletion django/core/serializers/xml_serializer.py
Expand Up @@ -154,13 +154,15 @@ def __init__(self, stream_or_string, **options):
self.event_stream = pulldom.parse(self.stream)
self.db = options.pop('using', DEFAULT_DB_ALIAS)

def next(self):
def __next__(self):
for event, node in self.event_stream:
if event == "START_ELEMENT" and node.nodeName == "object":
self.event_stream.expandNode(node)
return self._handle_object(node)
raise StopIteration

next = __next__ # Python 2 compatibility

def _handle_object(self, node):
"""
Convert an <object> node to a DeserializedObject.
Expand Down
4 changes: 3 additions & 1 deletion django/db/backends/oracle/base.py
Expand Up @@ -774,9 +774,11 @@ def __init__(self, cursor):
def __iter__(self):
return self

def next(self):
def __next__(self):
return _rowfactory(next(self.iter), self.cursor)

next = __next__ # Python 2 compatibility


def _rowfactory(row, cursor):
# Cast numeric values as the appropriate Python type based upon the
Expand Down
4 changes: 3 additions & 1 deletion django/http/__init__.py
Expand Up @@ -669,12 +669,14 @@ def __iter__(self):
self._iterator = iter(self._container)
return self

def next(self):
def __next__(self):
chunk = next(self._iterator)
if isinstance(chunk, six.text_type):
chunk = chunk.encode(self._charset)
return str(chunk)

next = __next__ # Python 2 compatibility

def close(self):
if hasattr(self._container, 'close'):
self._container.close()
Expand Down
16 changes: 12 additions & 4 deletions django/http/multipartparser.py
Expand Up @@ -305,7 +305,7 @@ def parts():
out = b''.join(parts())
return out

def next(self):
def __next__(self):
"""
Used when the exact number of bytes to read is unimportant.
Expand All @@ -322,6 +322,8 @@ def next(self):
self.position += len(output)
return output

next = __next__ # Python 2 compatibility

def close(self):
"""
Used to invalidate/disable this lazy stream.
Expand Down Expand Up @@ -376,7 +378,7 @@ def __init__(self, flo, chunk_size=64 * 1024):
self.flo = flo
self.chunk_size = chunk_size

def next(self):
def __next__(self):
try:
data = self.flo.read(self.chunk_size)
except InputStreamExhausted:
Expand All @@ -386,6 +388,8 @@ def next(self):
else:
raise StopIteration()

next = __next__ # Python 2 compatibility

def __iter__(self):
return self

Expand All @@ -400,12 +404,14 @@ def __init__(self, stream, boundary):
def __iter__(self):
return self

def next(self):
def __next__(self):
try:
return LazyStream(BoundaryIter(self._stream, self._boundary))
except InputStreamExhausted:
raise StopIteration()

next = __next__ # Python 2 compatibility

class BoundaryIter(object):
"""
A Producer that is sensitive to boundaries.
Expand Down Expand Up @@ -441,7 +447,7 @@ def __init__(self, stream, boundary):
def __iter__(self):
return self

def next(self):
def __next__(self):
if self._done:
raise StopIteration()

Expand Down Expand Up @@ -482,6 +488,8 @@ def next(self):
stream.unget(chunk[-rollback:])
return chunk[:-rollback]

next = __next__ # Python 2 compatibility

def _find_boundary(self, data, eof = False):
"""
Finds a multipart boundary in data.
Expand Down

0 comments on commit 5c09c59

Please sign in to comment.