[Jython-checkins] jython: Refactored list and tuple iteration to not call special methods. Fixes #2442

jim.baker jython-checkins at python.org
Fri Feb 12 22:32:04 EST 2016


https://hg.python.org/jython/rev/85d99f6152b1
changeset:   7899:85d99f6152b1
user:        Jim Baker <jim.baker at rackspace.com>
date:        Fri Feb 12 20:22:22 2016 -0700
summary:
  Refactored list and tuple iteration to not call special methods. Fixes #2442

files:
  CoreExposed.includes             |   2 ++
  Lib/test/test_list_jy.py         |  20 ++++++++++++++++++++
  Lib/test/test_tuple_jy.py        |  20 ++++++++++++++++++++
  src/org/python/core/PyList.java  |   4 ++--
  src/org/python/core/PyTuple.java |   2 +-
  5 files changed, 45 insertions(+), 3 deletions(-)


diff --git a/CoreExposed.includes b/CoreExposed.includes
--- a/CoreExposed.includes
+++ b/CoreExposed.includes
@@ -30,6 +30,7 @@
 org/python/core/PyInstance.class
 org/python/core/PyInteger.class
 org/python/core/PyList.class
+org/python/core/PyListIterator.class
 org/python/core/PyLong.class
 org/python/core/PyMemoryView.class
 org/python/core/PyMethod.class
@@ -47,6 +48,7 @@
 org/python/core/PySuper.class
 org/python/core/PyTraceback.class
 org/python/core/PyTuple.class
+org/python/core/PyTupleIterator.class
 org/python/core/PyType.class
 org/python/core/PyUnicode.class
 org/python/core/PyVersionInfo.class
diff --git a/Lib/test/test_list_jy.py b/Lib/test/test_list_jy.py
--- a/Lib/test/test_list_jy.py
+++ b/Lib/test/test_list_jy.py
@@ -276,6 +276,26 @@
         lst = MyList(['a', 'b', 'c'])
         self.assertEqual(list(lst), ['a', 'b', 'c', 3])
 
+    def test_subclass_iter_does_not_call_other_special_methods(self):
+        # Verify fix for http://bugs.jython.org/issue2442
+
+        class C(list):
+            def __init__(self, *args):
+                self.called = False
+                return super(C, type(self)).__init__(self, *args)
+
+            def __len__(self):
+                self.called = True
+                return super(C, type(self)).__len__(self)
+
+            def __getitem__(self, index):
+                self.called = True
+                return super(C, type(self)).__getitem__(self, index)
+
+        c = C(range(10))
+        self.assertEqual(list(iter(c)), range(10))
+        self.assertFalse(c.called)
+
 
 def test_main():
     test_support.run_unittest(ListSubclassTestCase,
diff --git a/Lib/test/test_tuple_jy.py b/Lib/test/test_tuple_jy.py
--- a/Lib/test/test_tuple_jy.py
+++ b/Lib/test/test_tuple_jy.py
@@ -10,6 +10,26 @@
         self.assertEqual(len(s), 64000)
         self.assertEqual(sum(s), 2047968000)
 
+    def test_subclass_iter_does_not_call_other_special_methods(self):
+        # Verify fix for http://bugs.jython.org/issue2442
+
+        class C(tuple):
+            def __init__(self, *args):
+                self.called = False
+                return super(C, type(self)).__init__(self, *args)
+
+            def __len__(self):
+                self.called = True
+                return super(C, type(self)).__len__(self)
+
+            def __getitem__(self, index):
+                self.called = True
+                return super(C, type(self)).__getitem__(self, index)
+
+        c = C(range(10))
+        self.assertEqual(list(iter(c)), range(10))
+        self.assertFalse(c.called)
+
 
 def test_main():
     test_support.run_unittest(TupleTestCase)
diff --git a/src/org/python/core/PyList.java b/src/org/python/core/PyList.java
--- a/src/org/python/core/PyList.java
+++ b/src/org/python/core/PyList.java
@@ -453,8 +453,8 @@
     }
 
     @ExposedMethod(doc = BuiltinDocs.list___iter___doc)
-    public synchronized PyObject list___iter__() {
-        return new PyFastSequenceIter(this);
+    final PyObject list___iter__() {
+        return new PyListIterator(this);
     }
 
     //@Override
diff --git a/src/org/python/core/PyTuple.java b/src/org/python/core/PyTuple.java
--- a/src/org/python/core/PyTuple.java
+++ b/src/org/python/core/PyTuple.java
@@ -246,7 +246,7 @@
 
     @ExposedMethod(doc = BuiltinDocs.tuple___iter___doc)
     public PyObject tuple___iter__() {
-        return new PyFastSequenceIter(this);
+        return new PyTupleIterator(this);
     }
 
     @ExposedMethod(defaults = "null", doc = BuiltinDocs.tuple___getslice___doc)

-- 
Repository URL: https://hg.python.org/jython


More information about the Jython-checkins mailing list