[pypy-commit] pypy reflex-support: initial improvement of iterator handling; Reflex-only

wlav noreply at buildbot.pypy.org
Fri Mar 16 23:03:26 CET 2012


Author: Wim Lavrijsen <WLavrijsen at lbl.gov>
Branch: reflex-support
Changeset: r53736:021c0dc1aac7
Date: 2012-03-16 15:03 -0700
http://bitbucket.org/pypy/pypy/changeset/021c0dc1aac7/

Log:	initial improvement of iterator handling; Reflex-only

diff --git a/pypy/module/cppyy/pythonify.py b/pypy/module/cppyy/pythonify.py
--- a/pypy/module/cppyy/pythonify.py
+++ b/pypy/module/cppyy/pythonify.py
@@ -320,21 +320,26 @@
             return self
         pyclass.__iadd__ = __iadd__
 
+    # for STL iterators, whose comparison functions live globally for gcc
+    # TODO: this needs to be solved fundamentally for all classes
+    if 'iterator' in pyclass.__name__:
+        if hasattr(gbl, '__gnu_cxx'):
+            if hasattr(gbl.__gnu_cxx, '__eq__'):
+                setattr(pyclass, '__eq__', gbl.__gnu_cxx.__eq__)
+            if hasattr(gbl.__gnu_cxx, '__ne__'):
+                setattr(pyclass, '__ne__', gbl.__gnu_cxx.__ne__)
+
     # map begin()/end() protocol to iter protocol
     if hasattr(pyclass, 'begin') and hasattr(pyclass, 'end'):
-        try:
-            # TODO: make gnu-independent
-            ne = gbl.__gnu_cxx.__ne__
-            def __iter__(self):
-                iter = self.begin()
-                while gbl.__gnu_cxx.__ne__(iter, self.end()):
-                    yield iter.__deref__()
-                    iter.__preinc__()
-                iter.destruct()
-                raise StopIteration
-            pyclass.__iter__ = __iter__
-        except AttributeError:
-            pass
+        # TODO: make gnu-independent
+        def __iter__(self):
+            iter = self.begin()
+            while gbl.__gnu_cxx.__ne__(iter, self.end()):
+                yield iter.__deref__()
+                iter.__preinc__()
+            iter.destruct()
+            raise StopIteration
+        pyclass.__iter__ = __iter__
 
     # combine __getitem__ and __len__ to make a pythonized __getitem__
     if hasattr(pyclass, '__getitem__') and hasattr(pyclass, '__len__'):
diff --git a/pypy/module/cppyy/test/test_stltypes.py b/pypy/module/cppyy/test/test_stltypes.py
--- a/pypy/module/cppyy/test/test_stltypes.py
+++ b/pypy/module/cppyy/test/test_stltypes.py
@@ -39,8 +39,18 @@
 
         assert tv1 is tv2
 
+        assert cppyy.gbl.std.vector(int).iterator is cppyy.gbl.std.vector(int).iterator
+
         #-----
         v = tv1(self.N)
+        # TODO: get the following in order
+        #assert v.begin().__eq__(v.begin())
+        #assert v.begin() == v.begin()
+        #assert v.end() == v.end()
+        #assert v.begin() != v.end()
+        #assert v.end() != v.begin()
+
+        #-----
         for i in range(self.N):
           # TODO:
           #  v[i] = i


More information about the pypy-commit mailing list