You can subscribe to this list here.
2000 |
Jan
|
Feb
|
Mar
|
Apr
|
May
|
Jun
|
Jul
|
Aug
|
Sep
|
Oct
(1) |
Nov
(107) |
Dec
(67) |
---|---|---|---|---|---|---|---|---|---|---|---|---|
2001 |
Jan
(76) |
Feb
(125) |
Mar
(72) |
Apr
(13) |
May
(18) |
Jun
(12) |
Jul
(129) |
Aug
(47) |
Sep
(1) |
Oct
(36) |
Nov
(128) |
Dec
(124) |
2002 |
Jan
(59) |
Feb
|
Mar
(14) |
Apr
(14) |
May
(72) |
Jun
(9) |
Jul
(3) |
Aug
(5) |
Sep
(18) |
Oct
(65) |
Nov
(28) |
Dec
(12) |
2003 |
Jan
(10) |
Feb
(2) |
Mar
(4) |
Apr
(33) |
May
(21) |
Jun
(9) |
Jul
(29) |
Aug
(34) |
Sep
(4) |
Oct
(8) |
Nov
(15) |
Dec
(4) |
2004 |
Jan
(26) |
Feb
(12) |
Mar
(11) |
Apr
(9) |
May
(7) |
Jun
|
Jul
(5) |
Aug
|
Sep
(3) |
Oct
(7) |
Nov
(1) |
Dec
(10) |
2005 |
Jan
(2) |
Feb
(72) |
Mar
(16) |
Apr
(39) |
May
(48) |
Jun
(97) |
Jul
(57) |
Aug
(13) |
Sep
(16) |
Oct
(24) |
Nov
(100) |
Dec
(24) |
2006 |
Jan
(15) |
Feb
(34) |
Mar
(33) |
Apr
(31) |
May
(79) |
Jun
(64) |
Jul
(41) |
Aug
(64) |
Sep
(31) |
Oct
(46) |
Nov
(55) |
Dec
(37) |
2007 |
Jan
(32) |
Feb
(61) |
Mar
(11) |
Apr
(58) |
May
(46) |
Jun
(30) |
Jul
(94) |
Aug
(93) |
Sep
(86) |
Oct
(69) |
Nov
(125) |
Dec
(177) |
2008 |
Jan
(169) |
Feb
(97) |
Mar
(74) |
Apr
(113) |
May
(120) |
Jun
(334) |
Jul
(215) |
Aug
(237) |
Sep
(72) |
Oct
(189) |
Nov
(126) |
Dec
(160) |
2009 |
Jan
(180) |
Feb
(45) |
Mar
(98) |
Apr
(140) |
May
(151) |
Jun
(71) |
Jul
(107) |
Aug
(119) |
Sep
(73) |
Oct
(121) |
Nov
(14) |
Dec
(6) |
2010 |
Jan
(13) |
Feb
(9) |
Mar
(10) |
Apr
(64) |
May
(3) |
Jun
(16) |
Jul
(7) |
Aug
(23) |
Sep
(17) |
Oct
(37) |
Nov
(5) |
Dec
(8) |
2011 |
Jan
(10) |
Feb
(11) |
Mar
(77) |
Apr
(11) |
May
(2) |
Jun
|
Jul
|
Aug
|
Sep
|
Oct
|
Nov
|
Dec
|
From: <fwi...@us...> - 2009-04-08 21:45:41
|
Revision: 6192 http://jython.svn.sourceforge.net/jython/?rev=6192&view=rev Author: fwierzbicki Date: 2009-04-08 21:45:35 +0000 (Wed, 08 Apr 2009) Log Message: ----------- Added a Comparator implementation for sorting. Removed Comparable interface from PyObject since it wasn't there before and I don't appear to need it yet. Modified Paths: -------------- branches/newlist/src/org/python/core/PyList.java branches/newlist/src/org/python/core/PyObject.java Added Paths: ----------- branches/newlist/src/org/python/core/PyComparator.java Added: branches/newlist/src/org/python/core/PyComparator.java =================================================================== --- branches/newlist/src/org/python/core/PyComparator.java (rev 0) +++ branches/newlist/src/org/python/core/PyComparator.java 2009-04-08 21:45:35 UTC (rev 6192) @@ -0,0 +1,48 @@ +package org.python.core; + +import java.util.Comparator; + +public class PyComparator implements Comparator<PyObject> { + + protected PyObject cmp = null; + protected PyObject key = null; + protected boolean reverse = false; + + PyComparator(PyObject cmp, PyObject key, boolean reverse) { + this.cmp = cmp; + this.key = key; + this.reverse = reverse; + } + + // First cut at an implementation. FIXME: In CPython key is supposed to + // make things fast, accessing each element once. For this first cut I am + // cheating and calling the key function on every pass to get something + // that works right away. + public int compare(PyObject o1, PyObject o2) { + int result; + if (key != null && key != Py.None) { + o1 = key.__call__(o1); + o2 = key.__call__(o2); + } + if (cmp != null && cmp != Py.None) { + result = cmp.__call__(o1, o2).asInt(); + } else { + result = o1._cmp(o2); + } + if (reverse) { + return -result; + } + return result; + } + + public boolean equals(Object o) { + if(o == this) { + return true; + } + + if (o instanceof PyComparator) { + return key.equals(((PyComparator)o).key) && cmp.equals(((PyComparator)o).cmp); + } + return false; + } +} Modified: branches/newlist/src/org/python/core/PyList.java =================================================================== --- branches/newlist/src/org/python/core/PyList.java 2009-04-08 19:50:54 UTC (rev 6191) +++ branches/newlist/src/org/python/core/PyList.java 2009-04-08 21:45:35 UTC (rev 6192) @@ -684,7 +684,8 @@ //System.out.println("sorting with " + cmp + ":" + key + ":" + reverse); //MergeState ms = new MergeState(this, cmp, key, reverse.__nonzero__()); //ms.sort(); - Collections.sort(list); + PyComparator c = new PyComparator(cmp, key, reverse.__nonzero__()); + Collections.sort(list, c); } public int hashCode() { Modified: branches/newlist/src/org/python/core/PyObject.java =================================================================== --- branches/newlist/src/org/python/core/PyObject.java 2009-04-08 19:50:54 UTC (rev 6191) +++ branches/newlist/src/org/python/core/PyObject.java 2009-04-08 21:45:35 UTC (rev 6192) @@ -20,7 +20,7 @@ * of the class <code>PyObject</code> or one of its subclasses. */ @ExposedType(name = "object") -public class PyObject implements Comparable<PyObject>, Serializable { +public class PyObject implements Serializable { public static final PyType TYPE = PyType.fromClass(PyObject.class); @@ -1284,10 +1284,6 @@ } } - public int compareTo(PyObject o) { - return this._cmp(o); - } - private PyObject make_pair(PyObject o) { if (System.identityHashCode(this) < System.identityHashCode(o)) return new PyIdentityTuple(new PyObject[] { this, o }); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2009-04-08 19:51:05
|
Revision: 6191 http://jython.svn.sourceforge.net/jython/?rev=6191&view=rev Author: fwierzbicki Date: 2009-04-08 19:50:54 +0000 (Wed, 08 Apr 2009) Log Message: ----------- PyList is now former PyNewList, PyObject now implements Comparable<PyObject> and sort() method in PyList now uses Collections.sort, but does not yet handle cmp or key args. Modified Paths: -------------- branches/newlist/src/org/python/core/PyList.java branches/newlist/src/org/python/core/PyObject.java Removed Paths: ------------- branches/newlist/src/org/python/core/PyNewList.java Modified: branches/newlist/src/org/python/core/PyList.java =================================================================== --- branches/newlist/src/org/python/core/PyList.java 2009-04-08 06:48:21 UTC (rev 6190) +++ branches/newlist/src/org/python/core/PyList.java 2009-04-08 19:50:54 UTC (rev 6191) @@ -1,38 +1,58 @@ -// Copyright (c) Corporation for National Research Initiatives + // Copyright (c) Corporation for National Research Initiatives package org.python.core; -import java.util.Collection; -import java.util.Iterator; -import java.util.List; - +import java.util.ArrayList; +import java.util.Arrays; import org.python.expose.ExposedMethod; import org.python.expose.ExposedNew; import org.python.expose.ExposedType; import org.python.expose.MethodType; import org.python.util.Generic; -/** - * A builtin python list. - */ +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; + @ExposedType(name = "list", base = PyObject.class) -public class PyList extends PySequenceObjectList { +public class PyList extends PySequenceList implements List { public static final PyType TYPE = PyType.fromClass(PyList.class); + protected final List<PyObject> list; public PyList() { - this(TYPE, Py.EmptyObjects); + this(TYPE); } public PyList(PyType type) { super(type); + list = Generic.list(); } + public PyList(List list, boolean convert) { + super(TYPE); + if (!convert) { + this.list = list; + } else { + this.list = Generic.list(); + for (Object o : list) { + add(o); + } + } + } + public PyList(PyType type, PyObject[] elements) { - super(type, elements); + super(type); + list = new ArrayList<PyObject>(Arrays.asList(elements)); } public PyList(PyType type, Collection c) { - super(type, c); + super(type); + list = new ArrayList<PyObject>(c.size()); + for (Object o : c) { + add(o); + } } public PyList(PyObject[] elements) { @@ -40,23 +60,28 @@ } public PyList(Collection c) { - super(TYPE, c); + this(TYPE, c); } public PyList(PyObject o) { this(TYPE); for (PyObject item : o.asIterable()) { - append(item); + list.add(item); } } + public PyList fromList(List list) { + return new PyList(list, false); + } + private static List<PyObject> listify(Iterator<PyObject> iter) { - List<PyObject> list = Generic.list(); - while (iter.hasNext()) { + List<PyObject> list = Generic.list(); + while (iter.hasNext()) { list.add(iter.next()); - } - return list; + } + return list; } + public PyList(Iterator<PyObject> iter) { this(TYPE, listify(iter)); } @@ -64,15 +89,14 @@ @ExposedNew @ExposedMethod(doc = BuiltinDocs.list___init___doc) final void list___init__(PyObject[] args, String[] kwds) { - ArgParser ap = new ArgParser("list", args, kwds, new String[] {"sequence"}, 0); + ArgParser ap = new ArgParser("list", args, kwds, new String[]{"sequence"}, 0); PyObject seq = ap.getPyObject(0, null); clear(); - if(seq == null) { + if (seq == null) { return; } - if(seq instanceof PySequenceObjectList) { - PySequenceObjectList p = (PySequenceObjectList)seq.__getslice__(Py.None, Py.None, Py.One); - this.list = p.list; + if (seq instanceof PyList) { + list.addAll((PyList) seq); // don't convert } else { for (PyObject item : seq.asIterable()) { append(item); @@ -80,6 +104,7 @@ } } + @Override public int __len__() { return list___len__(); } @@ -89,105 +114,59 @@ return size(); } - protected PyObject getslice(int start, int stop, int step) { - if(step > 0 && stop < start) { - stop = start; - } - int n = sliceLength(start, stop, step); - PyObject[] newList = new PyObject[n]; - PyObject[] array = getArray(); - if(step == 1) { - System.arraycopy(array, start, newList, 0, stop - start); - return new PyList(newList); - } - int j = 0; - for(int i = start; j < n; i += step) { - newList[j] = array[i]; - j++; - } - return new PyList(newList); - } - + @Override protected void del(int i) { remove(i); } + @Override protected void delRange(int start, int stop) { remove(start, stop); } + @Override protected void setslice(int start, int stop, int step, PyObject value) { - if(stop < start) { + if (stop < start) { stop = start; } - if(value instanceof PySequence) { - PySequence sequence = (PySequence) value; - setslicePySequence(start, stop, step, sequence); - } else if(value instanceof List) { - List list = (List)value.__tojava__(List.class); - if(list != null && list != Py.NoConversion) { - setsliceList(start, stop, step, list); + if ((value instanceof PySequence) || (!(value instanceof List))) { + if (value == this) { // copy + value = new PyList((PySequence) value); } + setsliceIterator(start, stop, step, value.asIterable().iterator()); } else { - setsliceIterable(start, stop, step, value); - } - } - - protected void setslicePySequence(int start, int stop, int step, PySequence value) { - if(step == 1) { - PyObject[] otherArray; - PyObject[] array = getArray(); - - int n = value.__len__(); - if (value instanceof PySequenceList) { - otherArray = ((PySequenceList)value).getArray(); - } else { - otherArray = Py.unpackSequence(value, value.__len__()); + System.err.println("List"); + List valueList = (List) value.__tojava__(List.class); + if (valueList != null && valueList != Py.NoConversion) { + setsliceList(start, stop, step, valueList); } - if (otherArray == array) { - otherArray = otherArray.clone(); - } - list.replaceSubArray(start, stop, otherArray, 0, n); - } else if(step != 0) { - if(value == this) { - PyList newseq = new PyList(); - PyObject iter = value.__iter__(); - for(PyObject item = null; (item = iter.__iternext__()) != null;) { - newseq.append(item); - } - value = newseq; - } - int n = value.__len__(); - for (int i = 0, j = start; i < n; i++, j += step) { - list.pyset(j, value.pyget(i)); - } } } protected void setsliceList(int start, int stop, int step, List value) { - if(step != 1) { - throw Py.TypeError("setslice with java.util.List and step != 1 not supported yet"); + int n = sliceLength(start, stop, step); + if (list instanceof ArrayList) { + ((ArrayList) list).ensureCapacity(start + n); } - int n = value.size(); - list.ensureCapacity(start + n); - for(int i = 0; i < n; i++) { - list.add(i + start, value.get(i)); + ListIterator src = value.listIterator(); + for (int j = start; src.hasNext(); j += step) { + set(j, src.next()); } } - protected void setsliceIterable(int start, int stop, int step, PyObject value) { - PyObject[] seq; - try { - seq = Py.make_array(value); - } catch (PyException pye) { - if (Py.matchException(pye, Py.TypeError)) { - throw Py.TypeError("can only assign an iterable"); + protected void setsliceIterator(int start, int stop, int step, Iterator<PyObject> iter) { + int size = list.size(); + for (int j = start; iter.hasNext(); j += step) { + PyObject item = iter.next(); + if (j >= size) { + list.add(item); + } else { + list.set(j, item); } - throw pye; } - setslicePySequence(start, stop, step, new PyList(seq)); } + @Override protected PyObject repeat(int count) { if (count < 0) { count = 0; @@ -198,12 +177,11 @@ throw Py.MemoryError(""); } - PyObject[] array = getArray(); - PyObject[] newArray = new PyObject[newSize]; - for(int i = 0; i < count; i++) { - System.arraycopy(array, 0, newArray, i * size, size); + PyList newList = new PyList(); + for (int i = 0; i < count; i++) { + newList.addAll(this); } - return new PyList(newArray); + return newList; } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___ne___doc) @@ -236,6 +214,7 @@ return seq___ge__(o); } + @Override public PyObject __imul__(PyObject o) { return list___imul__(o); } @@ -262,12 +241,14 @@ } int newSize = size * count; - list.setSize(newSize); - PyObject[] array = getArray(); + if (list instanceof ArrayList) { + ((ArrayList) list).ensureCapacity(newSize); + } + List oldList = new ArrayList<PyObject>(list); for (int i = 1; i < count; i++) { - System.arraycopy(array, 0, array, i * size, size); + list.addAll(oldList); } - gListAllocatedStatus = __len__(); + gListAllocatedStatus = __len__(); // now omit? return this; } @@ -297,6 +278,7 @@ return repeat(o.asIndex(Py.OverflowError)); } + @Override public PyObject __add__(PyObject o) { return list___add__(o); } @@ -304,22 +286,22 @@ @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___add___doc) final PyObject list___add__(PyObject o) { PyList sum = null; - if(o instanceof PyList) { - PyList other = (PyList)o; - int thisLen = size(); - int otherLen = other.size(); - PyObject[] newList = new PyObject[thisLen + otherLen]; - System.arraycopy(getArray(), 0, newList, 0, thisLen); - System.arraycopy(other.getArray(), 0, newList, thisLen, otherLen); - sum = new PyList(newList); - } else if(!(o instanceof PySequenceList)) { + if (o instanceof PySequenceList && !(o instanceof PyTuple)) { + if (o instanceof PyList) { + List oList = ((PyList) o).list; + List newList = new ArrayList(list.size() + oList.size()); + newList.addAll(list); + newList.addAll(oList); + sum = fromList(newList); + } + } else if (!(o instanceof PySequenceList)) { // also support adding java lists (but not PyTuple!) Object oList = o.__tojava__(List.class); - if(oList != Py.NoConversion && oList != null) { + if (oList != Py.NoConversion && oList != null) { List otherList = (List) oList; sum = new PyList(); sum.list_extend(this); - for(Iterator i = otherList.iterator(); i.hasNext();) { + for (Iterator i = otherList.iterator(); i.hasNext();) { sum.add(i.next()); } } @@ -327,6 +309,7 @@ return sum; } + @Override public PyObject __radd__(PyObject o) { return list___radd__(o); } @@ -335,9 +318,9 @@ @ExposedMethod(type = MethodType.BINARY) final PyObject list___radd__(PyObject o) { // Support adding java.util.List, but prevent adding PyTuple. - // 'o' should never be a PyList since __add__ is defined. + // 'o' should never be a PyNewList since __add__ is defined. PyList sum = null; - if(o instanceof PySequence) { + if (o instanceof PySequence) { return null; } Object oList = o.__tojava__(List.class); @@ -367,12 +350,13 @@ @ExposedMethod(doc = BuiltinDocs.list___getitem___doc) final PyObject list___getitem__(PyObject o) { PyObject ret = seq___finditem__(o); - if(ret == null) { + if (ret == null) { throw Py.IndexError("index out of range: " + o); } return ret; } + @Override public PyObject __iter__() { return list___iter__(); } @@ -397,8 +381,9 @@ seq___delslice__(start, stop, step); } + @Override protected String unsupportedopMessage(String op, PyObject o2) { - if(op.equals("+")) { + if (op.equals("+")) { return "can only concatenate list (not \"{2}\") to list"; } return super.unsupportedopMessage(op, o2); @@ -412,19 +397,19 @@ @ExposedMethod(names = "__repr__") final String list_toString() { ThreadState ts = Py.getThreadState(); - if(!ts.enterRepr(this)) { + if (!ts.enterRepr(this)) { return "[...]"; } StringBuilder buf = new StringBuilder("["); int length = size(); - PyObject[] array = getArray(); - for(int i = 0; i < length - 1; i++) { - buf.append((array[i]).__repr__().toString()); - buf.append(", "); + int i = 0; + for (PyObject item : list) { + buf.append(item.__repr__().toString()); + if (i < length - 1) { + buf.append(", "); + } + i++; } - if(length > 0) { - buf.append((array[length - 1]).__repr__().toString()); - } buf.append("]"); ts.exitRepr(this); return buf.toString(); @@ -460,8 +445,8 @@ final int list_count(PyObject o) { int count = 0; PyObject[] array = getArray(); - for(int i = 0, n = size(); i < n; i++) { - if(array[i].equals(o)) { + for (int i = 0, n = size(); i < n; i++) { + if (array[i].equals(o)) { count++; } } @@ -510,8 +495,8 @@ int validStop = boundToSequence(stop); int validStart = boundToSequence(start); PyObject[] array = getArray(); - for(int i = validStart; i < validStop && i < size(); i++) { - if(array[i].equals(o)) { + for (int i = validStart; i < validStop && i < size(); i++) { + if (array[i].equals(o)) { return i; } } @@ -533,13 +518,13 @@ @ExposedMethod(doc = BuiltinDocs.list_insert_doc) final void list_insert(int index, PyObject o) { - if(index < 0) { + if (index < 0) { index = Math.max(0, size() + index); } - if(index > size()) { + if (index > size()) { index = size(); } - list.pyadd(index, o); + pyadd(index, o); gListAllocatedStatus = __len__(); } @@ -572,15 +557,7 @@ @ExposedMethod(doc = BuiltinDocs.list_reverse_doc) final void list_reverse() { - PyObject tmp; - int n = size(); - PyObject[] array = getArray(); - int j = n - 1; - for(int i = 0; i < n / 2; i++, j--) { - tmp = array[i]; - array[i] = array[j]; - array[j] = tmp; - } + Collections.reverse(list); gListAllocatedStatus = __len__(); } @@ -604,17 +581,16 @@ @ExposedMethod(defaults = "-1", doc = BuiltinDocs.list_pop_doc) final PyObject list_pop(int n) { int length = size(); - if(length == 0) { + if (length == 0) { throw Py.IndexError("pop from empty list"); } - if(n < 0) { + if (n < 0) { n += length; } - if(n < 0 || n >= length) { + if (n < 0 || n >= length) { throw Py.IndexError("pop index out of range"); } - PyObject v = pyget(n); - setslice(n, n + 1, 1, Py.EmptyTuple); + PyObject v = list.remove(n); return v; } @@ -631,11 +607,22 @@ @ExposedMethod(doc = BuiltinDocs.list_extend_doc) final void list_extend(PyObject o) { - int length = size(); - setslice(length, length, 1, o); + if (o instanceof PyList) { + list.addAll(((PyList) o).list); + } else if (o instanceof PySequenceObjectList) { + PyObject other[] = ((PySequenceObjectList) o).getArray(); + for (int i = 0; i < other.length; i++) { + list.add(other[i]); + } + } else { + for (PyObject item : o.asIterable()) { + list.add(item); + } + } gListAllocatedStatus = __len__(); } + @Override public PyObject __iadd__(PyObject o) { return list___iadd__(o); } @@ -672,13 +659,10 @@ * @param compare * the comparison function. */ - - /** + /** * Sort the items of the list in place. Items is compared with the normal relative comparison * operators. */ - - @ExposedMethod(doc = BuiltinDocs.list_sort_doc) final void list_sort(PyObject[] args, String[] kwds) { ArgParser ap = new ArgParser("list", args, kwds, new String[]{"cmp", "key", "reverse"}, 0); @@ -697,8 +681,10 @@ } public void sort(PyObject cmp, PyObject key, PyObject reverse) { - MergeState ms = new MergeState(this, cmp, key, reverse.__nonzero__()); - ms.sort(); + //System.out.println("sorting with " + cmp + ":" + key + ":" + reverse); + //MergeState ms = new MergeState(this, cmp, key, reverse.__nonzero__()); + //ms.sort(); + Collections.sort(list); } public int hashCode() { @@ -710,7 +696,210 @@ throw Py.TypeError(String.format("unhashable type: '%.200s'", getType().fastGetName())); } + @Override public PyTuple __getnewargs__() { - return new PyTuple(new PyTuple(list.getArray())); + return new PyTuple(new PyTuple(getArray())); } + + @Override + public void add(int index, Object element) { + pyadd(index, Py.java2py(element)); + } + + @Override + public boolean add(Object o) { + pyadd(Py.java2py(o)); + return true; + } + + @Override + public boolean addAll(int index, Collection c) { + if (c instanceof PySequenceList) { + list.addAll(index, c); + } else { + // need to use add to convert anything pulled from a collection into a PyObject + for (Object element : c) { + add(element); + } + } + return c.size() > 0; + } + + @Override + public boolean addAll(Collection c) { + if (c instanceof PySequenceList) { + list.addAll(c); + } else { + // need to use add to convert anything pulled from a collection into a PyObject + for (Object element : c) { + add(element); + } + } + return c.size() > 0; + } + + @Override + public void clear() { + list.clear(); + } + + @Override + public boolean contains(Object o) { + return list.contains(Py.java2py(o)); + } + + @Override + public boolean containsAll(Collection c) { + if (c instanceof PySequenceList) { + return list.containsAll(c); + } else { + //XXX: FJW this seems unnecessary. + return list.containsAll(new PyList(c)); + } + } + + @Override + public boolean equals(Object o) { + if (o instanceof PyList) { + return (((PyList) o).list.equals(list)); + } + return false; + } + + @Override + public Object get(int index) { + return list.get(index).__tojava__(Object.class); + } + + /** @deprecated */ + @Override + public PyObject[] getArray() { + PyObject a[] = new PyObject[list.size()]; + return list.toArray(a); + } + + @Override + public int indexOf(Object o) { + return list.indexOf(o); + } + + @Override + public boolean isEmpty() { + return list.isEmpty(); + } + + @Override + public Iterator iterator() { + return list.iterator(); + } + + @Override + public int lastIndexOf(Object o) { + return list.lastIndexOf(o); + } + + @Override + public ListIterator listIterator() { + return list.listIterator(); + } + + @Override + public ListIterator listIterator(int index) { + return list.listIterator(index); + } + + @Override + public void pyadd(int index, PyObject element) { + list.add(index, element); + } + + @Override + public boolean pyadd(PyObject o) { + list.add(o); + return true; + } + + @Override + public PyObject pyget(int index) { + return list.get(index); + } + + public void pyset(int index, PyObject element) { + list.set(index, element); + } + + @Override + public Object remove(int index) { + return list.remove(index); + } + + @Override + public void remove(int start, int stop) { + list.subList(start, stop).clear(); + } + + @Override + public boolean removeAll(Collection c) { + if (c instanceof PySequenceList) { + return list.removeAll(c); + } else { + return list.removeAll(new PyList(c)); + } + } + + @Override + public boolean retainAll(Collection c) { + if (c instanceof PySequenceList) { + return list.retainAll(c); + } else { + return list.retainAll(new PyList(c)); + } + } + + @Override + public Object set(int index, Object element) { + return list.set(index, Py.java2py(element)).__tojava__(Object.class); + } + + @Override + public int size() { + return list.size(); + } + + @Override + public List subList(int fromIndex, int toIndex) { + return list.subList(fromIndex, toIndex); + } + + @Override + public Object[] toArray() { + return list.toArray(); + } + + @Override + public Object[] toArray(Object[] a) { + return list.toArray(a); + } + + protected PyObject getslice(int start, int stop, int step) { + if (step > 0 && stop < start) { + stop = start; + } + int n = sliceLength(start, stop, step); + List newList; + if (step == 1) { + newList = new ArrayList<PyObject>(list.subList(start, stop)); + } else { + newList = new ArrayList<PyObject>(n); + for (int i = start, j = 0; j < n; i += step, j++) { + newList.add(list.get(i)); + } + } + return fromList(newList); + } + + @Override + public boolean remove(Object o) { + return list.remove(Py.java2py(o)); + } } Deleted: branches/newlist/src/org/python/core/PyNewList.java =================================================================== --- branches/newlist/src/org/python/core/PyNewList.java 2009-04-08 06:48:21 UTC (rev 6190) +++ branches/newlist/src/org/python/core/PyNewList.java 2009-04-08 19:50:54 UTC (rev 6191) @@ -1,902 +0,0 @@ - // Copyright (c) Corporation for National Research Initiatives -package org.python.core; - -import java.util.ArrayList; -import java.util.Arrays; -import org.python.expose.ExposedMethod; -import org.python.expose.ExposedNew; -import org.python.expose.ExposedType; -import org.python.expose.MethodType; -import org.python.util.Generic; - -import java.util.Collection; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; -import java.util.ListIterator; - -@ExposedType(name = "newlist", base = PyObject.class) -public class PyNewList extends PySequenceList implements List { - - public static final PyType TYPE = PyType.fromClass(PyNewList.class); - protected final List<PyObject> list; - - public PyNewList() { - this(TYPE); - } - - public PyNewList(PyType type) { - super(type); - list = Generic.list(); - } - - public PyNewList(List list, boolean convert) { - super(TYPE); - if (!convert) { - this.list = list; - } else { - this.list = Generic.list(); - for (Object o : list) { - add(o); - } - } - } - - public PyNewList(PyType type, PyObject[] elements) { - super(type); - list = new ArrayList<PyObject>(Arrays.asList(elements)); - } - - public PyNewList(PyType type, Collection c) { - super(type); - list = new ArrayList<PyObject>(c.size()); - for (Object o : c) { - add(o); - } - } - - public PyNewList(PyObject[] elements) { - this(TYPE, elements); - } - - public PyNewList(Collection c) { - this(TYPE, c); - } - - public PyNewList(PyObject o) { - this(TYPE); - for (PyObject item : o.asIterable()) { - list.add(item); - } - } - - public PyNewList fromList(List list) { - return new PyNewList(list, false); - } - - private static List<PyObject> listify(Iterator<PyObject> iter) { - List<PyObject> list = Generic.list(); - while (iter.hasNext()) { - list.add(iter.next()); - } - return list; - } - - public PyNewList(Iterator<PyObject> iter) { - this(TYPE, listify(iter)); - } - - @ExposedNew - @ExposedMethod(doc = BuiltinDocs.list___init___doc) - final void newlist___init__(PyObject[] args, String[] kwds) { - ArgParser ap = new ArgParser("newlist", args, kwds, new String[]{"sequence"}, 0); - PyObject seq = ap.getPyObject(0, null); - clear(); - if (seq == null) { - return; - } - if (seq instanceof PyNewList) { - list.addAll((PyNewList) seq); // don't convert - } else { - for (PyObject item : seq.asIterable()) { - append(item); - } - } - } - - @Override - public int __len__() { - return newlist___len__(); - } - - @ExposedMethod(doc = BuiltinDocs.list___len___doc) - final int newlist___len__() { - return size(); - } - - @Override - protected void del(int i) { - remove(i); - } - - @Override - protected void delRange(int start, int stop) { - remove(start, stop); - } - - @Override - protected void setslice(int start, int stop, int step, PyObject value) { - if (stop < start) { - stop = start; - } - if ((value instanceof PySequence) || (!(value instanceof List))) { - if (value == this) { // copy - value = new PyNewList((PySequence) value); - } - setsliceIterator(start, stop, step, value.asIterable().iterator()); - } else { - System.err.println("List"); - List valueList = (List) value.__tojava__(List.class); - if (valueList != null && valueList != Py.NoConversion) { - setsliceList(start, stop, step, valueList); - } - } - } - - protected void setsliceList(int start, int stop, int step, List value) { - int n = sliceLength(start, stop, step); - if (list instanceof ArrayList) { - ((ArrayList) list).ensureCapacity(start + n); - } - ListIterator src = value.listIterator(); - for (int j = start; src.hasNext(); j += step) { - set(j, src.next()); - } - } - - protected void setsliceIterator(int start, int stop, int step, Iterator<PyObject> iter) { - int size = list.size(); - for (int j = start; iter.hasNext(); j += step) { - PyObject item = iter.next(); - if (j >= size) { - list.add(item); - } else { - list.set(j, item); - } - } - } - - @Override - protected PyObject repeat(int count) { - if (count < 0) { - count = 0; - } - int size = size(); - int newSize = size * count; - if (count != 0 && newSize / count != size) { - throw Py.MemoryError(""); - } - - PyNewList newList = new PyNewList(); - for (int i = 0; i < count; i++) { - newList.addAll(this); - } - return newList; - } - - @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___ne___doc) - final PyObject newlist___ne__(PyObject o) { - return seq___ne__(o); - } - - @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___eq___doc) - final PyObject newlist___eq__(PyObject o) { - return seq___eq__(o); - } - - @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___lt___doc) - final PyObject newlist___lt__(PyObject o) { - return seq___lt__(o); - } - - @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___le___doc) - final PyObject newlist___le__(PyObject o) { - return seq___le__(o); - } - - @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___gt___doc) - final PyObject newlist___gt__(PyObject o) { - return seq___gt__(o); - } - - @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___ge___doc) - final PyObject newlist___ge__(PyObject o) { - return seq___ge__(o); - } - - @Override - public PyObject __imul__(PyObject o) { - return newlist___imul__(o); - } - - @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___imul___doc) - final PyObject newlist___imul__(PyObject o) { - if (!o.isIndex()) { - return null; - } - int count = o.asIndex(Py.OverflowError); - - int size = size(); - if (size == 0 || count == 1) { - return this; - } - - if (count < 1) { - clear(); - return this; - } - - if (size > Integer.MAX_VALUE / count) { - throw Py.MemoryError(""); - } - - int newSize = size * count; - if (list instanceof ArrayList) { - ((ArrayList) list).ensureCapacity(newSize); - } - List oldList = new ArrayList<PyObject>(list); - for (int i = 1; i < count; i++) { - list.addAll(oldList); - } - gListAllocatedStatus = __len__(); // now omit? - return this; - } - - @Override - public PyObject __mul__(PyObject o) { - return newlist___mul__(o); - } - - @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___mul___doc) - final PyObject newlist___mul__(PyObject o) { - if (!o.isIndex()) { - return null; - } - return repeat(o.asIndex(Py.OverflowError)); - } - - @Override - public PyObject __rmul__(PyObject o) { - return newlist___rmul__(o); - } - - @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___rmul___doc) - final PyObject newlist___rmul__(PyObject o) { - if (!o.isIndex()) { - return null; - } - return repeat(o.asIndex(Py.OverflowError)); - } - - @Override - public PyObject __add__(PyObject o) { - return newlist___add__(o); - } - - @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___add___doc) - final PyObject newlist___add__(PyObject o) { - PyNewList sum = null; - if (o instanceof PySequenceList && !(o instanceof PyTuple)) { - if (o instanceof PyNewList) { - List oList = ((PyNewList) o).list; - List newList = new ArrayList(list.size() + oList.size()); - newList.addAll(list); - newList.addAll(oList); - sum = fromList(newList); - } - } else if (!(o instanceof PySequenceList)) { - // also support adding java lists (but not PyTuple!) - Object oList = o.__tojava__(List.class); - if (oList != Py.NoConversion && oList != null) { - List otherList = (List) oList; - sum = new PyNewList(); - sum.newlist_extend(this); - for (Iterator i = otherList.iterator(); i.hasNext();) { - sum.add(i.next()); - } - } - } - return sum; - } - - @Override - public PyObject __radd__(PyObject o) { - return newlist___radd__(o); - } - - //XXX: needs __doc__ - @ExposedMethod(type = MethodType.BINARY) - final PyObject newlist___radd__(PyObject o) { - // Support adding java.util.List, but prevent adding PyTuple. - // 'o' should never be a PyNewList since __add__ is defined. - PyNewList sum = null; - if (o instanceof PySequence) { - return null; - } - Object oList = o.__tojava__(List.class); - if (oList != Py.NoConversion && oList != null) { - sum = new PyNewList(); - sum.addAll((List) oList); - sum.extend(this); - } - return sum; - } - - @ExposedMethod(doc = BuiltinDocs.list___contains___doc) - final boolean newlist___contains__(PyObject o) { - return object___contains__(o); - } - - @ExposedMethod(doc = BuiltinDocs.list___delitem___doc) - final void newlist___delitem__(PyObject index) { - seq___delitem__(index); - } - - @ExposedMethod(doc = BuiltinDocs.list___setitem___doc) - final void newlist___setitem__(PyObject o, PyObject def) { - seq___setitem__(o, def); - } - - @ExposedMethod(doc = BuiltinDocs.list___getitem___doc) - final PyObject newlist___getitem__(PyObject o) { - PyObject ret = seq___finditem__(o); - if (ret == null) { - throw Py.IndexError("index out of range: " + o); - } - return ret; - } - - @Override - public PyObject __iter__() { - return newlist___iter__(); - } - - @ExposedMethod(doc = BuiltinDocs.list___iter___doc) - public PyObject newlist___iter__() { - return new PyFastSequenceIter(this); - } - - @ExposedMethod(defaults = "null", doc = BuiltinDocs.list___getslice___doc) - final PyObject newlist___getslice__(PyObject start, PyObject stop, PyObject step) { - return seq___getslice__(start, stop, step); - } - - @ExposedMethod(defaults = "null", doc = BuiltinDocs.list___setslice___doc) - final void newlist___setslice__(PyObject start, PyObject stop, PyObject step, PyObject value) { - seq___setslice__(start, stop, step, value); - } - - @ExposedMethod(defaults = "null", doc = BuiltinDocs.list___delslice___doc) - final void newlist___delslice__(PyObject start, PyObject stop, PyObject step) { - seq___delslice__(start, stop, step); - } - - @Override - protected String unsupportedopMessage(String op, PyObject o2) { - if (op.equals("+")) { - return "can only concatenate list (not \"{2}\") to list"; - } - return super.unsupportedopMessage(op, o2); - } - - public String toString() { - return newlist_toString(); - } - - //XXX: needs __doc__ - @ExposedMethod(names = "__repr__") - final String newlist_toString() { - ThreadState ts = Py.getThreadState(); - if (!ts.enterRepr(this)) { - return "[...]"; - } - StringBuilder buf = new StringBuilder("["); - int length = size(); - int i = 0; - for (PyObject item : list) { - buf.append(item.__repr__().toString()); - if (i < length - 1) { - buf.append(", "); - } - i++; - } - buf.append("]"); - ts.exitRepr(this); - return buf.toString(); - } - - /** - * Add a single element to the end of list. - * - * @param o - * the element to add. - */ - public void append(PyObject o) { - newlist_append(o); - } - - @ExposedMethod(doc = BuiltinDocs.list_append_doc) - final void newlist_append(PyObject o) { - pyadd(o); - gListAllocatedStatus = __len__(); - } - - /** - * Return the number elements in the list that equals the argument. - * - * @param o - * the argument to test for. Testing is done with the <code>==</code> operator. - */ - public int count(PyObject o) { - return newlist_count(o); - } - - @ExposedMethod(doc = BuiltinDocs.list_count_doc) - final int newlist_count(PyObject o) { - int count = 0; - PyObject[] array = getArray(); - for (int i = 0, n = size(); i < n; i++) { - if (array[i].equals(o)) { - count++; - } - } - return count; - } - - /** - * return smallest index where an element in the list equals the argument. - * - * @param o - * the argument to test for. Testing is done with the <code>==</code> operator. - */ - public int index(PyObject o) { - return index(o, 0); - } - - public int index(PyObject o, int start) { - return newlist_index(o, start, size()); - } - - public int index(PyObject o, int start, int stop) { - return newlist_index(o, start, stop); - } - - @ExposedMethod(defaults = {"null", "null"}, doc = BuiltinDocs.list_index_doc) - final int newlist_index(PyObject o, PyObject start, PyObject stop) { - int startInt = start == null ? 0 : PySlice.calculateSliceIndex(start); - int stopInt = stop == null ? size() : PySlice.calculateSliceIndex(stop); - return newlist_index(o, startInt, stopInt); - } - - final int newlist_index(PyObject o, int start, int stop) { - return _index(o, "list.index(x): x not in list", start, stop); - } - - final int newlist_index(PyObject o, int start) { - return _index(o, "list.index(x): x not in list", start, size()); - } - - final int newlist_index(PyObject o) { - return _index(o, "list.index(x): x not in list", 0, size()); - } - - private int _index(PyObject o, String message, int start, int stop) { - // Follow Python 2.3+ behavior - int validStop = boundToSequence(stop); - int validStart = boundToSequence(start); - PyObject[] array = getArray(); - for (int i = validStart; i < validStop && i < size(); i++) { - if (array[i].equals(o)) { - return i; - } - } - throw Py.ValueError(message); - } - - /** - * Insert the argument element into the list at the specified index. <br> - * Same as <code>s[index:index] = [o] if index >= 0</code>. - * - * @param index - * the position where the element will be inserted. - * @param o - * the element to insert. - */ - public void insert(int index, PyObject o) { - newlist_insert(index, o); - } - - @ExposedMethod(doc = BuiltinDocs.list_insert_doc) - final void newlist_insert(int index, PyObject o) { - if (index < 0) { - index = Math.max(0, size() + index); - } - if (index > size()) { - index = size(); - } - pyadd(index, o); - gListAllocatedStatus = __len__(); - } - - /** - * Remove the first occurence of the argument from the list. The elements arecompared with the - * <code>==</code> operator. <br> - * Same as <code>del s[s.index(x)]</code> - * - * @param o - * the element to search for and remove. - */ - public void remove(PyObject o) { - newlist_remove(o); - } - - @ExposedMethod(doc = BuiltinDocs.list_remove_doc) - final void newlist_remove(PyObject o) { - del(_index(o, "list.remove(x): x not in list", 0, size())); - gListAllocatedStatus = __len__(); - } - - /** - * Reverses the items of s in place. The reverse() methods modify the list in place for economy - * of space when reversing a large list. It doesn't return the reversed list to remind you of - * this side effect. - */ - public void reverse() { - newlist_reverse(); - } - - @ExposedMethod(doc = BuiltinDocs.list_reverse_doc) - final void newlist_reverse() { - Collections.reverse(list); - gListAllocatedStatus = __len__(); - } - - /** - * Removes and return the last element in the list. - */ - public PyObject pop() { - return pop(-1); - } - - /** - * Removes and return the <code>n</code> indexed element in the list. - * - * @param n - * the index of the element to remove and return. - */ - public PyObject pop(int n) { - return newlist_pop(n); - } - - @ExposedMethod(defaults = "-1", doc = BuiltinDocs.list_pop_doc) - final PyObject newlist_pop(int n) { - int length = size(); - if (length == 0) { - throw Py.IndexError("pop from empty list"); - } - if (n < 0) { - n += length; - } - if (n < 0 || n >= length) { - throw Py.IndexError("pop index out of range"); - } - PyObject v = list.remove(n); - return v; - } - - /** - * Append the elements in the argument sequence to the end of the list. <br> - * Same as <code>s[len(s):len(s)] = o</code>. - * - * @param o - * the sequence of items to append to the list. - */ - public void extend(PyObject o) { - newlist_extend(o); - } - - @ExposedMethod(doc = BuiltinDocs.list_extend_doc) - final void newlist_extend(PyObject o) { - if (o instanceof PyNewList) { - list.addAll(((PyNewList) o).list); - } else if (o instanceof PySequenceObjectList) { - PyObject other[] = ((PySequenceObjectList) o).getArray(); - for (int i = 0; i < other.length; i++) { - list.add(other[i]); - } - } else { - for (PyObject item : o.asIterable()) { - list.add(item); - } - } - gListAllocatedStatus = __len__(); - } - - @Override - public PyObject __iadd__(PyObject o) { - return newlist___iadd__(o); - } - - @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___iadd___doc) - final PyObject newlist___iadd__(PyObject o) { - PyType oType = o.getType(); - if (oType == TYPE || oType == PyTuple.TYPE || this == o) { - extend(fastSequence(o, "argument must be iterable")); - return this; - } - - PyObject it; - try { - it = o.__iter__(); - } catch (PyException pye) { - if (!Py.matchException(pye, Py.TypeError)) { - throw pye; - } - return null; - } - extend(it); - return this; - } - - /** - * Sort the items of the list in place. The compare argument is a function of two arguments - * (list items) which should return -1, 0 or 1 depending on whether the first argument is - * considered smaller than, equal to, or larger than the second argument. Note that this slows - * the sorting process down considerably; e.g. to sort a list in reverse order it is much faster - * to use calls to the methods sort() and reverse() than to use the built-in function sort() - * with a comparison function that reverses the ordering of the elements. - * - * @param compare - * the comparison function. - */ - /** - * Sort the items of the list in place. Items is compared with the normal relative comparison - * operators. - */ - @ExposedMethod(doc = BuiltinDocs.list_sort_doc) - final void newlist_sort(PyObject[] args, String[] kwds) { - ArgParser ap = new ArgParser("list", args, kwds, new String[]{"cmp", "key", "reverse"}, 0); - PyObject cmp = ap.getPyObject(0, Py.None); - PyObject key = ap.getPyObject(1, Py.None); - PyObject reverse = ap.getPyObject(2, Py.False); - sort(cmp, key, reverse); - } - - public void sort(PyObject compare) { - sort(compare, Py.None, Py.False); - } - - public void sort() { - sort(Py.None, Py.None, Py.False); - } - - public void sort(PyObject cmp, PyObject key, PyObject reverse) { - MergeState ms = new MergeState(new PyList((Collection) this), cmp, key, reverse.__nonzero__()); - ms.sort(); - } - - public int hashCode() { - return newlist___hash__(); - } - - @ExposedMethod(doc = BuiltinDocs.list___hash___doc) - final int newlist___hash__() { - throw Py.TypeError(String.format("unhashable type: '%.200s'", getType().fastGetName())); - } - - @Override - public PyTuple __getnewargs__() { - return new PyTuple(new PyTuple(getArray())); - } - - @Override - public void add(int index, Object element) { - pyadd(index, Py.java2py(element)); - } - - @Override - public boolean add(Object o) { - pyadd(Py.java2py(o)); - return true; - } - - @Override - public boolean addAll(int index, Collection c) { - if (c instanceof PySequenceList) { - list.addAll(index, c); - } else { - // need to use add to convert anything pulled from a collection into a PyObject - for (Object element : c) { - add(element); - } - } - return c.size() > 0; - } - - @Override - public boolean addAll(Collection c) { - if (c instanceof PySequenceList) { - list.addAll(c); - } else { - // need to use add to convert anything pulled from a collection into a PyObject - for (Object element : c) { - add(element); - } - } - return c.size() > 0; - } - - @Override - public void clear() { - list.clear(); - } - - @Override - public boolean contains(Object o) { - return list.contains(Py.java2py(o)); - } - - @Override - public boolean containsAll(Collection c) { - if (c instanceof PySequenceList) { - return list.containsAll(c); - } else { - return list.containsAll(new PyList(c)); - } - } - - @Override - public boolean equals(Object o) { - if (o instanceof PyNewList) { - return (((PyNewList) o).list.equals(list)); - } - return false; - } - - @Override - public Object get(int index) { - return list.get(index).__tojava__(Object.class); - } - - /** @deprecated */ - @Override - public PyObject[] getArray() { - PyObject a[] = new PyObject[list.size()]; - return list.toArray(a); - } - - @Override - public int indexOf(Object o) { - return list.indexOf(o); - } - - @Override - public boolean isEmpty() { - return list.isEmpty(); - } - - @Override - public Iterator iterator() { - return list.iterator(); - } - - @Override - public int lastIndexOf(Object o) { - return list.lastIndexOf(o); - } - - @Override - public ListIterator listIterator() { - return list.listIterator(); - } - - @Override - public ListIterator listIterator(int index) { - return list.listIterator(index); - } - - @Override - public void pyadd(int index, PyObject element) { - list.add(index, element); - } - - @Override - public boolean pyadd(PyObject o) { - list.add(o); - return true; - } - - @Override - public PyObject pyget(int index) { - return list.get(index); - } - - public void pyset(int index, PyObject element) { - list.set(index, element); - } - - @Override - public Object remove(int index) { - return list.remove(index); - } - - @Override - public void remove(int start, int stop) { - list.subList(start, stop).clear(); - } - - @Override - public boolean removeAll(Collection c) { - if (c instanceof PySequenceList) { - return list.removeAll(c); - } else { - return list.removeAll(new PyNewList(c)); - } - } - - @Override - public boolean retainAll(Collection c) { - if (c instanceof PySequenceList) { - return list.retainAll(c); - } else { - return list.retainAll(new PyNewList(c)); - } - } - - @Override - public Object set(int index, Object element) { - return list.set(index, Py.java2py(element)).__tojava__(Object.class); - } - - @Override - public int size() { - return list.size(); - } - - @Override - public List subList(int fromIndex, int toIndex) { - return list.subList(fromIndex, toIndex); - } - - @Override - public Object[] toArray() { - return list.toArray(); - } - - @Override - public Object[] toArray(Object[] a) { - return list.toArray(a); - } - - protected PyObject getslice(int start, int stop, int step) { - if (step > 0 && stop < start) { - stop = start; - } - int n = sliceLength(start, stop, step); - List newList; - if (step == 1) { - newList = new ArrayList<PyObject>(list.subList(start, stop)); - } else { - newList = new ArrayList<PyObject>(n); - for (int i = start, j = 0; j < n; i += step, j++) { - newList.add(list.get(i)); - } - } - return fromList(newList); - } - - @Override - public boolean remove(Object o) { - return list.remove(Py.java2py(o)); - } -} Modified: branches/newlist/src/org/python/core/PyObject.java =================================================================== --- branches/newlist/src/org/python/core/PyObject.java 2009-04-08 06:48:21 UTC (rev 6190) +++ branches/newlist/src/org/python/core/PyObject.java 2009-04-08 19:50:54 UTC (rev 6191) @@ -20,7 +20,7 @@ * of the class <code>PyObject</code> or one of its subclasses. */ @ExposedType(name = "object") -public class PyObject implements Serializable { +public class PyObject implements Comparable<PyObject>, Serializable { public static final PyType TYPE = PyType.fromClass(PyObject.class); @@ -1284,6 +1284,10 @@ } } + public int compareTo(PyObject o) { + return this._cmp(o); + } + private PyObject make_pair(PyObject o) { if (System.identityHashCode(this) < System.identityHashCode(o)) return new PyIdentityTuple(new PyObject[] { this, o }); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2009-04-08 06:48:27
|
Revision: 6190 http://jython.svn.sourceforge.net/jython/?rev=6190&view=rev Author: zyasoft Date: 2009-04-08 06:48:21 +0000 (Wed, 08 Apr 2009) Log Message: ----------- Fixed setslice/getslice problems. Modified Paths: -------------- branches/newlist/Lib/test/test_newlist.py branches/newlist/src/org/python/core/PyNewList.java Modified: branches/newlist/Lib/test/test_newlist.py =================================================================== --- branches/newlist/Lib/test/test_newlist.py 2009-04-08 01:27:07 UTC (rev 6189) +++ branches/newlist/Lib/test/test_newlist.py 2009-04-08 06:48:21 UTC (rev 6190) @@ -1,60 +1,14 @@ # Check every path through every method of UserList -from org.python.core import PyNewList as UserList +from org.python.core import PyNewList as newlist import unittest from test import test_support, list_tests -class UserListTest(list_tests.CommonTest): - type2test = UserList +class NewListTest(list_tests.CommonTest): + type2test = newlist - def test_getslice(self): - super(UserListTest, self).test_getslice() - l = [0, 1, 2, 3, 4] - u = self.type2test(l) - for i in range(-3, 6): - self.assertEqual(u[:i], l[:i]) - self.assertEqual(u[i:], l[i:]) - for j in xrange(-3, 6): - self.assertEqual(u[i:j], l[i:j]) - - def test_add_specials(self): - u = UserList("spam") - u2 = u + "eggs" - self.assertEqual(u2, list("spameggs")) - - def test_radd_specials(self): - u = UserList("eggs") - u2 = "spam" + u - self.assertEqual(u2, list("spameggs")) - u2 = u.__radd__(UserList("spam")) - self.assertEqual(u2, list("spameggs")) - - def test_iadd(self): - super(UserListTest, self).test_iadd() - u = [0, 1] - u += UserList([0, 1]) - self.assertEqual(u, [0, 1, 0, 1]) - - def test_mixedcmp(self): - u = self.type2test([0, 1]) - self.assertEqual(u, [0, 1]) - self.assertNotEqual(u, [0]) - self.assertNotEqual(u, [0, 2]) - - def test_mixedadd(self): - u = self.type2test([0, 1]) - self.assertEqual(u + [], u) - self.assertEqual(u + [2], [0, 1, 2]) - - def test_getitemoverwriteiter(self): - # Verify that __getitem__ overrides *are* recognized by __iter__ - class T(self.type2test): - def __getitem__(self, key): - return str(key) + '!!!' - self.assertEqual(iter(T((1,2))).next(), "0!!!") - def test_main(): - test_support.run_unittest(UserListTest) + test_support.run_unittest(NewListTest) if __name__ == "__main__": test_main() Modified: branches/newlist/src/org/python/core/PyNewList.java =================================================================== --- branches/newlist/src/org/python/core/PyNewList.java 2009-04-08 01:27:07 UTC (rev 6189) +++ branches/newlist/src/org/python/core/PyNewList.java 2009-04-08 06:48:21 UTC (rev 6190) @@ -16,7 +16,7 @@ import java.util.ListIterator; @ExposedType(name = "newlist", base = PyObject.class) -public class PyNewList extends PySequenceList implements List{ +public class PyNewList extends PySequenceList implements List { public static final PyType TYPE = PyType.fromClass(PyNewList.class); protected final List<PyObject> list; @@ -75,12 +75,13 @@ } private static List<PyObject> listify(Iterator<PyObject> iter) { - List<PyObject> list = Generic.list(); - while (iter.hasNext()) { + List<PyObject> list = Generic.list(); + while (iter.hasNext()) { list.add(iter.next()); - } - return list; + } + return list; } + public PyNewList(Iterator<PyObject> iter) { this(TYPE, listify(iter)); } @@ -88,14 +89,14 @@ @ExposedNew @ExposedMethod(doc = BuiltinDocs.list___init___doc) final void newlist___init__(PyObject[] args, String[] kwds) { - ArgParser ap = new ArgParser("newlist", args, kwds, new String[] {"sequence"}, 0); + ArgParser ap = new ArgParser("newlist", args, kwds, new String[]{"sequence"}, 0); PyObject seq = ap.getPyObject(0, null); clear(); - if(seq == null) { + if (seq == null) { return; } - if(seq instanceof PyNewList) { - list.addAll((PyNewList)seq); // don't convert + if (seq instanceof PyNewList) { + list.addAll((PyNewList) seq); // don't convert } else { for (PyObject item : seq.asIterable()) { append(item); @@ -125,22 +126,18 @@ @Override protected void setslice(int start, int stop, int step, PyObject value) { - if(stop < start) { + if (stop < start) { stop = start; } - if (value instanceof PySequenceList) { - setsliceIterator(start, stop, step, ((PySequenceList)value).listIterator()); - } - else if ((value instanceof PySequence) || (!(value instanceof List))) { - System.err.println("PySequence"); + if ((value instanceof PySequence) || (!(value instanceof List))) { if (value == this) { // copy - value = new PyNewList((PySequence)value); + value = new PyNewList((PySequence) value); } setsliceIterator(start, stop, step, value.asIterable().iterator()); } else { System.err.println("List"); - List valueList = (List)value.__tojava__(List.class); - if(valueList != null && valueList != Py.NoConversion) { + List valueList = (List) value.__tojava__(List.class); + if (valueList != null && valueList != Py.NoConversion) { setsliceList(start, stop, step, valueList); } } @@ -158,26 +155,17 @@ } protected void setsliceIterator(int start, int stop, int step, Iterator<PyObject> iter) { - int n = sliceLength(start, stop, step); int size = list.size(); - int delta = start > size ? n : start - size + n; - if (list instanceof ArrayList) { - ((ArrayList) list).ensureCapacity(size + delta); - } - System.err.println("setsliceIterator: start=" + start + ",stop=" + stop + ",step=" + step + ",n=" + n + ",delta=" + delta); - for (int j = start; iter.hasNext(); j += step) { - System.err.print(this); - if (j > size) { - list.add(iter.next()); + PyObject item = iter.next(); + if (j >= size) { + list.add(item); } else { - list.set(j, iter.next()); + list.set(j, item); } - System.err.println("-> " + this); } } - @Override protected PyObject repeat(int count) { if (count < 0) { @@ -254,7 +242,7 @@ int newSize = size * count; if (list instanceof ArrayList) { - ((ArrayList)list).ensureCapacity(newSize); + ((ArrayList) list).ensureCapacity(newSize); } List oldList = new ArrayList<PyObject>(list); for (int i = 1; i < count; i++) { @@ -300,20 +288,20 @@ PyNewList sum = null; if (o instanceof PySequenceList && !(o instanceof PyTuple)) { if (o instanceof PyNewList) { - List oList = ((PyNewList)o).list; + List oList = ((PyNewList) o).list; List newList = new ArrayList(list.size() + oList.size()); newList.addAll(list); newList.addAll(oList); sum = fromList(newList); } - } else if(!(o instanceof PySequenceList)) { + } else if (!(o instanceof PySequenceList)) { // also support adding java lists (but not PyTuple!) Object oList = o.__tojava__(List.class); - if(oList != Py.NoConversion && oList != null) { + if (oList != Py.NoConversion && oList != null) { List otherList = (List) oList; sum = new PyNewList(); sum.newlist_extend(this); - for(Iterator i = otherList.iterator(); i.hasNext();) { + for (Iterator i = otherList.iterator(); i.hasNext();) { sum.add(i.next()); } } @@ -332,7 +320,7 @@ // Support adding java.util.List, but prevent adding PyTuple. // 'o' should never be a PyNewList since __add__ is defined. PyNewList sum = null; - if(o instanceof PySequence) { + if (o instanceof PySequence) { return null; } Object oList = o.__tojava__(List.class); @@ -362,7 +350,7 @@ @ExposedMethod(doc = BuiltinDocs.list___getitem___doc) final PyObject newlist___getitem__(PyObject o) { PyObject ret = seq___finditem__(o); - if(ret == null) { + if (ret == null) { throw Py.IndexError("index out of range: " + o); } return ret; @@ -395,7 +383,7 @@ @Override protected String unsupportedopMessage(String op, PyObject o2) { - if(op.equals("+")) { + if (op.equals("+")) { return "can only concatenate list (not \"{2}\") to list"; } return super.unsupportedopMessage(op, o2); @@ -409,19 +397,19 @@ @ExposedMethod(names = "__repr__") final String newlist_toString() { ThreadState ts = Py.getThreadState(); - if(!ts.enterRepr(this)) { + if (!ts.enterRepr(this)) { return "[...]"; } StringBuilder buf = new StringBuilder("["); int length = size(); - PyObject[] array = getArray(); - for(int i = 0; i < length - 1; i++) { - buf.append((array[i]).__repr__().toString()); - buf.append(", "); + int i = 0; + for (PyObject item : list) { + buf.append(item.__repr__().toString()); + if (i < length - 1) { + buf.append(", "); + } + i++; } - if(length > 0) { - buf.append((array[length - 1]).__repr__().toString()); - } buf.append("]"); ts.exitRepr(this); return buf.toString(); @@ -457,8 +445,8 @@ final int newlist_count(PyObject o) { int count = 0; PyObject[] array = getArray(); - for(int i = 0, n = size(); i < n; i++) { - if(array[i].equals(o)) { + for (int i = 0, n = size(); i < n; i++) { + if (array[i].equals(o)) { count++; } } @@ -507,8 +495,8 @@ int validStop = boundToSequence(stop); int validStart = boundToSequence(start); PyObject[] array = getArray(); - for(int i = validStart; i < validStop && i < size(); i++) { - if(array[i].equals(o)) { + for (int i = validStart; i < validStop && i < size(); i++) { + if (array[i].equals(o)) { return i; } } @@ -530,10 +518,10 @@ @ExposedMethod(doc = BuiltinDocs.list_insert_doc) final void newlist_insert(int index, PyObject o) { - if(index < 0) { + if (index < 0) { index = Math.max(0, size() + index); } - if(index > size()) { + if (index > size()) { index = size(); } pyadd(index, o); @@ -593,13 +581,13 @@ @ExposedMethod(defaults = "-1", doc = BuiltinDocs.list_pop_doc) final PyObject newlist_pop(int n) { int length = size(); - if(length == 0) { + if (length == 0) { throw Py.IndexError("pop from empty list"); } - if(n < 0) { + if (n < 0) { n += length; } - if(n < 0 || n >= length) { + if (n < 0 || n >= length) { throw Py.IndexError("pop index out of range"); } PyObject v = list.remove(n); @@ -620,9 +608,9 @@ @ExposedMethod(doc = BuiltinDocs.list_extend_doc) final void newlist_extend(PyObject o) { if (o instanceof PyNewList) { - list.addAll(((PyNewList)o).list); + list.addAll(((PyNewList) o).list); } else if (o instanceof PySequenceObjectList) { - PyObject other[] = ((PySequenceObjectList)o).getArray(); + PyObject other[] = ((PySequenceObjectList) o).getArray(); for (int i = 0; i < other.length; i++) { list.add(other[i]); } @@ -671,13 +659,10 @@ * @param compare * the comparison function. */ - - /** + /** * Sort the items of the list in place. Items is compared with the normal relative comparison * operators. */ - - @ExposedMethod(doc = BuiltinDocs.list_sort_doc) final void newlist_sort(PyObject[] args, String[] kwds) { ArgParser ap = new ArgParser("list", args, kwds, new String[]{"cmp", "key", "reverse"}, 0); @@ -696,7 +681,7 @@ } public void sort(PyObject cmp, PyObject key, PyObject reverse) { - MergeState ms = new MergeState(new PyList((Collection)this), cmp, key, reverse.__nonzero__()); + MergeState ms = new MergeState(new PyList((Collection) this), cmp, key, reverse.__nonzero__()); ms.sort(); } @@ -713,7 +698,7 @@ public PyTuple __getnewargs__() { return new PyTuple(new PyTuple(getArray())); } - + @Override public void add(int index, Object element) { pyadd(index, Py.java2py(element)); @@ -765,8 +750,7 @@ public boolean containsAll(Collection c) { if (c instanceof PySequenceList) { return list.containsAll(c); - } - else { + } else { return list.containsAll(new PyList(c)); } } @@ -774,7 +758,7 @@ @Override public boolean equals(Object o) { if (o instanceof PyNewList) { - return (((PyNewList)o).list.equals(list)); + return (((PyNewList) o).list.equals(list)); } return false; } @@ -783,7 +767,7 @@ public Object get(int index) { return list.get(index).__tojava__(Object.class); } - + /** @deprecated */ @Override public PyObject[] getArray() { @@ -814,7 +798,7 @@ @Override public ListIterator listIterator() { return list.listIterator(); -} + } @Override public ListIterator listIterator(int index) { @@ -855,8 +839,7 @@ public boolean removeAll(Collection c) { if (c instanceof PySequenceList) { return list.removeAll(c); - } - else { + } else { return list.removeAll(new PyNewList(c)); } } @@ -865,8 +848,7 @@ public boolean retainAll(Collection c) { if (c instanceof PySequenceList) { return list.retainAll(c); - } - else { + } else { return list.retainAll(new PyNewList(c)); } } @@ -897,20 +879,17 @@ } protected PyObject getslice(int start, int stop, int step) { - if(step > 0 && stop < start) { + if (step > 0 && stop < start) { stop = start; } int n = sliceLength(start, stop, step); List newList; - if(step == 1) { + if (step == 1) { newList = new ArrayList<PyObject>(list.subList(start, stop)); - } - else { + } else { newList = new ArrayList<PyObject>(n); - int j = 0; - for(int i = start; j < n; i += step) { - newList.set(j, list.get(i)); - j++; + for (int i = start, j = 0; j < n; i += step, j++) { + newList.add(list.get(i)); } } return fromList(newList); @@ -920,5 +899,4 @@ public boolean remove(Object o) { return list.remove(Py.java2py(o)); } - } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2009-04-08 01:27:08
|
Revision: 6189 http://jython.svn.sourceforge.net/jython/?rev=6189&view=rev Author: pjenvey Date: 2009-04-08 01:27:07 +0000 (Wed, 08 Apr 2009) Log Message: ----------- relax the Windows platform matching, don't bother special casing ce fixes #1162 Modified Paths: -------------- trunk/jython/Lib/os.py trunk/jython/Lib/subprocess.py Modified: trunk/jython/Lib/os.py =================================================================== --- trunk/jython/Lib/os.py 2009-04-08 01:23:13 UTC (rev 6188) +++ trunk/jython/Lib/os.py 2009-04-08 01:27:07 UTC (rev 6189) @@ -57,17 +57,9 @@ # Mapping of: os._name: [name list, shell command list] _os_map = dict(nt=[ - ['Windows 95', 'Windows 98', 'Windows ME', 'Windows NT', - 'Windows NT 4.0', 'WindowsNT', 'Windows 2000', 'Windows 2003', - 'Windows XP', 'Windows Vista'], + ['Windows'], [['cmd.exe', '/c'], ['command.com', '/c']] ], - - ce=[ - ['Windows CE'], - [['cmd.exe', '/c']] - ], - posix=[ [], # posix is a fallback, instead of matching names [['/bin/sh', '-c']] @@ -133,7 +125,7 @@ _posix = POSIXFactory.getPOSIX(PythonPOSIXHandler(), True) _native_posix = not isinstance(_posix, JavaPOSIX) -if _name in ('nt', 'ce'): +if _name == 'nt': import ntpath as path else: import posixpath as path Modified: trunk/jython/Lib/subprocess.py =================================================================== --- trunk/jython/Lib/subprocess.py 2009-04-08 01:23:13 UTC (rev 6188) +++ trunk/jython/Lib/subprocess.py 2009-04-08 01:27:07 UTC (rev 6189) @@ -545,7 +545,7 @@ if jython: # Escape the command line arguments with list2cmdline on Windows - escape_args_oses = ['nt', 'ce'] + escape_args_oses = ['nt'] escape_args = None shell_command = None This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2009-04-08 01:23:15
|
Revision: 6188 http://jython.svn.sourceforge.net/jython/?rev=6188&view=rev Author: pjenvey Date: 2009-04-08 01:23:13 +0000 (Wed, 08 Apr 2009) Log Message: ----------- o reapply our compiler workarounds from r4114 o support compiler.compile via builtin compile Modified Paths: -------------- trunk/jython/Lib/compiler/pycodegen.py trunk/jython/Lib/compiler/transformer.py Modified: trunk/jython/Lib/compiler/pycodegen.py =================================================================== --- trunk/jython/Lib/compiler/pycodegen.py 2009-04-08 01:14:13 UTC (rev 6187) +++ trunk/jython/Lib/compiler/pycodegen.py 2009-04-08 01:23:13 UTC (rev 6188) @@ -4,14 +4,18 @@ import struct import sys from cStringIO import StringIO +is_jython = sys.platform.startswith('java') from compiler import ast, parse, walk, syntax -from compiler import pyassem, misc, future, symbols +from compiler import misc, future, symbols from compiler.consts import SC_LOCAL, SC_GLOBAL, SC_FREE, SC_CELL from compiler.consts import (CO_VARARGS, CO_VARKEYWORDS, CO_NEWLOCALS, CO_NESTED, CO_GENERATOR, CO_FUTURE_DIVISION, CO_FUTURE_ABSIMPORT, CO_FUTURE_WITH_STATEMENT) -from compiler.pyassem import TupleArg +if not is_jython: + from compiler.pyassem import TupleArg +else: + TupleArg = None # XXX The version-specific code can go, since this code only works with 2.x. # Do we have Python 1.x or Python 2.x? @@ -47,22 +51,26 @@ mod.dump(f) f.close() -def compile(source, filename, mode, flags=None, dont_inherit=None): - """Replacement for builtin compile() function""" - if flags is not None or dont_inherit is not None: - raise RuntimeError, "not implemented yet" +if is_jython: + # use __builtin__ compile + compile = compile +else: + def compile(source, filename, mode, flags=None, dont_inherit=None): + """Replacement for builtin compile() function""" + if flags is not None or dont_inherit is not None: + raise RuntimeError, "not implemented yet" - if mode == "single": - gen = Interactive(source, filename) - elif mode == "exec": - gen = Module(source, filename) - elif mode == "eval": - gen = Expression(source, filename) - else: - raise ValueError("compile() 3rd arg must be 'exec' or " - "'eval' or 'single'") - gen.compile() - return gen.code + if mode == "single": + gen = Interactive(source, filename) + elif mode == "exec": + gen = Module(source, filename) + elif mode == "eval": + gen = Expression(source, filename) + else: + raise ValueError("compile() 3rd arg must be 'exec' or " + "'eval' or 'single'") + gen.compile() + return gen.code class AbstractCompileMode: @@ -119,7 +127,7 @@ f.write(self.getPycHeader()) marshal.dump(self.code, f) - MAGIC = imp.get_magic() + MAGIC = None if is_jython else imp.get_magic() def getPycHeader(self): # compile.c uses marshal to write a long directly, with Modified: trunk/jython/Lib/compiler/transformer.py =================================================================== --- trunk/jython/Lib/compiler/transformer.py 2009-04-08 01:14:13 UTC (rev 6187) +++ trunk/jython/Lib/compiler/transformer.py 2009-04-08 01:23:13 UTC (rev 6188) @@ -26,10 +26,11 @@ # and replace OWNER, ORGANIZATION, and YEAR as appropriate. from compiler.ast import * -import parser import symbol import token import sys +if not sys.platform.startswith('java'): + import parser class WalkerError(StandardError): pass This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2009-04-08 01:14:15
|
Revision: 6187 http://jython.svn.sourceforge.net/jython/?rev=6187&view=rev Author: pjenvey Date: 2009-04-08 01:14:13 +0000 (Wed, 08 Apr 2009) Log Message: ----------- from: http://svn.python.org/projects/python/branches/release25-maint/Lib/compiler/pycodegen.py@53576 http://svn.python.org/projects/python/branches/release25-maint/Lib/compiler/transformer.py@53576 Modified Paths: -------------- trunk/jython/Lib/compiler/pycodegen.py trunk/jython/Lib/compiler/transformer.py Modified: trunk/jython/Lib/compiler/pycodegen.py =================================================================== --- trunk/jython/Lib/compiler/pycodegen.py 2009-04-07 13:09:48 UTC (rev 6186) +++ trunk/jython/Lib/compiler/pycodegen.py 2009-04-08 01:14:13 UTC (rev 6187) @@ -3,17 +3,15 @@ import marshal import struct import sys -import types from cStringIO import StringIO from compiler import ast, parse, walk, syntax -#from compiler import pyassem, misc, future, symbols -from compiler import misc, future, symbols +from compiler import pyassem, misc, future, symbols from compiler.consts import SC_LOCAL, SC_GLOBAL, SC_FREE, SC_CELL -from compiler.consts import CO_VARARGS, CO_VARKEYWORDS, CO_NEWLOCALS,\ - CO_NESTED, CO_GENERATOR, CO_GENERATOR_ALLOWED, CO_FUTURE_DIVISION -#from compiler.pyassem import TupleArg -TupleArg = None +from compiler.consts import (CO_VARARGS, CO_VARKEYWORDS, CO_NEWLOCALS, + CO_NESTED, CO_GENERATOR, CO_FUTURE_DIVISION, + CO_FUTURE_ABSIMPORT, CO_FUTURE_WITH_STATEMENT) +from compiler.pyassem import TupleArg # XXX The version-specific code can go, since this code only works with 2.x. # Do we have Python 1.x or Python 2.x? @@ -121,8 +119,7 @@ f.write(self.getPycHeader()) marshal.dump(self.code, f) - #MAGIC = imp.get_magic() - MAGIC = None + MAGIC = imp.get_magic() def getPycHeader(self): # compile.c uses marshal to write a long directly, with @@ -207,8 +204,6 @@ self.checkClass() self.locals = misc.Stack() self.setups = misc.Stack() - self.curStack = 0 - self.maxStack = 0 self.last_lineno = None self._setupGraphDelegation() self._div_op = "BINARY_DIVIDE" @@ -219,8 +214,10 @@ if feature == "division": self.graph.setFlag(CO_FUTURE_DIVISION) self._div_op = "BINARY_TRUE_DIVIDE" - elif feature == "generators": - self.graph.setFlag(CO_GENERATOR_ALLOWED) + elif feature == "absolute_import": + self.graph.setFlag(CO_FUTURE_ABSIMPORT) + elif feature == "with_statement": + self.graph.setFlag(CO_FUTURE_WITH_STATEMENT) def initClass(self): """This method is called once for each class""" @@ -371,6 +368,13 @@ self._visitFuncOrLambda(node, isLambda=1) def _visitFuncOrLambda(self, node, isLambda=0): + if not isLambda and node.decorators: + for decorator in node.decorators.nodes: + self.visit(decorator) + ndecorators = len(node.decorators.nodes) + else: + ndecorators = 0 + gen = self.FunctionGen(node, self.scopes, isLambda, self.class_name, self.get_module()) walk(node.code, gen) @@ -378,15 +382,9 @@ self.set_lineno(node) for default in node.defaults: self.visit(default) - frees = gen.scope.get_free_vars() - if frees: - for name in frees: - self.emit('LOAD_CLOSURE', name) - self.emit('LOAD_CONST', gen) - self.emit('MAKE_CLOSURE', len(node.defaults)) - else: - self.emit('LOAD_CONST', gen) - self.emit('MAKE_FUNCTION', len(node.defaults)) + self._makeClosure(gen, len(node.defaults)) + for i in range(ndecorators): + self.emit('CALL_FUNCTION', 1) def visitClass(self, node): gen = self.ClassGen(node, self.scopes, @@ -398,14 +396,7 @@ for base in node.bases: self.visit(base) self.emit('BUILD_TUPLE', len(node.bases)) - frees = gen.scope.get_free_vars() - for name in frees: - self.emit('LOAD_CLOSURE', name) - self.emit('LOAD_CONST', gen) - if frees: - self.emit('MAKE_CLOSURE', 0) - else: - self.emit('MAKE_FUNCTION', 0) + self._makeClosure(gen, 0) self.emit('CALL_FUNCTION', 0) self.emit('BUILD_CLASS') self.storeName(node.name) @@ -539,6 +530,19 @@ def visitOr(self, node): self.visitTest(node, 'JUMP_IF_TRUE') + def visitIfExp(self, node): + endblock = self.newBlock() + elseblock = self.newBlock() + self.visit(node.test) + self.emit('JUMP_IF_FALSE', elseblock) + self.emit('POP_TOP') + self.visit(node.then) + self.emit('JUMP_FORWARD', endblock) + self.nextBlock(elseblock) + self.emit('POP_TOP') + self.visit(node.else_) + self.nextBlock(endblock) + def visitCompare(self, node): self.visit(node.expr) cleanup = self.newBlock() @@ -624,33 +628,116 @@ self.newBlock() self.emit('POP_TOP') - # exception related + def _makeClosure(self, gen, args): + frees = gen.scope.get_free_vars() + if frees: + for name in frees: + self.emit('LOAD_CLOSURE', name) + self.emit('BUILD_TUPLE', len(frees)) + self.emit('LOAD_CONST', gen) + self.emit('MAKE_CLOSURE', args) + else: + self.emit('LOAD_CONST', gen) + self.emit('MAKE_FUNCTION', args) - def visitAssert(self, node): - # XXX would be interesting to implement this via a - # transformation of the AST before this stage + def visitGenExpr(self, node): + gen = GenExprCodeGenerator(node, self.scopes, self.class_name, + self.get_module()) + walk(node.code, gen) + gen.finish() + self.set_lineno(node) + self._makeClosure(gen, 0) + # precomputation of outmost iterable + self.visit(node.code.quals[0].iter) + self.emit('GET_ITER') + self.emit('CALL_FUNCTION', 1) + + def visitGenExprInner(self, node): + self.set_lineno(node) + # setup list + + stack = [] + for i, for_ in zip(range(len(node.quals)), node.quals): + start, anchor, end = self.visit(for_) + cont = None + for if_ in for_.ifs: + if cont is None: + cont = self.newBlock() + self.visit(if_, cont) + stack.insert(0, (start, cont, anchor, end)) + + self.visit(node.expr) + self.emit('YIELD_VALUE') + self.emit('POP_TOP') + + for start, cont, anchor, end in stack: + if cont: + skip_one = self.newBlock() + self.emit('JUMP_FORWARD', skip_one) + self.startBlock(cont) + self.emit('POP_TOP') + self.nextBlock(skip_one) + self.emit('JUMP_ABSOLUTE', start) + self.startBlock(anchor) + self.emit('POP_BLOCK') + self.setups.pop() + self.startBlock(end) + + self.emit('LOAD_CONST', None) + + def visitGenExprFor(self, node): + start = self.newBlock() + anchor = self.newBlock() end = self.newBlock() - self.set_lineno(node) - # XXX __debug__ and AssertionError appear to be special cases - # -- they are always loaded as globals even if there are local - # names. I guess this is a sort of renaming op. - self.emit('LOAD_GLOBAL', '__debug__') - self.emit('JUMP_IF_FALSE', end) + + self.setups.push((LOOP, start)) + self.emit('SETUP_LOOP', end) + + if node.is_outmost: + self.loadName('.0') + else: + self.visit(node.iter) + self.emit('GET_ITER') + + self.nextBlock(start) + self.set_lineno(node, force=True) + self.emit('FOR_ITER', anchor) self.nextBlock() - self.emit('POP_TOP') + self.visit(node.assign) + return start, anchor, end + + def visitGenExprIf(self, node, branch): + self.set_lineno(node, force=True) self.visit(node.test) - self.emit('JUMP_IF_TRUE', end) - self.nextBlock() + self.emit('JUMP_IF_FALSE', branch) + self.newBlock() self.emit('POP_TOP') - self.emit('LOAD_GLOBAL', 'AssertionError') - if node.fail: - self.visit(node.fail) - self.emit('RAISE_VARARGS', 2) - else: - self.emit('RAISE_VARARGS', 1) - self.nextBlock(end) - self.emit('POP_TOP') + # exception related + + def visitAssert(self, node): + # XXX would be interesting to implement this via a + # transformation of the AST before this stage + if __debug__: + end = self.newBlock() + self.set_lineno(node) + # XXX AssertionError appears to be special case -- it is always + # loaded as a global even if there is a local name. I guess this + # is a sort of renaming op. + self.nextBlock() + self.visit(node.test) + self.emit('JUMP_IF_TRUE', end) + self.nextBlock() + self.emit('POP_TOP') + self.emit('LOAD_GLOBAL', 'AssertionError') + if node.fail: + self.visit(node.fail) + self.emit('RAISE_VARARGS', 2) + else: + self.emit('RAISE_VARARGS', 1) + self.nextBlock(end) + self.emit('POP_TOP') + def visitRaise(self, node): self.set_lineno(node) n = 0 @@ -732,6 +819,45 @@ self.emit('END_FINALLY') self.setups.pop() + __with_count = 0 + + def visitWith(self, node): + body = self.newBlock() + final = self.newBlock() + exitvar = "$exit%d" % self.__with_count + valuevar = "$value%d" % self.__with_count + self.__with_count += 1 + self.set_lineno(node) + self.visit(node.expr) + self.emit('DUP_TOP') + self.emit('LOAD_ATTR', '__exit__') + self._implicitNameOp('STORE', exitvar) + self.emit('LOAD_ATTR', '__enter__') + self.emit('CALL_FUNCTION', 0) + if node.vars is None: + self.emit('POP_TOP') + else: + self._implicitNameOp('STORE', valuevar) + self.emit('SETUP_FINALLY', final) + self.nextBlock(body) + self.setups.push((TRY_FINALLY, body)) + if node.vars is not None: + self._implicitNameOp('LOAD', valuevar) + self._implicitNameOp('DELETE', valuevar) + self.visit(node.vars) + self.visit(node.body) + self.emit('POP_BLOCK') + self.setups.pop() + self.emit('LOAD_CONST', None) + self.nextBlock(final) + self.setups.push((END_FINALLY, final)) + self._implicitNameOp('LOAD', exitvar) + self._implicitNameOp('DELETE', exitvar) + self.emit('WITH_CLEANUP') + self.emit('END_FINALLY') + self.setups.pop() + self.__with_count -= 1 + # misc def visitDiscard(self, node): @@ -759,8 +885,10 @@ def visitImport(self, node): self.set_lineno(node) + level = 0 if self.graph.checkFlag(CO_FUTURE_ABSIMPORT) else -1 for name, alias in node.names: if VERSION > 1: + self.emit('LOAD_CONST', level) self.emit('LOAD_CONST', None) self.emit('IMPORT_NAME', name) mod = name.split(".")[0] @@ -772,8 +900,12 @@ def visitFrom(self, node): self.set_lineno(node) + level = node.level + if level == 0 and not self.graph.checkFlag(CO_FUTURE_ABSIMPORT): + level = -1 fromlist = map(lambda (name, alias): name, node.names) if VERSION > 1: + self.emit('LOAD_CONST', level) self.emit('LOAD_CONST', tuple(fromlist)) self.emit('IMPORT_NAME', node.modname) for name, alias in node.names: @@ -909,8 +1041,6 @@ self.emit('STORE_SLICE+%d' % slice) def visitAugSubscript(self, node, mode): - if len(node.subs) > 1: - raise SyntaxError, "augmented assignment to tuple is not possible" if mode == "load": self.visitSubscript(node, 1) elif mode == "store": @@ -1015,10 +1145,10 @@ self.visit(node.expr) for sub in node.subs: self.visit(sub) + if len(node.subs) > 1: + self.emit('BUILD_TUPLE', len(node.subs)) if aug_flag: self.emit('DUP_TOPX', 2) - if len(node.subs) > 1: - self.emit('BUILD_TUPLE', len(node.subs)) if node.flags == 'OP_APPLY': self.emit('BINARY_SUBSCR') elif node.flags == 'OP_ASSIGN': @@ -1204,6 +1334,7 @@ klass.lambdaCount = klass.lambdaCount + 1 else: name = func.name + args, hasTupleArg = generateArgList(func.argnames) self.graph = pyassem.PyFlowGraph(name, func.filename, args, optimized=1) @@ -1235,7 +1366,7 @@ def generateArgUnpack(self, args): for i in range(len(args)): arg = args[i] - if type(arg) == types.TupleType: + if isinstance(arg, tuple): self.emit('LOAD_FAST', '.%d' % (i * 2)) self.unpackSequence(arg) @@ -1245,7 +1376,7 @@ else: self.emit('UNPACK_TUPLE', len(tup)) for elt in tup: - if type(elt) == types.TupleType: + if isinstance(elt, tuple): self.unpackSequence(elt) else: self._nameOp('STORE', elt) @@ -1268,6 +1399,21 @@ if self.scope.generator is not None: self.graph.setFlag(CO_GENERATOR) +class GenExprCodeGenerator(NestedScopeMixin, AbstractFunctionCode, + CodeGenerator): + super_init = CodeGenerator.__init__ # call be other init + scopes = None + + __super_init = AbstractFunctionCode.__init__ + + def __init__(self, gexp, scopes, class_name, mod): + self.scopes = scopes + self.scope = scopes[gexp] + self.__super_init(gexp, scopes, 1, class_name, mod) + self.graph.setFreeVars(self.scope.get_free_vars()) + self.graph.setCellVars(self.scope.get_cell_vars()) + self.graph.setFlag(CO_GENERATOR) + class AbstractClassCode: def __init__(self, klass, scopes, module): @@ -1316,9 +1462,9 @@ count = 0 for i in range(len(arglist)): elt = arglist[i] - if type(elt) == types.StringType: + if isinstance(elt, str): args.append(elt) - elif type(elt) == types.TupleType: + elif isinstance(elt, tuple): args.append(TupleArg(i * 2, elt)) extra.extend(misc.flatten(elt)) count = count + 1 Modified: trunk/jython/Lib/compiler/transformer.py =================================================================== --- trunk/jython/Lib/compiler/transformer.py 2009-04-07 13:09:48 UTC (rev 6186) +++ trunk/jython/Lib/compiler/transformer.py 2009-04-08 01:14:13 UTC (rev 6187) @@ -14,7 +14,10 @@ # # Modifications and improvements for Python 2.0 by Jeremy Hylton and # Mark Hammond - +# +# Some fixes to try to have correct line number on almost all nodes +# (except Module, Discard and Stmt) added by Sylvain Thenault +# # Portions of this file are: # Copyright (C) 1997-1998 Greg Stein. All Rights Reserved. # @@ -22,22 +25,20 @@ # http://www.opensource.org/licenses/bsd-license.html # and replace OWNER, ORGANIZATION, and YEAR as appropriate. -from ast import * -#import parser -parser = None -# Care must be taken to use only symbols and tokens defined in Python -# 1.5.2 for code branches executed in 1.5.2 +from compiler.ast import * +import parser import symbol import token import sys -error = 'walker.error' +class WalkerError(StandardError): + pass -from consts import CO_VARARGS, CO_VARKEYWORDS -from consts import OP_ASSIGN, OP_DELETE, OP_APPLY +from compiler.consts import CO_VARARGS, CO_VARKEYWORDS +from compiler.consts import OP_ASSIGN, OP_DELETE, OP_APPLY def parseFile(path): - f = open(path) + f = open(path, "U") # XXX The parser API tolerates files without a trailing newline, # but not strings without a trailing newline. Always add an extra # newline to the file contents, since we're going through the string @@ -69,6 +70,16 @@ l.append(item) return l +def extractLineNo(ast): + if not isinstance(ast[1], tuple): + # get a terminal node + return ast[2] + for child in ast[1:]: + if isinstance(child, tuple): + lineno = extractLineNo(child) + if lineno is not None: + return lineno + def Node(*args): kind = args[0] if nodes.has_key(kind): @@ -78,7 +89,7 @@ print nodes[kind], len(args), args raise else: - raise error, "Can't find appropriate Node type: %s" % str(args) + raise WalkerError, "Can't find appropriate Node type: %s" % str(args) #return apply(ast.Node, args) class Transformer: @@ -109,15 +120,12 @@ def transform(self, tree): """Transform an AST into a modified parse tree.""" - if type(tree) != type(()) and type(tree) != type([]): + if not (isinstance(tree, tuple) or isinstance(tree, list)): tree = parser.ast2tuple(tree, line_info=1) return self.compile_node(tree) def parsesuite(self, text): """Return a modified parse tree for the given suite text.""" - # Hack for handling non-native line endings on non-DOS like OSs. - # this can go now we have universal newlines? - text = text.replace('\x0d', '') return self.transform(parser.suite(text)) def parseexpr(self, text): @@ -157,7 +165,7 @@ if n == symbol.classdef: return self.classdef(node[1:]) - raise error, ('unexpected node type', n) + raise WalkerError, ('unexpected node type', n) def single_input(self, node): ### do we want to do anything about being "interactive" ? @@ -186,31 +194,77 @@ ### is this sufficient? return Expression(self.com_node(nodelist[0])) + def decorator_name(self, nodelist): + listlen = len(nodelist) + assert listlen >= 1 and listlen % 2 == 1 + + item = self.atom_name(nodelist) + i = 1 + while i < listlen: + assert nodelist[i][0] == token.DOT + assert nodelist[i + 1][0] == token.NAME + item = Getattr(item, nodelist[i + 1][1]) + i += 2 + + return item + + def decorator(self, nodelist): + # '@' dotted_name [ '(' [arglist] ')' ] + assert len(nodelist) in (3, 5, 6) + assert nodelist[0][0] == token.AT + assert nodelist[-1][0] == token.NEWLINE + + assert nodelist[1][0] == symbol.dotted_name + funcname = self.decorator_name(nodelist[1][1:]) + + if len(nodelist) > 3: + assert nodelist[2][0] == token.LPAR + expr = self.com_call_function(funcname, nodelist[3]) + else: + expr = funcname + + return expr + + def decorators(self, nodelist): + # decorators: decorator ([NEWLINE] decorator)* NEWLINE + items = [] + for dec_nodelist in nodelist: + assert dec_nodelist[0] == symbol.decorator + items.append(self.decorator(dec_nodelist[1:])) + return Decorators(items) + def funcdef(self, nodelist): - # funcdef: 'def' NAME parameters ':' suite + # -6 -5 -4 -3 -2 -1 + # funcdef: [decorators] 'def' NAME parameters ':' suite # parameters: '(' [varargslist] ')' - lineno = nodelist[1][2] - name = nodelist[1][1] - args = nodelist[2][2] + if len(nodelist) == 6: + assert nodelist[0][0] == symbol.decorators + decorators = self.decorators(nodelist[0][1:]) + else: + assert len(nodelist) == 5 + decorators = None + lineno = nodelist[-4][2] + name = nodelist[-4][1] + args = nodelist[-3][2] + if args[0] == symbol.varargslist: names, defaults, flags = self.com_arglist(args[1:]) else: names = defaults = () flags = 0 - doc = self.get_docstring(nodelist[4]) + doc = self.get_docstring(nodelist[-1]) # code for function - code = self.com_node(nodelist[4]) + code = self.com_node(nodelist[-1]) if doc is not None: assert isinstance(code, Stmt) assert isinstance(code.nodes[0], Discard) del code.nodes[0] - n = Function(name, names, defaults, flags, doc, code) - n.lineno = lineno - return n + return Function(decorators, name, names, defaults, flags, doc, code, + lineno=lineno) def lambdef(self, nodelist): # lambdef: 'lambda' [varargslist] ':' test @@ -223,17 +277,18 @@ # code for lambda code = self.com_node(nodelist[-1]) - n = Lambda(names, defaults, flags, code) - n.lineno = nodelist[1][2] - return n + return Lambda(names, defaults, flags, code, lineno=nodelist[1][2]) + old_lambdef = lambdef def classdef(self, nodelist): - # classdef: 'class' NAME ['(' testlist ')'] ':' suite + # classdef: 'class' NAME ['(' [testlist] ')'] ':' suite name = nodelist[1][1] doc = self.get_docstring(nodelist[-1]) if nodelist[2][0] == token.COLON: bases = [] + elif nodelist[3][0] == token.RPAR: + bases = [] else: bases = self.com_bases(nodelist[3]) @@ -245,9 +300,7 @@ assert isinstance(code.nodes[0], Discard) del code.nodes[0] - n = Class(name, bases, doc, code) - n.lineno = nodelist[1][2] - return n + return Class(name, bases, doc, code, lineno=nodelist[1][2]) def stmt(self, nodelist): return self.com_stmt(nodelist[0]) @@ -264,31 +317,31 @@ return Stmt(stmts) def parameters(self, nodelist): - raise error + raise WalkerError def varargslist(self, nodelist): - raise error + raise WalkerError def fpdef(self, nodelist): - raise error + raise WalkerError def fplist(self, nodelist): - raise error + raise WalkerError def dotted_name(self, nodelist): - raise error + raise WalkerError def comp_op(self, nodelist): - raise error + raise WalkerError def trailer(self, nodelist): - raise error + raise WalkerError def sliceop(self, nodelist): - raise error + raise WalkerError def argument(self, nodelist): - raise error + raise WalkerError # -------------------------------------------------------------- # @@ -300,21 +353,17 @@ en = nodelist[-1] exprNode = self.lookup_node(en)(en[1:]) if len(nodelist) == 1: - n = Discard(exprNode) - n.lineno = exprNode.lineno - return n + return Discard(exprNode, lineno=exprNode.lineno) if nodelist[1][0] == token.EQUAL: nodesl = [] for i in range(0, len(nodelist) - 2, 2): nodesl.append(self.com_assign(nodelist[i], OP_ASSIGN)) - n = Assign(nodesl, exprNode) - n.lineno = nodelist[1][2] + return Assign(nodesl, exprNode, lineno=nodelist[1][2]) else: lval = self.com_augassign(nodelist[0]) op = self.com_augassign_op(nodelist[1]) - n = AugAssign(lval, op[1], exprNode) - n.lineno = op[2] - return n + return AugAssign(lval, op[1], exprNode, lineno=op[2]) + raise WalkerError, "can't get here" def print_stmt(self, nodelist): # print ([ test (',' test)* [','] ] | '>>' test [ (',' test)+ [','] ]) @@ -333,46 +382,38 @@ for i in range(start, len(nodelist), 2): items.append(self.com_node(nodelist[i])) if nodelist[-1][0] == token.COMMA: - n = Print(items, dest) - n.lineno = nodelist[0][2] - return n - n = Printnl(items, dest) - n.lineno = nodelist[0][2] - return n + return Print(items, dest, lineno=nodelist[0][2]) + return Printnl(items, dest, lineno=nodelist[0][2]) def del_stmt(self, nodelist): return self.com_assign(nodelist[1], OP_DELETE) def pass_stmt(self, nodelist): - n = Pass() - n.lineno = nodelist[0][2] - return n + return Pass(lineno=nodelist[0][2]) def break_stmt(self, nodelist): - n = Break() - n.lineno = nodelist[0][2] - return n + return Break(lineno=nodelist[0][2]) def continue_stmt(self, nodelist): - n = Continue() - n.lineno = nodelist[0][2] - return n + return Continue(lineno=nodelist[0][2]) def return_stmt(self, nodelist): # return: [testlist] if len(nodelist) < 2: - n = Return(Const(None)) - n.lineno = nodelist[0][2] - return n - n = Return(self.com_node(nodelist[1])) - n.lineno = nodelist[0][2] - return n + return Return(Const(None), lineno=nodelist[0][2]) + return Return(self.com_node(nodelist[1]), lineno=nodelist[0][2]) def yield_stmt(self, nodelist): - n = Yield(self.com_node(nodelist[1])) - n.lineno = nodelist[0][2] - return n + expr = self.com_node(nodelist[0]) + return Discard(expr, lineno=expr.lineno) + def yield_expr(self, nodelist): + if len(nodelist) > 1: + value = self.com_node(nodelist[1]) + else: + value = Const(None) + return Yield(value, lineno=nodelist[0][2]) + def raise_stmt(self, nodelist): # raise: [test [',' test [',' test]]] if len(nodelist) > 5: @@ -387,44 +428,46 @@ expr1 = self.com_node(nodelist[1]) else: expr1 = None - n = Raise(expr1, expr2, expr3) - n.lineno = nodelist[0][2] - return n + return Raise(expr1, expr2, expr3, lineno=nodelist[0][2]) def import_stmt(self, nodelist): - # import_stmt: 'import' dotted_as_name (',' dotted_as_name)* | - # from: 'from' dotted_name 'import' - # ('*' | import_as_name (',' import_as_name)*) - if nodelist[0][1] == 'from': - names = [] - if nodelist[3][0] == token.NAME: - for i in range(3, len(nodelist), 2): - names.append((nodelist[i][1], None)) - else: - for i in range(3, len(nodelist), 2): - names.append(self.com_import_as_name(nodelist[i])) - n = From(self.com_dotted_name(nodelist[1]), names) - n.lineno = nodelist[0][2] - return n + # import_stmt: import_name | import_from + assert len(nodelist) == 1 + return self.com_node(nodelist[0]) - if nodelist[1][0] == symbol.dotted_name: - names = [(self.com_dotted_name(nodelist[1][1:]), None)] + def import_name(self, nodelist): + # import_name: 'import' dotted_as_names + return Import(self.com_dotted_as_names(nodelist[1]), + lineno=nodelist[0][2]) + + def import_from(self, nodelist): + # import_from: 'from' ('.'* dotted_name | '.') 'import' ('*' | + # '(' import_as_names ')' | import_as_names) + assert nodelist[0][1] == 'from' + idx = 1 + while nodelist[idx][1] == '.': + idx += 1 + level = idx - 1 + if nodelist[idx][0] == symbol.dotted_name: + fromname = self.com_dotted_name(nodelist[idx]) + idx += 1 else: - names = [] - for i in range(1, len(nodelist), 2): - names.append(self.com_dotted_as_name(nodelist[i])) - n = Import(names) - n.lineno = nodelist[0][2] - return n + fromname = "" + assert nodelist[idx][1] == 'import' + if nodelist[idx + 1][0] == token.STAR: + return From(fromname, [('*', None)], level, + lineno=nodelist[0][2]) + else: + node = nodelist[idx + 1 + (nodelist[idx + 1][0] == token.LPAR)] + return From(fromname, self.com_import_as_names(node), level, + lineno=nodelist[0][2]) def global_stmt(self, nodelist): # global: NAME (',' NAME)* names = [] for i in range(1, len(nodelist), 2): names.append(nodelist[i][1]) - n = Global(names) - n.lineno = nodelist[0][2] - return n + return Global(names, lineno=nodelist[0][2]) def exec_stmt(self, nodelist): # exec_stmt: 'exec' expr ['in' expr [',' expr]] @@ -438,9 +481,7 @@ else: expr2 = expr3 = None - n = Exec(expr1, expr2, expr3) - n.lineno = nodelist[0][2] - return n + return Exec(expr1, expr2, expr3, lineno=nodelist[0][2]) def assert_stmt(self, nodelist): # 'assert': test, [',' test] @@ -449,9 +490,7 @@ expr2 = self.com_node(nodelist[3]) else: expr2 = None - n = Assert(expr1, expr2) - n.lineno = nodelist[0][2] - return n + return Assert(expr1, expr2, lineno=nodelist[0][2]) def if_stmt(self, nodelist): # if: test ':' suite ('elif' test ':' suite)* ['else' ':' suite] @@ -466,9 +505,7 @@ ## elseNode.lineno = nodelist[-1][1][2] else: elseNode = None - n = If(tests, elseNode) - n.lineno = nodelist[0][2] - return n + return If(tests, elseNode, lineno=nodelist[0][2]) def while_stmt(self, nodelist): # 'while' test ':' suite ['else' ':' suite] @@ -481,9 +518,7 @@ else: elseNode = None - n = While(testNode, bodyNode, elseNode) - n.lineno = nodelist[0][2] - return n + return While(testNode, bodyNode, elseNode, lineno=nodelist[0][2]) def for_stmt(self, nodelist): # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] @@ -497,18 +532,18 @@ else: elseNode = None - n = For(assignNode, listNode, bodyNode, elseNode) - n.lineno = nodelist[0][2] - return n + return For(assignNode, listNode, bodyNode, elseNode, + lineno=nodelist[0][2]) def try_stmt(self, nodelist): - # 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite] - # | 'try' ':' suite 'finally' ':' suite - if nodelist[3][0] != symbol.except_clause: - return self.com_try_finally(nodelist) + return self.com_try_except_finally(nodelist) - return self.com_try_except(nodelist) + def with_stmt(self, nodelist): + return self.com_with(nodelist) + def with_var(self, nodelist): + return self.com_with_var(nodelist) + def suite(self, nodelist): # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT if len(nodelist) == 1: @@ -535,11 +570,32 @@ testlist1 = testlist exprlist = testlist + def testlist_gexp(self, nodelist): + if len(nodelist) == 2 and nodelist[1][0] == symbol.gen_for: + test = self.com_node(nodelist[0]) + return self.com_generator_expression(test, nodelist[1]) + return self.testlist(nodelist) + def test(self, nodelist): + # or_test ['if' or_test 'else' test] | lambdef + if len(nodelist) == 1 and nodelist[0][0] == symbol.lambdef: + return self.lambdef(nodelist[0]) + then = self.com_node(nodelist[0]) + if len(nodelist) > 1: + assert len(nodelist) == 5 + assert nodelist[1][1] == 'if' + assert nodelist[3][1] == 'else' + test = self.com_node(nodelist[2]) + else_ = self.com_node(nodelist[4]) + return IfExp(test, then, else_, lineno=nodelist[1][2]) + return then + + def or_test(self, nodelist): # and_test ('or' and_test)* | lambdef if len(nodelist) == 1 and nodelist[0][0] == symbol.lambdef: return self.lambdef(nodelist[0]) return self.com_binary(Or, nodelist) + old_test = or_test def and_test(self, nodelist): # not_test ('and' not_test)* @@ -549,9 +605,7 @@ # 'not' not_test | comparison result = self.com_node(nodelist[-1]) if len(nodelist) == 2: - n = Not(result) - n.lineno = nodelist[0][2] - return n + return Not(result, lineno=nodelist[0][2]) return result def comparison(self, nodelist): @@ -585,9 +639,7 @@ # the two have very different semantics and results (note that the # latter form is always true) - n = Compare(node, results) - n.lineno = lineno - return n + return Compare(node, results, lineno=lineno) def expr(self, nodelist): # xor_expr ('|' xor_expr)* @@ -607,11 +659,9 @@ for i in range(2, len(nodelist), 2): right = self.com_node(nodelist[i]) if nodelist[i-1][0] == token.LEFTSHIFT: - node = LeftShift([node, right]) - node.lineno = nodelist[1][2] + node = LeftShift([node, right], lineno=nodelist[1][2]) elif nodelist[i-1][0] == token.RIGHTSHIFT: - node = RightShift([node, right]) - node.lineno = nodelist[1][2] + node = RightShift([node, right], lineno=nodelist[1][2]) else: raise ValueError, "unexpected token: %s" % nodelist[i-1][0] return node @@ -621,11 +671,9 @@ for i in range(2, len(nodelist), 2): right = self.com_node(nodelist[i]) if nodelist[i-1][0] == token.PLUS: - node = Add([node, right]) - node.lineno = nodelist[1][2] + node = Add([node, right], lineno=nodelist[1][2]) elif nodelist[i-1][0] == token.MINUS: - node = Sub([node, right]) - node.lineno = nodelist[1][2] + node = Sub([node, right], lineno=nodelist[1][2]) else: raise ValueError, "unexpected token: %s" % nodelist[i-1][0] return node @@ -651,17 +699,14 @@ def factor(self, nodelist): elt = nodelist[0] t = elt[0] - node = self.com_node(nodelist[-1]) + node = self.lookup_node(nodelist[-1])(nodelist[-1][1:]) # need to handle (unary op)constant here... if t == token.PLUS: - node = UnaryAdd(node) - node.lineno = elt[2] + return UnaryAdd(node, lineno=elt[2]) elif t == token.MINUS: - node = UnarySub(node) - node.lineno = elt[2] + return UnarySub(node, lineno=elt[2]) elif t == token.TILDE: - node = Invert(node) - node.lineno = elt[2] + node = Invert(node, lineno=elt[2]) return node def power(self, nodelist): @@ -670,49 +715,38 @@ for i in range(1, len(nodelist)): elt = nodelist[i] if elt[0] == token.DOUBLESTAR: - n = Power([node, self.com_node(nodelist[i+1])]) - n.lineno = elt[2] - return n + return Power([node, self.com_node(nodelist[i+1])], + lineno=elt[2]) node = self.com_apply_trailer(node, elt) return node def atom(self, nodelist): - n = self._atom_dispatch[nodelist[0][0]](nodelist) - n.lineno = nodelist[0][2] - return n + return self._atom_dispatch[nodelist[0][0]](nodelist) def atom_lpar(self, nodelist): if nodelist[1][0] == token.RPAR: - n = Tuple(()) - n.lineno = nodelist[0][2] - return n + return Tuple((), lineno=nodelist[0][2]) return self.com_node(nodelist[1]) def atom_lsqb(self, nodelist): if nodelist[1][0] == token.RSQB: - n = List(()) - n.lineno = nodelist[0][2] - return n + return List((), lineno=nodelist[0][2]) return self.com_list_constructor(nodelist[1]) def atom_lbrace(self, nodelist): if nodelist[1][0] == token.RBRACE: - return Dict(()) + return Dict((), lineno=nodelist[0][2]) return self.com_dictmaker(nodelist[1]) def atom_backquote(self, nodelist): - n = Backquote(self.com_node(nodelist[1])) - n.lineno = nodelist[0][2] - return n + return Backquote(self.com_node(nodelist[1])) def atom_number(self, nodelist): ### need to verify this matches compile.c k = eval(nodelist[0][1]) - n = Const(k) - n.lineno = nodelist[0][2] - return n + return Const(k, lineno=nodelist[0][2]) def decode_literal(self, lit): if self.encoding: @@ -729,15 +763,10 @@ k = '' for node in nodelist: k += self.decode_literal(node[1]) - n = Const(k) - n.lineno = nodelist[0][2] - return n + return Const(k, lineno=nodelist[0][2]) def atom_name(self, nodelist): - ### any processing to do? - n = Name(nodelist[0][1]) - n.lineno = nodelist[0][2] - return n + return Name(nodelist[0][1], lineno=nodelist[0][2]) # -------------------------------------------------------------- # @@ -805,16 +834,15 @@ names.append(self.com_fpdef(node)) i = i + 1 - if i >= len(nodelist): - break - - if nodelist[i][0] == token.EQUAL: + if i < len(nodelist) and nodelist[i][0] == token.EQUAL: defaults.append(self.com_node(nodelist[i + 1])) i = i + 2 elif len(defaults): - # Treat "(a=1, b)" as "(a=1, b=None)" - defaults.append(Const(None)) + # we have already seen an argument with default, but here + # came one without + raise SyntaxError, "non-default argument follows default argument" + # skip the comma i = i + 1 return names, defaults, flags @@ -843,48 +871,62 @@ return name[:-1] def com_dotted_as_name(self, node): - dot = self.com_dotted_name(node[1]) - if len(node) <= 2: + assert node[0] == symbol.dotted_as_name + node = node[1:] + dot = self.com_dotted_name(node[0][1:]) + if len(node) == 1: return dot, None - if node[0] == symbol.dotted_name: - pass - else: - assert node[2][1] == 'as' - assert node[3][0] == token.NAME - return dot, node[3][1] + assert node[1][1] == 'as' + assert node[2][0] == token.NAME + return dot, node[2][1] + def com_dotted_as_names(self, node): + assert node[0] == symbol.dotted_as_names + node = node[1:] + names = [self.com_dotted_as_name(node[0])] + for i in range(2, len(node), 2): + names.append(self.com_dotted_as_name(node[i])) + return names + def com_import_as_name(self, node): - if node[0] == token.STAR: - return '*', None assert node[0] == symbol.import_as_name node = node[1:] + assert node[0][0] == token.NAME if len(node) == 1: - assert node[0][0] == token.NAME return node[0][1], None - assert node[1][1] == 'as', node assert node[2][0] == token.NAME return node[0][1], node[2][1] + def com_import_as_names(self, node): + assert node[0] == symbol.import_as_names + node = node[1:] + names = [self.com_import_as_name(node[0])] + for i in range(2, len(node), 2): + names.append(self.com_import_as_name(node[i])) + return names + def com_bases(self, node): bases = [] for i in range(1, len(node), 2): bases.append(self.com_node(node[i])) return bases - def com_try_finally(self, nodelist): - # try_fin_stmt: "try" ":" suite "finally" ":" suite - n = TryFinally(self.com_node(nodelist[2]), - self.com_node(nodelist[5])) - n.lineno = nodelist[0][2] - return n + def com_try_except_finally(self, nodelist): + # ('try' ':' suite + # ((except_clause ':' suite)+ ['else' ':' suite] ['finally' ':' suite] + # | 'finally' ':' suite)) - def com_try_except(self, nodelist): - # try_except: 'try' ':' suite (except_clause ':' suite)* ['else' suite] + if nodelist[3][0] == token.NAME: + # first clause is a finally clause: only try-finally + return TryFinally(self.com_node(nodelist[2]), + self.com_node(nodelist[5]), + lineno=nodelist[0][2]) + #tryexcept: [TryNode, [except_clauses], elseNode)] - stmt = self.com_node(nodelist[2]) clauses = [] elseNode = None + finallyNode = None for i in range(3, len(nodelist), 3): node = nodelist[i] if node[0] == symbol.except_clause: @@ -900,11 +942,31 @@ clauses.append((expr1, expr2, self.com_node(nodelist[i+2]))) if node[0] == token.NAME: - elseNode = self.com_node(nodelist[i+2]) - n = TryExcept(self.com_node(nodelist[2]), clauses, elseNode) - n.lineno = nodelist[0][2] - return n + if node[1] == 'else': + elseNode = self.com_node(nodelist[i+2]) + elif node[1] == 'finally': + finallyNode = self.com_node(nodelist[i+2]) + try_except = TryExcept(self.com_node(nodelist[2]), clauses, elseNode, + lineno=nodelist[0][2]) + if finallyNode: + return TryFinally(try_except, finallyNode, lineno=nodelist[0][2]) + else: + return try_except + def com_with(self, nodelist): + # with_stmt: 'with' expr [with_var] ':' suite + expr = self.com_node(nodelist[1]) + body = self.com_node(nodelist[-1]) + if nodelist[2][0] == token.COLON: + var = None + else: + var = self.com_assign(nodelist[2][2], OP_ASSIGN) + return With(expr, var, body, lineno=nodelist[0][2]) + + def com_with_var(self, nodelist): + # with_var: 'as' expr + return self.com_node(nodelist[1]) + def com_augassign_op(self, node): assert node[0] == symbol.augassign return node[1] @@ -924,7 +986,7 @@ # loop to avoid trivial recursion while 1: t = node[0] - if t == symbol.exprlist or t == symbol.testlist: + if t in (symbol.exprlist, symbol.testlist, symbol.testlist_safe, symbol.testlist_gexp): if len(node) > 2: return self.com_assign_tuple(node, assigning) node = node[1] @@ -961,13 +1023,13 @@ else: raise SyntaxError, "can't assign to literal" else: - raise SyntaxError, "bad assignment" + raise SyntaxError, "bad assignment (%s)" % t def com_assign_tuple(self, node, assigning): assigns = [] for i in range(1, len(node), 2): assigns.append(self.com_assign(node[i], assigning)) - return AssTuple(assigns) + return AssTuple(assigns, lineno=extractLineNo(node)) def com_assign_list(self, node, assigning): assigns = [] @@ -977,12 +1039,10 @@ raise SyntaxError, "can't assign to list comprehension" assert node[i + 1][0] == token.COMMA, node[i + 1] assigns.append(self.com_assign(node[i], assigning)) - return AssList(assigns) + return AssList(assigns, lineno=extractLineNo(node)) def com_assign_name(self, node, assigning): - n = AssName(node[1], assigning) - n.lineno = node[2] - return n + return AssName(node[1], assigning, lineno=node[2]) def com_assign_trailer(self, primary, node, assigning): t = node[1][0] @@ -995,7 +1055,7 @@ raise SyntaxError, "unknown trailer type: %s" % t def com_assign_attr(self, primary, node, assigning): - return AssAttr(primary, node[1], assigning) + return AssAttr(primary, node[1], assigning, lineno=node[-1]) def com_binary(self, constructor, nodelist): "Compile 'NODE (OP NODE)*' into (type, [ node1, ..., nodeN ])." @@ -1007,7 +1067,7 @@ for i in range(0, l, 2): n = nodelist[i] items.append(self.lookup_node(n)(n[1:])) - return constructor(items) + return constructor(items, lineno=extractLineNo(nodelist)) def com_stmt(self, node): result = self.lookup_node(node)(node[1:]) @@ -1017,7 +1077,7 @@ return Stmt([result]) def com_append_stmt(self, stmts, node): - result = self.com_node(node) + result = self.lookup_node(node)(node[1:]) assert result is not None if isinstance(result, Stmt): stmts.extend(result.nodes) @@ -1036,7 +1096,7 @@ elif nodelist[i][0] == token.COMMA: continue values.append(self.com_node(nodelist[i])) - return List(values) + return List(values, lineno=values[0].lineno) def com_list_comprehension(self, expr, node): # list_iter: list_for | list_if @@ -1061,8 +1121,7 @@ node = self.com_list_iter(node[5]) elif t == 'if': test = self.com_node(node[2]) - newif = ListCompIf(test) - newif.lineno = node[1][2] + newif = ListCompIf(test, lineno=node[1][2]) newfor.ifs.append(newif) if len(node) == 3: node = None @@ -1072,9 +1131,7 @@ raise SyntaxError, \ ("unexpected list comprehension element: %s %d" % (node, lineno)) - n = ListComp(expr, fors) - n.lineno = lineno - return n + return ListComp(expr, fors, lineno=lineno) def com_list_iter(self, node): assert node[0] == symbol.list_iter @@ -1084,15 +1141,54 @@ values = [] for i in range(1, len(nodelist), 2): values.append(self.com_node(nodelist[i])) - return List(values) + return List(values, lineno=values[0].lineno) + if hasattr(symbol, 'gen_for'): + def com_generator_expression(self, expr, node): + # gen_iter: gen_for | gen_if + # gen_for: 'for' exprlist 'in' test [gen_iter] + # gen_if: 'if' test [gen_iter] + + lineno = node[1][2] + fors = [] + while node: + t = node[1][1] + if t == 'for': + assignNode = self.com_assign(node[2], OP_ASSIGN) + genNode = self.com_node(node[4]) + newfor = GenExprFor(assignNode, genNode, [], + lineno=node[1][2]) + fors.append(newfor) + if (len(node)) == 5: + node = None + else: + node = self.com_gen_iter(node[5]) + elif t == 'if': + test = self.com_node(node[2]) + newif = GenExprIf(test, lineno=node[1][2]) + newfor.ifs.append(newif) + if len(node) == 3: + node = None + else: + node = self.com_gen_iter(node[3]) + else: + raise SyntaxError, \ + ("unexpected generator expression element: %s %d" + % (node, lineno)) + fors[0].is_outmost = True + return GenExpr(GenExprInner(expr, fors), lineno=lineno) + + def com_gen_iter(self, node): + assert node[0] == symbol.gen_iter + return node[1] + def com_dictmaker(self, nodelist): # dictmaker: test ':' test (',' test ':' value)* [','] items = [] for i in range(1, len(nodelist), 4): items.append((self.com_node(nodelist[i]), self.com_node(nodelist[i+2]))) - return Dict(items) + return Dict(items, lineno=items[0][0].lineno) def com_apply_trailer(self, primaryNode, nodelist): t = nodelist[1][0] @@ -1108,13 +1204,11 @@ def com_select_member(self, primaryNode, nodelist): if nodelist[0] != token.NAME: raise SyntaxError, "member must be a name" - n = Getattr(primaryNode, nodelist[1]) - n.lineno = nodelist[2] - return n + return Getattr(primaryNode, nodelist[1], lineno=nodelist[2]) def com_call_function(self, primaryNode, nodelist): if nodelist[0] == token.RPAR: - return CallFunc(primaryNode, []) + return CallFunc(primaryNode, [], lineno=extractLineNo(nodelist)) args = [] kw = 0 len_nodelist = len(nodelist) @@ -1123,6 +1217,13 @@ if node[0] == token.STAR or node[0] == token.DOUBLESTAR: break kw, result = self.com_argument(node, kw) + + if len_nodelist != 2 and isinstance(result, GenExpr) \ + and len(node) == 3 and node[2][0] == symbol.gen_for: + # allow f(x for x in y), but reject f(x for x in y, 1) + # should use f((x for x in y), 1) instead of f(x for x in y, 1) + raise SyntaxError, 'generator expression needs parenthesis' + args.append(result) else: # No broken by star arg, so skip the last one we processed. @@ -1145,10 +1246,13 @@ dstar_node = self.com_node(ch) else: raise SyntaxError, 'unknown node type: %s' % tok + return CallFunc(primaryNode, args, star_node, dstar_node, + lineno=extractLineNo(nodelist)) - return CallFunc(primaryNode, args, star_node, dstar_node) - def com_argument(self, nodelist, kw): + if len(nodelist) == 3 and nodelist[2][0] == symbol.gen_for: + test = self.com_node(nodelist[1]) + return 0, self.com_generator_expression(test, nodelist[2]) if len(nodelist) == 2: if kw: raise SyntaxError, "non-keyword arg after keyword arg" @@ -1159,8 +1263,7 @@ n = n[1] if n[0] != token.NAME: raise SyntaxError, "keyword can't be an expression (%s)"%n[0] - node = Keyword(n[1], result) - node.lineno = n[2] + node = Keyword(n[1], result, lineno=n[2]) return 1, node def com_subscriptlist(self, primary, nodelist, assigning): @@ -1180,9 +1283,9 @@ subscripts = [] for i in range(1, len(nodelist), 2): subscripts.append(self.com_subscript(nodelist[i])) + return Subscript(primary, assigning, subscripts, + lineno=extractLineNo(nodelist)) - return Subscript(primary, assigning, subscripts) - def com_subscript(self, node): # slice_item: expression | proper_slice | ellipsis ch = node[1] @@ -1227,9 +1330,8 @@ items.append(Const(None)) else: items.append(self.com_node(ch[2])) + return Sliceobj(items, lineno=extractLineNo(node)) - return Sliceobj(items) - def com_slice(self, primary, node, assigning): # short_slice: [lower_bound] ":" [upper_bound] lower = upper = None @@ -1241,7 +1343,8 @@ elif len(node) == 4: lower = self.com_node(node[1]) upper = self.com_node(node[3]) - return Slice(primary, assigning, lower, upper) + return Slice(primary, assigning, lower, upper, + lineno=extractLineNo(node)) def get_docstring(self, node, n=None): if n is None: @@ -1279,6 +1382,7 @@ symbol.testlist, symbol.testlist_safe, symbol.test, + symbol.or_test, symbol.and_test, symbol.not_test, symbol.comparison, @@ -1328,6 +1432,7 @@ symbol.while_stmt, symbol.for_stmt, symbol.try_stmt, + symbol.with_stmt, symbol.suite, symbol.testlist, symbol.testlist_safe, @@ -1349,9 +1454,12 @@ if hasattr(symbol, 'yield_stmt'): _legal_node_types.append(symbol.yield_stmt) +if hasattr(symbol, 'yield_expr'): + _legal_node_types.append(symbol.yield_expr) _assign_types = [ symbol.test, + symbol.or_test, symbol.and_test, symbol.not_test, symbol.comparison, @@ -1364,7 +1472,6 @@ symbol.factor, ] -import types _names = {} for k, v in symbol.sym_name.items(): _names[k] = v @@ -1374,9 +1481,9 @@ def debug_tree(tree): l = [] for elt in tree: - if type(elt) == types.IntType: + if isinstance(elt, int): l.append(_names.get(elt, elt)) - elif type(elt) == types.StringType: + elif isinstance(elt, str): l.append(elt) else: l.append(debug_tree(elt)) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2009-04-07 13:10:03
|
Revision: 6186 http://jython.svn.sourceforge.net/jython/?rev=6186&view=rev Author: zyasoft Date: 2009-04-07 13:09:48 +0000 (Tue, 07 Apr 2009) Log Message: ----------- Added testing of newlist (derived from test_userlist), and some of these tests now pass. Modified Paths: -------------- branches/newlist/CoreExposed.includes branches/newlist/src/org/python/core/PyNewList.java branches/newlist/src/templates/README.txt branches/newlist/src/templates/mappings Added Paths: ----------- branches/newlist/Lib/test/test_newlist.py Modified: branches/newlist/CoreExposed.includes =================================================================== --- branches/newlist/CoreExposed.includes 2009-04-07 05:48:59 UTC (rev 6185) +++ branches/newlist/CoreExposed.includes 2009-04-07 13:09:48 UTC (rev 6186) @@ -25,6 +25,7 @@ org/python/core/PyMethod.class org/python/core/PyMethodDescr.class org/python/core/PyModule.class +org/python/core/PyNewList.class org/python/core/PyNone.class org/python/core/PyObject.class org/python/core/PyProperty.class Added: branches/newlist/Lib/test/test_newlist.py =================================================================== --- branches/newlist/Lib/test/test_newlist.py (rev 0) +++ branches/newlist/Lib/test/test_newlist.py 2009-04-07 13:09:48 UTC (rev 6186) @@ -0,0 +1,60 @@ +# Check every path through every method of UserList + +from org.python.core import PyNewList as UserList +import unittest +from test import test_support, list_tests + +class UserListTest(list_tests.CommonTest): + type2test = UserList + + def test_getslice(self): + super(UserListTest, self).test_getslice() + l = [0, 1, 2, 3, 4] + u = self.type2test(l) + for i in range(-3, 6): + self.assertEqual(u[:i], l[:i]) + self.assertEqual(u[i:], l[i:]) + for j in xrange(-3, 6): + self.assertEqual(u[i:j], l[i:j]) + + def test_add_specials(self): + u = UserList("spam") + u2 = u + "eggs" + self.assertEqual(u2, list("spameggs")) + + def test_radd_specials(self): + u = UserList("eggs") + u2 = "spam" + u + self.assertEqual(u2, list("spameggs")) + u2 = u.__radd__(UserList("spam")) + self.assertEqual(u2, list("spameggs")) + + def test_iadd(self): + super(UserListTest, self).test_iadd() + u = [0, 1] + u += UserList([0, 1]) + self.assertEqual(u, [0, 1, 0, 1]) + + def test_mixedcmp(self): + u = self.type2test([0, 1]) + self.assertEqual(u, [0, 1]) + self.assertNotEqual(u, [0]) + self.assertNotEqual(u, [0, 2]) + + def test_mixedadd(self): + u = self.type2test([0, 1]) + self.assertEqual(u + [], u) + self.assertEqual(u + [2], [0, 1, 2]) + + def test_getitemoverwriteiter(self): + # Verify that __getitem__ overrides *are* recognized by __iter__ + class T(self.type2test): + def __getitem__(self, key): + return str(key) + '!!!' + self.assertEqual(iter(T((1,2))).next(), "0!!!") + +def test_main(): + test_support.run_unittest(UserListTest) + +if __name__ == "__main__": + test_main() Modified: branches/newlist/src/org/python/core/PyNewList.java =================================================================== --- branches/newlist/src/org/python/core/PyNewList.java 2009-04-07 05:48:59 UTC (rev 6185) +++ branches/newlist/src/org/python/core/PyNewList.java 2009-04-07 13:09:48 UTC (rev 6186) @@ -10,6 +10,7 @@ import org.python.util.Generic; import java.util.Collection; +import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.ListIterator; @@ -65,7 +66,7 @@ public PyNewList(PyObject o) { this(TYPE); for (PyObject item : o.asIterable()) { - append(item); + list.add(item); } } @@ -86,8 +87,8 @@ @ExposedNew @ExposedMethod(doc = BuiltinDocs.list___init___doc) - final void list___init__(PyObject[] args, String[] kwds) { - ArgParser ap = new ArgParser("list", args, kwds, new String[] {"sequence"}, 0); + final void newlist___init__(PyObject[] args, String[] kwds) { + ArgParser ap = new ArgParser("newlist", args, kwds, new String[] {"sequence"}, 0); PyObject seq = ap.getPyObject(0, null); clear(); if(seq == null) { @@ -104,11 +105,11 @@ @Override public int __len__() { - return list___len__(); + return newlist___len__(); } @ExposedMethod(doc = BuiltinDocs.list___len___doc) - final int list___len__() { + final int newlist___len__() { return size(); } @@ -127,12 +128,17 @@ if(stop < start) { stop = start; } - if ((value instanceof PySequence) || (!(value instanceof List))) { + if (value instanceof PySequenceList) { + setsliceIterator(start, stop, step, ((PySequenceList)value).listIterator()); + } + else if ((value instanceof PySequence) || (!(value instanceof List))) { + System.err.println("PySequence"); if (value == this) { // copy value = new PyNewList((PySequence)value); } setsliceIterator(start, stop, step, value.asIterable().iterator()); } else { + System.err.println("List"); List valueList = (List)value.__tojava__(List.class); if(valueList != null && valueList != Py.NoConversion) { setsliceList(start, stop, step, valueList); @@ -146,32 +152,28 @@ ((ArrayList) list).ensureCapacity(start + n); } ListIterator src = value.listIterator(); - if (step == 1) { - ListIterator<PyObject> dest = list.subList(start, stop).listIterator(); - while (dest.hasNext() && src.hasNext()) { - dest.set(Py.java2py(src.next())); - } - } else { - for (int j = start; j < stop && src.hasNext(); j += step) { - set(j, src.next()); - } + for (int j = start; src.hasNext(); j += step) { + set(j, src.next()); } } protected void setsliceIterator(int start, int stop, int step, Iterator<PyObject> iter) { int n = sliceLength(start, stop, step); + int size = list.size(); + int delta = start > size ? n : start - size + n; if (list instanceof ArrayList) { - ((ArrayList) list).ensureCapacity(start + n); + ((ArrayList) list).ensureCapacity(size + delta); } - if (step == 1) { - ListIterator<PyObject> dest = list.subList(start, stop).listIterator(); - while (dest.hasNext() && iter.hasNext()) { - dest.set(iter.next()); + System.err.println("setsliceIterator: start=" + start + ",stop=" + stop + ",step=" + step + ",n=" + n + ",delta=" + delta); + + for (int j = start; iter.hasNext(); j += step) { + System.err.print(this); + if (j > size) { + list.add(iter.next()); + } else { + list.set(j, iter.next()); } - } else { - for (int i = 0, j = start; i < n && iter.hasNext(); i++, j += step) { - set(j, iter.next()); - } + System.err.println("-> " + this); } } @@ -195,42 +197,42 @@ } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___ne___doc) - final PyObject list___ne__(PyObject o) { + final PyObject newlist___ne__(PyObject o) { return seq___ne__(o); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___eq___doc) - final PyObject list___eq__(PyObject o) { + final PyObject newlist___eq__(PyObject o) { return seq___eq__(o); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___lt___doc) - final PyObject list___lt__(PyObject o) { + final PyObject newlist___lt__(PyObject o) { return seq___lt__(o); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___le___doc) - final PyObject list___le__(PyObject o) { + final PyObject newlist___le__(PyObject o) { return seq___le__(o); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___gt___doc) - final PyObject list___gt__(PyObject o) { + final PyObject newlist___gt__(PyObject o) { return seq___gt__(o); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___ge___doc) - final PyObject list___ge__(PyObject o) { + final PyObject newlist___ge__(PyObject o) { return seq___ge__(o); } @Override public PyObject __imul__(PyObject o) { - return list___imul__(o); + return newlist___imul__(o); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___imul___doc) - final PyObject list___imul__(PyObject o) { + final PyObject newlist___imul__(PyObject o) { if (!o.isIndex()) { return null; } @@ -264,11 +266,11 @@ @Override public PyObject __mul__(PyObject o) { - return list___mul__(o); + return newlist___mul__(o); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___mul___doc) - final PyObject list___mul__(PyObject o) { + final PyObject newlist___mul__(PyObject o) { if (!o.isIndex()) { return null; } @@ -277,11 +279,11 @@ @Override public PyObject __rmul__(PyObject o) { - return list___rmul__(o); + return newlist___rmul__(o); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___rmul___doc) - final PyObject list___rmul__(PyObject o) { + final PyObject newlist___rmul__(PyObject o) { if (!o.isIndex()) { return null; } @@ -290,11 +292,11 @@ @Override public PyObject __add__(PyObject o) { - return list___add__(o); + return newlist___add__(o); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___add___doc) - final PyObject list___add__(PyObject o) { + final PyObject newlist___add__(PyObject o) { PyNewList sum = null; if (o instanceof PySequenceList && !(o instanceof PyTuple)) { if (o instanceof PyNewList) { @@ -310,7 +312,7 @@ if(oList != Py.NoConversion && oList != null) { List otherList = (List) oList; sum = new PyNewList(); - sum.list_extend(this); + sum.newlist_extend(this); for(Iterator i = otherList.iterator(); i.hasNext();) { sum.add(i.next()); } @@ -321,12 +323,12 @@ @Override public PyObject __radd__(PyObject o) { - return list___radd__(o); + return newlist___radd__(o); } //XXX: needs __doc__ @ExposedMethod(type = MethodType.BINARY) - final PyObject list___radd__(PyObject o) { + final PyObject newlist___radd__(PyObject o) { // Support adding java.util.List, but prevent adding PyTuple. // 'o' should never be a PyNewList since __add__ is defined. PyNewList sum = null; @@ -343,22 +345,22 @@ } @ExposedMethod(doc = BuiltinDocs.list___contains___doc) - final boolean list___contains__(PyObject o) { + final boolean newlist___contains__(PyObject o) { return object___contains__(o); } @ExposedMethod(doc = BuiltinDocs.list___delitem___doc) - final void list___delitem__(PyObject index) { + final void newlist___delitem__(PyObject index) { seq___delitem__(index); } @ExposedMethod(doc = BuiltinDocs.list___setitem___doc) - final void list___setitem__(PyObject o, PyObject def) { + final void newlist___setitem__(PyObject o, PyObject def) { seq___setitem__(o, def); } @ExposedMethod(doc = BuiltinDocs.list___getitem___doc) - final PyObject list___getitem__(PyObject o) { + final PyObject newlist___getitem__(PyObject o) { PyObject ret = seq___finditem__(o); if(ret == null) { throw Py.IndexError("index out of range: " + o); @@ -368,26 +370,26 @@ @Override public PyObject __iter__() { - return list___iter__(); + return newlist___iter__(); } @ExposedMethod(doc = BuiltinDocs.list___iter___doc) - public PyObject list___iter__() { + public PyObject newlist___iter__() { return new PyFastSequenceIter(this); } @ExposedMethod(defaults = "null", doc = BuiltinDocs.list___getslice___doc) - final PyObject list___getslice__(PyObject start, PyObject stop, PyObject step) { + final PyObject newlist___getslice__(PyObject start, PyObject stop, PyObject step) { return seq___getslice__(start, stop, step); } @ExposedMethod(defaults = "null", doc = BuiltinDocs.list___setslice___doc) - final void list___setslice__(PyObject start, PyObject stop, PyObject step, PyObject value) { + final void newlist___setslice__(PyObject start, PyObject stop, PyObject step, PyObject value) { seq___setslice__(start, stop, step, value); } @ExposedMethod(defaults = "null", doc = BuiltinDocs.list___delslice___doc) - final void list___delslice__(PyObject start, PyObject stop, PyObject step) { + final void newlist___delslice__(PyObject start, PyObject stop, PyObject step) { seq___delslice__(start, stop, step); } @@ -400,12 +402,12 @@ } public String toString() { - return list_toString(); + return newlist_toString(); } //XXX: needs __doc__ @ExposedMethod(names = "__repr__") - final String list_toString() { + final String newlist_toString() { ThreadState ts = Py.getThreadState(); if(!ts.enterRepr(this)) { return "[...]"; @@ -432,11 +434,11 @@ * the element to add. */ public void append(PyObject o) { - list_append(o); + newlist_append(o); } @ExposedMethod(doc = BuiltinDocs.list_append_doc) - final void list_append(PyObject o) { + final void newlist_append(PyObject o) { pyadd(o); gListAllocatedStatus = __len__(); } @@ -448,11 +450,11 @@ * the argument to test for. Testing is done with the <code>==</code> operator. */ public int count(PyObject o) { - return list_count(o); + return newlist_count(o); } @ExposedMethod(doc = BuiltinDocs.list_count_doc) - final int list_count(PyObject o) { + final int newlist_count(PyObject o) { int count = 0; PyObject[] array = getArray(); for(int i = 0, n = size(); i < n; i++) { @@ -474,29 +476,29 @@ } public int index(PyObject o, int start) { - return list_index(o, start, size()); + return newlist_index(o, start, size()); } public int index(PyObject o, int start, int stop) { - return list_index(o, start, stop); + return newlist_index(o, start, stop); } @ExposedMethod(defaults = {"null", "null"}, doc = BuiltinDocs.list_index_doc) - final int list_index(PyObject o, PyObject start, PyObject stop) { + final int newlist_index(PyObject o, PyObject start, PyObject stop) { int startInt = start == null ? 0 : PySlice.calculateSliceIndex(start); int stopInt = stop == null ? size() : PySlice.calculateSliceIndex(stop); - return list_index(o, startInt, stopInt); + return newlist_index(o, startInt, stopInt); } - final int list_index(PyObject o, int start, int stop) { + final int newlist_index(PyObject o, int start, int stop) { return _index(o, "list.index(x): x not in list", start, stop); } - final int list_index(PyObject o, int start) { + final int newlist_index(PyObject o, int start) { return _index(o, "list.index(x): x not in list", start, size()); } - final int list_index(PyObject o) { + final int newlist_index(PyObject o) { return _index(o, "list.index(x): x not in list", 0, size()); } @@ -523,11 +525,11 @@ * the element to insert. */ public void insert(int index, PyObject o) { - list_insert(index, o); + newlist_insert(index, o); } @ExposedMethod(doc = BuiltinDocs.list_insert_doc) - final void list_insert(int index, PyObject o) { + final void newlist_insert(int index, PyObject o) { if(index < 0) { index = Math.max(0, size() + index); } @@ -547,11 +549,11 @@ * the element to search for and remove. */ public void remove(PyObject o) { - list_remove(o); + newlist_remove(o); } @ExposedMethod(doc = BuiltinDocs.list_remove_doc) - final void list_remove(PyObject o) { + final void newlist_remove(PyObject o) { del(_index(o, "list.remove(x): x not in list", 0, size())); gListAllocatedStatus = __len__(); } @@ -562,20 +564,12 @@ * this side effect. */ public void reverse() { - list_reverse(); + newlist_reverse(); } @ExposedMethod(doc = BuiltinDocs.list_reverse_doc) - final void list_reverse() { - PyObject tmp; - int n = size(); - PyObject[] array = getArray(); - int j = n - 1; - for(int i = 0; i < n / 2; i++, j--) { - tmp = array[i]; - array[i] = array[j]; - array[j] = tmp; - } + final void newlist_reverse() { + Collections.reverse(list); gListAllocatedStatus = __len__(); } @@ -593,11 +587,11 @@ * the index of the element to remove and return. */ public PyObject pop(int n) { - return list_pop(n); + return newlist_pop(n); } @ExposedMethod(defaults = "-1", doc = BuiltinDocs.list_pop_doc) - final PyObject list_pop(int n) { + final PyObject newlist_pop(int n) { int length = size(); if(length == 0) { throw Py.IndexError("pop from empty list"); @@ -608,8 +602,7 @@ if(n < 0 || n >= length) { throw Py.IndexError("pop index out of range"); } - PyObject v = pyget(n); - setslice(n, n + 1, 1, Py.EmptyTuple); + PyObject v = list.remove(n); return v; } @@ -621,23 +614,33 @@ * the sequence of items to append to the list. */ public void extend(PyObject o) { - list_extend(o); + newlist_extend(o); } @ExposedMethod(doc = BuiltinDocs.list_extend_doc) - final void list_extend(PyObject o) { - int length = size(); - setslice(length, length, 1, o); + final void newlist_extend(PyObject o) { + if (o instanceof PyNewList) { + list.addAll(((PyNewList)o).list); + } else if (o instanceof PySequenceObjectList) { + PyObject other[] = ((PySequenceObjectList)o).getArray(); + for (int i = 0; i < other.length; i++) { + list.add(other[i]); + } + } else { + for (PyObject item : o.asIterable()) { + list.add(item); + } + } gListAllocatedStatus = __len__(); } @Override public PyObject __iadd__(PyObject o) { - return list___iadd__(o); + return newlist___iadd__(o); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___iadd___doc) - final PyObject list___iadd__(PyObject o) { + final PyObject newlist___iadd__(PyObject o) { PyType oType = o.getType(); if (oType == TYPE || oType == PyTuple.TYPE || this == o) { extend(fastSequence(o, "argument must be iterable")); @@ -676,7 +679,7 @@ @ExposedMethod(doc = BuiltinDocs.list_sort_doc) - final void list_sort(PyObject[] args, String[] kwds) { + final void newlist_sort(PyObject[] args, String[] kwds) { ArgParser ap = new ArgParser("list", args, kwds, new String[]{"cmp", "key", "reverse"}, 0); PyObject cmp = ap.getPyObject(0, Py.None); PyObject key = ap.getPyObject(1, Py.None); @@ -698,11 +701,11 @@ } public int hashCode() { - return list___hash__(); + return newlist___hash__(); } @ExposedMethod(doc = BuiltinDocs.list___hash___doc) - final int list___hash__() { + final int newlist___hash__() { throw Py.TypeError(String.format("unhashable type: '%.200s'", getType().fastGetName())); } @@ -745,7 +748,8 @@ add(element); } } - return c.size() > 0; } + return c.size() > 0; + } @Override public void clear() { Modified: branches/newlist/src/templates/README.txt =================================================================== --- branches/newlist/src/templates/README.txt 2009-04-07 05:48:59 UTC (rev 6185) +++ branches/newlist/src/templates/README.txt 2009-04-07 13:09:48 UTC (rev 6186) @@ -1,17 +1,24 @@ -Some classes have generated code to enable their usage within Jython. Each -such file will have a generated section that is created with the gexpose.py -script. For the PyInteger class it is created thus: +Derived classes (classes that allow for user extension) are created as follows: - python gexpose.py int.expose ../../jython/src/org/python/core/PyInteger.java +1. Create a template file xxx.derived +2. Modify mappings, which associates a template with a specific class in + the source tree to be generated +3. Run (with CPython) gderived.py against the the template file -For each class there is an xxx.expose file describing what should be exposed. +Example: creating a derivable version of int -In addition there is an xxxDerived.java class that is completely generated -with the script gderived.py. For the PyInteger class it is created thus: +from the file int.derived: - python gderived.py int.derived >../../jython/src/org/python/core/PyIntegerDerived.java + base_class: PyInteger + want_dict: true + ctr: int v + incl: object -There is an ant target to generate these automatically. See the template -target in the top-level build file, or the org.python.util.TemplateAntTask -ant task. In the future, the template generation will be linked into the -main build targets. +from mappings, the relevant entry (please keep sorted): + + int.derived:org.python.core.PyIntegerDerived + +To generate the source of the class, src/org/python/core/PyInteger.java: + + python gderived.py int.derived + Modified: branches/newlist/src/templates/mappings =================================================================== --- branches/newlist/src/templates/mappings 2009-04-07 05:48:59 UTC (rev 6185) +++ branches/newlist/src/templates/mappings 2009-04-07 13:09:48 UTC (rev 6186) @@ -25,6 +25,7 @@ long.derived:org.python.core.PyLongDerived local.derived:org.python.modules.thread.PyLocalDerived module.derived:org.python.core.PyModuleDerived +newlist.derived:org.python.core.PyNewListDerived object.derived:org.python.core.PyObjectDerived partial.derived:org.python.modules._functools.PyPartialDerived property.derived:org.python.core.PyPropertyDerived This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2009-04-07 05:49:03
|
Revision: 6185 http://jython.svn.sourceforge.net/jython/?rev=6185&view=rev Author: pjenvey Date: 2009-04-07 05:48:59 +0000 (Tue, 07 Apr 2009) Log Message: ----------- parser string input shouldn't go through universal newlines mode Modified Paths: -------------- trunk/jython/Lib/test/test_builtin_jy.py trunk/jython/Lib/test/test_codeop_jy.py trunk/jython/src/org/python/core/ParserFacade.java Modified: trunk/jython/Lib/test/test_builtin_jy.py =================================================================== --- trunk/jython/Lib/test/test_builtin_jy.py 2009-04-07 02:44:38 UTC (rev 6184) +++ trunk/jython/Lib/test/test_builtin_jy.py 2009-04-07 05:48:59 UTC (rev 6185) @@ -141,23 +141,29 @@ def test_parse_str_eval(self): foo = 'föö' - for code in ("'%s'" % foo.decode('utf-8'), - "# coding: utf-8\n'%s'" % foo, - "%s'%s'" % (BOM_UTF8, foo)): - mod = compile(code, 'foo.py', 'eval') - bar = eval(mod) - self.assertEqual(foo, bar) - bar = eval(code) - self.assertEqual(foo, bar) + for code, expected in ( + ("'%s'" % foo.decode('utf-8'), foo), + ("# coding: utf-8\n'%s'" % foo, foo), + ("%s'%s'" % (BOM_UTF8, foo), foo), + ("'\rfoo\r'", '\rfoo\r') + ): + mod = compile(code, 'test.py', 'eval') + result = eval(mod) + self.assertEqual(result, expected) + result = eval(code) + self.assertEqual(result, expected) def test_parse_str_exec(self): foo = 'föö' - for code in ("a = '%s'" % foo.decode('utf-8'), - "# coding: utf-8\na = '%s'" % foo, - "%sa = '%s'" % (BOM_UTF8, foo)): + for code, expected in ( + ("bar = '%s'" % foo.decode('utf-8'), foo), + ("# coding: utf-8\nbar = '%s'" % foo, foo), + ("%sbar = '%s'" % (BOM_UTF8, foo), foo), + ("bar = '\rfoo\r'", '\rfoo\r') + ): ns = {} exec code in ns - self.assertEqual(foo, ns['a']) + self.assertEqual(ns['bar'], expected) def test_general_eval(self): # Tests that general mappings can be used for the locals argument Modified: trunk/jython/Lib/test/test_codeop_jy.py =================================================================== --- trunk/jython/Lib/test/test_codeop_jy.py 2009-04-07 02:44:38 UTC (rev 6184) +++ trunk/jython/Lib/test/test_codeop_jy.py 2009-04-07 05:48:59 UTC (rev 6185) @@ -1,6 +1,7 @@ """ test compile. derived from test_codeop """ +import codeop import unittest from test import test_support from test.test_support import run_unittest @@ -183,8 +184,17 @@ compile("a = 1\n", "def", 'single').co_filename) +class CodeopTests(unittest.TestCase): + + def test_no_universal_newlines(self): + # previously \r was translated due to universal newlines + code = codeop.compile_command("'\rfoo\r'", symbol='eval') + self.assertEqual(eval(code), '\rfoo\r') + + def test_main(): - run_unittest(CompileTests) + run_unittest(CompileTests, + CodeopTests) if __name__ == "__main__": Modified: trunk/jython/src/org/python/core/ParserFacade.java =================================================================== --- trunk/jython/src/org/python/core/ParserFacade.java 2009-04-07 02:44:38 UTC (rev 6184) +++ trunk/jython/src/org/python/core/ParserFacade.java 2009-04-07 05:48:59 UTC (rev 6185) @@ -220,6 +220,15 @@ CompilerFlags cflags, String filename, boolean fromString) + throws IOException { + return prepBufReader(input, cflags, filename, fromString, true); + } + + private static ExpectedEncodingBufferedReader prepBufReader(InputStream input, + CompilerFlags cflags, + String filename, + boolean fromString, + boolean universalNewlines) throws IOException { input = new BufferedInputStream(input); boolean bom = adjustForBOM(input); @@ -240,12 +249,14 @@ } cflags.encoding = encoding; - // Enable universal newlines mode on the input - StreamIO rawIO = new StreamIO(input, true); - org.python.core.io.BufferedReader bufferedIO = - new org.python.core.io.BufferedReader(rawIO, 0); - UniversalIOWrapper textIO = new UniversalIOWrapper(bufferedIO); - input = new TextIOInputStream(textIO); + if (universalNewlines) { + // Enable universal newlines mode on the input + StreamIO rawIO = new StreamIO(input, true); + org.python.core.io.BufferedReader bufferedIO = + new org.python.core.io.BufferedReader(rawIO, 0); + UniversalIOWrapper textIO = new UniversalIOWrapper(bufferedIO); + input = new TextIOInputStream(textIO); + } Charset cs; try { @@ -270,7 +281,8 @@ private static ExpectedEncodingBufferedReader prepBufReader(String string, CompilerFlags cflags, - String filename) throws IOException { + String filename) + throws IOException { byte[] stringBytes; if (cflags.source_is_utf8) { // Passed unicode, re-encode the String to raw bytes @@ -285,7 +297,7 @@ } else { stringBytes = StringUtil.toBytes(string); } - return prepBufReader(new ByteArrayInputStream(stringBytes), cflags, filename, true); + return prepBufReader(new ByteArrayInputStream(stringBytes), cflags, filename, true, false); } /** This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2009-04-07 02:44:47
|
Revision: 6184 http://jython.svn.sourceforge.net/jython/?rev=6184&view=rev Author: fwierzbicki Date: 2009-04-07 02:44:38 +0000 (Tue, 07 Apr 2009) Log Message: ----------- Adding the committer Leonardo Soto. Modified Paths: -------------- trunk/jython/ACKNOWLEDGMENTS Modified: trunk/jython/ACKNOWLEDGMENTS =================================================================== --- trunk/jython/ACKNOWLEDGMENTS 2009-04-07 02:32:17 UTC (rev 6183) +++ trunk/jython/ACKNOWLEDGMENTS 2009-04-07 02:44:38 UTC (rev 6184) @@ -81,6 +81,7 @@ Khalid Zuberi Sean McGrath Clark Updike + Leonardo Soto Local Variables: mode: indented-text This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2009-04-07 02:32:28
|
Revision: 6183 http://jython.svn.sourceforge.net/jython/?rev=6183&view=rev Author: fwierzbicki Date: 2009-04-07 02:32:17 +0000 (Tue, 07 Apr 2009) Log Message: ----------- By Jim Baker's suggestion (I agree of course!) specifically acknowledging Alan's contribution of the long standing external project Modjy. Modified Paths: -------------- trunk/jython/ACKNOWLEDGMENTS Modified: trunk/jython/ACKNOWLEDGMENTS =================================================================== --- trunk/jython/ACKNOWLEDGMENTS 2009-04-07 02:23:13 UTC (rev 6182) +++ trunk/jython/ACKNOWLEDGMENTS 2009-04-07 02:32:17 UTC (rev 6183) @@ -31,6 +31,8 @@ Cyrille Morvan has written the code for the Jythonc ant task. + Alan Kennedy contributed modjy, which bridges WSGI to the Servlet API + A huge thanks goes to all the members of the jpython/jython mailing lists. Other folks who have contributed to JPython and Jython in ways large and small, in no particular order: @@ -74,7 +76,6 @@ Charlie Groves Otmar Humbel Philip Jenvey - Alan Kennedy Nicholas Riley Frank Wierzbicki Khalid Zuberi This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2009-04-07 02:23:22
|
Revision: 6182 http://jython.svn.sourceforge.net/jython/?rev=6182&view=rev Author: zyasoft Date: 2009-04-07 02:23:13 +0000 (Tue, 07 Apr 2009) Log Message: ----------- Changed my name to "Jim Baker" Modified Paths: -------------- trunk/jython/ACKNOWLEDGMENTS Modified: trunk/jython/ACKNOWLEDGMENTS =================================================================== --- trunk/jython/ACKNOWLEDGMENTS 2009-04-07 01:52:46 UTC (rev 6181) +++ trunk/jython/ACKNOWLEDGMENTS 2009-04-07 02:23:13 UTC (rev 6182) @@ -70,7 +70,7 @@ Tobias Ivarsson Lino Mastrodomenico S\xE9bastien Boisg\xE9rault - James Baker + Jim Baker Charlie Groves Otmar Humbel Philip Jenvey This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2009-04-07 01:53:11
|
Revision: 6181 http://jython.svn.sourceforge.net/jython/?rev=6181&view=rev Author: pjenvey Date: 2009-04-07 01:52:46 +0000 (Tue, 07 Apr 2009) Log Message: ----------- str/unicode don't need __unicode__, and having it actually confuses some code Modified Paths: -------------- trunk/jython/src/org/python/core/PyString.java trunk/jython/src/org/python/core/PyUnicode.java Modified: trunk/jython/src/org/python/core/PyString.java =================================================================== --- trunk/jython/src/org/python/core/PyString.java 2009-04-07 01:49:15 UTC (rev 6180) +++ trunk/jython/src/org/python/core/PyString.java 2009-04-07 01:52:46 UTC (rev 6181) @@ -101,12 +101,6 @@ } public PyUnicode __unicode__() { - return str___unicode__(); - } - - //XXX: need doc - @ExposedMethod - final PyUnicode str___unicode__() { return new PyUnicode(this); } Modified: trunk/jython/src/org/python/core/PyUnicode.java =================================================================== --- trunk/jython/src/org/python/core/PyUnicode.java 2009-04-07 01:49:15 UTC (rev 6180) +++ trunk/jython/src/org/python/core/PyUnicode.java 2009-04-07 01:52:46 UTC (rev 6181) @@ -213,10 +213,8 @@ return fmt.format(other); } - //XXX: needs doc - @ExposedMethod/*(doc = BuiltinDocs.unicode___unicode___doc)*/ - final PyUnicode unicode___unicode__() { - return str___unicode__(); + public PyUnicode __unicode__() { + return this; } @Override This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2009-04-07 01:49:36
|
Revision: 6180 http://jython.svn.sourceforge.net/jython/?rev=6180&view=rev Author: pjenvey Date: 2009-04-07 01:49:15 +0000 (Tue, 07 Apr 2009) Log Message: ----------- shadow sys.platform Modified Paths: -------------- trunk/jython/src/org/python/core/PySystemState.java Modified: trunk/jython/src/org/python/core/PySystemState.java =================================================================== --- trunk/jython/src/org/python/core/PySystemState.java 2009-04-07 00:37:45 UTC (rev 6179) +++ trunk/jython/src/org/python/core/PySystemState.java 2009-04-07 01:49:15 UTC (rev 6180) @@ -93,7 +93,6 @@ public static Properties registry; // = init_registry(); public static PyObject prefix; public static PyObject exec_prefix = Py.EmptyString; - public static PyString platform = new PyString("java"); public static final PyString byteorder = new PyString("big"); public static final int maxint = Integer.MAX_VALUE; @@ -110,6 +109,7 @@ // shadowed statics - don't use directly public static PyList warnoptions = new PyList(); public static PyObject builtins; + public static PyObject platform = new PyString("java"); public PyList meta_path; public PyList path_hooks; @@ -197,7 +197,6 @@ name == "__class__" || name == "registry" || name == "exec_prefix" || - name == "platform" || name == "packageManager") { throw Py.TypeError("readonly attribute"); } @@ -242,8 +241,7 @@ public synchronized PyObject getBuiltins() { if (shadowing == null) { return getDefaultBuiltins(); - } - else { + } else { return shadowing.builtins; } } @@ -260,8 +258,7 @@ public synchronized PyObject getWarnoptions() { if (shadowing == null) { return warnoptions; - } - else { + } else { return shadowing.warnoptions; } } @@ -274,6 +271,22 @@ } } + public synchronized PyObject getPlatform() { + if (shadowing == null) { + return platform; + } else { + return shadowing.platform; + } + } + + public synchronized void setPlatform(PyObject value) { + if (shadowing == null) { + platform = value; + } else { + shadowing.platform = value; + } + } + // xxx fix this accessors public PyObject __findattr_ex__(String name) { if (name == "exc_value") { @@ -298,6 +311,8 @@ return getWarnoptions(); } else if (name == "builtins") { return getBuiltins(); + } else if (name == "platform") { + return getPlatform(); } else { PyObject ret = super.__findattr_ex__(name); if (ret != null) { @@ -321,10 +336,12 @@ if (name == "builtins") { shadow(); setBuiltins(value); - } - else if (name == "warnoptions") { + } else if (name == "warnoptions") { shadow(); setWarnoptions(value); + } else if (name == "platform") { + shadow(); + setPlatform(value); } else { PyObject ret = getType().lookup(name); // xxx fix fix fix if (ret != null && ret._doset(this, value)) { @@ -1228,9 +1245,11 @@ class Shadow { PyObject builtins; PyList warnoptions; + PyObject platform; Shadow() { builtins = PySystemState.getDefaultBuiltins(); warnoptions = PySystemState.warnoptions; + platform = PySystemState.platform; } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2009-04-07 00:37:55
|
Revision: 6179 http://jython.svn.sourceforge.net/jython/?rev=6179&view=rev Author: pjenvey Date: 2009-04-07 00:37:45 +0000 (Tue, 07 Apr 2009) Log Message: ----------- these now match CPythonLib's Removed Paths: ------------- trunk/jython/Lib/test/cfgparser.1 trunk/jython/Lib/test/test_doctest.py Deleted: trunk/jython/Lib/test/cfgparser.1 =================================================================== --- trunk/jython/Lib/test/cfgparser.1 2009-04-07 00:33:46 UTC (rev 6178) +++ trunk/jython/Lib/test/cfgparser.1 2009-04-07 00:37:45 UTC (rev 6179) @@ -1,2 +0,0 @@ -[Foo Bar] -foo=newbar Deleted: trunk/jython/Lib/test/test_doctest.py =================================================================== --- trunk/jython/Lib/test/test_doctest.py 2009-04-07 00:33:46 UTC (rev 6178) +++ trunk/jython/Lib/test/test_doctest.py 2009-04-07 00:37:45 UTC (rev 6179) @@ -1,2437 +0,0 @@ -""" -Test script for doctest. -""" - -from test import test_support -import doctest -import warnings - -###################################################################### -## Sample Objects (used by test cases) -###################################################################### - -def sample_func(v): - """ - Blah blah - - >>> print sample_func(22) - 44 - - Yee ha! - """ - return v+v - -class SampleClass: - """ - >>> print 1 - 1 - - >>> # comments get ignored. so are empty PS1 and PS2 prompts: - >>> - ... - - Multiline example: - >>> sc = SampleClass(3) - >>> for i in range(10): - ... sc = sc.double() - ... print sc.get(), - 6 12 24 48 96 192 384 768 1536 3072 - """ - def __init__(self, val): - """ - >>> print SampleClass(12).get() - 12 - """ - self.val = val - - def double(self): - """ - >>> print SampleClass(12).double().get() - 24 - """ - return SampleClass(self.val + self.val) - - def get(self): - """ - >>> print SampleClass(-5).get() - -5 - """ - return self.val - - def a_staticmethod(v): - """ - >>> print SampleClass.a_staticmethod(10) - 11 - """ - return v+1 - a_staticmethod = staticmethod(a_staticmethod) - - def a_classmethod(cls, v): - """ - >>> print SampleClass.a_classmethod(10) - 12 - >>> print SampleClass(0).a_classmethod(10) - 12 - """ - return v+2 - a_classmethod = classmethod(a_classmethod) - - a_property = property(get, doc=""" - >>> print SampleClass(22).a_property - 22 - """) - - class NestedClass: - """ - >>> x = SampleClass.NestedClass(5) - >>> y = x.square() - >>> print y.get() - 25 - """ - def __init__(self, val=0): - """ - >>> print SampleClass.NestedClass().get() - 0 - """ - self.val = val - def square(self): - return SampleClass.NestedClass(self.val*self.val) - def get(self): - return self.val - -class SampleNewStyleClass(object): - r""" - >>> print '1\n2\n3' - 1 - 2 - 3 - """ - def __init__(self, val): - """ - >>> print SampleNewStyleClass(12).get() - 12 - """ - self.val = val - - def double(self): - """ - >>> print SampleNewStyleClass(12).double().get() - 24 - """ - return SampleNewStyleClass(self.val + self.val) - - def get(self): - """ - >>> print SampleNewStyleClass(-5).get() - -5 - """ - return self.val - -###################################################################### -## Fake stdin (for testing interactive debugging) -###################################################################### - -class _FakeInput: - """ - A fake input stream for pdb's interactive debugger. Whenever a - line is read, print it (to simulate the user typing it), and then - return it. The set of lines to return is specified in the - constructor; they should not have trailing newlines. - """ - def __init__(self, lines): - self.lines = lines - - def readline(self): - line = self.lines.pop(0) - print line - return line+'\n' - -###################################################################### -## Test Cases -###################################################################### - -def test_Example(): r""" -Unit tests for the `Example` class. - -Example is a simple container class that holds: - - `source`: A source string. - - `want`: An expected output string. - - `exc_msg`: An expected exception message string (or None if no - exception is expected). - - `lineno`: A line number (within the docstring). - - `indent`: The example's indentation in the input string. - - `options`: An option dictionary, mapping option flags to True or - False. - -These attributes are set by the constructor. `source` and `want` are -required; the other attributes all have default values: - - >>> example = doctest.Example('print 1', '1\n') - >>> (example.source, example.want, example.exc_msg, - ... example.lineno, example.indent, example.options) - ('print 1\n', '1\n', None, 0, 0, {}) - -The first three attributes (`source`, `want`, and `exc_msg`) may be -specified positionally; the remaining arguments should be specified as -keyword arguments: - - >>> exc_msg = 'IndexError: pop from an empty list' - >>> example = doctest.Example('[].pop()', '', exc_msg, - ... lineno=5, indent=4, - ... options={doctest.ELLIPSIS: True}) - >>> (example.source, example.want, example.exc_msg, - ... example.lineno, example.indent, example.options) - ('[].pop()\n', '', 'IndexError: pop from an empty list\n', 5, 4, {8: True}) - -The constructor normalizes the `source` string to end in a newline: - - Source spans a single line: no terminating newline. - >>> e = doctest.Example('print 1', '1\n') - >>> e.source, e.want - ('print 1\n', '1\n') - - >>> e = doctest.Example('print 1\n', '1\n') - >>> e.source, e.want - ('print 1\n', '1\n') - - Source spans multiple lines: require terminating newline. - >>> e = doctest.Example('print 1;\nprint 2\n', '1\n2\n') - >>> e.source, e.want - ('print 1;\nprint 2\n', '1\n2\n') - - >>> e = doctest.Example('print 1;\nprint 2', '1\n2\n') - >>> e.source, e.want - ('print 1;\nprint 2\n', '1\n2\n') - - Empty source string (which should never appear in real examples) - >>> e = doctest.Example('', '') - >>> e.source, e.want - ('\n', '') - -The constructor normalizes the `want` string to end in a newline, -unless it's the empty string: - - >>> e = doctest.Example('print 1', '1\n') - >>> e.source, e.want - ('print 1\n', '1\n') - - >>> e = doctest.Example('print 1', '1') - >>> e.source, e.want - ('print 1\n', '1\n') - - >>> e = doctest.Example('print', '') - >>> e.source, e.want - ('print\n', '') - -The constructor normalizes the `exc_msg` string to end in a newline, -unless it's `None`: - - Message spans one line - >>> exc_msg = 'IndexError: pop from an empty list' - >>> e = doctest.Example('[].pop()', '', exc_msg) - >>> e.exc_msg - 'IndexError: pop from an empty list\n' - - >>> exc_msg = 'IndexError: pop from an empty list\n' - >>> e = doctest.Example('[].pop()', '', exc_msg) - >>> e.exc_msg - 'IndexError: pop from an empty list\n' - - Message spans multiple lines - >>> exc_msg = 'ValueError: 1\n 2' - >>> e = doctest.Example('raise ValueError("1\n 2")', '', exc_msg) - >>> e.exc_msg - 'ValueError: 1\n 2\n' - - >>> exc_msg = 'ValueError: 1\n 2\n' - >>> e = doctest.Example('raise ValueError("1\n 2")', '', exc_msg) - >>> e.exc_msg - 'ValueError: 1\n 2\n' - - Empty (but non-None) exception message (which should never appear - in real examples) - >>> exc_msg = '' - >>> e = doctest.Example('raise X()', '', exc_msg) - >>> e.exc_msg - '\n' -""" - -def test_DocTest(): r""" -Unit tests for the `DocTest` class. - -DocTest is a collection of examples, extracted from a docstring, along -with information about where the docstring comes from (a name, -filename, and line number). The docstring is parsed by the `DocTest` -constructor: - - >>> docstring = ''' - ... >>> print 12 - ... 12 - ... - ... Non-example text. - ... - ... >>> print 'another\example' - ... another - ... example - ... ''' - >>> globs = {} # globals to run the test in. - >>> parser = doctest.DocTestParser() - >>> test = parser.get_doctest(docstring, globs, 'some_test', - ... 'some_file', 20) - >>> print test - <DocTest some_test from some_file:20 (2 examples)> - >>> len(test.examples) - 2 - >>> e1, e2 = test.examples - >>> (e1.source, e1.want, e1.lineno) - ('print 12\n', '12\n', 1) - >>> (e2.source, e2.want, e2.lineno) - ("print 'another\\example'\n", 'another\nexample\n', 6) - -Source information (name, filename, and line number) is available as -attributes on the doctest object: - - >>> (test.name, test.filename, test.lineno) - ('some_test', 'some_file', 20) - -The line number of an example within its containing file is found by -adding the line number of the example and the line number of its -containing test: - - >>> test.lineno + e1.lineno - 21 - >>> test.lineno + e2.lineno - 26 - -If the docstring contains inconsistant leading whitespace in the -expected output of an example, then `DocTest` will raise a ValueError: - - >>> docstring = r''' - ... >>> print 'bad\nindentation' - ... bad - ... indentation - ... ''' - >>> parser.get_doctest(docstring, globs, 'some_test', 'filename', 0) - Traceback (most recent call last): - ValueError: line 4 of the docstring for some_test has inconsistent leading whitespace: 'indentation' - -If the docstring contains inconsistent leading whitespace on -continuation lines, then `DocTest` will raise a ValueError: - - >>> docstring = r''' - ... >>> print ('bad indentation', - ... ... 2) - ... ('bad', 'indentation') - ... ''' - >>> parser.get_doctest(docstring, globs, 'some_test', 'filename', 0) - Traceback (most recent call last): - ValueError: line 2 of the docstring for some_test has inconsistent leading whitespace: '... 2)' - -If there's no blank space after a PS1 prompt ('>>>'), then `DocTest` -will raise a ValueError: - - >>> docstring = '>>>print 1\n1' - >>> parser.get_doctest(docstring, globs, 'some_test', 'filename', 0) - Traceback (most recent call last): - ValueError: line 1 of the docstring for some_test lacks blank after >>>: '>>>print 1' - -If there's no blank space after a PS2 prompt ('...'), then `DocTest` -will raise a ValueError: - - >>> docstring = '>>> if 1:\n...print 1\n1' - >>> parser.get_doctest(docstring, globs, 'some_test', 'filename', 0) - Traceback (most recent call last): - ValueError: line 2 of the docstring for some_test lacks blank after ...: '...print 1' - -""" - -def test_DocTestFinder(): r""" -Unit tests for the `DocTestFinder` class. - -DocTestFinder is used to extract DocTests from an object's docstring -and the docstrings of its contained objects. It can be used with -modules, functions, classes, methods, staticmethods, classmethods, and -properties. - -Finding Tests in Functions -~~~~~~~~~~~~~~~~~~~~~~~~~~ -For a function whose docstring contains examples, DocTestFinder.find() -will return a single test (for that function's docstring): - - >>> finder = doctest.DocTestFinder() - -We'll simulate a __file__ attr that ends in pyc: - - >>> import test.test_doctest - >>> old = test.test_doctest.__file__ - >>> test.test_doctest.__file__ = 'test_doctest.pyc' - - >>> tests = finder.find(sample_func) - - >>> print tests # doctest: +ELLIPSIS - [<DocTest sample_func from ...:13 (1 example)>] - -The exact name depends on how test_doctest was invoked, so allow for -leading path components. - - >>> tests[0].filename # doctest: +ELLIPSIS - '...test_doctest.py' - - >>> test.test_doctest.__file__ = old - - - >>> e = tests[0].examples[0] - >>> (e.source, e.want, e.lineno) - ('print sample_func(22)\n', '44\n', 3) - -By default, tests are created for objects with no docstring: - - >>> def no_docstring(v): - ... pass - >>> finder.find(no_docstring) - [] - -However, the optional argument `exclude_empty` to the DocTestFinder -constructor can be used to exclude tests for objects with empty -docstrings: - - >>> def no_docstring(v): - ... pass - >>> excl_empty_finder = doctest.DocTestFinder(exclude_empty=True) - >>> excl_empty_finder.find(no_docstring) - [] - -If the function has a docstring with no examples, then a test with no -examples is returned. (This lets `DocTestRunner` collect statistics -about which functions have no tests -- but is that useful? And should -an empty test also be created when there's no docstring?) - - >>> def no_examples(v): - ... ''' no doctest examples ''' - >>> finder.find(no_examples) # doctest: +ELLIPSIS - [<DocTest no_examples from ...:1 (no examples)>] - -Finding Tests in Classes -~~~~~~~~~~~~~~~~~~~~~~~~ -For a class, DocTestFinder will create a test for the class's -docstring, and will recursively explore its contents, including -methods, classmethods, staticmethods, properties, and nested classes. - - >>> finder = doctest.DocTestFinder() - >>> tests = finder.find(SampleClass) - >>> for t in tests: - ... print '%2s %s' % (len(t.examples), t.name) - 3 SampleClass - 3 SampleClass.NestedClass - 1 SampleClass.NestedClass.__init__ - 1 SampleClass.__init__ - 2 SampleClass.a_classmethod - 1 SampleClass.a_property - 1 SampleClass.a_staticmethod - 1 SampleClass.double - 1 SampleClass.get - -New-style classes are also supported: - - >>> tests = finder.find(SampleNewStyleClass) - >>> for t in tests: - ... print '%2s %s' % (len(t.examples), t.name) - 1 SampleNewStyleClass - 1 SampleNewStyleClass.__init__ - 1 SampleNewStyleClass.double - 1 SampleNewStyleClass.get - -Finding Tests in Modules -~~~~~~~~~~~~~~~~~~~~~~~~ -For a module, DocTestFinder will create a test for the class's -docstring, and will recursively explore its contents, including -functions, classes, and the `__test__` dictionary, if it exists: - - >>> # A module - >>> import new - >>> m = new.module('some_module') - >>> def triple(val): - ... ''' - ... >>> print triple(11) - ... 33 - ... ''' - ... return val*3 - >>> m.__dict__.update({ - ... 'sample_func': sample_func, - ... 'SampleClass': SampleClass, - ... '__doc__': ''' - ... Module docstring. - ... >>> print 'module' - ... module - ... ''', - ... '__test__': { - ... 'd': '>>> print 6\n6\n>>> print 7\n7\n', - ... 'c': triple}}) - - >>> finder = doctest.DocTestFinder() - >>> # Use module=test.test_doctest, to prevent doctest from - >>> # ignoring the objects since they weren't defined in m. - >>> import test.test_doctest - >>> tests = finder.find(m, module=test.test_doctest) - >>> for t in tests: - ... print '%2s %s' % (len(t.examples), t.name) - 1 some_module - 3 some_module.SampleClass - 3 some_module.SampleClass.NestedClass - 1 some_module.SampleClass.NestedClass.__init__ - 1 some_module.SampleClass.__init__ - 2 some_module.SampleClass.a_classmethod - 1 some_module.SampleClass.a_property - 1 some_module.SampleClass.a_staticmethod - 1 some_module.SampleClass.double - 1 some_module.SampleClass.get - 1 some_module.__test__.c - 2 some_module.__test__.d - 1 some_module.sample_func - -Duplicate Removal -~~~~~~~~~~~~~~~~~ -If a single object is listed twice (under different names), then tests -will only be generated for it once: - - >>> from test import doctest_aliases - >>> tests = excl_empty_finder.find(doctest_aliases) - >>> print len(tests) - 2 - >>> print tests[0].name - test.doctest_aliases.TwoNames - - TwoNames.f and TwoNames.g are bound to the same object. - We can't guess which will be found in doctest's traversal of - TwoNames.__dict__ first, so we have to allow for either. - - >>> tests[1].name.split('.')[-1] in ['f', 'g'] - True - -Empty Tests -~~~~~~~~~~~ -By default, an object with no doctests doesn't create any tests: - - >>> tests = doctest.DocTestFinder().find(SampleClass) - >>> for t in tests: - ... print '%2s %s' % (len(t.examples), t.name) - 3 SampleClass - 3 SampleClass.NestedClass - 1 SampleClass.NestedClass.__init__ - 1 SampleClass.__init__ - 2 SampleClass.a_classmethod - 1 SampleClass.a_property - 1 SampleClass.a_staticmethod - 1 SampleClass.double - 1 SampleClass.get - -By default, that excluded objects with no doctests. exclude_empty=False -tells it to include (empty) tests for objects with no doctests. This feature -is really to support backward compatibility in what doctest.master.summarize() -displays. - - >>> tests = doctest.DocTestFinder(exclude_empty=False).find(SampleClass) - >>> for t in tests: - ... print '%2s %s' % (len(t.examples), t.name) - 3 SampleClass - 3 SampleClass.NestedClass - 1 SampleClass.NestedClass.__init__ - 0 SampleClass.NestedClass.get - 0 SampleClass.NestedClass.square - 1 SampleClass.__init__ - 2 SampleClass.a_classmethod - 1 SampleClass.a_property - 1 SampleClass.a_staticmethod - 1 SampleClass.double - 1 SampleClass.get - -Turning off Recursion -~~~~~~~~~~~~~~~~~~~~~ -DocTestFinder can be told not to look for tests in contained objects -using the `recurse` flag: - - >>> tests = doctest.DocTestFinder(recurse=False).find(SampleClass) - >>> for t in tests: - ... print '%2s %s' % (len(t.examples), t.name) - 3 SampleClass - -Line numbers -~~~~~~~~~~~~ -DocTestFinder finds the line number of each example: - - >>> def f(x): - ... ''' - ... >>> x = 12 - ... - ... some text - ... - ... >>> # examples are not created for comments & bare prompts. - ... >>> - ... ... - ... - ... >>> for x in range(10): - ... ... print x, - ... 0 1 2 3 4 5 6 7 8 9 - ... >>> x//2 - ... 6 - ... ''' - >>> test = doctest.DocTestFinder().find(f)[0] - >>> [e.lineno for e in test.examples] - [1, 9, 12] -""" - -def test_DocTestParser(): r""" -Unit tests for the `DocTestParser` class. - -DocTestParser is used to parse docstrings containing doctest examples. - -The `parse` method divides a docstring into examples and intervening -text: - - >>> s = ''' - ... >>> x, y = 2, 3 # no output expected - ... >>> if 1: - ... ... print x - ... ... print y - ... 2 - ... 3 - ... - ... Some text. - ... >>> x+y - ... 5 - ... ''' - >>> parser = doctest.DocTestParser() - >>> for piece in parser.parse(s): - ... if isinstance(piece, doctest.Example): - ... print 'Example:', (piece.source, piece.want, piece.lineno) - ... else: - ... print ' Text:', `piece` - Text: '\n' - Example: ('x, y = 2, 3 # no output expected\n', '', 1) - Text: '' - Example: ('if 1:\n print x\n print y\n', '2\n3\n', 2) - Text: '\nSome text.\n' - Example: ('x+y\n', '5\n', 9) - Text: '' - -The `get_examples` method returns just the examples: - - >>> for piece in parser.get_examples(s): - ... print (piece.source, piece.want, piece.lineno) - ('x, y = 2, 3 # no output expected\n', '', 1) - ('if 1:\n print x\n print y\n', '2\n3\n', 2) - ('x+y\n', '5\n', 9) - -The `get_doctest` method creates a Test from the examples, along with the -given arguments: - - >>> test = parser.get_doctest(s, {}, 'name', 'filename', lineno=5) - >>> (test.name, test.filename, test.lineno) - ('name', 'filename', 5) - >>> for piece in test.examples: - ... print (piece.source, piece.want, piece.lineno) - ('x, y = 2, 3 # no output expected\n', '', 1) - ('if 1:\n print x\n print y\n', '2\n3\n', 2) - ('x+y\n', '5\n', 9) -""" - -class test_DocTestRunner: - def basics(): r""" -Unit tests for the `DocTestRunner` class. - -DocTestRunner is used to run DocTest test cases, and to accumulate -statistics. Here's a simple DocTest case we can use: - - >>> def f(x): - ... ''' - ... >>> x = 12 - ... >>> print x - ... 12 - ... >>> x//2 - ... 6 - ... ''' - >>> test = doctest.DocTestFinder().find(f)[0] - -The main DocTestRunner interface is the `run` method, which runs a -given DocTest case in a given namespace (globs). It returns a tuple -`(f,t)`, where `f` is the number of failed tests and `t` is the number -of tried tests. - - >>> doctest.DocTestRunner(verbose=False).run(test) - (0, 3) - -If any example produces incorrect output, then the test runner reports -the failure and proceeds to the next example: - - >>> def f(x): - ... ''' - ... >>> x = 12 - ... >>> print x - ... 14 - ... >>> x//2 - ... 6 - ... ''' - >>> test = doctest.DocTestFinder().find(f)[0] - >>> doctest.DocTestRunner(verbose=True).run(test) - ... # doctest: +ELLIPSIS - Trying: - x = 12 - Expecting nothing - ok - Trying: - print x - Expecting: - 14 - ********************************************************************** - File ..., line 4, in f - Failed example: - print x - Expected: - 14 - Got: - 12 - Trying: - x//2 - Expecting: - 6 - ok - (1, 3) -""" - def verbose_flag(): r""" -The `verbose` flag makes the test runner generate more detailed -output: - - >>> def f(x): - ... ''' - ... >>> x = 12 - ... >>> print x - ... 12 - ... >>> x//2 - ... 6 - ... ''' - >>> test = doctest.DocTestFinder().find(f)[0] - - >>> doctest.DocTestRunner(verbose=True).run(test) - Trying: - x = 12 - Expecting nothing - ok - Trying: - print x - Expecting: - 12 - ok - Trying: - x//2 - Expecting: - 6 - ok - (0, 3) - -If the `verbose` flag is unspecified, then the output will be verbose -iff `-v` appears in sys.argv: - - >>> # Save the real sys.argv list. - >>> old_argv = sys.argv - - >>> # If -v does not appear in sys.argv, then output isn't verbose. - >>> sys.argv = ['test'] - >>> doctest.DocTestRunner().run(test) - (0, 3) - - >>> # If -v does appear in sys.argv, then output is verbose. - >>> sys.argv = ['test', '-v'] - >>> doctest.DocTestRunner().run(test) - Trying: - x = 12 - Expecting nothing - ok - Trying: - print x - Expecting: - 12 - ok - Trying: - x//2 - Expecting: - 6 - ok - (0, 3) - - >>> # Restore sys.argv - >>> sys.argv = old_argv - -In the remaining examples, the test runner's verbosity will be -explicitly set, to ensure that the test behavior is consistent. - """ - def exceptions(): r""" -Tests of `DocTestRunner`'s exception handling. - -An expected exception is specified with a traceback message. The -lines between the first line and the type/value may be omitted or -replaced with any other string: - - >>> def f(x): - ... ''' - ... >>> x = 12 - ... >>> print x//0 - ... Traceback (most recent call last): - ... ZeroDivisionError: integer division or modulo by zero - ... ''' - >>> test = doctest.DocTestFinder().find(f)[0] - >>> doctest.DocTestRunner(verbose=False).run(test) - (0, 2) - -An example may not generate output before it raises an exception; if -it does, then the traceback message will not be recognized as -signaling an expected exception, so the example will be reported as an -unexpected exception: - - >>> def f(x): - ... ''' - ... >>> x = 12 - ... >>> print 'pre-exception output', x//0 - ... pre-exception output - ... Traceback (most recent call last): - ... ZeroDivisionError: integer division or modulo by zero - ... ''' - >>> test = doctest.DocTestFinder().find(f)[0] - >>> doctest.DocTestRunner(verbose=False).run(test) - ... # doctest: +ELLIPSIS - ********************************************************************** - File ..., line 4, in f - Failed example: - print 'pre-exception output', x//0 - Exception raised: - ... - ZeroDivisionError: integer division or modulo by zero - (1, 2) - -Exception messages may contain newlines: - - >>> def f(x): - ... r''' - ... >>> raise ValueError, 'multi\nline\nmessage' - ... Traceback (most recent call last): - ... ValueError: multi - ... line - ... message - ... ''' - >>> test = doctest.DocTestFinder().find(f)[0] - >>> doctest.DocTestRunner(verbose=False).run(test) - (0, 1) - -If an exception is expected, but an exception with the wrong type or -message is raised, then it is reported as a failure: - - >>> def f(x): - ... r''' - ... >>> raise ValueError, 'message' - ... Traceback (most recent call last): - ... ValueError: wrong message - ... ''' - >>> test = doctest.DocTestFinder().find(f)[0] - >>> doctest.DocTestRunner(verbose=False).run(test) - ... # doctest: +ELLIPSIS - ********************************************************************** - File ..., line 3, in f - Failed example: - raise ValueError, 'message' - Expected: - Traceback (most recent call last): - ValueError: wrong message - Got: - Traceback (most recent call last): - ... - ValueError: message - (1, 1) - -However, IGNORE_EXCEPTION_DETAIL can be used to allow a mismatch in the -detail: - - >>> def f(x): - ... r''' - ... >>> raise ValueError, 'message' #doctest: +IGNORE_EXCEPTION_DETAIL - ... Traceback (most recent call last): - ... ValueError: wrong message - ... ''' - >>> test = doctest.DocTestFinder().find(f)[0] - >>> doctest.DocTestRunner(verbose=False).run(test) - (0, 1) - -But IGNORE_EXCEPTION_DETAIL does not allow a mismatch in the exception type: - - >>> def f(x): - ... r''' - ... >>> raise ValueError, 'message' #doctest: +IGNORE_EXCEPTION_DETAIL - ... Traceback (most recent call last): - ... TypeError: wrong type - ... ''' - >>> test = doctest.DocTestFinder().find(f)[0] - >>> doctest.DocTestRunner(verbose=False).run(test) - ... # doctest: +ELLIPSIS - ********************************************************************** - File ..., line 3, in f - Failed example: - raise ValueError, 'message' #doctest: +IGNORE_EXCEPTION_DETAIL - Expected: - Traceback (most recent call last): - TypeError: wrong type - Got: - Traceback (most recent call last): - ... - ValueError: message - (1, 1) - -If an exception is raised but not expected, then it is reported as an -unexpected exception: - - >>> def f(x): - ... r''' - ... >>> 1//0 - ... 0 - ... ''' - >>> test = doctest.DocTestFinder().find(f)[0] - >>> doctest.DocTestRunner(verbose=False).run(test) - ... # doctest: +ELLIPSIS - ********************************************************************** - File ..., line 3, in f - Failed example: - 1//0 - Exception raised: - Traceback (most recent call last): - ... - ZeroDivisionError: integer division or modulo by zero - (1, 1) -""" - def optionflags(): r""" -Tests of `DocTestRunner`'s option flag handling. - -Several option flags can be used to customize the behavior of the test -runner. These are defined as module constants in doctest, and passed -to the DocTestRunner constructor (multiple constants should be or-ed -together). - -The DONT_ACCEPT_TRUE_FOR_1 flag disables matches between True/False -and 1/0: - - >>> def f(x): - ... '>>> True\n1\n' - - >>> # Without the flag: - >>> test = doctest.DocTestFinder().find(f)[0] - >>> doctest.DocTestRunner(verbose=False).run(test) - (0, 1) - - >>> # With the flag: - >>> test = doctest.DocTestFinder().find(f)[0] - >>> flags = doctest.DONT_ACCEPT_TRUE_FOR_1 - >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) - ... # doctest: +ELLIPSIS - ********************************************************************** - File ..., line 2, in f - Failed example: - True - Expected: - 1 - Got: - True - (1, 1) - -The DONT_ACCEPT_BLANKLINE flag disables the match between blank lines -and the '<BLANKLINE>' marker: - - >>> def f(x): - ... '>>> print "a\\n\\nb"\na\n<BLANKLINE>\nb\n' - - >>> # Without the flag: - >>> test = doctest.DocTestFinder().find(f)[0] - >>> doctest.DocTestRunner(verbose=False).run(test) - (0, 1) - - >>> # With the flag: - >>> test = doctest.DocTestFinder().find(f)[0] - >>> flags = doctest.DONT_ACCEPT_BLANKLINE - >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) - ... # doctest: +ELLIPSIS - ********************************************************************** - File ..., line 2, in f - Failed example: - print "a\n\nb" - Expected: - a - <BLANKLINE> - b - Got: - a - <BLANKLINE> - b - (1, 1) - -The NORMALIZE_WHITESPACE flag causes all sequences of whitespace to be -treated as equal: - - >>> def f(x): - ... '>>> print 1, 2, 3\n 1 2\n 3' - - >>> # Without the flag: - >>> test = doctest.DocTestFinder().find(f)[0] - >>> doctest.DocTestRunner(verbose=False).run(test) - ... # doctest: +ELLIPSIS - ********************************************************************** - File ..., line 2, in f - Failed example: - print 1, 2, 3 - Expected: - 1 2 - 3 - Got: - 1 2 3 - (1, 1) - - >>> # With the flag: - >>> test = doctest.DocTestFinder().find(f)[0] - >>> flags = doctest.NORMALIZE_WHITESPACE - >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) - (0, 1) - - An example from the docs: - >>> print range(20) #doctest: +NORMALIZE_WHITESPACE - [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, - 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] - -The ELLIPSIS flag causes ellipsis marker ("...") in the expected -output to match any substring in the actual output: - - >>> def f(x): - ... '>>> print range(15)\n[0, 1, 2, ..., 14]\n' - - >>> # Without the flag: - >>> test = doctest.DocTestFinder().find(f)[0] - >>> doctest.DocTestRunner(verbose=False).run(test) - ... # doctest: +ELLIPSIS - ********************************************************************** - File ..., line 2, in f - Failed example: - print range(15) - Expected: - [0, 1, 2, ..., 14] - Got: - [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] - (1, 1) - - >>> # With the flag: - >>> test = doctest.DocTestFinder().find(f)[0] - >>> flags = doctest.ELLIPSIS - >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) - (0, 1) - - ... also matches nothing: - - >>> for i in range(100): - ... print i**2, #doctest: +ELLIPSIS - 0 1...4...9 16 ... 36 49 64 ... 9801 - - ... can be surprising; e.g., this test passes: - - >>> for i in range(21): #doctest: +ELLIPSIS - ... print i, - 0 1 2 ...1...2...0 - - Examples from the docs: - - >>> print range(20) # doctest:+ELLIPSIS - [0, 1, ..., 18, 19] - - >>> print range(20) # doctest: +ELLIPSIS - ... # doctest: +NORMALIZE_WHITESPACE - [0, 1, ..., 18, 19] - -The SKIP flag causes an example to be skipped entirely. I.e., the -example is not run. It can be useful in contexts where doctest -examples serve as both documentation and test cases, and an example -should be included for documentation purposes, but should not be -checked (e.g., because its output is random, or depends on resources -which would be unavailable.) The SKIP flag can also be used for -'commenting out' broken examples. - - >>> import unavailable_resource # doctest: +SKIP - >>> unavailable_resource.do_something() # doctest: +SKIP - >>> unavailable_resource.blow_up() # doctest: +SKIP - Traceback (most recent call last): - ... - UncheckedBlowUpError: Nobody checks me. - - >>> import random - >>> print random.random() # doctest: +SKIP - 0.721216923889 - -The REPORT_UDIFF flag causes failures that involve multi-line expected -and actual outputs to be displayed using a unified diff: - - >>> def f(x): - ... r''' - ... >>> print '\n'.join('abcdefg') - ... a - ... B - ... c - ... d - ... f - ... g - ... h - ... ''' - - >>> # Without the flag: - >>> test = doctest.DocTestFinder().find(f)[0] - >>> doctest.DocTestRunner(verbose=False).run(test) - ... # doctest: +ELLIPSIS - ********************************************************************** - File ..., line 3, in f - Failed example: - print '\n'.join('abcdefg') - Expected: - a - B - c - d - f - g - h - Got: - a - b - c - d - e - f - g - (1, 1) - - >>> # With the flag: - >>> test = doctest.DocTestFinder().find(f)[0] - >>> flags = doctest.REPORT_UDIFF - >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) - ... # doctest: +ELLIPSIS - ********************************************************************** - File ..., line 3, in f - Failed example: - print '\n'.join('abcdefg') - Differences (unified diff with -expected +actual): - @@ -1,7 +1,7 @@ - a - -B - +b - c - d - +e - f - g - -h - (1, 1) - -The REPORT_CDIFF flag causes failures that involve multi-line expected -and actual outputs to be displayed using a context diff: - - >>> # Reuse f() from the REPORT_UDIFF example, above. - >>> test = doctest.DocTestFinder().find(f)[0] - >>> flags = doctest.REPORT_CDIFF - >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) - ... # doctest: +ELLIPSIS - ********************************************************************** - File ..., line 3, in f - Failed example: - print '\n'.join('abcdefg') - Differences (context diff with expected followed by actual): - *************** - *** 1,7 **** - a - ! B - c - d - f - g - - h - --- 1,7 ---- - a - ! b - c - d - + e - f - g - (1, 1) - - -The REPORT_NDIFF flag causes failures to use the difflib.Differ algorithm -used by the popular ndiff.py utility. This does intraline difference -marking, as well as interline differences. - - >>> def f(x): - ... r''' - ... >>> print "a b c d e f g h i j k l m" - ... a b c d e f g h i j k 1 m - ... ''' - >>> test = doctest.DocTestFinder().find(f)[0] - >>> flags = doctest.REPORT_NDIFF - >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) - ... # doctest: +ELLIPSIS - ********************************************************************** - File ..., line 3, in f - Failed example: - print "a b c d e f g h i j k l m" - Differences (ndiff with -expected +actual): - - a b c d e f g h i j k 1 m - ? ^ - + a b c d e f g h i j k l m - ? + ++ ^ - (1, 1) - -The REPORT_ONLY_FIRST_FAILURE supresses result output after the first -failing example: - - >>> def f(x): - ... r''' - ... >>> print 1 # first success - ... 1 - ... >>> print 2 # first failure - ... 200 - ... >>> print 3 # second failure - ... 300 - ... >>> print 4 # second success - ... 4 - ... >>> print 5 # third failure - ... 500 - ... ''' - >>> test = doctest.DocTestFinder().find(f)[0] - >>> flags = doctest.REPORT_ONLY_FIRST_FAILURE - >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) - ... # doctest: +ELLIPSIS - ********************************************************************** - File ..., line 5, in f - Failed example: - print 2 # first failure - Expected: - 200 - Got: - 2 - (3, 5) - -However, output from `report_start` is not supressed: - - >>> doctest.DocTestRunner(verbose=True, optionflags=flags).run(test) - ... # doctest: +ELLIPSIS - Trying: - print 1 # first success - Expecting: - 1 - ok - Trying: - print 2 # first failure - Expecting: - 200 - ********************************************************************** - File ..., line 5, in f - Failed example: - print 2 # first failure - Expected: - 200 - Got: - 2 - (3, 5) - -For the purposes of REPORT_ONLY_FIRST_FAILURE, unexpected exceptions -count as failures: - - >>> def f(x): - ... r''' - ... >>> print 1 # first success - ... 1 - ... >>> raise ValueError(2) # first failure - ... 200 - ... >>> print 3 # second failure - ... 300 - ... >>> print 4 # second success - ... 4 - ... >>> print 5 # third failure - ... 500 - ... ''' - >>> test = doctest.DocTestFinder().find(f)[0] - >>> flags = doctest.REPORT_ONLY_FIRST_FAILURE - >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) - ... # doctest: +ELLIPSIS - ********************************************************************** - File ..., line 5, in f - Failed example: - raise ValueError(2) # first failure - Exception raised: - ... - ValueError: 2 - (3, 5) - -New option flags can also be registered, via register_optionflag(). Here -we reach into doctest's internals a bit. - - >>> unlikely = "UNLIKELY_OPTION_NAME" - >>> unlikely in doctest.OPTIONFLAGS_BY_NAME - False - >>> new_flag_value = doctest.register_optionflag(unlikely) - >>> unlikely in doctest.OPTIONFLAGS_BY_NAME - True - -Before 2.4.4/2.5, registering a name more than once erroneously created -more than one flag value. Here we verify that's fixed: - - >>> redundant_flag_value = doctest.register_optionflag(unlikely) - >>> redundant_flag_value == new_flag_value - True - -Clean up. - >>> del doctest.OPTIONFLAGS_BY_NAME[unlikely] - - """ - - def option_directives(): r""" -Tests of `DocTestRunner`'s option directive mechanism. - -Option directives can be used to turn option flags on or off for a -single example. To turn an option on for an example, follow that -example with a comment of the form ``# doctest: +OPTION``: - - >>> def f(x): r''' - ... >>> print range(10) # should fail: no ellipsis - ... [0, 1, ..., 9] - ... - ... >>> print range(10) # doctest: +ELLIPSIS - ... [0, 1, ..., 9] - ... ''' - >>> test = doctest.DocTestFinder().find(f)[0] - >>> doctest.DocTestRunner(verbose=False).run(test) - ... # doctest: +ELLIPSIS - ********************************************************************** - File ..., line 2, in f - Failed example: - print range(10) # should fail: no ellipsis - Expected: - [0, 1, ..., 9] - Got: - [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] - (1, 2) - -To turn an option off for an example, follow that example with a -comment of the form ``# doctest: -OPTION``: - - >>> def f(x): r''' - ... >>> print range(10) - ... [0, 1, ..., 9] - ... - ... >>> # should fail: no ellipsis - ... >>> print range(10) # doctest: -ELLIPSIS - ... [0, 1, ..., 9] - ... ''' - >>> test = doctest.DocTestFinder().find(f)[0] - >>> doctest.DocTestRunner(verbose=False, - ... optionflags=doctest.ELLIPSIS).run(test) - ... # doctest: +ELLIPSIS - ********************************************************************** - File ..., line 6, in f - Failed example: - print range(10) # doctest: -ELLIPSIS - Expected: - [0, 1, ..., 9] - Got: - [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] - (1, 2) - -Option directives affect only the example that they appear with; they -do not change the options for surrounding examples: - - >>> def f(x): r''' - ... >>> print range(10) # Should fail: no ellipsis - ... [0, 1, ..., 9] - ... - ... >>> print range(10) # doctest: +ELLIPSIS - ... [0, 1, ..., 9] - ... - ... >>> print range(10) # Should fail: no ellipsis - ... [0, 1, ..., 9] - ... ''' - >>> test = doctest.DocTestFinder().find(f)[0] - >>> doctest.DocTestRunner(verbose=False).run(test) - ... # doctest: +ELLIPSIS - ********************************************************************** - File ..., line 2, in f - Failed example: - print range(10) # Should fail: no ellipsis - Expected: - [0, 1, ..., 9] - Got: - [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] - ********************************************************************** - File ..., line 8, in f - Failed example: - print range(10) # Should fail: no ellipsis - Expected: - [0, 1, ..., 9] - Got: - [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] - (2, 3) - -Multiple options may be modified by a single option directive. They -may be separated by whitespace, commas, or both: - - >>> def f(x): r''' - ... >>> print range(10) # Should fail - ... [0, 1, ..., 9] - ... >>> print range(10) # Should succeed - ... ... # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE - ... [0, 1, ..., 9] - ... ''' - >>> test = doctest.DocTestFinder().find(f)[0] - >>> doctest.DocTestRunner(verbose=False).run(test) - ... # doctest: +ELLIPSIS - ********************************************************************** - File ..., line 2, in f - Failed example: - print range(10) # Should fail - Expected: - [0, 1, ..., 9] - Got: - [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] - (1, 2) - - >>> def f(x): r''' - ... >>> print range(10) # Should fail - ... [0, 1, ..., 9] - ... >>> print range(10) # Should succeed - ... ... # doctest: +ELLIPSIS,+NORMALIZE_WHITESPACE - ... [0, 1, ..., 9] - ... ''' - >>> test = doctest.DocTestFinder().find(f)[0] - >>> doctest.DocTestRunner(verbose=False).run(test) - ... # doctest: +ELLIPSIS - ********************************************************************** - File ..., line 2, in f - Failed example: - print range(10) # Should fail - Expected: - [0, 1, ..., 9] - Got: - [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] - (1, 2) - - >>> def f(x): r''' - ... >>> print range(10) # Should fail - ... [0, 1, ..., 9] - ... >>> print range(10) # Should succeed - ... ... # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE - ... [0, 1, ..., 9] - ... ''' - >>> test = doctest.DocTestFinder().find(f)[0] - >>> doctest.DocTestRunner(verbose=False).run(test) - ... # doctest: +ELLIPSIS - ********************************************************************** - File ..., line 2, in f - Failed example: - print range(10) # Should fail - Expected: - [0, 1, ..., 9] - Got: - [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] - (1, 2) - -The option directive may be put on the line following the source, as -long as a continuation prompt is used: - - >>> def f(x): r''' - ... >>> print range(10) - ... ... # doctest: +ELLIPSIS - ... [0, 1, ..., 9] - ... ''' - >>> test = doctest.DocTestFinder().find(f)[0] - >>> doctest.DocTestRunner(verbose=False).run(test) - (0, 1) - -For examples with multi-line source, the option directive may appear -at the end of any line: - - >>> def f(x): r''' - ... >>> for x in range(10): # doctest: +ELLIPSIS - ... ... print x, - ... 0 1 2 ... 9 - ... - ... >>> for x in range(10): - ... ... print x, # doctest: +ELLIPSIS - ... 0 1 2 ... 9 - ... ''' - >>> test = doctest.DocTestFinder().find(f)[0] - >>> doctest.DocTestRunner(verbose=False).run(test) - (0, 2) - -If more than one line of an example with multi-line source has an -option directive, then they are combined: - - >>> def f(x): r''' - ... Should fail (option directive not on the last line): - ... >>> for x in range(10): # doctest: +ELLIPSIS - ... ... print x, # doctest: +NORMALIZE_WHITESPACE - ... 0 1 2...9 - ... ''' - >>> test = doctest.DocTestFinder().find(f)[0] - >>> doctest.DocTestRunner(verbose=False).run(test) - (0, 1) - -It is an error to have a comment of the form ``# doctest:`` that is -*not* followed by words of the form ``+OPTION`` or ``-OPTION``, where -``OPTION`` is an option that has been registered with -`register_option`: - - >>> # Error: Option not registered - >>> s = '>>> print 12 #doctest: +BADOPTION' - >>> test = doctest.DocTestParser().get_doctest(s, {}, 's', 's.py', 0) - Traceback (most recent call last): - ValueError: line 1 of the doctest for s has an invalid option: '+BADOPTION' - - >>> # Error: No + or - prefix - >>> s = '>>> print 12 #doctest: ELLIPSIS' - >>> test = doctest.DocTestParser().get_doctest(s, {}, 's', 's.py', 0) - Traceback (most recent call last): - ValueError: line 1 of the doctest for s has an invalid option: 'ELLIPSIS' - -It is an error to use an option directive on a line that contains no -source: - - >>> s = '>>> # doctest: +ELLIPSIS' - >>> test = doctest.DocTestParser().get_doctest(s, {}, 's', 's.py', 0) - Traceback (most recent call last): - ValueError: line 0 of the doctest for s has an option directive on a line with no example: '# doctest: +ELLIPSIS' -""" - -def test_testsource(): r""" -Unit tests for `testsource()`. - -The testsource() function takes a module and a name, finds the (first) -test with that name in that module, and converts it to a script. The -example code is converted to regular Python code. The surrounding -words and expected output are converted to comments: - - >>> import test.test_doctest - >>> name = 'test.test_doctest.sample_func' - >>> print doctest.testsource(test.test_doctest, name) - # Blah blah - # - print sample_func(22) - # Expected: - ## 44 - # - # Yee ha! - <BLANKLINE> - - >>> name = 'test.test_doctest.SampleNewStyleClass' - >>> print doctest.testsource(test.test_doctest, name) - print '1\n2\n3' - # Expected: - ## 1 - ## 2 - ## 3 - <BLANKLINE> - - >>> name = 'test.test_doctest.SampleClass.a_classmethod' - >>> print doctest.testsource(test.test_doctest, name) - print SampleClass.a_classmethod(10) - # Expected: - ## 12 - print SampleClass(0).a_classmethod(10) - # Expected: - ## 12 - <BLANKLINE> -""" - -def test_debug(): r""" - -Create a docstring that we want to debug: - - >>> s = ''' - ... >>> x = 12 - ... >>> print x - ... 12 - ... ''' - -Create some fake stdin input, to feed to the debugger: - - >>> import tempfile - >>> real_stdin = sys.stdin - >>> sys.stdin = _FakeInput(['next', 'print x', 'continue']) - -Run the debugger on the docstring, and then restore sys.stdin. - - >>> try: doctest.debug_src(s) - ... finally: sys.stdin = real_stdin - > <string>(1)<module>() - (Pdb) next - 12 - --Return-- - > <string>(1)<module>()->None - (Pdb) print x - 12 - (Pdb) continue - -""" - -def test_pdb_set_trace(): - """Using pdb.set_trace from a doctest. - - You can use pdb.set_trace from a doctest. To do so, you must - retrieve the set_trace function from the pdb module at the time - you use it. The doctest module changes sys.stdout so that it can - capture program output. It also temporarily replaces pdb.set_trace - with a version that restores stdout. This is necessary for you to - see debugger output. - - >>> doc = ''' - ... >>> x = 42 - ... >>> import pdb; pdb.set_trace() - ... ''' - >>> parser = doctest.DocTestParser() - >>> test = parser.get_doctest(doc, {}, "foo", "foo.py", 0) - >>> runner = doctest.DocTestRunner(verbose=False) - - To demonstrate this, we'll create a fake standard input that - captures our debugger input: - - >>> import tempfile - >>> real_stdin = sys.stdin - >>> sys.stdin = _FakeInput([ - ... 'print x', # print data defined by the example - ... 'continue', # stop debugging - ... '']) - - >>> try: runner.run(test) - ... finally: sys.stdin = real_stdin - --Return-- - > <doctest foo[1]>(1)<module>()->None - -> import pdb; pdb.set_trace() - (Pdb) print x - 42 - (Pdb) continue - (0, 2) - - You can also put pdb.set_trace in a function called from a test: - - >>> def calls_set_trace(): - ... y=2 - ... import pdb; pdb.set_trace() - - >>> doc = ''' - ... >>> x=1 - ... >>> calls_set_trace() - ... ''' - >>> test = parser.get_doctest(doc, globals(), "foo", "foo.py", 0) - >>> real_stdin = sys.stdin - >>> sys.stdin = _FakeInput([ - ... 'print y', # print data defined in the function - ... 'up', # out of function - ... 'print x', # print data defined by the example - ... 'continue', # stop debugging - ... '']) - - >>> try: - ... runner.run(test) - ... finally: - ... sys.stdin = real_stdin - --Return-- - > <doctest test.test_doctest.test_pdb_set_trace[8]>(3)calls_set_trace()->None - -> import pdb; pdb.set_trace() - (Pdb) print y - 2 - (Pdb) up - > <doctest foo[1]>(1)<module>() - -> calls_set_trace() - (Pdb) print x - 1 - (Pdb) continue - (0, 2) - - During interactive debugging, source code is shown, even for - doctest examples: - - >>> doc = ''' - ... >>> def f(x): - ... ... g(x*2) - ... >>> def g(x): - ... ... print x+3 - ... ... import pdb; pdb.set_trace() - ... >>> f(3) - ... ''' - >>> test = parser.get_doctest(doc, globals(), "foo", "foo.py", 0) - >>> real_stdin = sys.stdin - >>> sys.stdin = _FakeInput([ - ... 'list', # list source from example 2 - ... 'next', # return from g() - ... 'list', # list source from example 1 - ... 'next', # return from f() - ... 'list', # list source from example 3 - ... 'continue', # stop debugging - ... '']) - >>> try: runner.run(test) - ... finally: sys.stdin = real_stdin - ... # doctest: +NORMALIZE_WHITESPACE - --Return-- - > <doctest foo[1]>(3)g()->None - -> import pdb; pdb.set_trace() - (Pdb) list - 1 def g(x): - 2 print x+3 - 3 -> import pdb; pdb.set_trace() - [EOF] - (Pdb) next - --Return-- - > <doctest foo[0]>(2)f()->None - -> g(x*2) - (Pdb) list - 1 def f(x): - 2 -> g(x*2) - [EOF] - (Pdb) next - --Return-- - > <doctest foo[2]>(1)<module>()->None - -> f(3) - (Pdb) list - 1 -> f(3) - [EOF] - (Pdb) continue - ********************************************************************** - File "foo.py", line 7, in foo - Failed example: - f(3) - Expected nothing - Got: - 9 - (1, 3) - """ - -def test_pdb_set_trace_nested(): - """This illustrates more-demanding use of set_trace with nested functions. - - >>> class C(object): - ... def calls_set_trace(self): - ... y = 1 - ... import pdb; pdb.set_trace() - ... self.f1() - ... y = 2 - ... def f1(self): - ... x = 1 - ... self.f2() - ... x = 2 - ... def f2(self): - ... z = 1 - ... z = 2 - - >>> calls_set_trace = C().calls_set_trace - - >>> doc = ''' - ... >>> a = 1 - ... >>> calls_set_trace() - ... ''' - >>> parser = doctest.DocTestParser() - >>> runner = doctest.DocTestRunner(verbose=False) - >>> test = parser.get_doctest(doc, globals(), "foo", "foo.py", 0) - >>> real_stdin = sys.stdin - >>> sys.stdin = _FakeInput([ - ... 'print y', # print data defined in the function - ... 'step', 'step', 'step', 'step', 'step', 'step', 'print z', - ... 'up', 'print x', - ... 'up', 'print y', - ... 'up', 'print foo', - ... 'continue', # stop debugging - ... '']) - - >>> try: - ... runner.run(test) - ... finally: - ... sys.stdin = real_stdin - > <doctest test.test_doctest.test_pdb_set_trace_nested[0]>(5)calls_set_trace() - -> self.f1() - (Pdb) print y - 1 - (Pdb) step - --Call-- - > <doctest test.test_doctest.test_pdb_set_trace_nested[0]>(7)f1() - -> def f1(self): - (Pdb) step - > <doctest test.test_doctest.test_pdb_set_trace_nested[0]>(8)f1() - -> x = 1 - (Pdb) step - > <doctest test.test_doctest.test_pdb_set_trace_nested[0]>(9)f1() - -> self.f2() - (Pdb) step - --Call-- - > <doctest test.test_doctest.test_pdb_set_trace_nested[0]>(11)f2() - -> def f2(self): - (Pdb) step - > <doctest test.test_doctest.test_pdb_set_trace_nested[0]>(12)f2() - -> z = 1 - (Pdb) step - > <doctest test.test_doctest.test_pdb_set_trace_nested[0]>(13)f2() - -> z = 2 - (Pdb) print z - 1 - (Pdb) up - > <doctest test.test_doctest.test_pdb_set_trace_nested[0]>(9)f1() - -> self.f2() - (Pdb) print x - 1 - (Pdb) up - > <doctest test.test_doctest.test_pdb_set_trace_nested[0]>(5)calls_set_trace() - -> self.f1() - (Pdb) print y - 1 - (Pdb) up - > <doctest foo[1]>(1)<module>() - -> calls_set_trace() - (Pdb) print foo - *** NameError: name 'foo' is not defined - (Pdb) continue - (0, 2) -""" - -def test_DocTestSuite(): - """DocTestSuite creates a unittest test suite from a doctest. - - We create a Suite by providing a module. A module can be provided - by passing a module object: - - >>> import unittest - >>> import test.sample_doctest - >>> suite = doctest.DocTestSuite(test.sample_doctest) - >>> suite.run(unittest.TestResult()) - <unittest.TestResult run=9 errors=0 failures=4> - - We can also supply the module by name: - - >>> suite = doctest.DocTestSuite('test.sample_doctest') - >>> suite.run(unittest.TestResult()) - <unittest.TestResult run=9 errors=0 failures=4> - - We can use the current module: - - >>> suite = test.sample_doctest.test_suite() - >>> suite.run(unittest.TestResult()) - <unittest.TestResult run=9 errors=0 failures=4> - - We can supply global variables. If we pass globs, they will be - used instead of the module globals. Here we'll pass an empty - globals, triggering an extra error: - - >>> suite = doctest.DocTestSuite('test.sample_doctest', globs={}) - >>> suite.run(unittest.TestResult()) - <unittest.TestResult run=9 errors=0 failures=5> - - Alternatively, we can provide extra globals. Here we'll make an - error go away by providing an extra global variable: - - >>> suite = doctest.DocTestSuite('test.sample_doctest', - ... extraglobs={'y': 1}) - >>> suite.run(unittest.TestResult()) - <unittest.TestResult run=9 errors=0 failures=3> - - You can pass option flags. Here we'll cause an extra error - by disabling the blank-line feature: - - >>> suite = doctest.DocTestSuite('test.sample_doctest', - ... optionflags=doctest.DONT_ACCEPT_BLANKLINE) - >>> suite.run(unittest.TestResult()) - <unittest.TestResult run=9 errors=0 failures=5> - - You can supply setUp and tearDown functions: - - >>> def setUp(t): - ... import test.test_doctest - ... test.test_doctest.sillySetup = True - - >>> def tearDown(t): - ... import test.test_doctest - ... del test.test_doctest.sillySetup - - Here, we installed a silly variable that the test expects: - - >>> suite = doctest.DocTestSuite('test.sample_doctest', - ... setUp=setUp, tearDown=tearDown) - >>> suite.run(unittest.TestResult()) - <unittest.TestResult run=9 errors=0 failures=3> - - But the tearDown restores sanity: - - >>> import test.test_doctest - >>> test.test_doctest.sillySetup - Traceback (most recent call last): - ... - AttributeError: 'module' object has no attribute 'sillySetup' - - The setUp and tearDown funtions are passed test objects. Here - we'll use the setUp function to supply the missing variable y: - - >>> def setUp(test): - ... test.globs['y'] = 1 - - >>> suite = doctest.DocTestSuite('test.sample_doctest', setUp=setUp) - >>> suite.run(unittest.TestResult()) - <unittest.TestResult run=9 errors=0 failures=3> - - Here, we didn't need to use a tearDown function because we - modified the test globals, which are a copy of the - sample_doctest module dictionary. The test globals are - automatically cleared for us after a test. - """ - -def test_DocFileSuite(): - """We can test tests found in text files using a DocFileSuite. - - We create a suite by providing the names of one or more text - files that include examples: - - >>> import unittest - >>> suite = doctest.DocFileSuite('test_doctest.txt', - ... 'test_doctest2.txt', - ... 'test_doctest4.txt') - >>> suite.run(unittest.TestResult()) - <unittest.TestResult run=3 errors=0 failures=3> - - The test files are looked for in the directory containing the - calling module. A package keyword argument can be provided to - specify a different relative location. - - >>> import unittest - >>> suite = doctest.DocFileSuite('test_doctest.txt', - ... 'test_doctest2.txt', - ... 'test_doctest4.txt', - ... package='test') - >>> suite.run(unittest.TestResult()) - <unittest.TestResult run=3 errors=0 failures=3> - - Support for using a package's __loader__.get_data() is also - provided. - - >>> import unittest, pkgutil, test - >>> added_loader = False - >>> if not hasattr(test, '__loader__'): - ... test.__loader__ = pkgutil.get_loader(test) - ... added_loader = True - >>> try: - ... suite = doctest.DocFileSuite('test_doctest.txt', - ... 'test_doctest2.txt', - ... 'test_doctest4.txt', - ... package='test') - ... suite.run(unittest.TestResult()) - ... [truncated message content] |
From: <fwi...@us...> - 2009-04-07 00:33:52
|
Revision: 6178 http://jython.svn.sourceforge.net/jython/?rev=6178&view=rev Author: fwierzbicki Date: 2009-04-07 00:33:46 +0000 (Tue, 07 Apr 2009) Log Message: ----------- Added missing committers to Acknowledgments file. Modified Paths: -------------- trunk/jython/ACKNOWLEDGMENTS Modified: trunk/jython/ACKNOWLEDGMENTS =================================================================== --- trunk/jython/ACKNOWLEDGMENTS 2009-04-06 23:52:09 UTC (rev 6177) +++ trunk/jython/ACKNOWLEDGMENTS 2009-04-07 00:33:46 UTC (rev 6178) @@ -70,6 +70,16 @@ Tobias Ivarsson Lino Mastrodomenico S\xE9bastien Boisg\xE9rault + James Baker + Charlie Groves + Otmar Humbel + Philip Jenvey + Alan Kennedy + Nicholas Riley + Frank Wierzbicki + Khalid Zuberi + Sean McGrath + Clark Updike Local Variables: mode: indented-text This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2009-04-06 23:52:19
|
Revision: 6177 http://jython.svn.sourceforge.net/jython/?rev=6177&view=rev Author: fwierzbicki Date: 2009-04-06 23:52:09 +0000 (Mon, 06 Apr 2009) Log Message: ----------- Added some missed contributors to ACKNOWLEDGMENTS file. Modified Paths: -------------- trunk/jython/ACKNOWLEDGMENTS Modified: trunk/jython/ACKNOWLEDGMENTS =================================================================== --- trunk/jython/ACKNOWLEDGMENTS 2009-04-06 13:52:40 UTC (rev 6176) +++ trunk/jython/ACKNOWLEDGMENTS 2009-04-06 23:52:09 UTC (rev 6177) @@ -67,6 +67,9 @@ Aleks Totic Randolph Brown Geoffrey French + Tobias Ivarsson + Lino Mastrodomenico + S\xE9bastien Boisg\xE9rault Local Variables: mode: indented-text This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2009-04-06 13:52:56
|
Revision: 6176 http://jython.svn.sourceforge.net/jython/?rev=6176&view=rev Author: zyasoft Date: 2009-04-06 13:52:40 +0000 (Mon, 06 Apr 2009) Log Message: ----------- Added PyNewList as a possible eventual replacement for PyList Added Paths: ----------- branches/newlist/src/org/python/core/PyNewList.java Added: branches/newlist/src/org/python/core/PyNewList.java =================================================================== --- branches/newlist/src/org/python/core/PyNewList.java (rev 0) +++ branches/newlist/src/org/python/core/PyNewList.java 2009-04-06 13:52:40 UTC (rev 6176) @@ -0,0 +1,920 @@ + // Copyright (c) Corporation for National Research Initiatives +package org.python.core; + +import java.util.ArrayList; +import java.util.Arrays; +import org.python.expose.ExposedMethod; +import org.python.expose.ExposedNew; +import org.python.expose.ExposedType; +import org.python.expose.MethodType; +import org.python.util.Generic; + +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; + +@ExposedType(name = "newlist", base = PyObject.class) +public class PyNewList extends PySequenceList implements List{ + + public static final PyType TYPE = PyType.fromClass(PyNewList.class); + protected final List<PyObject> list; + + public PyNewList() { + this(TYPE); + } + + public PyNewList(PyType type) { + super(type); + list = Generic.list(); + } + + public PyNewList(List list, boolean convert) { + super(TYPE); + if (!convert) { + this.list = list; + } else { + this.list = Generic.list(); + for (Object o : list) { + add(o); + } + } + } + + public PyNewList(PyType type, PyObject[] elements) { + super(type); + list = new ArrayList<PyObject>(Arrays.asList(elements)); + } + + public PyNewList(PyType type, Collection c) { + super(type); + list = new ArrayList<PyObject>(c.size()); + for (Object o : c) { + add(o); + } + } + + public PyNewList(PyObject[] elements) { + this(TYPE, elements); + } + + public PyNewList(Collection c) { + this(TYPE, c); + } + + public PyNewList(PyObject o) { + this(TYPE); + for (PyObject item : o.asIterable()) { + append(item); + } + } + + public PyNewList fromList(List list) { + return new PyNewList(list, false); + } + + private static List<PyObject> listify(Iterator<PyObject> iter) { + List<PyObject> list = Generic.list(); + while (iter.hasNext()) { + list.add(iter.next()); + } + return list; + } + public PyNewList(Iterator<PyObject> iter) { + this(TYPE, listify(iter)); + } + + @ExposedNew + @ExposedMethod(doc = BuiltinDocs.list___init___doc) + final void list___init__(PyObject[] args, String[] kwds) { + ArgParser ap = new ArgParser("list", args, kwds, new String[] {"sequence"}, 0); + PyObject seq = ap.getPyObject(0, null); + clear(); + if(seq == null) { + return; + } + if(seq instanceof PyNewList) { + list.addAll((PyNewList)seq); // don't convert + } else { + for (PyObject item : seq.asIterable()) { + append(item); + } + } + } + + @Override + public int __len__() { + return list___len__(); + } + + @ExposedMethod(doc = BuiltinDocs.list___len___doc) + final int list___len__() { + return size(); + } + + @Override + protected void del(int i) { + remove(i); + } + + @Override + protected void delRange(int start, int stop) { + remove(start, stop); + } + + @Override + protected void setslice(int start, int stop, int step, PyObject value) { + if(stop < start) { + stop = start; + } + if ((value instanceof PySequence) || (!(value instanceof List))) { + if (value == this) { // copy + value = new PyNewList((PySequence)value); + } + setsliceIterator(start, stop, step, value.asIterable().iterator()); + } else { + List valueList = (List)value.__tojava__(List.class); + if(valueList != null && valueList != Py.NoConversion) { + setsliceList(start, stop, step, valueList); + } + } + } + + protected void setsliceList(int start, int stop, int step, List value) { + int n = sliceLength(start, stop, step); + if (list instanceof ArrayList) { + ((ArrayList) list).ensureCapacity(start + n); + } + ListIterator src = value.listIterator(); + if (step == 1) { + ListIterator<PyObject> dest = list.subList(start, stop).listIterator(); + while (dest.hasNext() && src.hasNext()) { + dest.set(Py.java2py(src.next())); + } + } else { + for (int j = start; j < stop && src.hasNext(); j += step) { + set(j, src.next()); + } + } + } + + protected void setsliceIterator(int start, int stop, int step, Iterator<PyObject> iter) { + int n = sliceLength(start, stop, step); + if (list instanceof ArrayList) { + ((ArrayList) list).ensureCapacity(start + n); + } + if (step == 1) { + ListIterator<PyObject> dest = list.subList(start, stop).listIterator(); + while (dest.hasNext() && iter.hasNext()) { + dest.set(iter.next()); + } + } else { + for (int i = 0, j = start; i < n && iter.hasNext(); i++, j += step) { + set(j, iter.next()); + } + } + } + + + @Override + protected PyObject repeat(int count) { + if (count < 0) { + count = 0; + } + int size = size(); + int newSize = size * count; + if (count != 0 && newSize / count != size) { + throw Py.MemoryError(""); + } + + PyNewList newList = new PyNewList(); + for (int i = 0; i < count; i++) { + newList.addAll(this); + } + return newList; + } + + @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___ne___doc) + final PyObject list___ne__(PyObject o) { + return seq___ne__(o); + } + + @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___eq___doc) + final PyObject list___eq__(PyObject o) { + return seq___eq__(o); + } + + @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___lt___doc) + final PyObject list___lt__(PyObject o) { + return seq___lt__(o); + } + + @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___le___doc) + final PyObject list___le__(PyObject o) { + return seq___le__(o); + } + + @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___gt___doc) + final PyObject list___gt__(PyObject o) { + return seq___gt__(o); + } + + @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___ge___doc) + final PyObject list___ge__(PyObject o) { + return seq___ge__(o); + } + + @Override + public PyObject __imul__(PyObject o) { + return list___imul__(o); + } + + @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___imul___doc) + final PyObject list___imul__(PyObject o) { + if (!o.isIndex()) { + return null; + } + int count = o.asIndex(Py.OverflowError); + + int size = size(); + if (size == 0 || count == 1) { + return this; + } + + if (count < 1) { + clear(); + return this; + } + + if (size > Integer.MAX_VALUE / count) { + throw Py.MemoryError(""); + } + + int newSize = size * count; + if (list instanceof ArrayList) { + ((ArrayList)list).ensureCapacity(newSize); + } + List oldList = new ArrayList<PyObject>(list); + for (int i = 1; i < count; i++) { + list.addAll(oldList); + } + gListAllocatedStatus = __len__(); // now omit? + return this; + } + + @Override + public PyObject __mul__(PyObject o) { + return list___mul__(o); + } + + @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___mul___doc) + final PyObject list___mul__(PyObject o) { + if (!o.isIndex()) { + return null; + } + return repeat(o.asIndex(Py.OverflowError)); + } + + @Override + public PyObject __rmul__(PyObject o) { + return list___rmul__(o); + } + + @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___rmul___doc) + final PyObject list___rmul__(PyObject o) { + if (!o.isIndex()) { + return null; + } + return repeat(o.asIndex(Py.OverflowError)); + } + + @Override + public PyObject __add__(PyObject o) { + return list___add__(o); + } + + @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___add___doc) + final PyObject list___add__(PyObject o) { + PyNewList sum = null; + if (o instanceof PySequenceList && !(o instanceof PyTuple)) { + if (o instanceof PyNewList) { + List oList = ((PyNewList)o).list; + List newList = new ArrayList(list.size() + oList.size()); + newList.addAll(list); + newList.addAll(oList); + sum = fromList(newList); + } + } else if(!(o instanceof PySequenceList)) { + // also support adding java lists (but not PyTuple!) + Object oList = o.__tojava__(List.class); + if(oList != Py.NoConversion && oList != null) { + List otherList = (List) oList; + sum = new PyNewList(); + sum.list_extend(this); + for(Iterator i = otherList.iterator(); i.hasNext();) { + sum.add(i.next()); + } + } + } + return sum; + } + + @Override + public PyObject __radd__(PyObject o) { + return list___radd__(o); + } + + //XXX: needs __doc__ + @ExposedMethod(type = MethodType.BINARY) + final PyObject list___radd__(PyObject o) { + // Support adding java.util.List, but prevent adding PyTuple. + // 'o' should never be a PyNewList since __add__ is defined. + PyNewList sum = null; + if(o instanceof PySequence) { + return null; + } + Object oList = o.__tojava__(List.class); + if (oList != Py.NoConversion && oList != null) { + sum = new PyNewList(); + sum.addAll((List) oList); + sum.extend(this); + } + return sum; + } + + @ExposedMethod(doc = BuiltinDocs.list___contains___doc) + final boolean list___contains__(PyObject o) { + return object___contains__(o); + } + + @ExposedMethod(doc = BuiltinDocs.list___delitem___doc) + final void list___delitem__(PyObject index) { + seq___delitem__(index); + } + + @ExposedMethod(doc = BuiltinDocs.list___setitem___doc) + final void list___setitem__(PyObject o, PyObject def) { + seq___setitem__(o, def); + } + + @ExposedMethod(doc = BuiltinDocs.list___getitem___doc) + final PyObject list___getitem__(PyObject o) { + PyObject ret = seq___finditem__(o); + if(ret == null) { + throw Py.IndexError("index out of range: " + o); + } + return ret; + } + + @Override + public PyObject __iter__() { + return list___iter__(); + } + + @ExposedMethod(doc = BuiltinDocs.list___iter___doc) + public PyObject list___iter__() { + return new PyFastSequenceIter(this); + } + + @ExposedMethod(defaults = "null", doc = BuiltinDocs.list___getslice___doc) + final PyObject list___getslice__(PyObject start, PyObject stop, PyObject step) { + return seq___getslice__(start, stop, step); + } + + @ExposedMethod(defaults = "null", doc = BuiltinDocs.list___setslice___doc) + final void list___setslice__(PyObject start, PyObject stop, PyObject step, PyObject value) { + seq___setslice__(start, stop, step, value); + } + + @ExposedMethod(defaults = "null", doc = BuiltinDocs.list___delslice___doc) + final void list___delslice__(PyObject start, PyObject stop, PyObject step) { + seq___delslice__(start, stop, step); + } + + @Override + protected String unsupportedopMessage(String op, PyObject o2) { + if(op.equals("+")) { + return "can only concatenate list (not \"{2}\") to list"; + } + return super.unsupportedopMessage(op, o2); + } + + public String toString() { + return list_toString(); + } + + //XXX: needs __doc__ + @ExposedMethod(names = "__repr__") + final String list_toString() { + ThreadState ts = Py.getThreadState(); + if(!ts.enterRepr(this)) { + return "[...]"; + } + StringBuilder buf = new StringBuilder("["); + int length = size(); + PyObject[] array = getArray(); + for(int i = 0; i < length - 1; i++) { + buf.append((array[i]).__repr__().toString()); + buf.append(", "); + } + if(length > 0) { + buf.append((array[length - 1]).__repr__().toString()); + } + buf.append("]"); + ts.exitRepr(this); + return buf.toString(); + } + + /** + * Add a single element to the end of list. + * + * @param o + * the element to add. + */ + public void append(PyObject o) { + list_append(o); + } + + @ExposedMethod(doc = BuiltinDocs.list_append_doc) + final void list_append(PyObject o) { + pyadd(o); + gListAllocatedStatus = __len__(); + } + + /** + * Return the number elements in the list that equals the argument. + * + * @param o + * the argument to test for. Testing is done with the <code>==</code> operator. + */ + public int count(PyObject o) { + return list_count(o); + } + + @ExposedMethod(doc = BuiltinDocs.list_count_doc) + final int list_count(PyObject o) { + int count = 0; + PyObject[] array = getArray(); + for(int i = 0, n = size(); i < n; i++) { + if(array[i].equals(o)) { + count++; + } + } + return count; + } + + /** + * return smallest index where an element in the list equals the argument. + * + * @param o + * the argument to test for. Testing is done with the <code>==</code> operator. + */ + public int index(PyObject o) { + return index(o, 0); + } + + public int index(PyObject o, int start) { + return list_index(o, start, size()); + } + + public int index(PyObject o, int start, int stop) { + return list_index(o, start, stop); + } + + @ExposedMethod(defaults = {"null", "null"}, doc = BuiltinDocs.list_index_doc) + final int list_index(PyObject o, PyObject start, PyObject stop) { + int startInt = start == null ? 0 : PySlice.calculateSliceIndex(start); + int stopInt = stop == null ? size() : PySlice.calculateSliceIndex(stop); + return list_index(o, startInt, stopInt); + } + + final int list_index(PyObject o, int start, int stop) { + return _index(o, "list.index(x): x not in list", start, stop); + } + + final int list_index(PyObject o, int start) { + return _index(o, "list.index(x): x not in list", start, size()); + } + + final int list_index(PyObject o) { + return _index(o, "list.index(x): x not in list", 0, size()); + } + + private int _index(PyObject o, String message, int start, int stop) { + // Follow Python 2.3+ behavior + int validStop = boundToSequence(stop); + int validStart = boundToSequence(start); + PyObject[] array = getArray(); + for(int i = validStart; i < validStop && i < size(); i++) { + if(array[i].equals(o)) { + return i; + } + } + throw Py.ValueError(message); + } + + /** + * Insert the argument element into the list at the specified index. <br> + * Same as <code>s[index:index] = [o] if index >= 0</code>. + * + * @param index + * the position where the element will be inserted. + * @param o + * the element to insert. + */ + public void insert(int index, PyObject o) { + list_insert(index, o); + } + + @ExposedMethod(doc = BuiltinDocs.list_insert_doc) + final void list_insert(int index, PyObject o) { + if(index < 0) { + index = Math.max(0, size() + index); + } + if(index > size()) { + index = size(); + } + pyadd(index, o); + gListAllocatedStatus = __len__(); + } + + /** + * Remove the first occurence of the argument from the list. The elements arecompared with the + * <code>==</code> operator. <br> + * Same as <code>del s[s.index(x)]</code> + * + * @param o + * the element to search for and remove. + */ + public void remove(PyObject o) { + list_remove(o); + } + + @ExposedMethod(doc = BuiltinDocs.list_remove_doc) + final void list_remove(PyObject o) { + del(_index(o, "list.remove(x): x not in list", 0, size())); + gListAllocatedStatus = __len__(); + } + + /** + * Reverses the items of s in place. The reverse() methods modify the list in place for economy + * of space when reversing a large list. It doesn't return the reversed list to remind you of + * this side effect. + */ + public void reverse() { + list_reverse(); + } + + @ExposedMethod(doc = BuiltinDocs.list_reverse_doc) + final void list_reverse() { + PyObject tmp; + int n = size(); + PyObject[] array = getArray(); + int j = n - 1; + for(int i = 0; i < n / 2; i++, j--) { + tmp = array[i]; + array[i] = array[j]; + array[j] = tmp; + } + gListAllocatedStatus = __len__(); + } + + /** + * Removes and return the last element in the list. + */ + public PyObject pop() { + return pop(-1); + } + + /** + * Removes and return the <code>n</code> indexed element in the list. + * + * @param n + * the index of the element to remove and return. + */ + public PyObject pop(int n) { + return list_pop(n); + } + + @ExposedMethod(defaults = "-1", doc = BuiltinDocs.list_pop_doc) + final PyObject list_pop(int n) { + int length = size(); + if(length == 0) { + throw Py.IndexError("pop from empty list"); + } + if(n < 0) { + n += length; + } + if(n < 0 || n >= length) { + throw Py.IndexError("pop index out of range"); + } + PyObject v = pyget(n); + setslice(n, n + 1, 1, Py.EmptyTuple); + return v; + } + + /** + * Append the elements in the argument sequence to the end of the list. <br> + * Same as <code>s[len(s):len(s)] = o</code>. + * + * @param o + * the sequence of items to append to the list. + */ + public void extend(PyObject o) { + list_extend(o); + } + + @ExposedMethod(doc = BuiltinDocs.list_extend_doc) + final void list_extend(PyObject o) { + int length = size(); + setslice(length, length, 1, o); + gListAllocatedStatus = __len__(); + } + + @Override + public PyObject __iadd__(PyObject o) { + return list___iadd__(o); + } + + @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___iadd___doc) + final PyObject list___iadd__(PyObject o) { + PyType oType = o.getType(); + if (oType == TYPE || oType == PyTuple.TYPE || this == o) { + extend(fastSequence(o, "argument must be iterable")); + return this; + } + + PyObject it; + try { + it = o.__iter__(); + } catch (PyException pye) { + if (!Py.matchException(pye, Py.TypeError)) { + throw pye; + } + return null; + } + extend(it); + return this; + } + + /** + * Sort the items of the list in place. The compare argument is a function of two arguments + * (list items) which should return -1, 0 or 1 depending on whether the first argument is + * considered smaller than, equal to, or larger than the second argument. Note that this slows + * the sorting process down considerably; e.g. to sort a list in reverse order it is much faster + * to use calls to the methods sort() and reverse() than to use the built-in function sort() + * with a comparison function that reverses the ordering of the elements. + * + * @param compare + * the comparison function. + */ + + /** + * Sort the items of the list in place. Items is compared with the normal relative comparison + * operators. + */ + + + @ExposedMethod(doc = BuiltinDocs.list_sort_doc) + final void list_sort(PyObject[] args, String[] kwds) { + ArgParser ap = new ArgParser("list", args, kwds, new String[]{"cmp", "key", "reverse"}, 0); + PyObject cmp = ap.getPyObject(0, Py.None); + PyObject key = ap.getPyObject(1, Py.None); + PyObject reverse = ap.getPyObject(2, Py.False); + sort(cmp, key, reverse); + } + + public void sort(PyObject compare) { + sort(compare, Py.None, Py.False); + } + + public void sort() { + sort(Py.None, Py.None, Py.False); + } + + public void sort(PyObject cmp, PyObject key, PyObject reverse) { + MergeState ms = new MergeState(new PyList((Collection)this), cmp, key, reverse.__nonzero__()); + ms.sort(); + } + + public int hashCode() { + return list___hash__(); + } + + @ExposedMethod(doc = BuiltinDocs.list___hash___doc) + final int list___hash__() { + throw Py.TypeError(String.format("unhashable type: '%.200s'", getType().fastGetName())); + } + + @Override + public PyTuple __getnewargs__() { + return new PyTuple(new PyTuple(getArray())); + } + + @Override + public void add(int index, Object element) { + pyadd(index, Py.java2py(element)); + } + + @Override + public boolean add(Object o) { + pyadd(Py.java2py(o)); + return true; + } + + @Override + public boolean addAll(int index, Collection c) { + if (c instanceof PySequenceList) { + list.addAll(index, c); + } else { + // need to use add to convert anything pulled from a collection into a PyObject + for (Object element : c) { + add(element); + } + } + return c.size() > 0; + } + + @Override + public boolean addAll(Collection c) { + if (c instanceof PySequenceList) { + list.addAll(c); + } else { + // need to use add to convert anything pulled from a collection into a PyObject + for (Object element : c) { + add(element); + } + } + return c.size() > 0; } + + @Override + public void clear() { + list.clear(); + } + + @Override + public boolean contains(Object o) { + return list.contains(Py.java2py(o)); + } + + @Override + public boolean containsAll(Collection c) { + if (c instanceof PySequenceList) { + return list.containsAll(c); + } + else { + return list.containsAll(new PyList(c)); + } + } + + @Override + public boolean equals(Object o) { + if (o instanceof PyNewList) { + return (((PyNewList)o).list.equals(list)); + } + return false; + } + + @Override + public Object get(int index) { + return list.get(index).__tojava__(Object.class); + } + + /** @deprecated */ + @Override + public PyObject[] getArray() { + PyObject a[] = new PyObject[list.size()]; + return list.toArray(a); + } + + @Override + public int indexOf(Object o) { + return list.indexOf(o); + } + + @Override + public boolean isEmpty() { + return list.isEmpty(); + } + + @Override + public Iterator iterator() { + return list.iterator(); + } + + @Override + public int lastIndexOf(Object o) { + return list.lastIndexOf(o); + } + + @Override + public ListIterator listIterator() { + return list.listIterator(); +} + + @Override + public ListIterator listIterator(int index) { + return list.listIterator(index); + } + + @Override + public void pyadd(int index, PyObject element) { + list.add(index, element); + } + + @Override + public boolean pyadd(PyObject o) { + list.add(o); + return true; + } + + @Override + public PyObject pyget(int index) { + return list.get(index); + } + + public void pyset(int index, PyObject element) { + list.set(index, element); + } + + @Override + public Object remove(int index) { + return list.remove(index); + } + + @Override + public void remove(int start, int stop) { + list.subList(start, stop).clear(); + } + + @Override + public boolean removeAll(Collection c) { + if (c instanceof PySequenceList) { + return list.removeAll(c); + } + else { + return list.removeAll(new PyNewList(c)); + } + } + + @Override + public boolean retainAll(Collection c) { + if (c instanceof PySequenceList) { + return list.retainAll(c); + } + else { + return list.retainAll(new PyNewList(c)); + } + } + + @Override + public Object set(int index, Object element) { + return list.set(index, Py.java2py(element)).__tojava__(Object.class); + } + + @Override + public int size() { + return list.size(); + } + + @Override + public List subList(int fromIndex, int toIndex) { + return list.subList(fromIndex, toIndex); + } + + @Override + public Object[] toArray() { + return list.toArray(); + } + + @Override + public Object[] toArray(Object[] a) { + return list.toArray(a); + } + + protected PyObject getslice(int start, int stop, int step) { + if(step > 0 && stop < start) { + stop = start; + } + int n = sliceLength(start, stop, step); + List newList; + if(step == 1) { + newList = new ArrayList<PyObject>(list.subList(start, stop)); + } + else { + newList = new ArrayList<PyObject>(n); + int j = 0; + for(int i = start; j < n; i += step) { + newList.set(j, list.get(i)); + j++; + } + } + return fromList(newList); + } + + @Override + public boolean remove(Object o) { + return list.remove(Py.java2py(o)); + } + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2009-04-06 03:19:42
|
Revision: 6175 http://jython.svn.sourceforge.net/jython/?rev=6175&view=rev Author: pjenvey Date: 2009-04-06 03:19:30 +0000 (Mon, 06 Apr 2009) Log Message: ----------- fix complex.__nonzero__ Modified Paths: -------------- trunk/jython/Lib/test/test_complex_jy.py trunk/jython/src/org/python/core/PyComplex.java Modified: trunk/jython/Lib/test/test_complex_jy.py =================================================================== --- trunk/jython/Lib/test/test_complex_jy.py 2009-04-06 03:12:07 UTC (rev 6174) +++ trunk/jython/Lib/test/test_complex_jy.py 2009-04-06 03:19:30 UTC (rev 6175) @@ -18,7 +18,11 @@ # regression in 2.5 alphas self.assertEqual((4+0j) ** Foo(), (16+0j)) + def test___nonzero__(self): + self.assertTrue(0.25+0j) + self.assertTrue(25j) + def test_main(): test_support.run_unittest(ComplexTest) Modified: trunk/jython/src/org/python/core/PyComplex.java =================================================================== --- trunk/jython/src/org/python/core/PyComplex.java 2009-04-06 03:12:07 UTC (rev 6174) +++ trunk/jython/src/org/python/core/PyComplex.java 2009-04-06 03:19:30 UTC (rev 6175) @@ -164,7 +164,7 @@ @ExposedMethod(doc = BuiltinDocs.complex___nonzero___doc) final boolean complex___nonzero__() { - return real != 0 && imag != 0; + return real != 0 || imag != 0; } /*public Object __tojava__(Class c) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2009-04-06 03:12:09
|
Revision: 6174 http://jython.svn.sourceforge.net/jython/?rev=6174&view=rev Author: pjenvey Date: 2009-04-06 03:12:07 +0000 (Mon, 06 Apr 2009) Log Message: ----------- avoid __tojava__ conversion to int in ldexp so it'll acutally raise OverflowErors. helps out SymPy Modified Paths: -------------- trunk/jython/src/org/python/modules/math.java Modified: trunk/jython/src/org/python/modules/math.java =================================================================== --- trunk/jython/src/org/python/modules/math.java 2009-04-06 02:35:31 UTC (rev 6173) +++ trunk/jython/src/org/python/modules/math.java 2009-04-06 03:12:07 UTC (rev 6174) @@ -184,7 +184,8 @@ return new PyTuple(new PyFloat(x), new PyInteger(exponent)); } - public static double ldexp(double v, int w) { + public static double ldexp(double v, PyObject wObj) { + int w = wObj.asInt(); return check(v * Math.pow(2.0, w)); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2009-04-06 02:35:32
|
Revision: 6173 http://jython.svn.sourceforge.net/jython/?rev=6173&view=rev Author: pjenvey Date: 2009-04-06 02:35:31 +0000 (Mon, 06 Apr 2009) Log Message: ----------- allow comparison of builtin methods, fixes test_descr.methodwrapper minus its implementation detail Modified Paths: -------------- trunk/jython/Lib/test/test_descr.py trunk/jython/src/org/python/core/PyBuiltinMethod.java Modified: trunk/jython/Lib/test/test_descr.py =================================================================== --- trunk/jython/Lib/test/test_descr.py 2009-04-06 01:38:32 UTC (rev 6172) +++ trunk/jython/Lib/test/test_descr.py 2009-04-06 02:35:31 UTC (rev 6173) @@ -4199,7 +4199,10 @@ verify(l.__add__ != l.__mul__) verify(l.__add__.__name__ == '__add__') verify(l.__add__.__self__ is l) - verify(l.__add__.__objclass__ is list) + if not is_jython: + # XXX: method-wrapper implementation detail, not all builtin + # instance methods have __objclass__ (e.g. l.append) + verify(l.__add__.__objclass__ is list) vereq(l.__add__.__doc__, list.__add__.__doc__) try: hash(l.__add__) @@ -4419,10 +4422,6 @@ # Lack __basicsize__: http://bugs.jython.org/issue1017 slotmultipleinheritance, - # Jython lacks CPython method-wrappers (though maybe this - # should pass anyway?) - methodwrapper, - # derived classes don't support coerce: # http://bugs.jython.org/issue1060 notimplemented Modified: trunk/jython/src/org/python/core/PyBuiltinMethod.java =================================================================== --- trunk/jython/src/org/python/core/PyBuiltinMethod.java 2009-04-06 01:38:32 UTC (rev 6172) +++ trunk/jython/src/org/python/core/PyBuiltinMethod.java 2009-04-06 02:35:31 UTC (rev 6173) @@ -50,4 +50,25 @@ int hashCode = self == null ? 0 : self.hashCode(); return hashCode ^ getClass().hashCode(); } + + @Override + public int __cmp__(PyObject other) { + if (!(other instanceof PyBuiltinMethod)) { + return -2; + } + PyBuiltinMethod otherMethod = (PyBuiltinMethod)other; + if (self != otherMethod.self) { + if (self == null) { + return -1; + } else if (otherMethod.self == null) { + return 1; + } + return self._cmp(otherMethod.self); + } + if (getClass() == otherMethod.getClass()) { + return 0; + } + int compareTo = info.getName().compareTo(otherMethod.info.getName()); + return compareTo < 0 ? -1 : compareTo > 0 ? 1 : 0; + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2009-04-06 02:22:25
|
Revision: 6172 http://jython.svn.sourceforge.net/jython/?rev=6172&view=rev Author: pjenvey Date: 2009-04-06 01:38:32 +0000 (Mon, 06 Apr 2009) Log Message: ----------- convert PyInstance to exposed annotations. fixes its binop rule problems (#1197) Modified Paths: -------------- trunk/jython/CoreExposed.includes trunk/jython/Lib/test/test_descr_jy.py trunk/jython/Misc/make_binops.py trunk/jython/src/org/python/core/PyInstance.java Modified: trunk/jython/CoreExposed.includes =================================================================== --- trunk/jython/CoreExposed.includes 2009-04-06 01:03:47 UTC (rev 6171) +++ trunk/jython/CoreExposed.includes 2009-04-06 01:38:32 UTC (rev 6172) @@ -20,6 +20,7 @@ org/python/core/PyFrozenSet.class org/python/core/PyFunction.class org/python/core/PyGenerator.class +org/python/core/PyInstance.class org/python/core/PyInteger.class org/python/core/PyList.class org/python/core/PyLong.class Modified: trunk/jython/Lib/test/test_descr_jy.py =================================================================== --- trunk/jython/Lib/test/test_descr_jy.py 2009-04-06 01:03:47 UTC (rev 6171) +++ trunk/jython/Lib/test/test_descr_jy.py 2009-04-06 01:38:32 UTC (rev 6172) @@ -96,6 +96,7 @@ except AttributeError, e: self.assertEquals("Custom message", str(e)) + class SubclassDescrTestCase(unittest.TestCase): def test_subclass_cmp_right_op(self): @@ -223,7 +224,15 @@ pass self.assertEquals(DoublerBase(2) * AnotherDoubler(3), 12) + def test_oldstyle_binop_notimplemented(self): + class Foo: + pass + class Bar(object): + def __radd__(self, other): + return 3 + self.assertEqual(Foo() + Bar(), 3) + class InPlaceTestCase(unittest.TestCase): def test_iadd(self): Modified: trunk/jython/Misc/make_binops.py =================================================================== --- trunk/jython/Misc/make_binops.py 2009-04-06 01:03:47 UTC (rev 6171) +++ trunk/jython/Misc/make_binops.py 2009-04-06 01:38:32 UTC (rev 6172) @@ -97,8 +97,15 @@ **/ """ -template1 = comment + """\ +template1 = """\ + @Override public %(ret)s __%(name)s__() { + return instance___%(name)s__(); + } + +""" + comment + """\ + @ExposedMethod + final %(ret)s instance___%(name)s__() { PyObject ret = invoke("__%(name)s__"); if (%(checks)s) return %(cast)sret; @@ -106,8 +113,16 @@ } """ -template2 = comment + """\ + +template2 = """\ + @Override public PyObject __%(name)s__() { + return instance___%(name)s__(); + } + +""" + comment + """\ + @ExposedMethod + public PyObject instance___%(name)s__() { return invoke("__%(name)s__"); } @@ -145,8 +160,15 @@ -template = comment + """\ +template = """\ + @Override public PyObject __%(name)s__(PyObject o) { + return instance___%(name)s__(o); + } + +""" + comment + """\ + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___%(name)s__(PyObject o) { Object ctmp = __coerce_ex__(o); if (ctmp == null || ctmp == Py.None) return invoke_ex("__%(name)s__", o); @@ -173,8 +195,15 @@ """ -template2 = comment + """\ +template2 = """\ + @Override public PyObject __%(name)s__(PyObject o) { + return instance___%(name)s__(o); + } + +""" + comment + """\ + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___%(name)s__(PyObject o) { PyObject ret = invoke_ex("__%(name)s__", o); if (ret != null) return ret; Modified: trunk/jython/src/org/python/core/PyInstance.java =================================================================== --- trunk/jython/src/org/python/core/PyInstance.java 2009-04-06 01:03:47 UTC (rev 6171) +++ trunk/jython/src/org/python/core/PyInstance.java 2009-04-06 01:38:32 UTC (rev 6172) @@ -1,25 +1,60 @@ // Copyright (c) Corporation for National Research Initiatives package org.python.core; +import org.python.expose.ExposedMethod; +import org.python.expose.ExposedNew; +import org.python.expose.ExposedType; +import org.python.expose.MethodType; + /** - * A python class instance. + * An instance of a classic Python class. */ +@ExposedType(name = "instance", isBaseType = false) +public class PyInstance extends PyObject { -public class PyInstance extends PyObject -{ + public static final PyType TYPE = PyType.fromClass(PyInstance.class); + // xxx doc, final name public transient PyClass instclass; - // xxx + /** The namespace of this instance. Contains all instance attributes. */ + public PyObject __dict__; + + public PyInstance() { + super(TYPE); + } + + public PyInstance(PyClass iclass, PyObject dict) { + super(TYPE); + instclass = iclass; + if (dict == null) { + dict = new PyStringMap(); + } + __dict__ = dict; + } + + public PyInstance(PyClass iclass) { + this(iclass, null); + } + + @ExposedNew + public static PyObject instance___new__(PyNewWrapper new_, boolean init, PyType subtype, + PyObject[] args, String[] keywords) { + ArgParser ap = new ArgParser("instance", args, keywords, "name", "bases", "dict"); + PyClass klass = (PyClass)ap.getPyObjectByType(0, PyClass.TYPE); + PyObject dict = ap.getPyObject(1, Py.None); + if (dict == Py.None) { + dict = null; + } else if (!(dict instanceof PyStringMap || dict instanceof PyDictionary)) { + throw Py.TypeError("instance() second arg must be dictionary or None"); + } + return new PyInstance(klass, dict); + } + public PyObject fastGetClass() { return instclass; } - /** - The namespace of this instance. Contains all instance attributes. - **/ - public PyObject __dict__; - /* Override serialization behavior */ private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, ClassNotFoundException @@ -56,25 +91,7 @@ out.writeUTF(name.toString()); } - - /** - Returns a new - **/ - - public PyInstance(PyClass iclass, PyObject dict) { - instclass = iclass; - if (dict == Py.None) { - dict = new PyStringMap(); - } - __dict__ = dict; - } - - public PyInstance(PyClass iclass) { - this(iclass, new PyStringMap()); - } - - public PyInstance() {} - + @Override public Object __tojava__(Class c) { if (c.isInstance(this)) return this; @@ -117,6 +134,7 @@ } } + @Override public PyObject __findattr_ex__(String name) { PyObject result = ifindlocal(name); if (result != null) { @@ -150,6 +168,7 @@ return getter.__call__(this, new PyString(name)); } + @Override public boolean isCallable() { return __findattr__("__call__") != null; } @@ -159,6 +178,7 @@ return __findattr__("__index__") != null; } + @Override public PyObject invoke(String name) { PyObject f = ifindlocal(name); if (f == null) { @@ -176,6 +196,7 @@ return f.__call__(); } + @Override public PyObject invoke(String name, PyObject arg1) { PyObject f = ifindlocal(name); if (f == null) { @@ -193,6 +214,7 @@ return f.__call__(arg1); } + @Override public PyObject invoke(String name, PyObject arg1, PyObject arg2) { PyObject f = ifindlocal(name); if (f == null) { @@ -210,13 +232,20 @@ return f.__call__(arg1, arg2); } + @Override public void noAttributeError(String name) { throw Py.AttributeError(String.format("%.50s instance has no attribute '%.400s'", instclass.__name__, name)); } + @Override public void __setattr__(String name, PyObject value) { + instance___setattr__(name, value); + } + + @ExposedMethod + final void instance___setattr__(String name, PyObject value) { if (name == "__class__") { if (value instanceof PyClass) { instclass = (PyClass)value; @@ -245,7 +274,13 @@ __dict__.__setitem__(name, value); } + @Override public void __delattr__(String name) { + instance___delattr__(name); + } + + @ExposedMethod + final void instance___delattr__(String name) { PyObject deller = instclass.__delattr__; if (deller != null) { deller.__call__(this, new PyString(name)); @@ -287,7 +322,13 @@ return meth.__call__(arg1, arg2); } + @Override public PyObject __call__(PyObject args[], String keywords[]) { + return instance___call__(args, keywords); + } + + @ExposedMethod + final PyObject instance___call__(PyObject args[], String keywords[]) { ThreadState ts = Py.getThreadState(); if (ts.recursion_depth++ > ts.systemState.getrecursionlimit()) throw Py.RuntimeError("maximum __call__ recursion depth exceeded"); @@ -298,7 +339,18 @@ } } + @Override + public String toString() { + return __repr__().toString(); + } + + @Override public PyString __repr__() { + return instance___repr__(); + } + + @ExposedMethod + final PyString instance___repr__() { PyObject ret = invoke_ex("__repr__"); if (ret == null) { return makeDefaultRepr(); @@ -328,7 +380,13 @@ Py.idstr(this) + ">"); } + @Override public PyString __str__() { + return instance___str__(); + } + + @ExposedMethod + final PyString instance___str__() { PyObject ret = invoke_ex("__str__"); if (ret == null) return __repr__(); @@ -337,7 +395,13 @@ return (PyString)ret; } + @Override public PyUnicode __unicode__() { + return instance___unicode__(); + } + + @ExposedMethod + final PyUnicode instance___unicode__() { PyObject ret = invoke_ex("__unicode__"); if(ret == null) { return super.__unicode__(); @@ -350,6 +414,7 @@ } } + @Override public int hashCode() { PyObject ret; ret = invoke_ex("__hash__"); @@ -368,8 +433,14 @@ throw Py.TypeError("__hash__() must really return int" + ret.getType() ); } + @Override + public int __cmp__(PyObject other) { + return instance___cmp__(other); + } + // special case: does all the work - public int __cmp__(PyObject other) { + @ExposedMethod + final int instance___cmp__(PyObject other) { PyObject[] coerced = this._coerce(other); PyObject v; PyObject w; @@ -415,31 +486,73 @@ return ret; } + @Override public PyObject __lt__(PyObject o) { + return instance___lt__(o); + } + + @ExposedMethod + final PyObject instance___lt__(PyObject o) { return invoke_ex_richcmp("__lt__", o); } + @Override public PyObject __le__(PyObject o) { + return instance___le__(o); + } + + @ExposedMethod + final PyObject instance___le__(PyObject o) { return invoke_ex_richcmp("__le__", o); } + @Override public PyObject __gt__(PyObject o) { + return instance___gt__(o); + } + + @ExposedMethod + final PyObject instance___gt__(PyObject o) { return invoke_ex_richcmp("__gt__", o); } + @Override public PyObject __ge__(PyObject o) { + return instance___ge__(o); + } + + @ExposedMethod + final PyObject instance___ge__(PyObject o) { return invoke_ex_richcmp("__ge__", o); } + @Override public PyObject __eq__(PyObject o) { + return instance___eq__(o); + } + + @ExposedMethod + final PyObject instance___eq__(PyObject o) { return invoke_ex_richcmp("__eq__", o); } + @Override public PyObject __ne__(PyObject o) { + return instance___ne__(o); + } + + @ExposedMethod + final PyObject instance___ne__(PyObject o) { return invoke_ex_richcmp("__ne__", o); } + @Override public boolean __nonzero__() { + return instance___nonzero__(); + } + + @ExposedMethod + final boolean instance___nonzero__() { PyObject meth = null; try { meth = __findattr__("__nonzero__"); @@ -458,13 +571,20 @@ return ret.__nonzero__(); } + @Override public int __len__() { + return instance___len__(); + } + + @ExposedMethod + final int instance___len__() { PyObject ret = invoke("__len__"); if (ret instanceof PyInteger) return ((PyInteger)ret).getValue(); throw Py.TypeError("__len__() should return an int"); } + @Override public PyObject __finditem__(int key) { return __finditem__(new PyInteger(key)); } @@ -490,6 +610,7 @@ } } + @Override public PyObject __finditem__(PyObject key) { try { return invoke("__getitem__", key); @@ -502,19 +623,43 @@ } } + @Override public PyObject __getitem__(PyObject key) { + return instance___getitem__(key); + } + + @ExposedMethod + final PyObject instance___getitem__(PyObject key) { return invoke("__getitem__", key); } + @Override public void __setitem__(PyObject key, PyObject value) { + instance___setitem__(key, value); + } + + @ExposedMethod + final void instance___setitem__(PyObject key, PyObject value) { invoke("__setitem__", key, value); } + @Override public void __delitem__(PyObject key) { + instance___delitem__(key); + } + + @ExposedMethod + final void instance___delitem__(PyObject key) { invoke("__delitem__", key); } + @Override public PyObject __getslice__(PyObject start, PyObject stop, PyObject step) { + return instance___getslice__(start, stop, step); + } + + @ExposedMethod + final PyObject instance___getslice__(PyObject start, PyObject stop, PyObject step) { if (step != null) { return __getitem__(new PySlice(start, stop, step)); } @@ -525,7 +670,13 @@ return super.__getslice__(start, stop, step); } + @Override public void __setslice__(PyObject start, PyObject stop, PyObject step, PyObject value) { + instance___setslice__(start, stop, step, value); + } + + @ExposedMethod + final void instance___setslice__(PyObject start, PyObject stop, PyObject step, PyObject value) { if (step != null) { __setitem__(new PySlice(start, stop, step), value); } else if (trySlice("__setslice__", start, stop, value) == null) { @@ -533,7 +684,13 @@ } } + @Override public void __delslice__(PyObject start, PyObject stop, PyObject step) { + instance___delslice__(start, stop, step); + } + + @ExposedMethod + final void instance___delslice__(PyObject start, PyObject stop, PyObject step) { if (step != null) { __delitem__(new PySlice(start, stop, step)); } else if (trySlice("__delslice__", start, stop) == null) { @@ -541,7 +698,13 @@ } } + @Override public PyObject __iter__() { + return instance___iter__(); + } + + @ExposedMethod + final PyObject instance___iter__() { PyObject func = __findattr__("__iter__"); if (func != null) return func.__call__(); @@ -552,6 +715,7 @@ return new PySequenceIter(this); } + @Override public PyObject __iternext__() { PyObject func = __findattr__("next"); if (func != null) { @@ -566,7 +730,13 @@ throw Py.TypeError("instance has no next() method"); } + @Override public boolean __contains__(PyObject o) { + return instance___contains__(o); + } + + @ExposedMethod + final boolean instance___contains__(PyObject o) { PyObject func = __findattr__("__contains__"); if (func == null) return super.__contains__(o); @@ -574,7 +744,7 @@ return ret.__nonzero__(); } - //Begin the numeric methods here + @Override public Object __coerce_ex__(PyObject o) { PyObject ret = invoke_ex("__coerce__", o); if (ret == null || ret == Py.None) @@ -584,11 +754,17 @@ return ((PyTuple)ret).getArray(); } + @Override + public PyObject __index__() { + return instance___index__(); + } + /** * Implements the __index__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __index__() { + @ExposedMethod + final PyObject instance___index__() { PyObject ret; try { ret = invoke("__index__"); @@ -605,116 +781,181 @@ ret.getType().fastGetName())); } - // Generated by make_binops.py // Unary ops + @Override + public PyString __hex__() { + return instance___hex__(); + } + /** * Implements the __hex__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyString __hex__() { + @ExposedMethod + final PyString instance___hex__() { PyObject ret = invoke("__hex__"); if (ret instanceof PyString) return (PyString)ret; throw Py.TypeError("__hex__() should return a string"); } + @Override + public PyString __oct__() { + return instance___oct__(); + } + /** * Implements the __oct__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyString __oct__() { + @ExposedMethod + final PyString instance___oct__() { PyObject ret = invoke("__oct__"); if (ret instanceof PyString) return (PyString)ret; throw Py.TypeError("__oct__() should return a string"); } + @Override + public PyObject __int__() { + return instance___int__(); + } + /** * Implements the __int__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __int__() { + @ExposedMethod + final PyObject instance___int__() { PyObject ret = invoke("__int__"); if (ret instanceof PyLong || ret instanceof PyInteger) return ret; throw Py.TypeError("__int__() should return a int"); } + @Override + public PyFloat __float__() { + return instance___float__(); + } + /** * Implements the __float__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyFloat __float__() { + @ExposedMethod + final PyFloat instance___float__() { PyObject ret = invoke("__float__"); if (ret instanceof PyFloat) return (PyFloat)ret; throw Py.TypeError("__float__() should return a float"); } + @Override + public PyObject __long__() { + return instance___long__(); + } + /** * Implements the __long__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __long__() { + @ExposedMethod + final PyObject instance___long__() { PyObject ret = invoke("__long__"); if (ret instanceof PyLong || ret instanceof PyInteger) return ret; throw Py.TypeError("__long__() should return a long"); } + @Override + public PyComplex __complex__() { + return instance___complex__(); + } + /** * Implements the __complex__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyComplex __complex__() { + @ExposedMethod + final PyComplex instance___complex__() { PyObject ret = invoke("__complex__"); if (ret instanceof PyComplex) return (PyComplex)ret; throw Py.TypeError("__complex__() should return a complex"); } + @Override + public PyObject __pos__() { + return instance___pos__(); + } + /** * Implements the __pos__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __pos__() { + @ExposedMethod + public PyObject instance___pos__() { return invoke("__pos__"); } + @Override + public PyObject __neg__() { + return instance___neg__(); + } + /** * Implements the __neg__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __neg__() { + @ExposedMethod + public PyObject instance___neg__() { return invoke("__neg__"); } + @Override + public PyObject __abs__() { + return instance___abs__(); + } + /** * Implements the __abs__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __abs__() { + @ExposedMethod + public PyObject instance___abs__() { return invoke("__abs__"); } + @Override + public PyObject __invert__() { + return instance___invert__(); + } + /** * Implements the __invert__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __invert__() { + @ExposedMethod + public PyObject instance___invert__() { return invoke("__invert__"); } // Binary ops + @Override + public PyObject __add__(PyObject o) { + return instance___add__(o); + } + /** * Implements the __add__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __add__(PyObject o) { + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___add__(PyObject o) { Object ctmp = __coerce_ex__(o); if (ctmp == null || ctmp == Py.None) return invoke_ex("__add__", o); @@ -738,11 +979,17 @@ } } + @Override + public PyObject __radd__(PyObject o) { + return instance___radd__(o); + } + /** * Implements the __radd__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __radd__(PyObject o) { + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___radd__(PyObject o) { Object ctmp = __coerce_ex__(o); if (ctmp == null || ctmp == Py.None) return invoke_ex("__radd__", o); @@ -766,22 +1013,34 @@ } } + @Override + public PyObject __iadd__(PyObject o) { + return instance___iadd__(o); + } + /** * Implements the __iadd__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __iadd__(PyObject o) { + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___iadd__(PyObject o) { PyObject ret = invoke_ex("__iadd__", o); if (ret != null) return ret; return super.__iadd__(o); } + @Override + public PyObject __sub__(PyObject o) { + return instance___sub__(o); + } + /** * Implements the __sub__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __sub__(PyObject o) { + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___sub__(PyObject o) { Object ctmp = __coerce_ex__(o); if (ctmp == null || ctmp == Py.None) return invoke_ex("__sub__", o); @@ -805,11 +1064,17 @@ } } + @Override + public PyObject __rsub__(PyObject o) { + return instance___rsub__(o); + } + /** * Implements the __rsub__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __rsub__(PyObject o) { + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___rsub__(PyObject o) { Object ctmp = __coerce_ex__(o); if (ctmp == null || ctmp == Py.None) return invoke_ex("__rsub__", o); @@ -833,22 +1098,34 @@ } } + @Override + public PyObject __isub__(PyObject o) { + return instance___isub__(o); + } + /** * Implements the __isub__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __isub__(PyObject o) { + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___isub__(PyObject o) { PyObject ret = invoke_ex("__isub__", o); if (ret != null) return ret; return super.__isub__(o); } + @Override + public PyObject __mul__(PyObject o) { + return instance___mul__(o); + } + /** * Implements the __mul__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __mul__(PyObject o) { + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___mul__(PyObject o) { Object ctmp = __coerce_ex__(o); if (ctmp == null || ctmp == Py.None) return invoke_ex("__mul__", o); @@ -872,11 +1149,17 @@ } } + @Override + public PyObject __rmul__(PyObject o) { + return instance___rmul__(o); + } + /** * Implements the __rmul__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __rmul__(PyObject o) { + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___rmul__(PyObject o) { Object ctmp = __coerce_ex__(o); if (ctmp == null || ctmp == Py.None) return invoke_ex("__rmul__", o); @@ -900,22 +1183,34 @@ } } + @Override + public PyObject __imul__(PyObject o) { + return instance___imul__(o); + } + /** * Implements the __imul__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __imul__(PyObject o) { + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___imul__(PyObject o) { PyObject ret = invoke_ex("__imul__", o); if (ret != null) return ret; return super.__imul__(o); } + @Override + public PyObject __div__(PyObject o) { + return instance___div__(o); + } + /** * Implements the __div__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __div__(PyObject o) { + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___div__(PyObject o) { Object ctmp = __coerce_ex__(o); if (ctmp == null || ctmp == Py.None) return invoke_ex("__div__", o); @@ -939,11 +1234,17 @@ } } + @Override + public PyObject __rdiv__(PyObject o) { + return instance___rdiv__(o); + } + /** * Implements the __rdiv__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __rdiv__(PyObject o) { + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___rdiv__(PyObject o) { Object ctmp = __coerce_ex__(o); if (ctmp == null || ctmp == Py.None) return invoke_ex("__rdiv__", o); @@ -967,22 +1268,34 @@ } } + @Override + public PyObject __idiv__(PyObject o) { + return instance___idiv__(o); + } + /** * Implements the __idiv__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __idiv__(PyObject o) { + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___idiv__(PyObject o) { PyObject ret = invoke_ex("__idiv__", o); if (ret != null) return ret; return super.__idiv__(o); } + @Override + public PyObject __floordiv__(PyObject o) { + return instance___floordiv__(o); + } + /** * Implements the __floordiv__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __floordiv__(PyObject o) { + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___floordiv__(PyObject o) { Object ctmp = __coerce_ex__(o); if (ctmp == null || ctmp == Py.None) return invoke_ex("__floordiv__", o); @@ -1006,11 +1319,17 @@ } } + @Override + public PyObject __rfloordiv__(PyObject o) { + return instance___rfloordiv__(o); + } + /** * Implements the __rfloordiv__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __rfloordiv__(PyObject o) { + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___rfloordiv__(PyObject o) { Object ctmp = __coerce_ex__(o); if (ctmp == null || ctmp == Py.None) return invoke_ex("__rfloordiv__", o); @@ -1034,22 +1353,34 @@ } } + @Override + public PyObject __ifloordiv__(PyObject o) { + return instance___ifloordiv__(o); + } + /** * Implements the __ifloordiv__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __ifloordiv__(PyObject o) { + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___ifloordiv__(PyObject o) { PyObject ret = invoke_ex("__ifloordiv__", o); if (ret != null) return ret; return super.__ifloordiv__(o); } + @Override + public PyObject __truediv__(PyObject o) { + return instance___truediv__(o); + } + /** * Implements the __truediv__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __truediv__(PyObject o) { + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___truediv__(PyObject o) { Object ctmp = __coerce_ex__(o); if (ctmp == null || ctmp == Py.None) return invoke_ex("__truediv__", o); @@ -1073,11 +1404,17 @@ } } + @Override + public PyObject __rtruediv__(PyObject o) { + return instance___rtruediv__(o); + } + /** * Implements the __rtruediv__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __rtruediv__(PyObject o) { + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___rtruediv__(PyObject o) { Object ctmp = __coerce_ex__(o); if (ctmp == null || ctmp == Py.None) return invoke_ex("__rtruediv__", o); @@ -1101,22 +1438,34 @@ } } + @Override + public PyObject __itruediv__(PyObject o) { + return instance___itruediv__(o); + } + /** * Implements the __itruediv__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __itruediv__(PyObject o) { + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___itruediv__(PyObject o) { PyObject ret = invoke_ex("__itruediv__", o); if (ret != null) return ret; return super.__itruediv__(o); } + @Override + public PyObject __mod__(PyObject o) { + return instance___mod__(o); + } + /** * Implements the __mod__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __mod__(PyObject o) { + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___mod__(PyObject o) { Object ctmp = __coerce_ex__(o); if (ctmp == null || ctmp == Py.None) return invoke_ex("__mod__", o); @@ -1140,11 +1489,17 @@ } } + @Override + public PyObject __rmod__(PyObject o) { + return instance___rmod__(o); + } + /** * Implements the __rmod__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __rmod__(PyObject o) { + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___rmod__(PyObject o) { Object ctmp = __coerce_ex__(o); if (ctmp == null || ctmp == Py.None) return invoke_ex("__rmod__", o); @@ -1168,22 +1523,34 @@ } } + @Override + public PyObject __imod__(PyObject o) { + return instance___imod__(o); + } + /** * Implements the __imod__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __imod__(PyObject o) { + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___imod__(PyObject o) { PyObject ret = invoke_ex("__imod__", o); if (ret != null) return ret; return super.__imod__(o); } + @Override + public PyObject __divmod__(PyObject o) { + return instance___divmod__(o); + } + /** * Implements the __divmod__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __divmod__(PyObject o) { + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___divmod__(PyObject o) { Object ctmp = __coerce_ex__(o); if (ctmp == null || ctmp == Py.None) return invoke_ex("__divmod__", o); @@ -1207,11 +1574,17 @@ } } + @Override + public PyObject __rdivmod__(PyObject o) { + return instance___rdivmod__(o); + } + /** * Implements the __rdivmod__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __rdivmod__(PyObject o) { + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___rdivmod__(PyObject o) { Object ctmp = __coerce_ex__(o); if (ctmp == null || ctmp == Py.None) return invoke_ex("__rdivmod__", o); @@ -1235,11 +1608,17 @@ } } + @Override + public PyObject __pow__(PyObject o) { + return instance___pow__(o); + } + /** * Implements the __pow__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __pow__(PyObject o) { + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___pow__(PyObject o) { Object ctmp = __coerce_ex__(o); if (ctmp == null || ctmp == Py.None) return invoke_ex("__pow__", o); @@ -1263,11 +1642,17 @@ } } + @Override + public PyObject __rpow__(PyObject o) { + return instance___rpow__(o); + } + /** * Implements the __rpow__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __rpow__(PyObject o) { + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___rpow__(PyObject o) { Object ctmp = __coerce_ex__(o); if (ctmp == null || ctmp == Py.None) return invoke_ex("__rpow__", o); @@ -1291,22 +1676,34 @@ } } + @Override + public PyObject __ipow__(PyObject o) { + return instance___ipow__(o); + } + /** * Implements the __ipow__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __ipow__(PyObject o) { + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___ipow__(PyObject o) { PyObject ret = invoke_ex("__ipow__", o); if (ret != null) return ret; return super.__ipow__(o); } + @Override + public PyObject __lshift__(PyObject o) { + return instance___lshift__(o); + } + /** * Implements the __lshift__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __lshift__(PyObject o) { + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___lshift__(PyObject o) { Object ctmp = __coerce_ex__(o); if (ctmp == null || ctmp == Py.None) return invoke_ex("__lshift__", o); @@ -1330,11 +1727,17 @@ } } + @Override + public PyObject __rlshift__(PyObject o) { + return instance___rlshift__(o); + } + /** * Implements the __rlshift__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __rlshift__(PyObject o) { + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___rlshift__(PyObject o) { Object ctmp = __coerce_ex__(o); if (ctmp == null || ctmp == Py.None) return invoke_ex("__rlshift__", o); @@ -1358,22 +1761,34 @@ } } + @Override + public PyObject __ilshift__(PyObject o) { + return instance___ilshift__(o); + } + /** * Implements the __ilshift__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __ilshift__(PyObject o) { + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___ilshift__(PyObject o) { PyObject ret = invoke_ex("__ilshift__", o); if (ret != null) return ret; return super.__ilshift__(o); } + @Override + public PyObject __rshift__(PyObject o) { + return instance___rshift__(o); + } + /** * Implements the __rshift__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __rshift__(PyObject o) { + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___rshift__(PyObject o) { Object ctmp = __coerce_ex__(o); if (ctmp == null || ctmp == Py.None) return invoke_ex("__rshift__", o); @@ -1397,11 +1812,17 @@ } } + @Override + public PyObject __rrshift__(PyObject o) { + return instance___rrshift__(o); + } + /** * Implements the __rrshift__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __rrshift__(PyObject o) { + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___rrshift__(PyObject o) { Object ctmp = __coerce_ex__(o); if (ctmp == null || ctmp == Py.None) return invoke_ex("__rrshift__", o); @@ -1425,22 +1846,34 @@ } } + @Override + public PyObject __irshift__(PyObject o) { + return instance___irshift__(o); + } + /** * Implements the __irshift__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __irshift__(PyObject o) { + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___irshift__(PyObject o) { PyObject ret = invoke_ex("__irshift__", o); if (ret != null) return ret; return super.__irshift__(o); } + @Override + public PyObject __and__(PyObject o) { + return instance___and__(o); + } + /** * Implements the __and__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __and__(PyObject o) { + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___and__(PyObject o) { Object ctmp = __coerce_ex__(o); if (ctmp == null || ctmp == Py.None) return invoke_ex("__and__", o); @@ -1464,11 +1897,17 @@ } } + @Override + public PyObject __rand__(PyObject o) { + return instance___rand__(o); + } + /** * Implements the __rand__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __rand__(PyObject o) { + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___rand__(PyObject o) { Object ctmp = __coerce_ex__(o); if (ctmp == null || ctmp == Py.None) return invoke_ex("__rand__", o); @@ -1492,22 +1931,34 @@ } } + @Override + public PyObject __iand__(PyObject o) { + return instance___iand__(o); + } + /** * Implements the __iand__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __iand__(PyObject o) { + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___iand__(PyObject o) { PyObject ret = invoke_ex("__iand__", o); if (ret != null) return ret; return super.__iand__(o); } + @Override + public PyObject __or__(PyObject o) { + return instance___or__(o); + } + /** * Implements the __or__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __or__(PyObject o) { + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___or__(PyObject o) { Object ctmp = __coerce_ex__(o); if (ctmp == null || ctmp == Py.None) return invoke_ex("__or__", o); @@ -1531,11 +1982,17 @@ } } + @Override + public PyObject __ror__(PyObject o) { + return instance___ror__(o); + } + /** * Implements the __ror__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __ror__(PyObject o) { + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___ror__(PyObject o) { Object ctmp = __coerce_ex__(o); if (ctmp == null || ctmp == Py.None) return invoke_ex("__ror__", o); @@ -1559,22 +2016,34 @@ } } + @Override + public PyObject __ior__(PyObject o) { + return instance___ior__(o); + } + /** * Implements the __ior__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __ior__(PyObject o) { + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___ior__(PyObject o) { PyObject ret = invoke_ex("__ior__", o); if (ret != null) return ret; return super.__ior__(o); } + @Override + public PyObject __xor__(PyObject o) { + return instance___xor__(o); + } + /** * Implements the __xor__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __xor__(PyObject o) { + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___xor__(PyObject o) { Object ctmp = __coerce_ex__(o); if (ctmp == null || ctmp == Py.None) return invoke_ex("__xor__", o); @@ -1598,11 +2067,17 @@ } } + @Override + public PyObject __rxor__(PyObject o) { + return instance___rxor__(o); + } + /** * Implements the __rxor__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __rxor__(PyObject o) { + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___rxor__(PyObject o) { Object ctmp = __coerce_ex__(o); if (ctmp == null || ctmp == Py.None) return invoke_ex("__rxor__", o); @@ -1626,11 +2101,17 @@ } } + @Override + public PyObject __ixor__(PyObject o) { + return instance___ixor__(o); + } + /** * Implements the __ixor__ method by looking it up * in the instance's dictionary and calling it if it is found. **/ - public PyObject __ixor__(PyObject o) { + @ExposedMethod(type = MethodType.BINARY) + public PyObject instance___ixor__(PyObject o) { PyObject ret = invoke_ex("__ixor__", o); if (ret != null) return ret; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2009-04-06 01:03:49
|
Revision: 6171 http://jython.svn.sourceforge.net/jython/?rev=6171&view=rev Author: pjenvey Date: 2009-04-06 01:03:47 +0000 (Mon, 06 Apr 2009) Log Message: ----------- make exposed methods final Modified Paths: -------------- trunk/jython/src/org/python/core/PyClass.java trunk/jython/src/org/python/core/PyType.java Modified: trunk/jython/src/org/python/core/PyClass.java =================================================================== --- trunk/jython/src/org/python/core/PyClass.java 2009-04-05 23:21:56 UTC (rev 6170) +++ trunk/jython/src/org/python/core/PyClass.java 2009-04-06 01:03:47 UTC (rev 6171) @@ -149,8 +149,12 @@ } @Override + public PyObject __call__(PyObject[] args, String[] keywords) { + return classobj___call__(args, keywords); + } + @ExposedMethod - public PyObject __call__(PyObject[] args, String[] keywords) { + final PyObject classobj___call__(PyObject[] args, String[] keywords) { PyInstance inst; if (__del__ == null) { inst = new PyInstance(this); Modified: trunk/jython/src/org/python/core/PyType.java =================================================================== --- trunk/jython/src/org/python/core/PyType.java 2009-04-05 23:21:56 UTC (rev 6170) +++ trunk/jython/src/org/python/core/PyType.java 2009-04-06 01:03:47 UTC (rev 6171) @@ -1313,7 +1313,7 @@ } @ExposedMethod(names = "__repr__", doc = BuiltinDocs.type___repr___doc) - public String type_toString() { + final String type_toString() { String kind; if (!builtin) { kind = "class"; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2009-04-05 23:22:05
|
Revision: 6170 http://jython.svn.sourceforge.net/jython/?rev=6170&view=rev Author: pjenvey Date: 2009-04-05 23:21:56 +0000 (Sun, 05 Apr 2009) Log Message: ----------- type shouldn't actually have a __str__ Modified Paths: -------------- trunk/jython/Lib/test/test_class_jy.py trunk/jython/src/org/python/core/PyType.java Modified: trunk/jython/Lib/test/test_class_jy.py =================================================================== --- trunk/jython/Lib/test/test_class_jy.py 2009-04-05 22:13:33 UTC (rev 6169) +++ trunk/jython/Lib/test/test_class_jy.py 2009-04-05 23:21:56 UTC (rev 6170) @@ -336,9 +336,11 @@ Foo.keys = class_dict.keys assert sorted(Foo().keys()) == sorted(['a', 'b']), sorted(Foo().keys()) + class ClassMetaclassRepr(unittest.TestCase): - """Verifies #1131 is fixed""" + def test_repr_with_metaclass(self): + # http://bugs.jython.org/issue1131 class FooMetaclass(type): def __new__(cls, name, bases, attrs): return super(FooMetaclass, cls).__new__(cls, name, bases, attrs) @@ -347,7 +349,17 @@ __metaclass__ = FooMetaclass self.assertEqual("<class '%s.Foo'>" % __name__, repr(Foo)) + def test_metaclass_str(self): + class Foo(type): + def __repr__(cls): + return 'foo' + class Bar(object): + __metaclass__ = Foo + self.assertEqual(repr(Bar), 'foo') + # type.__str__ previously broke this + self.assertEqual(str(Bar), 'foo') + def test_main(): test_support.run_unittest(ClassGeneralTestCase, ClassNamelessModuleTestCase, Modified: trunk/jython/src/org/python/core/PyType.java =================================================================== --- trunk/jython/src/org/python/core/PyType.java 2009-04-05 22:13:33 UTC (rev 6169) +++ trunk/jython/src/org/python/core/PyType.java 2009-04-05 23:21:56 UTC (rev 6170) @@ -1312,7 +1312,7 @@ return numSlots; } - @ExposedMethod(names = {"__repr__", "__str__"}, doc = BuiltinDocs.type___str___doc) + @ExposedMethod(names = "__repr__", doc = BuiltinDocs.type___repr___doc) public String type_toString() { String kind; if (!builtin) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2009-04-05 22:13:46
|
Revision: 6169 http://jython.svn.sourceforge.net/jython/?rev=6169&view=rev Author: pjenvey Date: 2009-04-05 22:13:33 +0000 (Sun, 05 Apr 2009) Log Message: ----------- fix dictproxy equality checks Modified Paths: -------------- trunk/jython/Lib/test/test_dictproxy_jy.py trunk/jython/src/org/python/core/PyDictProxy.java Modified: trunk/jython/Lib/test/test_dictproxy_jy.py =================================================================== --- trunk/jython/Lib/test/test_dictproxy_jy.py 2009-04-05 19:27:20 UTC (rev 6168) +++ trunk/jython/Lib/test/test_dictproxy_jy.py 2009-04-05 22:13:33 UTC (rev 6169) @@ -31,8 +31,18 @@ self.assert_(proxy is not copy) self.assertEqual(len(proxy), len(copy)) + def test_dictproxy_equality(self): + self.assertEqual(type.__dict__, type.__dict__) + self.assertEqual(type.__dict__, type.__dict__.copy()) + self.assertEqual(type.__dict__, dict(type.__dict__)) + self.assertEqual(cmp(type.__dict__, type.__dict__), 0) + self.assertEqual(cmp(type.__dict__, type.__dict__.copy()), 0) + self.assertEqual(cmp(type.__dict__, dict(type.__dict__)), 0) + + def test_main(): test_support.run_unittest(DictproxyTestCase) + if __name__ == '__main__': test_main() Modified: trunk/jython/src/org/python/core/PyDictProxy.java =================================================================== --- trunk/jython/src/org/python/core/PyDictProxy.java 2009-04-05 19:27:20 UTC (rev 6168) +++ trunk/jython/src/org/python/core/PyDictProxy.java 2009-04-05 22:13:33 UTC (rev 6169) @@ -93,7 +93,7 @@ @ExposedMethod(type = MethodType.CMP) public int dictproxy___cmp__(PyObject other) { - return dict.__cmp__(other); + return dict._cmp(other); } @ExposedMethod(type = MethodType.BINARY) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2009-04-05 19:27:21
|
Revision: 6168 http://jython.svn.sourceforge.net/jython/?rev=6168&view=rev Author: zyasoft Date: 2009-04-05 19:27:20 +0000 (Sun, 05 Apr 2009) Log Message: ----------- Shim in PySequenceObjectList so that we can begin a gradual replacement of the underlying collection support for list (and probably tuple too). test_itertools and test_javalist currently fail, due to a slightly deeper stack exceeding the recursion limit. That's fine for now. Modified Paths: -------------- branches/newlist/src/org/python/core/PyList.java branches/newlist/src/org/python/core/PySequenceList.java branches/newlist/src/org/python/core/PyTuple.java Added Paths: ----------- branches/newlist/src/org/python/core/PySequenceObjectList.java Modified: branches/newlist/src/org/python/core/PyList.java =================================================================== --- branches/newlist/src/org/python/core/PyList.java 2009-04-05 19:15:19 UTC (rev 6167) +++ branches/newlist/src/org/python/core/PyList.java 2009-04-05 19:27:20 UTC (rev 6168) @@ -15,7 +15,7 @@ * A builtin python list. */ @ExposedType(name = "list", base = PyObject.class) -public class PyList extends PySequenceList { +public class PyList extends PySequenceObjectList { public static final PyType TYPE = PyType.fromClass(PyList.class); @@ -70,8 +70,8 @@ if(seq == null) { return; } - if(seq instanceof PySequenceList) { - PySequenceList p = (PySequenceList)seq.__getslice__(Py.None, Py.None, Py.One); + if(seq instanceof PySequenceObjectList) { + PySequenceObjectList p = (PySequenceObjectList)seq.__getslice__(Py.None, Py.None, Py.One); this.list = p.list; } else { for (PyObject item : seq.asIterable()) { Modified: branches/newlist/src/org/python/core/PySequenceList.java =================================================================== --- branches/newlist/src/org/python/core/PySequenceList.java 2009-04-05 19:15:19 UTC (rev 6167) +++ branches/newlist/src/org/python/core/PySequenceList.java 2009-04-05 19:27:20 UTC (rev 6168) @@ -5,171 +5,81 @@ import java.util.List; import java.util.ListIterator; -public abstract class PySequenceList extends PySequence implements List { +public abstract class PySequenceList extends PySequence { - protected PyObjectList list; - public PySequenceList() { - list = new PyObjectList(); } protected PySequenceList(PyType type) { super(type); - list = new PyObjectList(); } - protected PySequenceList(PyType type, PyObject[] elements) { - super(type); - list = new PyObjectList(elements); - } + public abstract void add(int index, Object element); - /** - * Creates an instance directly backed by the array of PyObject elements. - */ - public PySequenceList(PyObject[] elements) { - list = new PyObjectList(elements); - } + public abstract boolean add(Object o); - public PySequenceList(PyType type, Collection<PyObject> c) { - super(type); - list = new PyObjectList(c); - } + public abstract boolean addAll(int index, Collection c); - public void add(int index, Object element) { - list.add(index, element); - } + public abstract boolean addAll(Collection c); - public boolean add(Object o) { - return list.add(o); - } + public abstract void clear(); - public boolean addAll(int index, Collection c) { - return list.addAll(index, c); - } + public abstract boolean contains(Object o); - public boolean addAll(Collection c) { - return list.addAll(c); - } + public abstract boolean containsAll(Collection c); - public void clear() { - list.clear(); - } + public abstract boolean equals(Object o); - public boolean contains(Object o) { - return list.contains(o); - } + public abstract Object get(int index); - public boolean containsAll(Collection c) { - return list.containsAll(c); - } + /** + * Get the backing array. The array should not be modified. To get a copy of the array, see + * {@link #toArray()}. + */ + public abstract PyObject[] getArray(); - public Object get(int index) { - return list.get(index); - } + public abstract int hashCode(); - public int indexOf(Object o) { - return list.indexOf(o); - } + public abstract int indexOf(Object o); - public boolean isEmpty() { - return list.isEmpty(); - } + public abstract boolean isEmpty(); - public Iterator iterator() { - return list.iterator(); - } + public abstract Iterator iterator(); - public int lastIndexOf(Object o) { - return list.lastIndexOf(o); - } + public abstract int lastIndexOf(Object o); - public ListIterator listIterator() { - return list.listIterator(); - } + public abstract ListIterator listIterator(); - public ListIterator listIterator(int index) { - return list.listIterator(index); - } + public abstract ListIterator listIterator(int index); - public void pyadd(int index, PyObject element) { - list.pyadd(index, element); - } + public abstract void pyadd(int index, PyObject element); - public PyObject pyget(int index) { - return list.pyget(index); - } + public abstract boolean pyadd(PyObject o); - public void pyset(int index, PyObject element) { - list.pyset(index, element); - } + public abstract PyObject pyget(int index); - public Object remove(int index) { - return list.remove(index); - } + public abstract void pyset(int index, PyObject element); - public void remove(int start, int stop) { - list.remove(start, stop); - } + public abstract Object remove(int index); - public boolean remove(Object o) { - return list.remove(o); - } + public abstract void remove(int start, int stop); - public boolean removeAll(Collection c) { - return list.removeAll(c); - } + public abstract boolean remove(Object o); - public boolean retainAll(Collection c) { - return list.retainAll(c); - } + public abstract boolean removeAll(Collection c); - public Object set(int index, Object element) { - return list.set(index, element); - } + public abstract boolean retainAll(Collection c); - public int size() { - return list.size(); - } + public abstract Object set(int index, Object element); - public List subList(int fromIndex, int toIndex) { - return list.subList(fromIndex, toIndex); - } + public abstract int size(); - public Object[] toArray() { - return list.toArray(); - } + public abstract List subList(int fromIndex, int toIndex); - public Object[] toArray(Object[] a) { - return list.toArray(a); - } + public abstract Object[] toArray(); - public String toString() { - return list.toString(); - } + public abstract Object[] toArray(Object[] a); - public boolean pyadd(PyObject o) { - return list.pyadd(o); - } + public abstract String toString(); - public boolean equals(Object o) { - if(o instanceof PySequenceList) { - return list.equals(((PySequenceList)o).list); - } else if(o instanceof List) { - return o.equals(this); - } else { - return super.equals(o); - } - } - - public int hashCode() { - return list.hashCode(); - } - - /** - * Get the backing array. The array should not be modified. To get a copy of the array, see - * {@link #toArray()}. - */ - public PyObject[] getArray() { - return list.getArray(); - } } Added: branches/newlist/src/org/python/core/PySequenceObjectList.java =================================================================== --- branches/newlist/src/org/python/core/PySequenceObjectList.java (rev 0) +++ branches/newlist/src/org/python/core/PySequenceObjectList.java 2009-04-05 19:27:20 UTC (rev 6168) @@ -0,0 +1,175 @@ +package org.python.core; + +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; + +public abstract class PySequenceObjectList extends PySequenceList implements List { + + protected PyObjectList list; + + public PySequenceObjectList() { + list = new PyObjectList(); + } + + protected PySequenceObjectList(PyType type) { + super(type); + list = new PyObjectList(); + } + + protected PySequenceObjectList(PyType type, PyObject[] elements) { + super(type); + list = new PyObjectList(elements); + } + + /** + * Creates an instance directly backed by the array of PyObject elements. + */ + public PySequenceObjectList(PyObject[] elements) { + list = new PyObjectList(elements); + } + + public PySequenceObjectList(PyType type, Collection<PyObject> c) { + super(type); + list = new PyObjectList(c); + } + + public void add(int index, Object element) { + list.add(index, element); + } + + public boolean add(Object o) { + return list.add(o); + } + + public boolean addAll(int index, Collection c) { + return list.addAll(index, c); + } + + public boolean addAll(Collection c) { + return list.addAll(c); + } + + public void clear() { + list.clear(); + } + + public boolean contains(Object o) { + return list.contains(o); + } + + public boolean containsAll(Collection c) { + return list.containsAll(c); + } + + public Object get(int index) { + return list.get(index); + } + + public int indexOf(Object o) { + return list.indexOf(o); + } + + public boolean isEmpty() { + return list.isEmpty(); + } + + public Iterator iterator() { + return list.iterator(); + } + + public int lastIndexOf(Object o) { + return list.lastIndexOf(o); + } + + public ListIterator listIterator() { + return list.listIterator(); + } + + public ListIterator listIterator(int index) { + return list.listIterator(index); + } + + public void pyadd(int index, PyObject element) { + list.pyadd(index, element); + } + + public PyObject pyget(int index) { + return list.pyget(index); + } + + public void pyset(int index, PyObject element) { + list.pyset(index, element); + } + + public Object remove(int index) { + return list.remove(index); + } + + public void remove(int start, int stop) { + list.remove(start, stop); + } + + public boolean remove(Object o) { + return list.remove(o); + } + + public boolean removeAll(Collection c) { + return list.removeAll(c); + } + + public boolean retainAll(Collection c) { + return list.retainAll(c); + } + + public Object set(int index, Object element) { + return list.set(index, element); + } + + public int size() { + return list.size(); + } + + public List subList(int fromIndex, int toIndex) { + return list.subList(fromIndex, toIndex); + } + + public Object[] toArray() { + return list.toArray(); + } + + public Object[] toArray(Object[] a) { + return list.toArray(a); + } + + public String toString() { + return list.toString(); + } + + public boolean pyadd(PyObject o) { + return list.pyadd(o); + } + + public boolean equals(Object o) { + if(o instanceof PySequenceObjectList) { + return list.equals(((PySequenceObjectList)o).list); + } else if(o instanceof List) { + return o.equals(this); + } else { + return ((PySequence)this).equals(o); + } + } + + public int hashCode() { + return list.hashCode(); + } + + /** + * Get the backing array. The array should not be modified. To get a copy of the array, see + * {@link #toArray()}. + */ + public PyObject[] getArray() { + return list.getArray(); + } +} Modified: branches/newlist/src/org/python/core/PyTuple.java =================================================================== --- branches/newlist/src/org/python/core/PyTuple.java 2009-04-05 19:15:19 UTC (rev 6167) +++ branches/newlist/src/org/python/core/PyTuple.java 2009-04-05 19:27:20 UTC (rev 6168) @@ -17,7 +17,7 @@ */ @ExposedType(name = "tuple", base = PyObject.class) -public class PyTuple extends PySequenceList +public class PyTuple extends PySequenceObjectList { public static final PyType TYPE = PyType.fromClass(PyTuple.class); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |