From: <pj...@us...> - 2009-10-24 01:43:08
|
Revision: 6892 http://jython.svn.sourceforge.net/jython/?rev=6892&view=rev Author: pjenvey Date: 2009-10-24 01:42:48 +0000 (Sat, 24 Oct 2009) Log Message: ----------- move os.open to PosixModule to fix the recent test_threaded_import regression Modified Paths: -------------- trunk/jython/Lib/posix.py trunk/jython/src/org/python/core/Py.java trunk/jython/src/org/python/modules/posix/PosixModule.java Modified: trunk/jython/Lib/posix.py =================================================================== --- trunk/jython/Lib/posix.py 2009-10-23 21:55:28 UTC (rev 6891) +++ trunk/jython/Lib/posix.py 2009-10-24 01:42:48 UTC (rev 6892) @@ -15,7 +15,7 @@ import stat as _stat import sys from java.io import File -from org.python.core.io import FileDescriptors, FileIO, IOBase +from org.python.core.io import FileDescriptors, IOBase from org.python.core.Py import newString as asPyString __all__ = [name for name in _posix.__all__ if not name.startswith('__doc__')] @@ -245,64 +245,6 @@ rawio = FileDescriptors.get(fd) return _handle_oserror(rawio.seek, pos, how) -def open(filename, flag, mode=0777): - """open(filename, flag [, mode=0777]) -> fd - - Open a file (for low level IO). - """ - reading = flag & O_RDONLY - writing = flag & O_WRONLY - updating = flag & O_RDWR - creating = flag & O_CREAT - - truncating = flag & O_TRUNC - exclusive = flag & O_EXCL - sync = flag & O_SYNC - appending = flag & O_APPEND - - if updating and writing: - raise OSError(errno.EINVAL, strerror(errno.EINVAL), filename) - - if not creating: - # raises ENOENT if it doesn't exist - stat(filename) - - if not writing: - if updating: - writing = True - else: - reading = True - - if truncating and not writing: - # Explicitly truncate, writing will truncate anyway - FileIO(filename, 'w').close() - - if exclusive and creating: - from java.io import IOException - try: - if not File(sys.getPath(filename)).createNewFile(): - raise OSError(errno.EEXIST, strerror(errno.EEXIST), - filename) - except IOException, ioe: - raise OSError(ioe) - - mode = '%s%s%s%s' % (reading and 'r' or '', - (not appending and writing) and 'w' or '', - (appending and (writing or updating)) and 'a' or '', - updating and '+' or '') - - if sync and (writing or updating): - from java.io import FileNotFoundException, RandomAccessFile - try: - fchannel = RandomAccessFile(sys.getPath(filename), 'rws').getChannel() - except FileNotFoundException, fnfe: - if _stat.S_ISDIR(stat(filename).st_mode): - raise OSError(errno.EISDIR, strerror(errno.EISDIR)) - raise OSError(errno.ENOENT, strerror(errno.ENOENT), filename) - return FileIO(fchannel, mode) - - return FileIO(filename, mode) - def read(fd, buffersize): """read(fd, buffersize) -> string Modified: trunk/jython/src/org/python/core/Py.java =================================================================== --- trunk/jython/src/org/python/core/Py.java 2009-10-23 21:55:28 UTC (rev 6891) +++ trunk/jython/src/org/python/core/Py.java 2009-10-24 01:42:48 UTC (rev 6892) @@ -104,6 +104,10 @@ return new PyException(Py.OSError, message); } + public static PyException OSError(IOException ioe) { + return fromIOException(ioe, Py.OSError); + } + public static PyException OSError(Constant errno, String filename) { int value = errno.value(); // Pass to strerror because constantine currently lacks Errno descriptions on @@ -158,16 +162,7 @@ public static PyObject IOError; public static PyException IOError(IOException ioe) { - String message = ioe.getMessage(); - if (message == null) { - message = ioe.getClass().getName(); - } - if (ioe instanceof FileNotFoundException) { - PyTuple args = new PyTuple(Py.newInteger(Errno.ENOENT.value()), - Py.newString("File not found - " + message)); - return new PyException(Py.IOError, args); - } - return new PyException(Py.IOError, message); + return fromIOException(ioe, Py.IOError); } public static PyException IOError(String message) { @@ -187,6 +182,19 @@ return new PyException(Py.IOError, args); } + private static PyException fromIOException(IOException ioe, PyObject err) { + String message = ioe.getMessage(); + if (message == null) { + message = ioe.getClass().getName(); + } + if (ioe instanceof FileNotFoundException) { + PyTuple args = new PyTuple(Py.newInteger(Errno.ENOENT.value()), + Py.newString("File not found - " + message)); + return new PyException(err, args); + } + return new PyException(err, message); + } + public static PyObject KeyError; public static PyException KeyError(String message) { Modified: trunk/jython/src/org/python/modules/posix/PosixModule.java =================================================================== --- trunk/jython/src/org/python/modules/posix/PosixModule.java 2009-10-23 21:55:28 UTC (rev 6891) +++ trunk/jython/src/org/python/modules/posix/PosixModule.java 2009-10-24 01:42:48 UTC (rev 6892) @@ -4,6 +4,10 @@ import com.kenai.constantine.Constant; import com.kenai.constantine.platform.Errno; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.RandomAccessFile; import java.util.Map; import org.jruby.ext.posix.JavaPOSIX; @@ -17,6 +21,7 @@ import org.python.core.PyObject; import org.python.core.PyString; import org.python.core.PyTuple; +import org.python.core.io.FileIO; import org.python.core.util.RelativeFile; /** @@ -42,16 +47,26 @@ /** Platform specific POSIX services. */ private static POSIX posix = POSIXFactory.getPOSIX(new PythonPOSIXHandler(), true); + /** os.open flags. */ + private static int O_RDONLY = 0x0; + private static int O_WRONLY = 0x1; + private static int O_RDWR = 0x2; + private static int O_APPEND = 0x8; + private static int O_SYNC = 0x80; + private static int O_CREAT = 0x200; + private static int O_TRUNC = 0x400; + private static int O_EXCL = 0x800; + public static void classDictInit(PyObject dict) { - // os.open flags, only expose what we support - dict.__setitem__("O_RDONLY", Py.newInteger(0x0)); - dict.__setitem__("O_WRONLY", Py.newInteger(0x1)); - dict.__setitem__("O_RDWR", Py.newInteger(0x2)); - dict.__setitem__("O_APPEND", Py.newInteger(0x8)); - dict.__setitem__("O_SYNC", Py.newInteger(0x80)); - dict.__setitem__("O_CREAT", Py.newInteger(0x200)); - dict.__setitem__("O_TRUNC", Py.newInteger(0x400)); - dict.__setitem__("O_EXCL", Py.newInteger(0x800)); + // only expose the open flags we support + dict.__setitem__("O_RDONLY", Py.newInteger(O_RDONLY)); + dict.__setitem__("O_WRONLY", Py.newInteger(O_WRONLY)); + dict.__setitem__("O_RDWR", Py.newInteger(O_RDWR)); + dict.__setitem__("O_APPEND", Py.newInteger(O_APPEND)); + dict.__setitem__("O_SYNC", Py.newInteger(O_SYNC)); + dict.__setitem__("O_CREAT", Py.newInteger(O_CREAT)); + dict.__setitem__("O_TRUNC", Py.newInteger(O_TRUNC)); + dict.__setitem__("O_EXCL", Py.newInteger(O_EXCL)); // os.access constants dict.__setitem__("F_OK", Py.Zero); @@ -115,6 +130,71 @@ return PyStatResult.fromFileStat(posix.lstat(new RelativeFile(path).getPath())); } + public static PyString __doc__open = new PyString( + "open(filename, flag [, mode=0777]) -> fd\n\n" + + "Open a file (for low level IO).\n\n" + + "Note that the mode argument is not currently supported on Jython."); + public static PyObject open(String path, int flag) { + return open(path, flag, 0777); + } + + public static PyObject open(String path, int flag, int mode) { + boolean reading = (flag & O_RDONLY) != 0; + boolean writing = (flag & O_WRONLY) != 0; + boolean updating = (flag & O_RDWR) != 0; + boolean creating = (flag & O_CREAT) != 0; + boolean appending = (flag & O_APPEND) != 0; + boolean truncating = (flag & O_TRUNC) != 0; + boolean exclusive = (flag & O_EXCL) != 0; + boolean sync = (flag & O_SYNC) != 0; + File file = new RelativeFile(path); + + if (updating && writing) { + throw Py.OSError(Errno.EINVAL, path); + } + if (!creating && !file.exists()) { + throw Py.OSError(Errno.ENOENT, path); + } + + if (!writing) { + if (updating) { + writing = true; + } else { + reading = true; + } + } + + if (truncating && !writing) { + // Explicitly truncate, writing will truncate anyway + new FileIO(path, "w").close(); + } + + if (exclusive && creating) { + try { + if (!file.createNewFile()) { + throw Py.OSError(Errno.EEXIST, path); + } + } catch (IOException ioe) { + throw Py.OSError(ioe); + } + } + + String fileIOMode = (reading ? "r" : "") + (!appending && writing ? "w" : "") + + (appending && (writing || updating) ? "a" : "") + (updating ? "+" : ""); + FileIO fileIO; + if (sync && (writing || updating)) { + try { + fileIO = new FileIO(new RandomAccessFile(file, "rws").getChannel(), fileIOMode); + } catch (FileNotFoundException fnfe) { + throw Py.OSError(file.isDirectory() ? Errno.EISDIR : Errno.ENOENT, path); + } + } else { + fileIO = new FileIO(path, fileIOMode); + } + + return Py.java2py(fileIO); + } + public static PyString __doc__stat = new PyString( "stat(path) -> stat result\n\n" + "Perform a stat system call on the given path.\n\n" + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |