[Jython-checkins] jython (merge default -> default): Move memoryview support from sandbox (which is now retired)

jim.baker jython-checkins at python.org
Wed Mar 14 18:33:20 CET 2012


http://hg.python.org/jython/rev/e553ba3b064e
changeset:   6356:e553ba3b064e
parent:      6354:ee37bd0384c4
parent:      6355:4411db4dd246
user:        Jim Baker <jbaker at zyasoft.com>
date:        Wed Mar 14 10:33:09 2012 -0700
summary:
  Move memoryview support from sandbox (which is now retired)

files:
  CoreExposed.includes                        |   1 +
  src/org/python/core/MemoryView.java         |  12 +
  src/org/python/core/MemoryViewProtocol.java |   6 +
  src/org/python/core/PyMemoryView.java       |  61 ++++++++++
  src/org/python/core/PyString.java           |  30 ++++-
  src/org/python/core/__builtin__.java        |   1 +
  6 files changed, 109 insertions(+), 2 deletions(-)


diff --git a/CoreExposed.includes b/CoreExposed.includes
--- a/CoreExposed.includes
+++ b/CoreExposed.includes
@@ -26,6 +26,7 @@
 org/python/core/PyInteger.class
 org/python/core/PyList.class
 org/python/core/PyLong.class
+org/python/core/PyMemoryView.class
 org/python/core/PyMethod.class
 org/python/core/PyMethodDescr.class
 org/python/core/PyModule.class
diff --git a/src/org/python/core/MemoryView.java b/src/org/python/core/MemoryView.java
new file mode 100644
--- /dev/null
+++ b/src/org/python/core/MemoryView.java
@@ -0,0 +1,12 @@
+package org.python.core;
+
+public interface MemoryView {
+    // readonly attributes XXX just the boring stuff so far
+
+    public String get_format();
+    public int get_itemsize();
+    public PyTuple get_shape();
+    public int get_ndim();
+    public PyTuple get_strides();
+    public boolean get_readonly();
+}
diff --git a/src/org/python/core/MemoryViewProtocol.java b/src/org/python/core/MemoryViewProtocol.java
new file mode 100644
--- /dev/null
+++ b/src/org/python/core/MemoryViewProtocol.java
@@ -0,0 +1,6 @@
+package org.python.core;
+
+public interface MemoryViewProtocol {
+
+    public MemoryView getMemoryView();
+}
diff --git a/src/org/python/core/PyMemoryView.java b/src/org/python/core/PyMemoryView.java
new file mode 100644
--- /dev/null
+++ b/src/org/python/core/PyMemoryView.java
@@ -0,0 +1,61 @@
+package org.python.core;
+
+import org.python.expose.ExposedGet;
+import org.python.expose.ExposedMethod;
+import org.python.expose.ExposedNew;
+import org.python.expose.ExposedType;
+
+ at ExposedType(name = "memoryview", base = PyObject.class, isBaseType = false)
+public class PyMemoryView extends PyObject {
+
+    public static final PyType TYPE = PyType.fromClass(PyMemoryView.class);
+
+    MemoryView backing;
+
+    public PyMemoryView(MemoryViewProtocol obj) {
+        backing = obj.getMemoryView();
+    }
+
+    @ExposedNew
+    static PyObject memoryview_new(PyNewWrapper new_, boolean init, PyType subtype, PyObject[] args,
+                              String[] keywords) {
+        PyObject obj = args[0];
+        if (obj instanceof MemoryViewProtocol) {
+            return new PyMemoryView((MemoryViewProtocol)obj);
+        }
+        else throw Py.TypeError("cannot make memory view because object does not have the buffer interface");
+    }
+
+    @ExposedGet(name = "format")
+    public String get_format() {
+        return backing.get_format();
+    }
+
+    @ExposedGet(name = "itemsize")
+    public int get_itemsize() {
+        return backing.get_itemsize();
+    }
+
+    @ExposedGet(name = "shape")
+    public PyTuple get_shape() {
+        return backing.get_shape();
+    }
+
+    @ExposedGet(name = "ndim")
+    public int get_ndim() {
+        return backing.get_ndim();
+    }
+
+    @ExposedGet(name = "strides")
+    public PyTuple get_strides() {
+        return backing.get_strides();
+    }
+
+    @ExposedGet(name = "readonly")
+    public boolean get_readonly() {
+        return backing.get_readonly();
+    }
+
+}
+
+
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
@@ -18,7 +18,7 @@
  * A builtin python string.
  */
 @ExposedType(name = "str", doc = BuiltinDocs.str_doc)
-public class PyString extends PyBaseString
+public class PyString extends PyBaseString implements MemoryViewProtocol
 {
     public static final PyType TYPE = PyType.fromClass(PyString.class);
     protected String string; // cannot make final because of Python intern support
@@ -90,7 +90,31 @@
         }
         return codePoints;
     }
-    
+
+    public MemoryView getMemoryView() {
+        return new MemoryView() {
+            // beginning of support
+            public String get_format() {
+                 return "B";
+            }
+            public int get_itemsize() {
+                return 2;
+            }
+            public PyTuple get_shape() {
+                return new PyTuple(Py.newInteger(getString().length()));
+            }
+            public int get_ndim() {
+                return 1;
+            }
+            public PyTuple get_strides() {
+                return new PyTuple(Py.newInteger(1));
+            }
+            public boolean get_readonly() {
+                return true;
+            }
+        };
+    }
+
     public String substring(int start, int end) {
         return getString().substring(start, end);
     }
@@ -100,6 +124,8 @@
         return str___str__();
     }
 
+    public
+
     @ExposedMethod(doc = BuiltinDocs.str___str___doc) 
     final PyString str___str__() {
         if (getClass() == PyString.class) {
diff --git a/src/org/python/core/__builtin__.java b/src/org/python/core/__builtin__.java
--- a/src/org/python/core/__builtin__.java
+++ b/src/org/python/core/__builtin__.java
@@ -306,6 +306,7 @@
         dict.__setitem__("True", Py.True);
         dict.__setitem__("False", Py.False);
         dict.__setitem__("bytes", PyString.TYPE);
+        dict.__setitem__("memoryview", PyMemoryView.TYPE);
 
         // Work in debug mode by default
         // Hopefully add -O option in the future to change this

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


More information about the Jython-checkins mailing list