From: <pj...@us...> - 2008-07-21 19:11:52
|
Revision: 4982 http://jython.svn.sourceforge.net/jython/?rev=4982&view=rev Author: pjenvey Date: 2008-07-21 19:11:48 +0000 (Mon, 21 Jul 2008) Log Message: ----------- fix exposed __getitem__ possibly returning null Modified Paths: -------------- branches/asm/src/org/python/core/PyString.java branches/asm/src/org/python/core/PyUnicode.java Modified: branches/asm/src/org/python/core/PyString.java =================================================================== --- branches/asm/src/org/python/core/PyString.java 2008-07-21 19:10:02 UTC (rev 4981) +++ branches/asm/src/org/python/core/PyString.java 2008-07-21 19:11:48 UTC (rev 4982) @@ -468,7 +468,11 @@ @ExposedMethod final PyObject str___getitem__(PyObject index) { - return seq___finditem__(index); + PyObject ret = seq___finditem__(index); + if (ret == null) { + throw Py.IndexError("string index out of range"); + } + return ret; } @ExposedMethod(defaults = "null") Modified: branches/asm/src/org/python/core/PyUnicode.java =================================================================== --- branches/asm/src/org/python/core/PyUnicode.java 2008-07-21 19:10:02 UTC (rev 4981) +++ branches/asm/src/org/python/core/PyUnicode.java 2008-07-21 19:11:48 UTC (rev 4982) @@ -245,12 +245,12 @@ } @ExposedMethod - public PyObject unicode___getitem__(PyObject index) { - return seq___finditem__(index); + final PyObject unicode___getitem__(PyObject index) { + return str___getitem__(index); } @ExposedMethod(defaults = "null") - public PyObject unicode___getslice__(PyObject start, PyObject stop, PyObject step) { + final PyObject unicode___getslice__(PyObject start, PyObject stop, PyObject step) { return seq___getslice__(start, stop, step); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2008-07-23 01:18:55
|
Revision: 4985 http://jython.svn.sourceforge.net/jython/?rev=4985&view=rev Author: pjenvey Date: 2008-07-23 01:18:53 +0000 (Wed, 23 Jul 2008) Log Message: ----------- o make Py.makeCharacter consisitent in never returning unicode unless it was asked for o move calculateIndex into PySequence for PyArray o allow PyLong.getLong access for PyArray Modified Paths: -------------- branches/asm/src/org/python/core/Py.java branches/asm/src/org/python/core/PyList.java branches/asm/src/org/python/core/PyLong.java branches/asm/src/org/python/core/PySequence.java Modified: branches/asm/src/org/python/core/Py.java =================================================================== --- branches/asm/src/org/python/core/Py.java 2008-07-21 19:48:17 UTC (rev 4984) +++ branches/asm/src/org/python/core/Py.java 2008-07-23 01:18:53 UTC (rev 4985) @@ -1441,8 +1441,8 @@ return makeCharacter(c, false); } - static final PyString makeCharacter(int codepoint, boolean explicitUnicode) { - if (explicitUnicode || codepoint > 255) { + static final PyString makeCharacter(int codepoint, boolean toUnicode) { + if (toUnicode) { return new PyUnicode(codepoint); } Modified: branches/asm/src/org/python/core/PyList.java =================================================================== --- branches/asm/src/org/python/core/PyList.java 2008-07-21 19:48:17 UTC (rev 4984) +++ branches/asm/src/org/python/core/PyList.java 2008-07-23 01:18:53 UTC (rev 4985) @@ -528,25 +528,6 @@ throw Py.ValueError(message); } - // This is closely related to fixindex in PySequence, but less strict - // fixindex returns -1 if index += length < 0 or if index >= length - // where this function returns 0 in former case and length in the latter. - // I think both are needed in different cases, but if this method turns - // out to be needed in other sequence subclasses, it should be moved to - // PySequence. - private int calculateIndex(int index) { - int length = size(); - if(index < 0) { - index = index += length; - if(index < 0) { - index = 0; - } - } else if(index > length) { - index = length; - } - return index; - } - /** * Insert the argument element into the list at the specified index. <br> * Same as <code>s[index:index] = [o] if index >= 0</code>. Modified: branches/asm/src/org/python/core/PyLong.java =================================================================== --- branches/asm/src/org/python/core/PyLong.java 2008-07-21 19:48:17 UTC (rev 4984) +++ branches/asm/src/org/python/core/PyLong.java 2008-07-23 01:18:53 UTC (rev 4985) @@ -182,11 +182,11 @@ } - private long getLong(long min, long max) { + public long getLong(long min, long max) { return getLong(min, max, "long int too large to convert"); } - private long getLong(long min, long max, String overflowMsg) { + public long getLong(long min, long max, String overflowMsg) { if (value.compareTo(maxLong) <= 0 && value.compareTo(minLong) >= 0) { long v = value.longValue(); if (v >= min && v <= max) Modified: branches/asm/src/org/python/core/PySequence.java =================================================================== --- branches/asm/src/org/python/core/PySequence.java 2008-07-21 19:48:17 UTC (rev 4984) +++ branches/asm/src/org/python/core/PySequence.java 2008-07-23 01:18:53 UTC (rev 4985) @@ -256,6 +256,20 @@ } } + // XXX: more appropriate naming of this vs fixindex + protected int calculateIndex(int index) { + int length = __len__(); + if(index < 0) { + index = index += length; + if(index < 0) { + index = 0; + } + } else if(index > length) { + index = length; + } + return index; + } + public PyObject __finditem__(int index) { return seq___finditem__(index); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2008-07-26 01:55:06
|
Revision: 5002 http://jython.svn.sourceforge.net/jython/?rev=5002&view=rev Author: fwierzbicki Date: 2008-07-26 01:55:04 +0000 (Sat, 26 Jul 2008) Log Message: ----------- Removed stray System.outs. Modified Paths: -------------- branches/asm/src/org/python/core/PyClass.java branches/asm/src/org/python/core/PyString.java Modified: branches/asm/src/org/python/core/PyClass.java =================================================================== --- branches/asm/src/org/python/core/PyClass.java 2008-07-26 01:51:48 UTC (rev 5001) +++ branches/asm/src/org/python/core/PyClass.java 2008-07-26 01:55:04 UTC (rev 5002) @@ -162,14 +162,11 @@ for (java.util.Iterator iter = super__methods.entrySet().iterator(); iter .hasNext();) { java.util.Map.Entry entry = (java.util.Map.Entry) iter.next(); - // System.out.println(entry.getKey()); // debug entry.setValue(((java.util.ArrayList) entry.getValue()) .toArray(empty_methods)); } } - // System.out.println("proxyClasses: "+proxyClasses+", "+ - // proxyClasses[0]); if (dict.__finditem__("__doc__") == null) { dict.__setitem__("__doc__", Py.None); } @@ -186,7 +183,6 @@ protected void findModule(PyObject dict) { PyObject module = dict.__finditem__("__module__"); if (module == null || module == Py.None) { - // System.out.println("in PyClass getFrame: "+__name__.string); PyFrame f = Py.getFrame(); if (f != null) { PyObject nm = f.f_globals.__finditem__("__name__"); Modified: branches/asm/src/org/python/core/PyString.java =================================================================== --- branches/asm/src/org/python/core/PyString.java 2008-07-26 01:51:48 UTC (rev 5001) +++ branches/asm/src/org/python/core/PyString.java 2008-07-26 01:55:04 UTC (rev 5002) @@ -2739,7 +2739,6 @@ } c = pop(); if (c == '(') { - //System.out.println("( found"); if (dict == null) throw Py.TypeError("format requires a mapping"); int parens = 1; @@ -2753,7 +2752,6 @@ } String tmp = format.substring(keyStart, index-1); this.args = dict.__getitem__(new PyString(tmp)); - //System.out.println("args: "+args+", "+argIndex); } else { push(); } @@ -2789,7 +2787,6 @@ continue; } PyObject arg = getarg(); - //System.out.println("args: "+args+", "+argIndex+", "+arg); char fill = ' '; String string=null; negative = false; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
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. |
From: <zy...@us...> - 2008-08-03 03:09:51
|
Revision: 5058 http://jython.svn.sourceforge.net/jython/?rev=5058&view=rev Author: zyasoft Date: 2008-08-03 03:09:49 +0000 (Sun, 03 Aug 2008) Log Message: ----------- Unicode arrays -- array.array('u') -- now work as expected, by special casing the fact that we are storing them as an array of codepoints (ints). Because of this, byteswap doesn't make sense. Modified Paths: -------------- branches/asm/src/org/python/core/PyArray.java branches/asm/src/org/python/core/PyUnicode.java Modified: branches/asm/src/org/python/core/PyArray.java =================================================================== --- branches/asm/src/org/python/core/PyArray.java 2008-08-03 02:13:36 UTC (rev 5057) +++ branches/asm/src/org/python/core/PyArray.java 2008-08-03 03:09:49 UTC (rev 5058) @@ -119,8 +119,13 @@ self.fromlist(initial); } else if (initial instanceof PyString && !(initial instanceof PyUnicode)) { self.fromstring(initial.toString()); - } else if (initial instanceof PyUnicode && "u".equals(typecode)) { - self.fromunicode(initial.toString()); + } else if ("u".equals(typecode)) { + if (initial instanceof PyUnicode) { + self.extendArray(((PyUnicode) initial).toCodePoints()); + } + else { + self.extendUnicodeIter(initial); + } } else { self.extendInternal(initial); } @@ -376,6 +381,8 @@ String value; if ("c".equals(typecode)) { value = PyString.encode_UnicodeEscape(tostring(), true); + } else if ("u".equals(typecode)) { + value = (new PyUnicode(tounicode())).__repr__().toString(); } else { value = tolist().toString(); } @@ -408,28 +415,68 @@ append(value); } + private static int getCodePoint(PyObject obj) { + if (obj instanceof PyUnicode) { + PyUnicode u = (PyUnicode) obj; + int[] codepoints = u.toCodePoints(); + if (codepoints.length == 1) { + return codepoints[0]; + } + } + throw Py.TypeError("array item must be unicode character"); + } + + + // relax to allow mixing with PyString, integers + private static int getCodePointOrInt(PyObject obj) { + if (obj instanceof PyUnicode) { + PyUnicode u = (PyUnicode) obj; + return u.toCodePoints()[0]; + } + else if (obj instanceof PyString) { + PyString s = (PyString) obj; + return s.toString().charAt(0); + } + else if (obj.__nonzero__()) { + return ((PyInteger)obj.__int__()).getValue(); + } + else { + return -1; + } + } + /** * Append new value x to the end of the array. * * @param value * item to be appended to the array */ + public void append(PyObject value) { // Currently, this is asymmetric with extend, which // *will* do conversions like append(5.0) to an int array. // Also, cpython 2.2 will do the append coersion. However, // it is deprecated in cpython 2.3, so maybe we are just // ahead of our time ;-) + int afterLast = delegate.getSize(); - delegate.makeInsertSpace(afterLast); - try { - set(afterLast, value); - } catch(PyException e) { - delegate.setSize(afterLast); - throw new PyException(e.type, e.value); + if ("u".equals(typecode)) { + int codepoint = getCodePoint(value); + delegate.makeInsertSpace(afterLast); + Array.setInt(data, afterLast, codepoint); + } else { + + delegate.makeInsertSpace(afterLast); + try { + set(afterLast, value); + } catch (PyException e) { + delegate.setSize(afterLast); + throw new PyException(e.type, e.value); + } } } + @ExposedMethod public void array_byteswap() { byteswap(); @@ -442,7 +489,7 @@ * written on a machine with a different byte order. */ public void byteswap() { - if (getItemsize() == 0) { + if (getItemsize() == 0 || "u".equals(typecode)) { throw Py.RuntimeError("don't know how to byteswap this array type"); } ByteSwapper.swap(data); @@ -567,13 +614,24 @@ public final int array_count(PyObject value) { // note: cpython does not raise type errors based on item type; int iCount = 0; - for(int i = 0; i < delegate.getSize(); i++) { - if(value.equals(Py.java2py(Array.get(data, i)))) - iCount++; + int len = delegate.getSize(); + if ("u".equals(typecode)) { + int codepoint = getCodePointOrInt(value); + for (int i = 0; i < len; i++) { + if (codepoint == Array.getInt(data, i)) { + iCount++; + } + } + } else { + + for (int i = 0; i < len; i++) { + if (value.equals(Py.java2py(Array.get(data, i)))) { + iCount++; + } + } } return iCount; } - /** * Return the number of occurrences of x in the array. * @@ -654,13 +712,20 @@ * object of type PyString, PyArray or any object that can be * iterated over. */ + private void extendInternal(PyObject iterable) { - // string input - if (iterable instanceof PyString) { - fromstring(((PyString)iterable).toString()); - // PyArray input + if (iterable instanceof PyUnicode) { + if ("u".equals(typecode)) { + extendUnicodeIter(iterable); + } else if ("c".equals(typecode)){ + throw Py.TypeError("array item must be char"); + } else { + throw Py.TypeError("an integer is required"); + } + } else if (iterable instanceof PyString) { + fromstring(((PyString) iterable).toString()); } else if (iterable instanceof PyArray) { - PyArray source = (PyArray)iterable; + PyArray source = (PyArray) iterable; if (!source.typecode.equals(typecode)) { throw Py.TypeError("can only extend with array of same kind"); } @@ -693,7 +758,32 @@ } } } + + private void extendUnicodeIter(PyObject iterable) { + for (PyObject item : iterable.asIterable()) { + PyUnicode uitem; + try { + uitem = (PyUnicode) item; + } catch (ClassCastException e) { + throw Py.TypeError("Type not compatible with array type"); + } + for (int codepoint : uitem.toCodePoints()) { + int afterLast = delegate.getSize(); + delegate.makeInsertSpace(afterLast); + Array.setInt(data, afterLast, codepoint); + } + } + } + private void extendArray(int[] items) { + int last = delegate.getSize(); + delegate.ensureCapacity(last + items.length); + for (int item : items) { + Array.set(data, last++, item); + delegate.size++; + } + } + @ExposedMethod public final void array_fromfile(PyObject f, int count){ fromfile(f, count); @@ -883,16 +973,16 @@ } } - public void fromunicode(String input) { + public void fromunicode(PyUnicode input) { array_fromunicode(input); } @ExposedMethod - final void array_fromunicode(String input) { + final void array_fromunicode(PyUnicode input) { if (!"u".equals(typecode)) { throw Py.ValueError("fromunicode() may only be called on type 'u' arrays"); } - array_fromstring(input); + extend(input); } /** @@ -902,6 +992,9 @@ * index of the item to be retrieved from the array */ protected PyObject pyget(int i) { + if ("u".equals(typecode)) { + return new PyUnicode(Array.getInt(data, i)); + } return Py.java2py(Array.get(data, i)); } @@ -1067,10 +1160,21 @@ */ private int indexInternal(PyObject value) { // note: cpython does not raise type errors based on item type - for(int i = 0; i < delegate.getSize(); i++) { - if(value.equals(Py.java2py(Array.get(data, i)))) { - return i; + + int len = delegate.getSize(); + if ("u".equals(typecode)) { + int codepoint = getCodePointOrInt(value); + for (int i = 0; i < len; i++) { + if (codepoint == Array.getInt(data, i)) { + return i; + } } + } else { + for (int i = 0; i < len; i++) { + if (value.equals(Py.java2py(Array.get(data, i)))) { + return i; + } + } } return -1; } @@ -1092,13 +1196,24 @@ */ public void insert(int index, PyObject value) { index = calculateIndex(index); - delegate.makeInsertSpace(index); - Array.set(data, index, Py.tojava(value, type)); + if ("u".equals(typecode)) { + int codepoint = getCodePoint(value); + delegate.makeInsertSpace(index); + Array.setInt(data, index, codepoint); + + } else { + delegate.makeInsertSpace(index); + Array.set(data, index, Py.tojava(value, type)); + } } @ExposedMethod(defaults="-1") public final PyObject array_pop(int i){ - return pop(i); + PyObject val = pop(i); + if ("u".equals(typecode)) { + return new PyUnicode(val.asInt()); + } + return val; } /** @@ -1207,6 +1322,11 @@ * value to set the element to */ public void set(int i, PyObject value) { + if ("u".equals(typecode)) { + Array.setInt(data, i, getCodePoint(value)); + return; + } + // check for overflow of the integral types if(type == Byte.TYPE) { long val; @@ -1387,8 +1507,15 @@ */ public PyObject tolist() { PyList list = new PyList(); - for(int i = 0; i < delegate.getSize(); i++) { - list.append(Py.java2py(Array.get(data, i))); + int len = delegate.getSize(); + if ("u".equals(typecode)) { + for (int i = 0; i < len; i++) { + list.append(new PyUnicode(Array.getInt(data, i))); + } + } else { + for (int i = 0; i < len; i++) { + list.append(Py.java2py(Array.get(data, i))); + } } return list; } @@ -1456,13 +1583,22 @@ return StringUtil.fromBytes(bos.toByteArray()); } - @ExposedMethod - public final PyObject array_tounicode() { + public String tounicode() { if (!"u".equals(typecode)) { throw Py.ValueError("tounicode() may only be called on type 'u' arrays"); } - return new PyUnicode(tostring()); + int len = delegate.getSize(); + int[] codepoints = new int[len]; + for(int i = 0; i < len; i++) + codepoints[i] = Array.getInt(data, i); + return new String(codepoints, 0, codepoints.length); } + + + @ExposedMethod + public final PyObject array_tounicode() { + return new PyUnicode(tounicode()); + } // PyArray can't extend anymore, so delegate private class ArrayDelegate extends AbstractArray { Modified: branches/asm/src/org/python/core/PyUnicode.java =================================================================== --- branches/asm/src/org/python/core/PyUnicode.java 2008-08-03 02:13:36 UTC (rev 5057) +++ branches/asm/src/org/python/core/PyUnicode.java 2008-08-03 03:09:49 UTC (rev 5058) @@ -62,6 +62,10 @@ this(TYPE, new String(new int[]{codepoint}, 0, 1)); } + public PyUnicode(int[] codepoints) { + this(new String(codepoints, 0, codepoints.length)); + } + PyUnicode(StringBuilder buffer) { this(TYPE, new String(buffer)); } @@ -73,7 +77,7 @@ } return buffer; } - + PyUnicode(Iterator<Integer> iter) { this(fromCodePoints(iter)); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2008-08-10 21:30:20
|
Revision: 5137 http://jython.svn.sourceforge.net/jython/?rev=5137&view=rev Author: pjenvey Date: 2008-08-10 21:30:15 +0000 (Sun, 10 Aug 2008) Log Message: ----------- o __float__ should always return a PyFloat, no need to cast in complex new o prefer PyObject.isCallable to __builtin__'s Modified Paths: -------------- branches/asm/src/org/python/core/PyComplex.java branches/asm/src/org/python/core/PyMethod.java Modified: branches/asm/src/org/python/core/PyComplex.java =================================================================== --- branches/asm/src/org/python/core/PyComplex.java 2008-08-10 21:19:00 UTC (rev 5136) +++ branches/asm/src/org/python/core/PyComplex.java 2008-08-10 21:30:15 UTC (rev 5137) @@ -53,7 +53,7 @@ PyComplex complexReal; PyComplex complexImag; - PyObject toFloat = null; + PyFloat toFloat = null; if (real instanceof PyComplex) { complexReal = (PyComplex)real; } else { @@ -66,11 +66,7 @@ } throw pye; } - if (!(toFloat instanceof PyFloat)) { - throw Py.TypeError(String.format("__float__ returned non-float (type %.200s)", - imag.getType().fastGetName())); - } - complexReal = new PyComplex(((PyFloat)toFloat).getValue()); + complexReal = new PyComplex(toFloat.getValue()); } if (imag == null) { @@ -88,11 +84,7 @@ } throw pye; } - if (!(toFloat instanceof PyFloat)) { - throw Py.TypeError(String.format("__float__ returned non-float (type %.200s)", - imag.getType().fastGetName())); - } - complexImag = new PyComplex(((PyFloat)toFloat).getValue()); + complexImag = new PyComplex(toFloat.getValue()); } complexReal.real -= complexImag.imag; Modified: branches/asm/src/org/python/core/PyMethod.java =================================================================== --- branches/asm/src/org/python/core/PyMethod.java 2008-08-10 21:19:00 UTC (rev 5136) +++ branches/asm/src/org/python/core/PyMethod.java 2008-08-10 21:30:15 UTC (rev 5137) @@ -45,7 +45,7 @@ PyObject self = ap.getPyObject(1); PyObject classObj = ap.getPyObject(2, null); - if (!__builtin__.callable(func)) { + if (!func.isCallable()) { throw Py.TypeError("first argument must be callable"); } if (self == Py.None && classObj == null) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2008-08-11 06:59:31
|
Revision: 5147 http://jython.svn.sourceforge.net/jython/?rev=5147&view=rev Author: pjenvey Date: 2008-08-11 06:59:28 +0000 (Mon, 11 Aug 2008) Log Message: ----------- o change PySlice.indices to work like CPython's PySlice_GetIndicesEx (include a slicelength value) o reject PySequence slice assignments with a step and mismatching slicelengths Modified Paths: -------------- branches/asm/src/org/python/core/PySequence.java branches/asm/src/org/python/core/PySlice.java Modified: branches/asm/src/org/python/core/PySequence.java =================================================================== --- branches/asm/src/org/python/core/PySequence.java 2008-08-11 05:52:43 UTC (rev 5146) +++ branches/asm/src/org/python/core/PySequence.java 2008-08-11 06:59:28 UTC (rev 5147) @@ -327,8 +327,8 @@ } final synchronized PyObject seq___getslice__(PyObject start, PyObject stop, PyObject step) { - int[] slice = new PySlice(start, stop, step).indices(__len__()); - return getslice(slice[0], slice[1], slice[2]); + int[] indices = new PySlice(start, stop, step).indicesEx(__len__()); + return getslice(indices[0], indices[1], indices[2]); } public synchronized void __setslice__(PyObject start, @@ -346,8 +346,13 @@ PyObject stop, PyObject step, PyObject value) { - int[] slice = new PySlice(start, stop, step).indices(__len__()); - setslice(slice[0], slice[1], slice[2], value); + PySlice slice = new PySlice(start, stop, step); + int[] indices = slice.indicesEx(__len__()); + if ((slice.step != Py.None) && value.__len__() != indices[3]) { + throw Py.ValueError(String.format("attempt to assign sequence of size %d to extended " + + "slice of size %d", value.__len__(), indices[3])); + } + setslice(indices[0], indices[1], indices[2], value); } public synchronized void __delslice__(PyObject start, PyObject stop, PyObject step) { @@ -355,8 +360,8 @@ } final synchronized void seq___delslice__(PyObject start, PyObject stop, PyObject step) { - int[] slice = new PySlice(start, stop, step).indices(__len__()); - delRange(slice[0], slice[1], slice[2]); + int[] indices = new PySlice(start, stop, step).indicesEx(__len__()); + delRange(indices[0], indices[1], indices[2]); } public synchronized void __setitem__(int index, PyObject value) { Modified: branches/asm/src/org/python/core/PySlice.java =================================================================== --- branches/asm/src/org/python/core/PySlice.java 2008-08-11 05:52:43 UTC (rev 5146) +++ branches/asm/src/org/python/core/PySlice.java 2008-08-11 06:59:28 UTC (rev 5147) @@ -114,12 +114,9 @@ @ExposedMethod public PyObject slice_indices(PyObject len) { - int[] slice = indices(len.asIndex(Py.OverflowError)); - PyInteger[] pyInts = new PyInteger[slice.length]; - for(int i = 0; i < pyInts.length; i++) { - pyInts[i] = Py.newInteger(slice[i]); - } - return new PyTuple(pyInts); + int[] indices = indicesEx(len.asIndex(Py.OverflowError)); + return new PyTuple(Py.newInteger(indices[0]), Py.newInteger(indices[1]), + Py.newInteger(indices[2])); } public static int calculateSliceIndex(PyObject v) { @@ -130,52 +127,66 @@ } /** - * Calculates the actual indices of a slice with this slice's start, stop - * and step values for a sequence of length <code>len</code>. + * Calculates the actual indices of a slice with this slice's start, stop, step and + * slicelength values for a sequence of length <code>len</code>. * - * @return an array with the start at index 0, stop at index 1 and step and - * index 2. + * @return an array with the start at index 0, stop at index 1, step at index 2 and + * slicelength at index 3 */ - public int[] indices(int len) { - int[] slice = new int[3]; + public int[] indicesEx(int len) { + int start; + int stop; + int step; + int slicelength; + if (getStep() == Py.None) { - slice[STEP] = 1; + step = 1; } else { - slice[STEP] = calculateSliceIndex(getStep()); - if (slice[STEP] == 0) { + step = calculateSliceIndex(getStep()); + if (step == 0) { throw Py.ValueError("slice step cannot be zero"); } } if (getStart() == Py.None) { - slice[START] = slice[STEP] < 0 ? len - 1 : 0; + start = step < 0 ? len - 1 : 0; } else { - slice[START] = calculateSliceIndex(getStart()); - if(slice[START] < 0) { - slice[START] += len; + start = calculateSliceIndex(getStart()); + if (start < 0) { + start += len; } - if(slice[START] < 0) { - slice[START] = slice[STEP] < 0 ? -1 : 0; + if (start < 0) { + start = step < 0 ? -1 : 0; } - if(slice[START] >= len) { - slice[START] = slice[STEP] < 0 ? len - 1 : len; + if (start >= len) { + start = step < 0 ? len - 1 : len; } } - if(getStop() == Py.None) { - slice[STOP] = slice[STEP] < 0 ? -1 : len; + + if (getStop() == Py.None) { + stop = step < 0 ? -1 : len; } else { - slice[STOP] = calculateSliceIndex(getStop()); - if(slice[STOP] < 0) { - slice[STOP] += len; + stop = calculateSliceIndex(getStop()); + if (stop < 0) { + stop += len; } - if(slice[STOP] < 0) { - slice[STOP] = -1; + if (stop < 0) { + stop = -1; } - if(slice[STOP] > len) { - slice[STOP] = len; + if (stop > len) { + stop = len; } } - return slice; + + if ((step < 0 && stop >= start) || (step > 0 && start >= stop)) { + slicelength = 0; + } else if (step < 0) { + slicelength = (stop - start + 1) / (step) + 1; + } else { + slicelength = (stop - start - 1) / (step) + 1; + } + + return new int[] {start, stop, step, slicelength}; } /** @@ -211,8 +222,6 @@ return indices; } - private static final int START = 0, STOP = 1, STEP = 2; - @ExposedGet public PyObject start = Py.None; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2008-08-12 18:11:41
|
Revision: 5157 http://jython.svn.sourceforge.net/jython/?rev=5157&view=rev Author: fwierzbicki Date: 2008-08-12 18:11:38 +0000 (Tue, 12 Aug 2008) Log Message: ----------- Disallow encodings from unicode input. Fixes one test in test_compile.py. Modified Paths: -------------- branches/asm/src/org/python/core/CompilerFlags.java branches/asm/src/org/python/core/ParserFacade.java branches/asm/src/org/python/core/PyTableCode.java branches/asm/src/org/python/core/__builtin__.java Modified: branches/asm/src/org/python/core/CompilerFlags.java =================================================================== --- branches/asm/src/org/python/core/CompilerFlags.java 2008-08-12 17:17:12 UTC (rev 5156) +++ branches/asm/src/org/python/core/CompilerFlags.java 2008-08-12 18:11:38 UTC (rev 5157) @@ -6,11 +6,13 @@ public boolean nested_scopes = true; public boolean division; public boolean generator_allowed = true; - public boolean only_ast = false; - public boolean dont_imply_dedent = false; public boolean with_statement = false; public boolean absolute_import = false; + public boolean only_ast = false; + public boolean dont_imply_dedent = false; + public boolean source_is_utf8 = false; + public String encoding; public CompilerFlags(){} @@ -37,13 +39,18 @@ if ((co_flags & org.python.core.PyTableCode.PyCF_DONT_IMPLY_DEDENT) != 0) { this.dont_imply_dedent = true; } + if ((co_flags & org.python.core.PyTableCode.PyCF_SOURCE_IS_UTF8) != 0) { + this.source_is_utf8 = true; + } + } - + public String toString() { return String.format("CompilerFlags[division=%s nested_scopes=%s generators=%s " + "with_statement=%s absolute_import=%s only_ast=%s " - + "dont_imply_dedent=%s]", division, nested_scopes, generator_allowed, - with_statement, absolute_import, only_ast, dont_imply_dedent); + + "dont_imply_dedent=%s source_is_utf8=%s]", division, nested_scopes, + generator_allowed, with_statement, absolute_import, only_ast, + dont_imply_dedent, source_is_utf8); } } Modified: branches/asm/src/org/python/core/ParserFacade.java =================================================================== --- branches/asm/src/org/python/core/ParserFacade.java 2008-08-12 17:17:12 UTC (rev 5156) +++ branches/asm/src/org/python/core/ParserFacade.java 2008-08-12 18:11:38 UTC (rev 5157) @@ -199,13 +199,17 @@ InputStream bstream = new BufferedInputStream(istream); bom = adjustForBOM(bstream); encoding = readEncoding(bstream); - if(encoding == null) { + + if (encoding == null) { if (bom) { encoding = "UTF-8"; } else if (cflags != null && cflags.encoding != null) { encoding = cflags.encoding; } + } else if (cflags.source_is_utf8) { + throw new ParseException("encoding declaration in Unicode string"); } + // Enable universal newlines mode on the input StreamIO rawIO = new StreamIO(bstream, true); org.python.core.io.BufferedReader bufferedIO = Modified: branches/asm/src/org/python/core/PyTableCode.java =================================================================== --- branches/asm/src/org/python/core/PyTableCode.java 2008-08-12 17:17:12 UTC (rev 5156) +++ branches/asm/src/org/python/core/PyTableCode.java 2008-08-12 18:11:38 UTC (rev 5157) @@ -32,17 +32,19 @@ final public static int CO_GENERATOR = 0x0020; // these are defined in __future__.py - final public static int CO_NESTED = 0x0010; - final public static int CO_GENERATOR_ALLOWED = 0x0; - final public static int CO_FUTUREDIVISION = 0x2000; + final public static int CO_NESTED = 0x0010; + final public static int CO_GENERATOR_ALLOWED = 0x0; + final public static int CO_FUTUREDIVISION = 0x2000; final public static int CO_FUTURE_ABSOLUTE_IMPORT = 0x4000; - final public static int CO_WITH_STATEMENT = 0x8000; + final public static int CO_WITH_STATEMENT = 0x8000; //XXX: I'm not positive that this is the right place for these constants. + final public static int PyCF_SOURCE_IS_UTF8 = 0x0100; final public static int PyCF_DONT_IMPLY_DEDENT = 0x0200; final public static int PyCF_ONLY_AST = 0x0400; - final public static int CO_ALL_FEATURES = PyCF_DONT_IMPLY_DEDENT|PyCF_ONLY_AST|CO_NESTED| + final public static int CO_ALL_FEATURES = PyCF_DONT_IMPLY_DEDENT|PyCF_ONLY_AST| + PyCF_SOURCE_IS_UTF8|CO_NESTED| CO_GENERATOR_ALLOWED| CO_FUTUREDIVISION| CO_FUTURE_ABSOLUTE_IMPORT|CO_WITH_STATEMENT; Modified: branches/asm/src/org/python/core/__builtin__.java =================================================================== --- branches/asm/src/org/python/core/__builtin__.java 2008-08-12 17:17:12 UTC (rev 5156) +++ branches/asm/src/org/python/core/__builtin__.java 2008-08-12 18:11:38 UTC (rev 5157) @@ -276,6 +276,9 @@ dont_inherit = Py.py2boolean(args[4]); } + if (args[0] instanceof PyUnicode) { + flags += PyTableCode.PyCF_SOURCE_IS_UTF8; + } return __builtin__.compile(args[0].toString(), args[1].toString(), args[2].toString(), flags, dont_inherit); case 29: return __builtin__.map(args); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |