Author: Amaury Forgeot d'Arc <[email protected]>
Branch: py3k
Changeset: r59374:8666c3c00e7e
Date: 2012-12-09 19:03 +0100
http://bitbucket.org/pypy/pypy/changeset/8666c3c00e7e/
Log: Add array.__iter__ (don't rely on the default behavior with
__getitem__), to make arrays instances of collections.Iterable.
diff --git a/pypy/module/array/interp_array.py
b/pypy/module/array/interp_array.py
--- a/pypy/module/array/interp_array.py
+++ b/pypy/module/array/interp_array.py
@@ -3,7 +3,8 @@
from pypy.interpreter.buffer import RWBuffer
from pypy.interpreter.error import OperationError
from pypy.interpreter.gateway import interp2app, unwrap_spec
-from pypy.interpreter.typedef import GetSetProperty, make_weakref_descr
+from pypy.interpreter.typedef import GetSetProperty, make_weakref_descr,
TypeDef
+from pypy.interpreter.baseobjspace import Wrappable
from pypy.objspace.std.model import W_Object
from pypy.objspace.std.multimethod import FailedToImplement
from pypy.objspace.std.stdtypedef import SMM, StdTypeDef
@@ -172,6 +173,25 @@
def get_raw_address(self):
return self.array._charbuf_start()
+
+class ArrayIterator(Wrappable):
+ def __init__(self, array):
+ self.index = 0
+ self.array = array
+
+ def next_w(self, space):
+ if self.index < self.array.len:
+ w_value = self.array.w_getitem(space, self.index)
+ self.index += 1
+ return w_value
+ raise OperationError(space.w_StopIteration, space.w_None)
+
+ArrayIterator.typedef = TypeDef(
+ 'arrayiterator',
+ __next__ = interp2app(ArrayIterator.next_w),
+ )
+
+
def make_array(mytype):
W_ArrayBase = globals()['W_ArrayBase']
@@ -640,6 +660,8 @@
return _cmp_impl(space, self, other, space.ge)
# Misc methods
+ def iter__Array(space, self):
+ return space.wrap(ArrayIterator(self))
def buffer__Array(space, self):
return space.wrap(ArrayBuffer(self))
diff --git a/pypy/module/array/test/test_array.py
b/pypy/module/array/test/test_array.py
--- a/pypy/module/array/test/test_array.py
+++ b/pypy/module/array/test/test_array.py
@@ -714,6 +714,11 @@
assert type(self.array(t)) is self.array
assert isinstance(self.array(t), self.array)
+ def test_iterable(self):
+ import collections
+ for t in 'bBhHiIlLfdu':
+ assert isinstance(self.array(t), collections.Iterable)
+
def test_subclass(self):
assert len(self.array('b')) == 0
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit