[Jython-checkins] jython: Add long_info and factor out big int construction from atoi.

frank.wierzbicki jython-checkins at python.org
Thu Apr 19 02:05:51 CEST 2012


http://hg.python.org/jython/rev/8a79569384de
changeset:   6606:8a79569384de
user:        Frank Wierzbicki <fwierzbicki at gmail.com>
date:        Wed Apr 18 16:21:06 2012 -0700
summary:
  Add long_info and factor out big int construction from atoi.

files:
  src/org/python/core/PyString.java      |  46 +++++++++----
  src/org/python/core/PySystemState.java |  28 ++++++++
  2 files changed, 58 insertions(+), 16 deletions(-)


diff --git a/src/org/python/core/PyString.java b/src/org/python/core/PyString.java
--- a/src/org/python/core/PyString.java
+++ b/src/org/python/core/PyString.java
@@ -1581,15 +1581,7 @@
         }
     }
 
-    public int atoi() {
-        return atoi(10);
-    }
-
-    public int atoi(int base) {
-        if ((base != 0 && base < 2) || (base > 36)) {
-            throw Py.ValueError("invalid base for atoi()");
-        }
-
+    private BigInteger asciiToBigInteger(int base) {
         int b = 0;
         int e = getString().length();
 
@@ -1641,19 +1633,35 @@
             }
         }
 
-        if (base == 0)
+        if (base == 0) {
             base = 10;
+        }
 
         String s = getString();
-        if (b > 0 || e < getString().length())
+        if (b > 0 || e < getString().length()) {
             s = getString().substring(b, e);
+        }
+
+        BigInteger bi;
+        if (sign == '-') {
+            bi = new BigInteger("-" + s, base);
+        } else {
+            bi = new BigInteger(s, base);
+        }
+        return bi;
+    }
+
+    public int atoi() {
+        return atoi(10);
+    }
+
+    public int atoi(int base) {
+        if ((base != 0 && base < 2) || (base > 36)) {
+            throw Py.ValueError("invalid base for atoi()");
+        }
 
         try {
-            BigInteger bi;
-            if (sign == '-') {
-                bi = new BigInteger("-" + s, base);
-            } else
-                bi = new BigInteger(s, base);
+            BigInteger bi = asciiToBigInteger(base);
             if (bi.compareTo(PyInteger.MAX_INT) > 0 || bi.compareTo(PyInteger.MIN_INT) < 0) {
                 throw Py.OverflowError("long int too large to convert to int");
             }
@@ -1702,6 +1710,12 @@
                     }
                 }
             }
+            if (base == 2) {
+                if (b < e-1 &&
+                       Character.toUpperCase(getString().charAt(b+1)) == 'B') {
+                    b += 2;
+                }
+            }
         }
         if (base == 0)
             base = 10;
diff --git a/src/org/python/core/PySystemState.java b/src/org/python/core/PySystemState.java
--- a/src/org/python/core/PySystemState.java
+++ b/src/org/python/core/PySystemState.java
@@ -182,6 +182,9 @@
     // float_info
     public static PyObject float_info;
 
+    // long_info
+    public static PyObject long_info;
+
     public PySystemState() {
         initialize();
         closer = new PySystemStateCloser(this);
@@ -975,6 +978,7 @@
                                  Py.newString(Version.getHGVersion()));
 
         float_info = FloatInfo.getInfo();
+        long_info = LongInfo.getInfo();
     }
 
 
@@ -1527,3 +1531,27 @@
         );
     }
 }
+
+ at ExposedType(name = "sys.long_info", isBaseType = false)
+class LongInfo extends PyTuple {
+    @ExposedGet
+    public PyObject bits_per_digit, sizeof_digit;
+
+    public static final PyType TYPE = PyType.fromClass(LongInfo.class);
+    
+    private LongInfo(PyObject ...vals) {
+        super(TYPE, vals);
+
+        bits_per_digit = vals[0];
+        sizeof_digit = vals[1];
+    }
+
+    //XXX: I've cheated and just used the values that CPython gives me for my
+    //     local Ubuntu system. I'm not sure that they are correct.
+    static public LongInfo getInfo() {
+        return new LongInfo(
+            Py.newLong(30),
+            Py.newLong(4)
+        );
+    }
+}

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


More information about the Jython-checkins mailing list