[Jython-checkins] jython: Support calling overloaded method j.u.List#remove(index). Fixes #2456

jim.baker jython-checkins at python.org
Tue Feb 2 18:19:09 EST 2016


https://hg.python.org/jython/rev/01ef8d1c6d0e
changeset:   7890:01ef8d1c6d0e
user:        Jim Baker <jim.baker at rackspace.com>
date:        Tue Feb 02 16:19:02 2016 -0700
summary:
  Support calling overloaded method j.u.List#remove(index). Fixes #2456

Such indices support Python convention with respect to negative
indexes, so x.remove(i) is equivalent to del x[i].

This fix restores support for using remove in this fashion against
java.util.List, as was available in 2.5.x.

files:
  Lib/test/test_list_jy.py               |  25 ++++++++++++-
  src/org/python/core/JavaProxyList.java |   6 +++
  src/org/python/core/PyInteger.java     |   2 +-
  3 files changed, 30 insertions(+), 3 deletions(-)


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
@@ -7,7 +7,7 @@
 
 if test_support.is_jython:
     from java.util import ArrayList
-    from java.lang import String
+    from java.lang import Integer, String
 
 class ListTestCase(unittest.TestCase):
 
@@ -227,13 +227,34 @@
         self.assertNotEqual(id(a), id(b))
         self.assertEqual(a, b)
 
-
     def test_extend_java_ArrayList(self):
         jl = ArrayList([])
         jl.extend([1,2])
         self.assertEqual(jl, ArrayList([1,2]))
         jl.extend(ArrayList([3,4]))
         self.assertEqual(jl, [1,2,3,4])
+    
+    def test_remove(self):
+        # Verifies that overloaded java.util.List#remove(int) method can still be used, but with Python index semantics
+        # http://bugs.jython.org/issue2456
+        jl = ArrayList(xrange(10, -1, -1))      # 10 .. 0, inclusive
+        jl.remove(0)  # removes jl[-1] (=0) 
+        self.assertEqual(jl, range(10, 0, -1))  # 10 .. 1
+        self.assertRaises(ValueError, jl.remove, Integer(0))  # j.l.Integer does not support __index__ - maybe it should!
+        jl.remove(0)  # removes jl[0] (=10)
+        self.assertEqual(jl, range(9, 0, -1))   #  9 .. 1
+        jl.remove(-1) # removes jl[-1] (=1) - support same index calculations as Python (= del jl[-1])
+        self.assertEqual(jl, range(9, 1, -1))   #  9 .. 2
+        jl.remove(3)
+        jl.remove(5)
+        self.assertEqual(jl, [9, 8, 7, 6, 4, 2])
+
+        a_to_z = list(chr(i) for i in xrange(ord('a'), ord('z') + 1))
+        b_to_z_by_2 = list(chr(i) for i in xrange(ord('b'), ord('z') + 1, 2))
+        jl = ArrayList(a_to_z)
+        for i in xrange(13):
+            jl.remove(i)
+        self.assertEqual(jl, b_to_z_by_2)
 
 
 class ListSubclassTestCase(unittest.TestCase):
diff --git a/src/org/python/core/JavaProxyList.java b/src/org/python/core/JavaProxyList.java
--- a/src/org/python/core/JavaProxyList.java
+++ b/src/org/python/core/JavaProxyList.java
@@ -492,6 +492,12 @@
                     return Py.None;
                 }
             }
+            if (object.isIndex()) {
+                // this op is still O(n), but with also the extra O(n) above
+                ListIndexDelegate ldel = new ListIndexDelegate(jlist);
+                ldel.checkIdxAndDelItem(object);
+                return Py.None;
+            }
             throw Py.ValueError(object.toString() + " is not in list");
         }
     };
diff --git a/src/org/python/core/PyInteger.java b/src/org/python/core/PyInteger.java
--- a/src/org/python/core/PyInteger.java
+++ b/src/org/python/core/PyInteger.java
@@ -1056,7 +1056,7 @@
     }
 
     /**
-     * Common code for PyInteger and PyLong to prepare an IntegerFormatter. This object has an
+     * Common code for PyInteger     and PyLong to prepare an IntegerFormatter. This object has an
      * overloaded format method {@link IntegerFormatter#format(int)} and
      * {@link IntegerFormatter#format(BigInteger)} to support the two types.
      *

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


More information about the Jython-checkins mailing list