From: <cg...@us...> - 2008-11-23 23:12:51
|
Revision: 5622 http://jython.svn.sourceforge.net/jython/?rev=5622&view=rev Author: cgroves Date: 2008-11-23 23:12:46 +0000 (Sun, 23 Nov 2008) Log Message: ----------- PyInstance ignored __tojava__ methods on its classes, so Java classes in the old world with a __tojava__ would be ignored as well. This differed from most other dunder methods, so the new PyJavaType exposes __tojava__ through PyObjectDerived. IOBase was using __tojava__ to mean something different than what PyObject was doing with it, so change the name to keep regular jython object conversion from picking up on it. Modified Paths: -------------- branches/newstyle-java-types/src/org/python/core/PyFile.java branches/newstyle-java-types/src/org/python/core/io/BufferedIOMixin.java branches/newstyle-java-types/src/org/python/core/io/FileIO.java branches/newstyle-java-types/src/org/python/core/io/IOBase.java branches/newstyle-java-types/src/org/python/core/io/StreamIO.java branches/newstyle-java-types/src/org/python/core/io/TextIOBase.java Modified: branches/newstyle-java-types/src/org/python/core/PyFile.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyFile.java 2008-11-23 22:22:05 UTC (rev 5621) +++ branches/newstyle-java-types/src/org/python/core/PyFile.java 2008-11-23 23:12:46 UTC (rev 5622) @@ -529,7 +529,12 @@ } public Object __tojava__(Class<?> cls) { - Object o = file.__tojava__(cls); + Object o = null; + if (InputStream.class.isAssignableFrom(cls)) { + o = file.asInputStream(); + } else if (OutputStream.class.isAssignableFrom(cls)) { + o = file.asOutputStream(); + } if (o == null) { o = super.__tojava__(cls); } Modified: branches/newstyle-java-types/src/org/python/core/io/BufferedIOMixin.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/io/BufferedIOMixin.java 2008-11-23 22:22:05 UTC (rev 5621) +++ branches/newstyle-java-types/src/org/python/core/io/BufferedIOMixin.java 2008-11-23 23:12:46 UTC (rev 5622) @@ -1,6 +1,9 @@ /* Copyright (c) 2007 Jython Developers */ package org.python.core.io; +import java.io.InputStream; +import java.io.OutputStream; + import org.python.core.Py; import org.python.core.PyException; @@ -102,8 +105,13 @@ return rawIO.closed(); } - /** {@inheritDoc} */ - public Object __tojava__(Class cls) { - return rawIO.__tojava__(cls); + @Override + public InputStream asInputStream() { + return rawIO.asInputStream(); } + + @Override + public OutputStream asOutputStream() { + return rawIO.asOutputStream(); + } } Modified: branches/newstyle-java-types/src/org/python/core/io/FileIO.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/io/FileIO.java 2008-11-23 22:22:05 UTC (rev 5621) +++ branches/newstyle-java-types/src/org/python/core/io/FileIO.java 2008-11-23 23:12:46 UTC (rev 5622) @@ -14,7 +14,6 @@ import org.python.core.imp; import org.python.core.Py; -import org.python.core.PyObject; import org.python.core.util.RelativeFile; import org.python.modules.errno; @@ -27,7 +26,7 @@ /** The underlying file channel */ private FileChannel fileChannel; - + /** The underlying file (if known) */ private RandomAccessFile file; @@ -336,16 +335,16 @@ super.close(); } - /** {@inheritDoc} */ - public Object __tojava__(Class cls) { - if (OutputStream.class.isAssignableFrom(cls) && writing) { - return Channels.newOutputStream(fileChannel); - } else if (InputStream.class.isAssignableFrom(cls) && readable()) { - return Channels.newInputStream(fileChannel); - } - return super.__tojava__(cls); + @Override + public OutputStream asOutputStream() { + return writing ? Channels.newOutputStream(fileChannel) : super.asOutputStream(); } + @Override + public InputStream asInputStream() { + return readable() ? Channels.newInputStream(fileChannel) : super.asInputStream(); + } + /** {@inheritDoc} */ public boolean readable() { return reading || plus; Modified: branches/newstyle-java-types/src/org/python/core/io/IOBase.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/io/IOBase.java 2008-11-23 22:22:05 UTC (rev 5621) +++ branches/newstyle-java-types/src/org/python/core/io/IOBase.java 2008-11-23 23:12:46 UTC (rev 5622) @@ -1,6 +1,9 @@ /* Copyright (c) 2007 Jython Developers */ package org.python.core.io; +import java.io.InputStream; +import java.io.OutputStream; + import org.python.core.Py; import org.python.core.PyException; import org.python.modules.errno; @@ -191,17 +194,20 @@ } /** - * Coerce this Python object (the parent PyFile) into a java - * object. - * - * @param cls the desired Class to coerce the object to - * @return the desired object or null + * Coerce this into an OutputStream if possible, or return null. */ - public Object __tojava__(Class cls) { + public OutputStream asOutputStream() { return null; } /** + * Coerce this into an InputStream if possible, or return null. + */ + public InputStream asInputStream() { + return null; + } + + /** * Raise a TypeError indicating the specified operation is not * supported. * Modified: branches/newstyle-java-types/src/org/python/core/io/StreamIO.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/io/StreamIO.java 2008-11-23 22:22:05 UTC (rev 5621) +++ branches/newstyle-java-types/src/org/python/core/io/StreamIO.java 2008-11-23 23:12:46 UTC (rev 5622) @@ -18,7 +18,6 @@ import org.python.core.imp; import org.python.core.Py; -import org.python.core.PyObject; /** * Raw I/O implementation for simple streams. @@ -158,7 +157,7 @@ } super.close(); } - + /** Unwrap one or more nested FilterInputStreams. */ private static FileDescriptor getInputFileDescriptor(InputStream stream) throws IOException { if (stream == null) @@ -200,12 +199,12 @@ } return null; } - + /** {@inheritDoc} */ - + public boolean isatty() { checkClosed(); - + FileDescriptor fd; try { if ( ((fd = getInputFileDescriptor(inputStream)) == null) && @@ -214,7 +213,7 @@ } catch (IOException e) { return false; } - + return imp.load("os").__getattr__("isatty").__call__(Py.java2py(fd)).__nonzero__(); } @@ -228,20 +227,26 @@ return writeChannel != null; } - /** {@inheritDoc} */ - public Object __tojava__(Class cls) { - if (OutputStream.class.isAssignableFrom(cls) && writable()) { + @Override + public OutputStream asOutputStream() { + if (writable()) { if (outputStream == null) { return Channels.newOutputStream(writeChannel); } return outputStream; - } else if (InputStream.class.isAssignableFrom(cls) && readable()) { + } + return super.asOutputStream(); + } + + @Override + public InputStream asInputStream() { + if (readable()) { if (inputStream == null) { return Channels.newInputStream(readChannel); } return inputStream; } - return super.__tojava__(cls); + return super.asInputStream(); } /** {@inheritDoc} */ Modified: branches/newstyle-java-types/src/org/python/core/io/TextIOBase.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/io/TextIOBase.java 2008-11-23 22:22:05 UTC (rev 5621) +++ branches/newstyle-java-types/src/org/python/core/io/TextIOBase.java 2008-11-23 23:12:46 UTC (rev 5622) @@ -1,6 +1,8 @@ /* Copyright (c) 2007 Jython Developers */ package org.python.core.io; +import java.io.InputStream; +import java.io.OutputStream; import java.nio.ByteBuffer; import org.python.core.Py; @@ -188,11 +190,16 @@ return bufferedIO.closed(); } - /** {@inheritDoc} */ - public Object __tojava__(Class cls) { - return bufferedIO.__tojava__(cls); + @Override + public InputStream asInputStream() { + return bufferedIO.asInputStream(); } + @Override + public OutputStream asOutputStream() { + return bufferedIO.asOutputStream(); + } + /** * Return the known Newline types, as a PyObject, encountered * while reading this file. This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |