From: <pj...@us...> - 2008-07-29 20:29:10
|
Revision: 5019 http://jython.svn.sourceforge.net/jython/?rev=5019&view=rev Author: pjenvey Date: 2008-07-29 20:29:08 +0000 (Tue, 29 Jul 2008) Log Message: ----------- add an InputStream tie-in to TextIOBases, for parsing in universal newlines mode fixes #1082 Modified Paths: -------------- branches/asm/src/org/python/core/ParserFacade.java branches/asm/src/org/python/core/imp.java Added Paths: ----------- branches/asm/src/org/python/core/io/TextIOInputStream.java Modified: branches/asm/src/org/python/core/ParserFacade.java =================================================================== --- branches/asm/src/org/python/core/ParserFacade.java 2008-07-29 12:31:46 UTC (rev 5018) +++ branches/asm/src/org/python/core/ParserFacade.java 2008-07-29 20:29:08 UTC (rev 5019) @@ -15,6 +15,7 @@ import org.antlr.runtime.ANTLRReaderStream; import org.antlr.runtime.CharStream; import org.antlr.runtime.CommonTokenStream; + import org.python.antlr.ExpressionParser; import org.python.antlr.InteractiveParser; import org.python.antlr.LeadingSpaceSkippingStream; @@ -23,12 +24,15 @@ import org.python.antlr.NoCloseReaderStream; import org.python.antlr.PythonParser; import org.python.antlr.PythonTree; -import org.python.core.util.StringUtil; import org.python.antlr.PythonTree; import org.python.antlr.PythonPartialLexer; import org.python.antlr.PythonPartialParser; import org.python.antlr.PythonPartialTokenSource; import org.python.antlr.ast.modType; +import org.python.core.io.StreamIO; +import org.python.core.io.TextIOInputStream; +import org.python.core.io.UniversalIOWrapper; +import org.python.core.util.StringUtil; /** * Facade for the classes in the org.python.antlr package. @@ -187,6 +191,13 @@ encoding = cflags.encoding; } + // Enable universal newlines mode on the input + StreamIO rawIO = new StreamIO(istream, true); + org.python.core.io.BufferedReader bufferedIO = + new org.python.core.io.BufferedReader(rawIO, 0); + UniversalIOWrapper textIO = new UniversalIOWrapper(bufferedIO); + istream = new TextIOInputStream(textIO); + Reader reader; if(encoding != null) { try { Modified: branches/asm/src/org/python/core/imp.java =================================================================== --- branches/asm/src/org/python/core/imp.java 2008-07-29 12:31:46 UTC (rev 5018) +++ branches/asm/src/org/python/core/imp.java 2008-07-29 20:29:08 UTC (rev 5019) @@ -22,7 +22,7 @@ private static final String UNKNOWN_SOURCEFILE = "<unknown>"; - public static final int APIVersion = 13; + public static final int APIVersion = 14; /** A non-empty fromlist for __import__'ing sub-modules. */ private static final PyObject nonEmptyFromlist = new PyTuple(Py.newString("__doc__")); Added: branches/asm/src/org/python/core/io/TextIOInputStream.java =================================================================== --- branches/asm/src/org/python/core/io/TextIOInputStream.java (rev 0) +++ branches/asm/src/org/python/core/io/TextIOInputStream.java 2008-07-29 20:29:08 UTC (rev 5019) @@ -0,0 +1,60 @@ +/* Copyright (c) Jython Developers */ +package org.python.core.io; + +import java.io.InputStream; +import java.io.IOException; + +/** + * An InputStream tie-in to a TextIOBase. + */ +public class TextIOInputStream extends InputStream { + + private TextIOBase textIO; + + /** + * Creates an InputStream wrapper to a given TextIOBase. + * + * @param textIO a TextIOBase + */ + public TextIOInputStream(TextIOBase textIO) { + this.textIO = textIO; + } + + @Override + public int read() throws IOException { + String result = textIO.read(1); + if (result.length() == 0) { + return -1; + } + return (int)result.charAt(0); + } + + @Override + public int read(byte[] b, int off, int len) throws IOException { + if (b == null) { + throw new NullPointerException(); + } else if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) + || ((off + len) < 0)) { + throw new IndexOutOfBoundsException(); + } else if (len == 0) { + return 0; + } + + String result = textIO.read(len); + len = result.length(); + for (int i = 0; i < len; i++) { + b[off + i] = (byte)result.charAt(i); + } + return len == 0 ? -1 : len; + } + + @Override + public void close() throws IOException { + textIO.close(); + } + + @Override + public long skip(long n) throws IOException { + return textIO.seek(n, 1); + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |