[Jython-checkins] jython: Modify enumerate such that it can it work with arbitrarily long integers.

jim.baker jython-checkins at python.org
Thu Jun 19 00:12:31 CEST 2014


http://hg.python.org/jython/rev/7516d3820146
changeset:   7303:7516d3820146
user:        Santoso Wijaya <santoso.wijaya at gmail.com>
date:        Wed Jun 18 15:37:29 2014 -0600
summary:
  Modify enumerate such that it can it work with arbitrarily long integers.

Completes fix for http://bugs.jython.org/issue1982
Merged from https://bitbucket.org/jython/jython/pull-request/37/fix-enumerate-s-start-parameter-to-accept/

files:
  Lib/test/test_enumerate_jy.py               |   4 ++
  src/org/python/core/PyEnumerate.java        |  19 ++++++---
  src/org/python/core/PyEnumerateDerived.java |   2 +-
  3 files changed, 17 insertions(+), 8 deletions(-)


diff --git a/Lib/test/test_enumerate_jy.py b/Lib/test/test_enumerate_jy.py
--- a/Lib/test/test_enumerate_jy.py
+++ b/Lib/test/test_enumerate_jy.py
@@ -24,6 +24,10 @@
         self.assertEqual(iter(e), e)
         self.assertEqual(list(e), self.res)
 
+    def test_start_maxint(self):
+        e = self.enum(self.seq, sys.maxint)
+        self.assertEqual(list(e), [(2147483647, 'a'), (2147483648L, 'b'), (2147483649L, 'c')])
+
 
 def test_main(verbose=None):
     testclasses = (EnumerateJyTestCase,)
diff --git a/src/org/python/core/PyEnumerate.java b/src/org/python/core/PyEnumerate.java
--- a/src/org/python/core/PyEnumerate.java
+++ b/src/org/python/core/PyEnumerate.java
@@ -14,7 +14,7 @@
     public static final PyType TYPE = PyType.fromClass(PyEnumerate.class);
 
     /** Current index of enumeration. */
-    private long index;
+    private PyObject index;     // using PyObject so we are not limited to sys.maxint or Integer.MAX_VALUE
 
     /** Secondary iterator of enumeration. */
     private PyObject sit;
@@ -23,13 +23,13 @@
         super(subType);
     }
 
-    public PyEnumerate(PyType subType, PyObject seq, long start) {
+    public PyEnumerate(PyType subType, PyObject seq, PyObject start) {
         super(subType);
         index = start;
         sit = seq.__iter__();
     }
 
-    public PyEnumerate(PyObject seq, long start) {
+    public PyEnumerate(PyObject seq, PyObject start) {
         this(TYPE, seq, start);
     }
 
@@ -51,13 +51,15 @@
     public final static PyObject enumerate_new(PyNewWrapper new_, boolean init, PyType subtype,
                                                PyObject[] args, String[] keywords) {
         if (args.length > 2 || args.length <= 0) {
-            throw PyBuiltinCallable.DefaultInfo.unexpectedCall(args.length, false, "enumerate", 0,
-                                                               1);
+            throw PyBuiltinCallable.DefaultInfo.unexpectedCall(args.length, true, "enumerate", 1, 2);
         }
 
         ArgParser ap = new ArgParser("enumerate", args, keywords, new String[] {"sequence", "start"});
         PyObject seq = ap.getPyObject(0);
-        long start = (long) ap.getInt(1, 0);
+        PyObject start = ap.getPyObject(1, Py.newInteger(0));
+        if (!start.isIndex()) {
+            throw Py.TypeError("an integer is required");
+        }
 
         if (new_.for_type == subtype) {
             return new PyEnumerate(seq, start);
@@ -81,6 +83,9 @@
             return null;
         }
 
-        return new PyTuple(new PyInteger((int)index++), nextItem);
+        PyObject next = new PyTuple(index, nextItem);
+        index = index.__radd__(Py.newInteger(1));
+
+        return next;
     }
 }
diff --git a/src/org/python/core/PyEnumerateDerived.java b/src/org/python/core/PyEnumerateDerived.java
--- a/src/org/python/core/PyEnumerateDerived.java
+++ b/src/org/python/core/PyEnumerateDerived.java
@@ -38,7 +38,7 @@
         dict=new PyStringMap();
     }
 
-    public PyEnumerateDerived(PyType subtype,PyObject seq,long start) {
+    public PyEnumerateDerived(PyType subtype,PyObject seq,PyObject start) {
         super(subtype,seq,start);
         slots=new PyObject[subtype.getNumSlots()];
         dict=subtype.instDict();

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


More information about the Jython-checkins mailing list