[Jython-checkins] jython: Move some of the common methods from JITRuntime to Util and fixup the Util

wayne.meissner jython-checkins at python.org
Sun Sep 4 03:55:25 CEST 2011


http://hg.python.org/jython/rev/9c75ec1c71f5
changeset:   6242:9c75ec1c71f5
user:        Wayne Meissner <wmeissner at gmail.com>
date:        Sat Jun 18 19:16:53 2011 +1000
summary:
  Move some of the common methods from JITRuntime to Util and fixup the Util equivalents of some JITRuntime helper methods

files:
  src/org/python/modules/jffi/DefaultInvokerFactory.java |  14 +-
  src/org/python/modules/jffi/JITRuntime.java            |  73 +-------
  src/org/python/modules/jffi/Util.java                  |  84 ++++++---
  3 files changed, 73 insertions(+), 98 deletions(-)


diff --git a/src/org/python/modules/jffi/DefaultInvokerFactory.java b/src/org/python/modules/jffi/DefaultInvokerFactory.java
--- a/src/org/python/modules/jffi/DefaultInvokerFactory.java
+++ b/src/org/python/modules/jffi/DefaultInvokerFactory.java
@@ -282,7 +282,7 @@
         }
 
         public final PyObject invoke(PyObject[] args) {
-            return Util.newSigned16(jffiInvoker.invokeInt(jffiFunction, convertArguments(args)));
+            return JITRuntime.newSigned16(jffiInvoker.invokeInt(jffiFunction, convertArguments(args)));
         }
     }
 
@@ -293,7 +293,7 @@
         }
 
         public final PyObject invoke(PyObject[] args) {
-            return Util.newUnsigned16(jffiInvoker.invokeInt(jffiFunction, convertArguments(args)));
+            return JITRuntime.newUnsigned16(jffiInvoker.invokeInt(jffiFunction, convertArguments(args)));
         }
     }
 
@@ -304,7 +304,7 @@
         }
 
         public final PyObject invoke(PyObject[] args) {
-            return Util.newSigned32(jffiInvoker.invokeInt(jffiFunction, convertArguments(args)));
+            return JITRuntime.newSigned32(jffiInvoker.invokeInt(jffiFunction, convertArguments(args)));
         }
     }
 
@@ -315,7 +315,7 @@
         }
 
         public final PyObject invoke(PyObject[] args) {
-            return Util.newUnsigned32(jffiInvoker.invokeInt(jffiFunction, convertArguments(args)));
+            return JITRuntime.newUnsigned32(jffiInvoker.invokeInt(jffiFunction, convertArguments(args)));
         }
     }
 
@@ -326,7 +326,7 @@
         }
 
         public final PyObject invoke(PyObject[] args) {
-            return Util.newSigned64(jffiInvoker.invokeLong(jffiFunction, convertArguments(args)));
+            return JITRuntime.newSigned64(jffiInvoker.invokeLong(jffiFunction, convertArguments(args)));
         }
     }
 
@@ -337,7 +337,7 @@
         }
 
         public final PyObject invoke(PyObject[] args) {
-            return Util.newUnsigned64(jffiInvoker.invokeLong(jffiFunction, convertArguments(args)));
+            return JITRuntime.newUnsigned64(jffiInvoker.invokeLong(jffiFunction, convertArguments(args)));
         }
     }
 
@@ -381,7 +381,7 @@
         }
 
         public final PyObject invoke(PyObject[] args) {
-            return Util.newString(jffiInvoker.invokeAddress(jffiFunction, convertArguments(args)));
+            return JITRuntime.newString(jffiInvoker.invokeAddress(jffiFunction, convertArguments(args)));
         }
     }
     
diff --git a/src/org/python/modules/jffi/JITRuntime.java b/src/org/python/modules/jffi/JITRuntime.java
--- a/src/org/python/modules/jffi/JITRuntime.java
+++ b/src/org/python/modules/jffi/JITRuntime.java
@@ -1,8 +1,6 @@
 package org.python.modules.jffi;
 
 import org.python.core.Py;
-import org.python.core.PyInteger;
-import org.python.core.PyLong;
 import org.python.core.PyObject;
 
 import java.math.BigInteger;
@@ -23,49 +21,6 @@
         return ((Pointer) ptr).getMemory().getAddress();
     }
 
