Index: src/org/python/core/PyFile.java =================================================================== --- src/org/python/core/PyFile.java (revision 3740) +++ src/org/python/core/PyFile.java (working copy) @@ -876,6 +876,7 @@ checkClosed(); softspace = false; file.write(s); + file.flush(); } public void write(String s) { Index: src/org/python/modules/Setup.java =================================================================== --- src/org/python/modules/Setup.java (revision 3740) +++ src/org/python/modules/Setup.java (working copy) @@ -56,6 +56,7 @@ "_random:org.python.modules.random.RandomModule", "cmath", "itertools", - "zipimport:org.python.modules.zipimport.zipimport" + "zipimport:org.python.modules.zipimport.zipimport", + "mmap:org.python.modules.mmap.MMAP" }; } Index: src/org/python/modules/mmap/MMAP.java =================================================================== --- src/org/python/modules/mmap/MMAP.java (revision 0) +++ src/org/python/modules/mmap/MMAP.java (revision 0) @@ -0,0 +1,51 @@ +package org.python.modules.mmap; + +import org.python.core.PyObject; +import org.python.core.PyInteger; +import org.python.core.PyException; +import org.python.core.PyTuple; +import org.python.core.PyString; +import org.python.core.ClassDictInit; +import org.python.core.Py; + +public class MMAP implements ClassDictInit { + + public static PyString __doc__ = new PyString(""); + public static final int ACCESS_COPY = 3; + public static final int ACCESS_DEFAULT = 0; + public static final int ACCESS_READ = 1; + public static final int ACCESS_WRITE = 2; + public static final int MAP_ANON = 32; + public static final int MAP_ANONYMOUS = 32; + public static int MAP_PRIVATE = 2; + public static int MAP_SHARED = 1; + public static int PAGESIZE = 4096; + public static int PROT_READ = 1; + public static int PROT_WRITE = 2; + public static PyObject error = Py.EnvironmentError; + + static PyException error(String message) { + return new PyException(error, message); + } + + static PyException error(int errno, String message) { + PyTuple args = new PyTuple(new PyObject[] {new PyInteger(errno), + new PyString(message)}); + return new PyException(error, args); + } + + static PyException error(java.io.IOException ioe) { + String message = ioe.getMessage(); + if (ioe instanceof java.io.FileNotFoundException) { + message = "File not found - " + message; + } + return new PyException(error, message); + } + + public static void classDictInit(PyObject dict) { + dict.__setitem__("classDictInit" , null); + dict.__setitem__("mmap", Py.java2py(PyMmap.class)); + dict.__setitem__("__doc__", __doc__); + dict.__setitem__("__name__", new PyString("mmap")); + } +} Index: src/org/python/modules/mmap/PyMmap.java =================================================================== --- src/org/python/modules/mmap/PyMmap.java (revision 0) +++ src/org/python/modules/mmap/PyMmap.java (revision 0) @@ -0,0 +1,1067 @@ +package org.python.modules.mmap; + +import org.python.core.PyObject; +import org.python.core.PyType; +import org.python.core.PyBuiltinMethod; +import org.python.core.PyBuiltinMethodNarrow; +import org.python.core.PyBuiltinFunction; +import org.python.core.PyNewWrapper; +import org.python.core.Py; +import org.python.core.PyMethodDescr; +import org.python.core.PyString; +import org.python.core.PyLong; +import org.python.core.PyInteger; +import org.python.core.ArgParser; +import org.python.core.PyException; +import org.python.core.PyFile; +import org.python.core.PyUnicode; +import org.python.modules.errno; + +import java.nio.MappedByteBuffer; +import java.nio.ByteBuffer; +import java.nio.BufferUnderflowException; +import java.nio.ReadOnlyBufferException; +import java.nio.channels.FileChannel; +import java.io.IOException; +import java.io.FileNotFoundException; +import java.io.RandomAccessFile; +import java.security.AccessController; +import java.security.PrivilegedAction; + +public class PyMmap extends PyString { + //~ BEGIN GENERATED REGION -- DO NOT EDIT SEE gexpose.py + /* type info */ + + public static final String exposed_name="mmap"; + + public static final Class exposed_base=PyObject.class; + + public static void typeSetup(PyObject dict,PyType.Newstyle marker) { + class exposed_close extends PyBuiltinMethodNarrow { + + exposed_close(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed_close(self,info); + } + + public PyObject __call__() { + ((PyMmap)self).mmap_close(); + return Py.None; + } + + } + dict.__setitem__("close",new PyMethodDescr("close",PyMmap.class,0,0,new exposed_close(null,null))); + class exposed_find extends PyBuiltinMethodNarrow { + + exposed_find(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed_find(self,info); + } + + public PyObject __call__(PyObject arg0,PyObject arg1) { + try { + return Py.newInteger(((PyMmap)self).mmap_find(arg0.asString(0),arg1)); + } catch (PyObject.ConversionException e) { + String msg; + switch (e.index) { + case 0: + msg="expected a string"; + break; + default: + msg="xxx"; + } + throw Py.TypeError(msg); + } + } + + public PyObject __call__(PyObject arg0) { + try { + return Py.newInteger(((PyMmap)self).mmap_find(arg0.asString(0))); + } catch (PyObject.ConversionException e) { + String msg; + switch (e.index) { + case 0: + msg="expected a string"; + break; + default: + msg="xxx"; + } + throw Py.TypeError(msg); + } + } + + } + dict.__setitem__("find",new PyMethodDescr("find",PyMmap.class,1,2,new exposed_find(null,null))); + class exposed_flush extends PyBuiltinMethodNarrow { + + exposed_flush(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed_flush(self,info); + } + + public PyObject __call__(PyObject arg0,PyObject arg1) { + return Py.newInteger(((PyMmap)self).mmap_flush(arg0,arg1)); + } + + public PyObject __call__(PyObject arg0) { + return Py.newInteger(((PyMmap)self).mmap_flush(arg0)); + } + + public PyObject __call__() { + return Py.newInteger(((PyMmap)self).mmap_flush()); + } + + } + dict.__setitem__("flush",new PyMethodDescr("flush",PyMmap.class,0,2,new exposed_flush(null,null))); + class exposed_move extends PyBuiltinMethodNarrow { + + exposed_move(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed_move(self,info); + } + + public PyObject __call__(PyObject arg0,PyObject arg1,PyObject arg2) { + try { + ((PyMmap)self).mmap_move(arg0.asInt(0),arg1.asInt(1),arg2.asInt(2)); + return Py.None; + } catch (PyObject.ConversionException e) { + String msg; + switch (e.index) { + case 0: + case 1: + case 2: + msg="expected an integer"; + break; + default: + msg="xxx"; + } + throw Py.TypeError(msg); + } + } + + } + dict.__setitem__("move",new PyMethodDescr("move",PyMmap.class,3,3,new exposed_move(null,null))); + class exposed_read extends PyBuiltinMethodNarrow { + + exposed_read(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed_read(self,info); + } + + public PyObject __call__(PyObject arg0) { + return new PyString(((PyMmap)self).mmap_read(arg0)); + } + + } + dict.__setitem__("read",new PyMethodDescr("read",PyMmap.class,1,1,new exposed_read(null,null))); + class exposed_read_byte extends PyBuiltinMethodNarrow { + + exposed_read_byte(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed_read_byte(self,info); + } + + public PyObject __call__() { + return new PyString(((PyMmap)self).mmap_read_byte()); + } + + } + dict.__setitem__("read_byte",new PyMethodDescr("read_byte",PyMmap.class,0,0,new exposed_read_byte(null,null))); + class exposed_readline extends PyBuiltinMethodNarrow { + + exposed_readline(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed_readline(self,info); + } + + public PyObject __call__() { + return new PyString(((PyMmap)self).mmap_readline()); + } + + } + dict.__setitem__("readline",new PyMethodDescr("readline",PyMmap.class,0,0,new exposed_readline(null,null))); + class exposed_resize extends PyBuiltinMethodNarrow { + + exposed_resize(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed_resize(self,info); + } + + public PyObject __call__(PyObject arg0) { + try { + ((PyMmap)self).mmap_resize(arg0.asInt(0)); + return Py.None; + } catch (PyObject.ConversionException e) { + String msg; + switch (e.index) { + case 0: + msg="expected an integer"; + break; + default: + msg="xxx"; + } + throw Py.TypeError(msg); + } + } + + } + dict.__setitem__("resize",new PyMethodDescr("resize",PyMmap.class,1,1,new exposed_resize(null,null))); + class exposed_seek extends PyBuiltinMethodNarrow { + + exposed_seek(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed_seek(self,info); + } + + public PyObject __call__(PyObject arg0,PyObject arg1) { + ((PyMmap)self).mmap_seek(arg0,arg1); + return Py.None; + } + + public PyObject __call__(PyObject arg0) { + ((PyMmap)self).mmap_seek(arg0); + return Py.None; + } + + } + dict.__setitem__("seek",new PyMethodDescr("seek",PyMmap.class,1,2,new exposed_seek(null,null))); + class exposed_size extends PyBuiltinMethodNarrow { + + exposed_size(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed_size(self,info); + } + + public PyObject __call__() { + return Py.newInteger(((PyMmap)self).mmap_size()); + } + + } + dict.__setitem__("size",new PyMethodDescr("size",PyMmap.class,0,0,new exposed_size(null,null))); + class exposed_tell extends PyBuiltinMethodNarrow { + + exposed_tell(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed_tell(self,info); + } + + public PyObject __call__() { + return Py.newInteger(((PyMmap)self).mmap_tell()); + } + + } + dict.__setitem__("tell",new PyMethodDescr("tell",PyMmap.class,0,0,new exposed_tell(null,null))); + class exposed_write extends PyBuiltinMethodNarrow { + + exposed_write(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed_write(self,info); + } + + public PyObject __call__(PyObject arg0) { + ((PyMmap)self).mmap_write(arg0); + return Py.None; + } + + } + dict.__setitem__("write",new PyMethodDescr("write",PyMmap.class,1,1,new exposed_write(null,null))); + class exposed_write_byte extends PyBuiltinMethodNarrow { + + exposed_write_byte(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed_write_byte(self,info); + } + + public PyObject __call__(PyObject arg0) { + ((PyMmap)self).mmap_write_byte(arg0); + return Py.None; + } + + } + dict.__setitem__("write_byte",new PyMethodDescr("write_byte",PyMmap.class,1,1,new exposed_write_byte(null,null))); + class exposed___init__ extends PyBuiltinMethod { + + exposed___init__(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed___init__(self,info); + } + + public PyObject __call__(PyObject[]args) { + return __call__(args,Py.NoKeywords); + } + + public PyObject __call__(PyObject[]args,String[]keywords) { + ((PyMmap)self).mmap_init(args,keywords); + return Py.None; + } + + } + dict.__setitem__("__init__",new PyMethodDescr("__init__",PyMmap.class,-1,-1,new exposed___init__(null,null))); + dict.__setitem__("__new__",new PyNewWrapper(PyMmap.class,"__new__",-1,-1) { + + public PyObject new_impl(boolean init,PyType subtype,PyObject[]args,String[]keywords) { + PyMmap newobj; + if (for_type==subtype) { + newobj=new PyMmap(); + if (init) + newobj.mmap_init(args,keywords); + } else { + newobj=new PyMmapDerived(subtype); + } + return newobj; + } + + }); + } + //~ END GENERATED REGION -- DO NOT EDIT SEE gexpose.py + + private static final PyType MMAP_TYPE = PyType.fromClass(PyMmap.class); + + private MappedByteBuffer buffer; + private FileChannel channel; + private FileChannel.MapMode access_mode; + private String tagname; + private String filename; + private boolean isOpen; + private boolean isAnon; + + public PyMmap() { + this(MMAP_TYPE); + } + + public PyMmap(PyType subType) { + super(subType, ""); + isOpen = true; + isAnon = false; + } + + private void check_valid() { + if (!isOpen) { + throw Py.ValueError("mmap closed or invalid"); + } + } + + public void close() { + mmap_close(); + } + + final void mmap_close() { + if (!isOpen) { + return; + } + clean(buffer); + try { + if (channel != null) { + channel.close(); + } + } catch (IOException e) { + throw MMAP.error(e); + } + isOpen = false; + } + + protected void finalize() { + close(); + } + + private static boolean clean(final ByteBuffer buffer) { + System.gc(); + if (buffer == null || !buffer.isDirect()) { + return false; + } + + Boolean b = (Boolean) AccessController.doPrivileged(new PrivilegedAction() { + public Object run() { + Boolean success = Boolean.FALSE; + try { + java.lang.reflect.Method getCleanerMethod = buffer.getClass().getMethod("cleaner", new Class[0]); + getCleanerMethod.setAccessible(true); + sun.misc.Cleaner cleaner = (sun.misc.Cleaner)getCleanerMethod.invoke(buffer, new Object[0]); + cleaner.clean(); + success = Boolean.TRUE; + } catch (Exception e) { + //e.printStackTrace(); + } + return success; + } + }); + return b.booleanValue(); + } + + public int find(String str) { + return mmap_find(str); + } + + final int mmap_find(String str) { + return mmap_find(str, Py.Zero); + } + + final int mmap_find(String str , PyObject offset) { + check_valid(); + if (!offset.isNumberType()) { + throw Py.TypeError("an integer is required"); + } + int from = ((PyInteger)offset.__int__()).getValue(); + if (from < 0) { + return -1; + } + int pos = buffer.position(); + int len = str.length(); + String sub = ""; + int capacity = buffer.capacity(); + + for (int i = from ; i <= capacity ; i++) { + mmap_seek(new PyInteger(i)); + sub = mmap_read(new PyInteger(len)); + if (str.equals(sub)) { + buffer.position(pos); + return i; + } + } + buffer.position(pos); + return -1; + } + + public int find(String str ,PyObject offset) { + return mmap_find(str, offset); + } + + public int flush() { + return mmap_flush(); + } + + final int mmap_flush() { + return mmap_flush(Py.Zero, new PyInteger(buffer.capacity())); + } + + public int flush(PyObject value) { + return mmap_flush(value); + } + + final int mmap_flush(PyObject value) { + return mmap_flush(Py.Zero, new PyInteger(buffer.capacity())); + } + + public int flush(PyObject offset ,PyObject size) { + return mmap_flush(offset, size); + } + + final int mmap_flush(PyObject offset , PyObject size) { + check_valid(); + if (isAnon) { + throw MMAP.error(errno.EBADF,"Bad file descriptor"); + } + if (!offset.isNumberType() || !size.isNumberType()) { + throw Py.TypeError("an integer is required"); + } + int noffset = ((PyInteger)offset.__int__()).getValue(); + int nsize = ((PyInteger)size.__int__()).getValue(); + if ((noffset + nsize) > buffer.capacity() || (noffset + nsize) < 0) { + throw Py.ValueError("flush values out of range"); + } + if (buffer.force() == null) { + return -1; + } else { + return 0; + } + } + + public void move(int dest, int src, int count) { + mmap_move(dest, src, count); + } + + final void mmap_move(int dest, int src, int count) { + check_valid(); + if (access_mode == FileChannel.MapMode.READ_ONLY) { + throw Py.TypeError("mmap can't modify a readonly memory map."); + } + if (dest < 0 || src < 0 || count < 0) { + throw Py.ValueError("source or destination or count out of range"); + } + int capacity = buffer.capacity(); + if ((src + count) > capacity || (dest + count) > capacity) { + throw Py.ValueError("source or destination out of range"); + } + int pos = buffer.position(); + buffer.position(src); + String str = mmap_read(new PyInteger(count)); + buffer.position(dest); + mmap_write(new PyString(str)); + buffer.position(pos); + updateMmapString(); + } + + public String read(PyObject count) { + return mmap_read(count); + } + + final String mmap_read(PyObject count) { + check_valid(); + if (!count.isNumberType()) { + throw Py.TypeError("an integer is required"); + } + int num = ((PyInteger)count.__int__()).getValue(); + int pos = buffer.position(); + int limit = buffer.capacity(); + if (num < 0) { + if (pos == 0) { + String str = mmap_read(new PyInteger(limit)); + buffer.position(limit); + return str; + } else { + buffer.position(pos + num); + return ""; + } + } + if (pos == limit) { + return ""; + } + int newlimit = pos + num; + if (newlimit > limit) { + num = limit - pos; + } + byte[] buff = new byte[num]; + buffer.get(buff , 0, num); + return new String(buff); + } + + public String read_byte() { + return mmap_read_byte(); + } + + final String mmap_read_byte() { + check_valid(); + try { + byte buf[] = new byte[]{buffer.get()}; + return new String(buf, 0, buf.length, "ISO-8859-1"); + } catch(BufferUnderflowException e) { + throw Py.ValueError("read byte out of range"); + } catch(IOException e) { + throw MMAP.error(e); + } + } + + public String readline() { + return mmap_readline(); + } + + final String mmap_readline() { + check_valid(); + int capacity = buffer.capacity(); + int from = buffer.position(); + while ((buffer.get() != '\n') && buffer.position() < capacity); + int to = buffer.position(); + mmap_seek(new PyInteger(from)); + String str = mmap_read(new PyInteger(to-from)); + return str; + } + + public void resize(int newSize) { + mmap_resize(newSize); + } + + final void mmap_resize(int newSize) { + check_valid(); + if (isAnon) { + throw MMAP.error(errno.EBADF, "Bad file descriptor"); + } + if (access_mode != FileChannel.MapMode.READ_WRITE) { + throw Py.TypeError("mmap can't resize a readonly or copy-on-write memory map."); + } + clean(buffer); + try { + if (newSize < channel.size()) { + channel.truncate(newSize); + } + + buffer = channel.map(access_mode, 0L, (long)newSize); + buffer.load(); + } catch(IOException e) { + throw MMAP.error(e); + } + + updateMmapString(); + } + + public void seek(PyObject pos) { + mmap_seek(pos); + } + + final void mmap_seek(PyObject pos) { + mmap_seek(pos, Py.Zero); + } + + + public void seek(PyObject pos, PyObject whence) { + mmap_seek(pos, whence); + } + + final void mmap_seek(PyObject pos, PyObject whence) { + check_valid(); + if (!pos.isNumberType() || !whence.isNumberType()) { + throw Py.TypeError("an integer is required"); + } + int position = ((PyInteger)pos.__int__()).getValue(); + int offset = ((PyInteger)whence.__int__()).getValue(); + int refpos = 0; + switch(offset) { + case 0: + refpos = 0; + break; + case 1: + refpos = buffer.position(); + break; + case 2: + refpos = buffer.capacity(); + break; + default: + throw Py.ValueError("unknown seek type"); + } + if ((refpos + position) < 0 || (refpos + position) > buffer.capacity()) { + throw Py.ValueError("seek out of range"); + } else { + buffer.position((int)(refpos + position)); + } + } + + public int size() { + return mmap_size(); + } + + final int mmap_size() { + check_valid(); + if (isAnon) { + throw MMAP.error(errno.EBADF, "Bad file descriptor"); + } + try { + return (int)channel.size(); + } catch(IOException e) { + throw MMAP.error(e); + } + } + + public int tell() { + return mmap_tell(); + } + + final int mmap_tell() { + check_valid(); + return buffer.position(); + } + + public void write(PyObject obj) { + mmap_write(obj); + } + + final void mmap_write(PyObject obj) { + check_valid(); + if (!(obj instanceof PyString)) { + throw Py.TypeError("write() argument 1 must be string, not " + obj.getType().fastGetName()); + } + + if (access_mode == FileChannel.MapMode.READ_ONLY) { + throw Py.TypeError("mmap can't modify a readonly memory map."); + } + + int len = obj.__len__(); + if ((len + buffer.position()) > buffer.capacity()) { + throw Py.ValueError("data out of range"); + } + + byte src[] = ((PyString)obj).toBytes(); + buffer.put(src); + updateMmapString(); + } + + public void write_byte(PyObject obj) { + mmap_write_byte(obj); + } + + final void mmap_write_byte(PyObject obj) { + check_valid(); + + if (obj instanceof PyUnicode || !(obj instanceof PyString) + || obj.__len__() != 1) { + throw Py.TypeError("write_byte() argument 1 must be char, not " + obj.getType().fastGetName()); + } + + if (access_mode == FileChannel.MapMode.READ_ONLY) { + throw Py.TypeError("mmap can't modify a readonly memory map."); + } + + if (buffer.position() >= buffer.capacity()) { + return; + } + byte src[] = ((PyString)obj).toBytes(); + buffer.put(src); + updateMmapString(); + } + + private void updateMmapString() { + this.string = mmap_toString(); + } + + public synchronized PyObject __finditem__(PyObject idx) { + check_valid(); + int pos = buffer.position(); + int capacity = buffer.capacity(); + int index = ((PyInteger)idx.__int__()).getValue(); + if (index < 0) { + index = capacity + index; + } + if (index > buffer.capacity() || index < 0) { + throw Py.TypeError("mmap index out of range"); + } + buffer.position(index); + String str = mmap_read_byte(); + buffer.position(pos); + return new PyString(str); + } + + public synchronized void __setitem__(PyObject index, PyObject value) { + check_valid(); + + if (access_mode == FileChannel.MapMode.READ_ONLY) { + throw Py.TypeError("mmap can't modify a readonly memory map."); + } + + if (!(value instanceof PyString && value.__len__() == 1)) { + throw Py.IndexError("mmap assignment must be single-character string"); + } + + try { + int pos = getIndex(index, 0); + if (pos < 0) { + pos = buffer.capacity() + pos; + } + byte src[] = ((PyString)value).toBytes(); + buffer.put(pos, src[0]); + } catch(IndexOutOfBoundsException e) { + throw Py.IndexError("mmap index out of range"); + } catch(ReadOnlyBufferException e) { + throw Py.TypeError("mmap can't modify a readonly memory map."); + } + updateMmapString(); + } + + public int getIndex(PyObject index, int defaultValue) { + if (index == Py.None || index == null) { + return defaultValue; + } + int pos = defaultValue; + if (index instanceof PyInteger || index instanceof PyLong) { + pos = ((PyInteger)index.__int__()).getValue(); + } else { + throw Py.TypeError("mmap index must be int"); + } + return pos; + } + + public int __len__() { + return mmap___len__(); + } + + final int mmap___len__() { + return buffer.capacity(); + } + + public synchronized PyObject __getslice__(PyObject s_start, PyObject s_stop, PyObject s_step) { + check_valid(); + int capacity = buffer.capacity(); + int start = getIndex(s_start,0); + int stop = getIndex(s_stop, capacity); + + if (start < 0) { + start = 0; + } else if (start > capacity) { + start = capacity; + } + if (stop < 0) { + stop = 0; + } + if (stop < start) { + stop = start; + } else if (stop > capacity) { + stop = capacity; + } + + int pos = buffer.position(); + buffer.position(start); + + StringBuffer buf = new StringBuffer(); + for (int i =0 ; i < (stop - start); i++) { + buf.append(mmap_read_byte()); + } + + buffer.position(pos); + return new PyString(buf.toString()); + } + + public synchronized void __setslice__(PyObject s_start, PyObject s_stop, PyObject s_step, PyObject value) { + check_valid(); + if (access_mode == FileChannel.MapMode.READ_ONLY) { + throw Py.TypeError("mmap can't modify a readonly memory map."); + } + + int capacity = buffer.capacity(); + int start = getIndex(s_start,0); + int stop = getIndex(s_stop, capacity); + + if (start < 0) { + start = 0; + } else if (start > capacity) { + start = capacity; + } + if (stop < 0) { + stop = 0; + } + if (stop < start) { + stop = start; + } else if (stop > capacity) { + stop = capacity; + } + + if (!(value instanceof PyString)) { + throw Py.IndexError("mmap slice assignment must be a string"); + } + + int length = stop - start; + if (value.__len__() != length) { + throw Py.IndexError("mmap slice assignment is wrong size"); + } + + int pos = buffer.position(); + buffer.position(start); + mmap_write(value); + buffer.position(pos); + updateMmapString(); + } + + public synchronized void __delitem__(PyObject index) { + throw Py.TypeError("mmap object doesn't support item deletion"); + } + + public synchronized void __delslice__(PyObject s_start, PyObject s_stop, + PyObject s_step) { + throw Py.TypeError("mmap object doesn't support slice deletion"); + } + + public PyObject __add__(PyObject o) { + throw Py.SystemError("mmaps don't support concatenation"); + } + + public PyObject __radd__(PyObject o) { + throw Py.SystemError("mmaps don't support concatenation"); + } + + public PyObject __mul__(PyObject o) { + throw Py.SystemError("mmaps don't support repeat operation"); + } + + public PyObject __rmul__(PyObject o) { + throw Py.SystemError("mmaps don't support repeat operation"); + } + + public String toString() { + return mmap_toString(); + } + + final String mmap_toString() { + StringBuffer buf = new StringBuffer(); + int pos = buffer.position(); + buffer.position(0); + int capacity = buffer.capacity(); + for(int i = 0; i < capacity; i++) { + buf.append(mmap_read_byte()); + } + buffer.position(pos); + return buf.toString(); + } + + final void mmap_init(PyObject[] args , String[] kwds) { + String os = System.getProperty("os.name"); + ArgParser ap = null; + RandomAccessFile raf = null; + + if (os.startsWith("Lin")) { + ap = new ArgParser("mmap",args,kwds,new String[] {"file", "length", "flags", "prot", "access"}); + } else if (os.startsWith("Win")){ + ap = new ArgParser("mmap",args,kwds,new String[] {"file", "length", "tagname", "access"}); + } else { + throw new PyException(Py.NotImplementedError, "No mmap support for os: " + os); + } + + PyObject arg1 = ap.getPyObject(0); + if ((arg1 instanceof PyInteger || arg1 instanceof PyLong) && ((PyInteger)arg1.__int__()).getValue() == -1) { + isAnon = true; + try { + buffer = (MappedByteBuffer)ByteBuffer.allocateDirect(ap.getInt(1)); + } catch(IllegalArgumentException e) { + throw Py.ValueError("length parameter cannot be negative"); + } + } else if (arg1 instanceof PyFile) { + filename = ((PyString)(((PyFile)(ap.getPyObject(0))).getName())).toString(); + } else { + throw MMAP.error("mmap takes a file object as the first argument"); + } + + int length = ap.getInt(1); + if (length < 0) { + throw Py.ValueError("length parameter cannot be negative"); + } + + if (os.startsWith("Lin")) { + int flags = ap.getInt(2, MMAP.MAP_SHARED); + int prot = ap.getInt(3, MMAP.PROT_WRITE | MMAP.PROT_READ); + int access = ap.getInt(4, MMAP.ACCESS_DEFAULT); + + if((access != MMAP.ACCESS_DEFAULT) && ((flags != MMAP.MAP_SHARED) || (prot != (MMAP.PROT_WRITE | MMAP.PROT_READ)))) { + throw Py.ValueError("mmap can't specify both access and flags, prot."); + } + + switch(access) { + case MMAP.ACCESS_READ: + flags = MMAP.MAP_SHARED; + prot = MMAP.PROT_READ; + break; + case MMAP.ACCESS_DEFAULT: + case MMAP.ACCESS_WRITE: + flags = MMAP.MAP_SHARED; + prot = MMAP.PROT_READ | MMAP.PROT_WRITE; + break; + case MMAP.ACCESS_COPY: + flags = MMAP.MAP_PRIVATE; + prot = MMAP.PROT_READ | MMAP.PROT_WRITE; + break; + default: + throw Py.ValueError("mmap invalid access parameter"); + } + + if (flags != MMAP.MAP_PRIVATE && flags != MMAP.MAP_SHARED) { + throw Py.ValueError("mmap invalid flag parameter"); + } + + if (prot != MMAP.PROT_READ && prot != (MMAP.PROT_READ | MMAP.PROT_WRITE)) { + throw Py.ValueError("mmap invalid prot parameter"); + } + + if (flags == MMAP.MAP_SHARED) { + if(prot == (MMAP.PROT_READ | MMAP.PROT_WRITE)) { + access_mode = FileChannel.MapMode.READ_WRITE; + } else { + access_mode = FileChannel.MapMode.READ_ONLY; + } + } else { + access_mode = FileChannel.MapMode.PRIVATE; + } + } else if (os.startsWith("Win")) { + String tag = ap.getString(2, ""); + if (!tag.equals("")) { + tagname = tag; + throw new PyException(Py.NotImplemented, "No support for tagname argument"); + } + + int access = ap.getInt(3, MMAP.ACCESS_DEFAULT); + + switch(access) { + case MMAP.ACCESS_READ: + access_mode = FileChannel.MapMode.READ_ONLY; + break; + case MMAP.ACCESS_DEFAULT: + case MMAP.ACCESS_WRITE: + access_mode = FileChannel.MapMode.READ_WRITE; + break; + case MMAP.ACCESS_COPY: + access_mode = FileChannel.MapMode.PRIVATE; + break; + default: + throw Py.ValueError("mmap invalid access parameter"); + } + } + + if (!isAnon) { + try { + if (access_mode == FileChannel.MapMode.READ_WRITE) { + raf = new RandomAccessFile(filename,"rw"); + } else if (access_mode == FileChannel.MapMode.READ_ONLY) { + raf = new RandomAccessFile(filename,"r"); + } else if (access_mode == FileChannel.MapMode.PRIVATE) { + raf = new RandomAccessFile(filename,"rw"); + } + } catch(FileNotFoundException e) { + throw MMAP.error(errno.ENOENT, "file could not be found"); + } + try { + channel = raf.getChannel(); + if (os.startsWith("Win")) { + if (raf.length() < length) { + raf.setLength(length); + } else if (length == 0) { + if (channel.size() == 0) { + throw MMAP.error("Windows error occurred"); + } else { + length = (int)(channel.size()); + } + } + } else if (os.startsWith("Lin")) { + if (raf.length() < length) { + throw Py.ValueError("mmap length is greater than file size"); + } else if (length == 0) { + length = (int)(channel.size()); + } + } + } catch(IOException e) { + throw MMAP.error(e); + } + try { + buffer = channel.map(access_mode, 0L, (long)length); + buffer.load(); + } catch(IOException e) { + throw MMAP.error(e); + } + } + updateMmapString(); + } +} Index: src/org/python/modules/mmap/PyMmapDerived.java =================================================================== --- src/org/python/modules/mmap/PyMmapDerived.java (revision 0) +++ src/org/python/modules/mmap/PyMmapDerived.java (revision 0) @@ -0,0 +1,956 @@ +package org.python.modules.mmap; + +import org.python.core.*; + +public class PyMmapDerived extends PyMmap implements Slotted { + + public PyObject getSlot(int index) { + return slots[index]; + } + + public void setSlot(int index,PyObject value) { + slots[index]=value; + } + + private PyObject[]slots; + + private PyObject dict; + + public PyObject fastGetDict() { + return dict; + } + + public PyObject getDict() { + return dict; + } + + public void setDict(PyObject newDict) { + if (newDict instanceof PyStringMap||newDict instanceof PyDictionary) { + dict=newDict; + } else { + throw Py.TypeError("__dict__ must be set to a Dictionary "+newDict.getClass().getName()); + } + } + + public void delDict() { + // deleting an object's instance dict makes it grow a new one + dict=new PyStringMap(); + } + + public PyMmapDerived(PyType subtype) { + super(subtype); + slots=new PyObject[subtype.getNumSlots()]; + dict=subtype.instDict(); + } + + public PyString __str__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__str__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(); + if (res instanceof PyString) + return(PyString)res; + throw Py.TypeError("__str__"+" should return a "+"string"); + } + return super.__str__(); + } + + public PyString __repr__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__repr__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(); + if (res instanceof PyString) + return(PyString)res; + throw Py.TypeError("__repr__"+" should return a "+"string"); + } + return super.__repr__(); + } + + public PyString __hex__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__hex__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(); + if (res instanceof PyString) + return(PyString)res; + throw Py.TypeError("__hex__"+" should return a "+"string"); + } + return super.__hex__(); + } + + public PyString __oct__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__oct__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(); + if (res instanceof PyString) + return(PyString)res; + throw Py.TypeError("__oct__"+" should return a "+"string"); + } + return super.__oct__(); + } + + public PyFloat __float__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__float__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(); + if (res instanceof PyFloat) + return(PyFloat)res; + throw Py.TypeError("__float__"+" should return a "+"float"); + } + return super.__float__(); + } + + public PyLong __long__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__long__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(); + if (res instanceof PyLong) + return(PyLong)res; + throw Py.TypeError("__long__"+" should return a "+"long"); + } + return super.__long__(); + } + + public PyComplex __complex__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__complex__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(); + if (res instanceof PyComplex) + return(PyComplex)res; + throw Py.TypeError("__complex__"+" should return a "+"complex"); + } + return super.__complex__(); + } + + public PyObject __pos__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__pos__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(); + return super.__pos__(); + } + + public PyObject __neg__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__neg__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(); + return super.__neg__(); + } + + public PyObject __abs__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__abs__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(); + return super.__abs__(); + } + + public PyObject __invert__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__invert__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(); + return super.__invert__(); + } + + public PyObject __reduce__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__reduce__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(); + return super.__reduce__(); + } + + public PyObject __add__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__add__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__add__(other); + } + + public PyObject __radd__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__radd__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__radd__(other); + } + + public PyObject __sub__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__sub__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__sub__(other); + } + + public PyObject __rsub__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__rsub__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__rsub__(other); + } + + public PyObject __mul__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__mul__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__mul__(other); + } + + public PyObject __rmul__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__rmul__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__rmul__(other); + } + + public PyObject __div__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__div__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__div__(other); + } + + public PyObject __rdiv__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__rdiv__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__rdiv__(other); + } + + public PyObject __floordiv__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__floordiv__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__floordiv__(other); + } + + public PyObject __rfloordiv__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__rfloordiv__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__rfloordiv__(other); + } + + public PyObject __truediv__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__truediv__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__truediv__(other); + } + + public PyObject __rtruediv__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__rtruediv__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__rtruediv__(other); + } + + public PyObject __mod__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__mod__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__mod__(other); + } + + public PyObject __rmod__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__rmod__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__rmod__(other); + } + + public PyObject __divmod__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__divmod__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__divmod__(other); + } + + public PyObject __rdivmod__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__rdivmod__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__rdivmod__(other); + } + + public PyObject __pow__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__pow__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__pow__(other); + } + + public PyObject __rpow__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__rpow__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__rpow__(other); + } + + public PyObject __lshift__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__lshift__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__lshift__(other); + } + + public PyObject __rlshift__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__rlshift__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__rlshift__(other); + } + + public PyObject __rshift__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__rshift__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__rshift__(other); + } + + public PyObject __rrshift__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__rrshift__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__rrshift__(other); + } + + public PyObject __and__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__and__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__and__(other); + } + + public PyObject __rand__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__rand__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__rand__(other); + } + + public PyObject __or__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__or__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__or__(other); + } + + public PyObject __ror__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__ror__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__ror__(other); + } + + public PyObject __xor__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__xor__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__xor__(other); + } + + public PyObject __rxor__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__rxor__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__rxor__(other); + } + + public PyObject __lt__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__lt__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__lt__(other); + } + + public PyObject __le__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__le__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__le__(other); + } + + public PyObject __gt__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__gt__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__gt__(other); + } + + public PyObject __ge__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__ge__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__ge__(other); + } + + public PyObject __eq__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__eq__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__eq__(other); + } + + public PyObject __ne__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__ne__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__ne__(other); + } + + public PyObject __iadd__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__iadd__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(other); + return super.__iadd__(other); + } + + public PyObject __isub__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__isub__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(other); + return super.__isub__(other); + } + + public PyObject __imul__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__imul__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(other); + return super.__imul__(other); + } + + public PyObject __idiv__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__idiv__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(other); + return super.__idiv__(other); + } + + public PyObject __ifloordiv__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__ifloordiv__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(other); + return super.__ifloordiv__(other); + } + + public PyObject __itruediv__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__itruediv__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(other); + return super.__itruediv__(other); + } + + public PyObject __imod__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__imod__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(other); + return super.__imod__(other); + } + + public PyObject __ipow__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__ipow__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(other); + return super.__ipow__(other); + } + + public PyObject __ilshift__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__ilshift__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(other); + return super.__ilshift__(other); + } + + public PyObject __irshift__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__irshift__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(other); + return super.__irshift__(other); + } + + public PyObject __iand__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__iand__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(other); + return super.__iand__(other); + } + + public PyObject __ior__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__ior__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(other); + return super.__ior__(other); + } + + public PyObject __ixor__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__ixor__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(other); + return super.__ixor__(other); + } + + public PyObject __int__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__int__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(); + if (res instanceof PyInteger||res instanceof PyLong) + return(PyObject)res; + throw Py.TypeError("__int__"+" should return an integer"); + } + return super.__int__(); + } + + public String toString() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__repr__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(); + if (!(res instanceof PyString)) + throw Py.TypeError("__repr__ should return a string"); + return((PyString)res).toString(); + } + return super.toString(); + } + + public int hashCode() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__hash__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(); + if (res instanceof PyInteger) + return((PyInteger)res).getValue(); + throw Py.TypeError("__hash__ should return a int"); + } + if (self_type.lookup("__eq__")!=null||self_type.lookup("__cmp__")!=null) + throw Py.TypeError("unhashable type"); + return super.hashCode(); + } + + public PyUnicode __unicode__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__unicode__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(); + if (res instanceof PyUnicode) + return(PyUnicode)res; + if (res instanceof PyString) + return new PyUnicode((PyString)res); + throw Py.TypeError("__unicode__"+" should return a "+"unicode"); + } + return super.__unicode__(); + } + + public int __cmp__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__cmp__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res instanceof PyInteger) { + int v=((PyInteger)res).getValue(); + return v<0?-1:v>0?1:0; + } + throw Py.TypeError("__cmp__ should return a int"); + } + return super.__cmp__(other); + } + + public boolean __nonzero__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__nonzero__"); + if (impl==null) { + impl=self_type.lookup("__len__"); + if (impl==null) + return super.__nonzero__(); + } + return impl.__get__(this,self_type).__call__().__nonzero__(); + } + + public boolean __contains__(PyObject o) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__contains__"); + if (impl==null) + return super.__contains__(o); + return impl.__get__(this,self_type).__call__(o).__nonzero__(); + } + + public int __len__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__len__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(); + if (res instanceof PyInteger) + return((PyInteger)res).getValue(); + throw Py.TypeError("__len__ should return a int"); + } + return super.__len__(); + } + + public PyObject __iter__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__iter__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(); + impl=self_type.lookup("__getitem__"); + if (impl==null) + return super.__iter__(); + return new PySequenceIter(this); + } + + public PyObject __iternext__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("next"); + if (impl!=null) { + try { + return impl.__get__(this,self_type).__call__(); + } catch (PyException exc) { + if (Py.matchException(exc,Py.StopIteration)) + return null; + throw exc; + } + } + return super.__iternext__(); // ??? + } + + public PyObject __finditem__(PyObject key) { // ??? + PyType self_type=getType(); + PyObject impl=self_type.lookup("__getitem__"); + if (impl!=null) + try { + return impl.__get__(this,self_type).__call__(key); + } catch (PyException exc) { + if (Py.matchException(exc,Py.LookupError)) + return null; + throw exc; + } + return super.__finditem__(key); + } + + public void __setitem__(PyObject key,PyObject value) { // ??? + PyType self_type=getType(); + PyObject impl=self_type.lookup("__setitem__"); + if (impl!=null) { + impl.__get__(this,self_type).__call__(key,value); + return; + } + super.__setitem__(key,value); + } + + public PyObject __getslice__(PyObject start,PyObject stop,PyObject step) { // ??? + PyType self_type=getType(); + PyObject impl=self_type.lookup("__getslice__"); + if (impl!=null) + try { + return impl.__get__(this,self_type).__call__(start,stop); + } catch (PyException exc) { + if (Py.matchException(exc,Py.LookupError)) + return null; + throw exc; + } + return super.__getslice__(start,stop,step); + } + + public void __delitem__(PyObject key) { // ??? + PyType self_type=getType(); + PyObject impl=self_type.lookup("__delitem__"); + if (impl!=null) { + impl.__get__(this,self_type).__call__(key); + return; + } + super.__delitem__(key); + } + + public PyObject __call__(PyObject args[],String keywords[]) { + ThreadState ts=Py.getThreadState(); + if (ts.recursion_depth++>ts.systemState.getrecursionlimit()) + throw Py.RuntimeError("maximum __call__ recursion depth exceeded"); + try { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__call__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(args,keywords); + return super.__call__(args,keywords); + } finally { + --ts.recursion_depth; + } + } + + public PyObject __findattr__(String name) { + PyType self_type=getType(); + PyObject getattribute=self_type.lookup("__getattribute__"); + PyString py_name=null; + try { + if (getattribute!=null) { + return getattribute.__get__(this,self_type).__call__(py_name=new PyString(name)); + } else { + return super.__findattr__(name); + } + } catch (PyException e) { + if (Py.matchException(e,Py.AttributeError)) { + PyObject getattr=self_type.lookup("__getattr__"); + if (getattr!=null) + try { + return getattr.__get__(this,self_type).__call__(py_name!=null?py_name:new PyString(name)); + } catch (PyException e1) { + if (!Py.matchException(e1,Py.AttributeError)) + throw e1; + } + return null; + } + throw e; + } + } + + public void __setattr__(String name,PyObject value) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__setattr__"); + if (impl!=null) { + impl.__get__(this,self_type).__call__(new PyString(name),value); + return; + } + super.__setattr__(name,value); + } + + public void __delattr__(String name) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__delattr__"); + if (impl!=null) { + impl.__get__(this,self_type).__call__(new PyString(name)); + return; + } + super.__delattr__(name); + } + + public PyObject __get__(PyObject obj,PyObject type) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__get__"); + if (impl!=null) { + if (obj==null) + obj=Py.None; + if (type==null) + type=Py.None; + return impl.__get__(this,self_type).__call__(obj,type); + } + return super.__get__(obj,type); + } + + public void __set__(PyObject obj,PyObject value) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__set__"); + if (impl!=null) { + impl.__get__(this,self_type).__call__(obj,value); + return; + } + super.__set__(obj,value); + } + + public void __delete__(PyObject obj) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__delete__"); + if (impl!=null) { + impl.__get__(this,self_type).__call__(obj); + return; + } + super.__delete__(obj); + } + + public void dispatch__init__(PyType type,PyObject[]args,String[]keywords) { + PyType self_type=getType(); + if (self_type.isSubType(type)) { + PyObject impl=self_type.lookup("__init__"); + if (impl!=null) + impl.__get__(this,self_type).__call__(args,keywords); + } + } + +} Index: src/templates/mmap.expose =================================================================== --- src/templates/mmap.expose (revision 0) +++ src/templates/mmap.expose (revision 0) @@ -0,0 +1,24 @@ +#setup +type_name: mmap +type_class: PyMmap +type_base_class: PyObject +#exposed methods +expose_meth: :- close +expose_meth: :i find s o? +expose_meth: :i flush o? o? +expose_meth: :- move i i i +expose_meth: :s read o +expose_meth: :s read_byte +expose_meth: :s readline +expose_meth: :- resize i +expose_meth: :- seek o o? +expose_meth: :i size +expose_meth: :i tell +expose_meth: :- write o +expose_meth: :- write_byte o + +expose_new_mutable: +expose_wide_meth: __init__ -1 -1 + `vdeleg`(init); + `void; + Index: src/templates/mmap.derived =================================================================== --- src/templates/mmap.derived (revision 0) +++ src/templates/mmap.derived (revision 0) @@ -0,0 +1,4 @@ +base_class: PyMmap +want_dict: true +ctr: +incl: object Index: Lib/test/test_mmap.py =================================================================== --- Lib/test/test_mmap.py (revision 0) +++ Lib/test/test_mmap.py (revision 0) @@ -0,0 +1,398 @@ +from test.test_support import verify, vereq, TESTFN +import mmap +import os, re +import javashell +PAGESIZE = mmap.PAGESIZE + +def test_both(): + "Test mmap module on Unix systems and Windows" + + # Create a file to be mmap'ed. + if os.path.exists(TESTFN): + os.unlink(TESTFN) + f = open(TESTFN, 'w+') + + try: # unlink TESTFN no matter what + # Write 2 pages worth of data to the file + f.write('\0'* PAGESIZE) + f.write('foo') + f.write('\0'* (PAGESIZE-3) ) + f.flush() + m = mmap.mmap(f, 2 * PAGESIZE) + f.close() + + # Simple sanity checks + + print type(m) # SF bug 128713: segfaulted on Linux + print ' Position of foo:', m.find('foo') / float(PAGESIZE), 'pages' + vereq(m.find('foo'), PAGESIZE) + + print ' Length of file:', len(m) / float(PAGESIZE), 'pages' + vereq(len(m), 2*PAGESIZE) + + print ' Contents of byte 0:', repr(m[0]) + vereq(m[0], '\0') + print ' Contents of first 3 bytes:', repr(m[0:3]) + vereq(m[0:3], '\0\0\0') + + # Modify the file's content + print "\n Modifying file's content..." + m[0] = '3' + m[PAGESIZE +3: PAGESIZE +3+3] = 'bar' + + # Check that the modification worked + print ' Contents of byte 0:', repr(m[0]) + vereq(m[0], '3') + print ' Contents of first 3 bytes:', repr(m[0:3]) + vereq(m[0:3], '3\0\0') + print ' Contents of second page:', repr(m[PAGESIZE-1 : PAGESIZE + 7]) + vereq(m[PAGESIZE-1 : PAGESIZE + 7], '\0foobar\0') + + m.flush() + + # Test doing a regular expression match in an mmap'ed file + match = re.search('[A-Za-z]+', m) + if match is None: + print ' ERROR: regex match on mmap failed!' + else: + start, end = match.span(0) + length = end - start + + print ' Regex match on mmap (page start, length of match):', + print start / float(PAGESIZE), length + + vereq(start, PAGESIZE) + vereq(end, PAGESIZE + 6) + + # test seeking around (try to overflow the seek implementation) + m.seek(0,0) + print ' Seek to zeroth byte' + vereq(m.tell(), 0) + m.seek(42,1) + print ' Seek to 42nd byte' + vereq(m.tell(), 42) + m.seek(0,2) + print ' Seek to last byte' + vereq(m.tell(), len(m)) + + print ' Try to seek to negative position...' + try: + m.seek(-1) + except ValueError: + pass + else: + verify(0, 'expected a ValueError but did not get it') + + print ' Try to seek beyond end of mmap...' + try: + m.seek(1,2) + except ValueError: + pass + else: + verify(0, 'expected a ValueError but did not get it') + + print ' Try to seek to negative position...' + try: + m.seek(-len(m)-1,2) + except ValueError: + pass + else: + verify(0, 'expected a ValueError but did not get it') + + # Try resizing map + print ' Attempting resize()' + try: + m.resize(512) + except SystemError: + # resize() not supported + # No messages are printed, since the output of this test suite + # would then be different across platforms. + pass + else: + # resize() is supported + verify(len(m) == 512, + "len(m) is %d, but expecting 512" % (len(m),) ) + # Check that we can no longer seek beyond the new size. + try: + m.seek(513,0) + except ValueError: + pass + else: + verify(0, 'Could seek beyond the new size') + + # Check that the underlying file is truncated too + # (bug #728515) + f = open(TESTFN) + f.seek(0, 2) + verify(f.tell() == 512, 'Underlying file not truncated') + f.close() + verify(m.size() == 512, 'New size not reflected in file') + + m.close() + + finally: + try: + f.close() + except OSError: + pass + try: + os.unlink(TESTFN) + except OSError: + pass + + # Test for "access" keyword parameter + try: + mapsize = 10 + print " Creating", mapsize, "byte test data file." + open(TESTFN, "wb").write("a"*mapsize) + print " Opening mmap with access=ACCESS_READ" + f = open(TESTFN, "rb") + m = mmap.mmap(f, mapsize, access=mmap.ACCESS_READ) + verify(m[:] == 'a'*mapsize, "Readonly memory map data incorrect.") + + print " Ensuring that readonly mmap can't be slice assigned." + try: + m[:] = 'b'*mapsize + except TypeError: + pass + else: + verify(0, "Able to write to readonly memory map") + + print " Ensuring that readonly mmap can't be item assigned." + try: + m[0] = 'b' + except TypeError: + pass + else: + verify(0, "Able to write to readonly memory map") + + print " Ensuring that readonly mmap can't be write() to." + try: + m.seek(0,0) + m.write('abc') + except TypeError: + pass + else: + verify(0, "Able to write to readonly memory map") + + print " Ensuring that readonly mmap can't be write_byte() to." + try: + m.seek(0,0) + m.write_byte('d') + except TypeError: + pass + else: + verify(0, "Able to write to readonly memory map") + + print " Ensuring that readonly mmap can't be resized." + try: + m.resize(2*mapsize) + except SystemError: # resize is not universally supported + pass + except TypeError: + pass + else: + verify(0, "Able to resize readonly memory map") + del m, f + verify(open(TESTFN, "rb").read() == 'a'*mapsize, + "Readonly memory map data file was modified") + + print " Opening mmap with size too big" + import sys + f = open(TESTFN, "r+b") + try: + m = mmap.mmap(f, mapsize+1) + except ValueError: + # we do not expect a ValueError on Windows + # CAUTION: This also changes the size of the file on disk, and + # later tests assume that the length hasn't changed. We need to + # repair that. + if javashell._getOsType().startswith('nt'): + verify(0, "Opening mmap with size+1 should work on Windows.") + else: + # we expect a ValueError on Unix, but not on Windows + if not javashell._getOsType().startswith('nt'): + verify(0, "Opening mmap with size+1 should raise ValueError.") + m.close() + f.close() + if javashell._getOsType().startswith('nt'): + # Repair damage from the resizing test. + f = open(TESTFN, 'r+b') + f.truncate(mapsize) + f.close() + + print " Opening mmap with access=ACCESS_WRITE" + f = open(TESTFN, "r+b") + m = mmap.mmap(f, mapsize, access=mmap.ACCESS_WRITE) + print " Modifying write-through memory map." + m[:] = 'c'*mapsize + verify(m[:] == 'c'*mapsize, + "Write-through memory map memory not updated properly.") + m.flush() + m.close() + f.close() + f = open(TESTFN, 'rb') + stuff = f.read() + f.close() + verify(stuff == 'c'*mapsize, + "Write-through memory map data file not updated properly.") + + print " Opening mmap with access=ACCESS_COPY" + f = open(TESTFN, "r+b") + m = mmap.mmap(f, mapsize, access=mmap.ACCESS_COPY) + print " Modifying copy-on-write memory map." + m[:] = 'd'*mapsize + verify(m[:] == 'd' * mapsize, + "Copy-on-write memory map data not written correctly.") + m.flush() + verify(open(TESTFN, "rb").read() == 'c'*mapsize, + "Copy-on-write test data file should not be modified.") + try: + print " Ensuring copy-on-write maps cannot be resized." + m.resize(2*mapsize) + except TypeError: + pass + else: + verify(0, "Copy-on-write mmap resize did not raise exception.") + del m, f + try: + print " Ensuring invalid access parameter raises exception." + f = open(TESTFN, "r+b") + m = mmap.mmap(f, mapsize, access=4) + except ValueError: + pass + else: + verify(0, "Invalid access code should have raised exception.") + + if javashell._getOsType() == "posix": + # Try incompatible flags, prot and access parameters. + f = open(TESTFN, "r+b") + try: + m = mmap.mmap(f, mapsize, flags=mmap.MAP_PRIVATE, + prot=mmap.PROT_READ, access=mmap.ACCESS_WRITE) + except ValueError: + pass + else: + verify(0, "Incompatible parameters should raise ValueError.") + f.close() + finally: + try: + os.unlink(TESTFN) + except OSError: + pass + + print ' Try opening a bad file descriptor...' + try: + mmap.mmap(-2, 4096) + except mmap.error: + pass + else: + verify(0, 'expected a mmap.error but did not get it') + + # Do a tougher .find() test. SF bug 515943 pointed out that, in 2.2, + # searching for data with embedded \0 bytes didn't work. + f = open(TESTFN, 'w+') + + try: # unlink TESTFN no matter what + data = 'aabaac\x00deef\x00\x00aa\x00' + n = len(data) + f.write(data) + f.flush() + m = mmap.mmap(f, n) + f.close() + + for start in range(n+1): + for finish in range(start, n+1): + slice = data[start : finish] + vereq(m.find(slice), data.find(slice)) + vereq(m.find(slice + 'x'), -1) + m.close() + + finally: + try: + os.unlink(TESTFN) + except OSError: + pass + + # make sure a double close doesn't crash on Solaris (Bug# 665913) + f = open(TESTFN, 'w+') + + try: # unlink TESTFN no matter what + f.write(2**16 * 'a') # Arbitrary character + f.close() + + f = open(TESTFN) + mf = mmap.mmap(f, 2**16, access=mmap.ACCESS_READ) + mf.close() + mf.close() + f.close() + + finally: + os.unlink(TESTFN) + + # test mapping of entire file by passing 0 for map length + if hasattr(os, "stat"): + print " Ensuring that passing 0 as map length sets map size to current file size." + f = open(TESTFN, "w+") + + try: + f.write(2**16 * 'm') # Arbitrary character + f.close() + + f = open(TESTFN, "rb+") + mf = mmap.mmap(f, 0) + verify(len(mf) == 2**16, "Map size should equal file size.") + vereq(mf.read(2**16), 2**16 * "m") + mf.close() + f.close() + + finally: + os.unlink(TESTFN) + + # test mapping of entire file by passing 0 for map length + if hasattr(os, "stat"): + print " Ensuring that passing 0 as map length sets map size to current file size." + f = open(TESTFN, "w+") + try: + f.write(2**16 * 'm') # Arbitrary character + f.close() + + f = open(TESTFN, "rb+") + mf = mmap.mmap(f, 0) + verify(len(mf) == 2**16, "Map size should equal file size.") + vereq(mf.read(2**16), 2**16 * "m") + mf.close() + f.close() + + finally: + os.unlink(TESTFN) + + # make move works everywhere (64-bit format problem earlier) + f = open(TESTFN, 'w+') + + try: # unlink TESTFN no matter what + f.write("ABCDEabcde") # Arbitrary character + f.flush() + + mf = mmap.mmap(f, 10) + mf.move(5, 0, 5) + verify(mf[:] == "ABCDEABCDE", "Map move should have duplicated front 5") + mf.close() + f.close() + + finally: + os.unlink(TESTFN) + +def test_anon(): + print " anonymous mmap.mmap(-1, PAGESIZE)..." + m = mmap.mmap(-1, PAGESIZE) + for x in xrange(PAGESIZE): + verify(m[x] == '\0', "anonymously mmap'ed contents should be zero") + + for x in xrange(PAGESIZE): + m[x] = ch = chr(x & 255) + vereq(m[x], ch) + +test_both() +test_anon() +print ' Test passed'