-    public static int intValue(PyObject parameter) {
-        if (parameter instanceof PyInteger) {
-            return ((PyInteger) parameter).getValue();
-
-        } else if (parameter instanceof PyLong) {
-            return ((PyLong) parameter).getValue().intValue();
-
-        } else if (parameter instanceof ScalarCData) {
-            return intValue(((ScalarCData) parameter).getValue());
-
-        } else {
-            return (int) __long__value(parameter);
-        }
-    }
-
-    public static long longValue(PyObject parameter) {
-        if (parameter instanceof PyInteger) {
-            return ((PyInteger) parameter).getValue();
-
-        } else if (parameter instanceof PyLong) {
-            return ((PyLong) parameter).getValue().longValue();
-
-        } else if (parameter instanceof ScalarCData) {
-            return longValue(((ScalarCData) parameter).getValue());
-
-        } else {
-            return __long__value(parameter);
-        }
-    }
-
-    static final long __long__value(PyObject parameter) {
-        PyObject value = parameter.__long__();
-
-        if (value instanceof PyLong) {
-            return ((PyLong) value).getValue().longValue();
-
-        } else if (value instanceof PyInteger) {
-            return ((PyInteger) value).getValue();
-        }
-
-        throw Py.TypeError("invalid __long__() result");
-    }
-
     public static int boolValue32(PyObject parameter) {
         return parameter.__nonzero__() ? 1 : 0;
     }
@@ -75,60 +30,60 @@
     }
 
     public static int s8Value32(PyObject parameter) {
-        return (byte) intValue(parameter);
+        return (byte) Util.intValue(parameter);
     }
 
     public static long s8Value64(PyObject parameter) {
-        return (byte) intValue(parameter);
+        return (byte) Util.intValue(parameter);
     }
 
     public static int u8Value32(PyObject parameter) {
-        return intValue(parameter) & 0xff;
+        return Util.intValue(parameter) & 0xff;
     }
 
     public static long u8Value64(PyObject parameter) {
-        return intValue(parameter) & 0xff;
+        return Util.intValue(parameter) & 0xff;
     }
 
     public static int s16Value32(PyObject parameter) {
-        return (short) intValue(parameter);
+        return (short) Util.intValue(parameter);
     }
 
     public static long s16Value64(PyObject parameter) {
-        return (short) intValue(parameter);
+        return (short) Util.intValue(parameter);
     }
 
     public static int u16Value32(PyObject parameter) {
-        return intValue(parameter) & 0xffff;
+        return Util.intValue(parameter) & 0xffff;
     }
 
     public static long u16Value64(PyObject parameter) {
-        return intValue(parameter) & 0xffff;
+        return Util.intValue(parameter) & 0xffff;
     }
 
 
     public static int s32Value32(PyObject parameter) {
-        return intValue(parameter);
+        return Util.intValue(parameter);
     }
 
     public static long s32Value64(PyObject parameter) {
-        return intValue(parameter);
+        return Util.intValue(parameter);
     }
 
     public static int u32Value32(PyObject parameter) {
-        return intValue(parameter);
+        return Util.intValue(parameter);
     }
 
     public static long u32Value64(PyObject parameter) {
-        return intValue(parameter) & 0xffffffffL;
+        return Util.intValue(parameter) & 0xffffffffL;
     }
 
     public static long s64Value64(PyObject parameter) {
-        return longValue(parameter);
+        return Util.longValue(parameter);
     }
 
     public static long u64Value64(PyObject parameter) {
-        return longValue(parameter);
+        return Util.longValue(parameter);
     }
 
     public static int f32Value32(PyObject parameter) {
diff --git a/src/org/python/modules/jffi/Util.java b/src/org/python/modules/jffi/Util.java
--- a/src/org/python/modules/jffi/Util.java
+++ b/src/org/python/modules/jffi/Util.java
@@ -2,8 +2,7 @@
 package org.python.modules.jffi;
 
 import java.math.BigInteger;
-import java.util.HashMap;
-import java.util.Map;
+
 import org.python.core.Py;
 import org.python.core.PyInteger;
 import org.python.core.PyLong;
@@ -13,23 +12,23 @@
     private static final com.kenai.jffi.MemoryIO IO = com.kenai.jffi.MemoryIO.getInstance();
 
     private Util() {}
-    
+
     public static final PyObject newSigned8(int value) {
-        value &= 0xff;
-        return Py.newInteger(value < 0x80 ? value : -0x80 + (value - 0x80));
+        return Py.newInteger((byte) value);
     }
 
     public static final PyObject newUnsigned8(int value) {
-        return Py.newInteger(value < 0 ? (long)((value & 0x7FL) + 0x80L) : value);
+        int n = (byte) value; // sign-extend the low 8 bits to 32
+        return Py.newInteger(n < 0 ? ((n & 0x7F) + 0x80) : n);
     }
 
     public static final PyObject newSigned16(int value) {
-        value &= 0xffff;
-        return Py.newInteger(value < 0x8000 ? value : -0x8000 + (value - 0x8000));
+        return Py.newInteger((short) value);
     }
 
     public static final PyObject newUnsigned16(int value) {
-        return Py.newInteger(value < 0 ? (long)((value & 0x7FFFL) + 0x8000L) : value);
+        int n = (short) value; // sign-extend the low 16 bits to 32
+        return Py.newInteger(n < 0 ? ((n & 0x7FFF) + 0x8000) : n);
     }
 
     public static final PyObject newSigned32(int value) {
@@ -37,7 +36,8 @@
     }
 
     public static final PyObject newUnsigned32(int value) {
-        return Py.newInteger(value < 0 ? (long)((value & 0x7FFFFFFFL) + 0x80000000L) : value);
+        int n = value;
+        return n < 0 ? Py.newInteger(((n & 0x7FFFFFFFL) + 0x80000000L)) : Py.newInteger(n);
     }
 
     public static final PyObject newSigned64(long value) {
@@ -58,47 +58,35 @@
     }
 
     public static final byte int8Value(PyObject parameter) {
-        return (byte) parameter.asInt();
+        return (byte) intValue(parameter);
     }
     
     public static final byte uint8Value(PyObject parameter) {
-        return (byte) parameter.asInt();
+        return (byte) intValue(parameter);
     }
     
     public static final short int16Value(PyObject parameter) {
-        return (short) parameter.asInt();
+        return (short) intValue(parameter);
     }
     
     public static final short uint16Value(PyObject parameter) {
-        return (short) parameter.asInt();
+        return (short) intValue(parameter);
     }
 
     public static final int int32Value(PyObject parameter) {
-        return parameter.asInt();
+        return intValue(parameter);
     }
     
-    public static final int uint32Value(PyObject value) {
-        if (value instanceof PyInteger) {
-            return value.asInt();
-        } else if (value instanceof PyLong) {
-            return (int) ((PyLong) value).asLong(0);
-        } else {
-            return (int) __long__value(value);
-        }
+    public static final int uint32Value(PyObject parameter) {
+        return intValue(parameter);
     }
 
     public static final long int64Value(PyObject value) {
-        return value.asLong();
+        return longValue(value);
     }
 
     public static final long uint64Value(PyObject value) {
-        if (value instanceof PyLong) {
-            return ((PyLong) value).getValue().longValue();
-        } else if (value instanceof PyInteger) {
-            return value.asInt();
-        } else {
-            return __long__value(value);
-        }
+        return longValue(value);
     }
 
     public static final float floatValue(PyObject parameter) {
@@ -113,9 +101,11 @@
         PyObject l = value.__long__();
         if (l instanceof PyLong) {
             return ((PyLong) l).getValue().longValue();
+
         } else if (l instanceof PyInteger) {
-            return value.asInt();
+            return ((PyInteger) l).getValue();
         }
+
         throw Py.TypeError("invalid __long__() result");
     }
 
@@ -140,4 +130,34 @@
     static final com.kenai.jffi.Type jffiType(CType type) {
         return (com.kenai.jffi.Type) type.jffiType();
     }
+
+    public static int intValue(PyObject parameter) {
+        if (parameter instanceof PyInteger) {
+            return ((PyInteger) parameter).getValue();
+
+        } else if (parameter instanceof PyLong) {
+            return ((PyLong) parameter).getValue().intValue();
+
+        } else if (parameter instanceof ScalarCData) {
+            return intValue(((ScalarCData) parameter).getValue());
+
+        } else {
+            return (int) __long__value(parameter);
+        }
+    }
+
+    public static long longValue(PyObject parameter) {
+        if (parameter instanceof PyInteger) {
+            return ((PyInteger) parameter).getValue();
+
+        } else if (parameter instanceof PyLong) {
+            return ((PyLong) parameter).getValue().longValue();
+
+        } else if (parameter instanceof ScalarCData) {
+            return longValue(((ScalarCData) parameter).getValue());
+
+        } else {
+            return __long__value(parameter);
+        }
+    }
 }

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


More information about the Jython-checkins mailing list