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: <zy...@us...> - 2009-04-11 07:03:00
|
Revision: 6217 http://jython.svn.sourceforge.net/jython/?rev=6217&view=rev Author: zyasoft Date: 2009-04-11 07:02:56 +0000 (Sat, 11 Apr 2009) Log Message: ----------- Fixed PyTuple#index, #toArray Modified Paths: -------------- branches/newlist/src/org/python/core/PyList.java branches/newlist/src/org/python/core/PyTuple.java Modified: branches/newlist/src/org/python/core/PyList.java =================================================================== --- branches/newlist/src/org/python/core/PyList.java 2009-04-11 05:58:57 UTC (rev 6216) +++ branches/newlist/src/org/python/core/PyList.java 2009-04-11 07:02:56 UTC (rev 6217) @@ -11,6 +11,7 @@ import java.util.Collection; import java.util.Collections; +import java.util.ConcurrentModificationException; import java.util.Iterator; import java.util.List; import java.util.ListIterator; @@ -510,12 +511,18 @@ // Follow Python 2.3+ behavior int validStop = boundToSequence(stop); int validStart = boundToSequence(start); - int i = 0; - for (PyObject item : list.subList(validStart, validStop)) { - if (item.equals(o)) { - return i; + int i = validStart; + if (validStart <= validStop) { + try { + for (PyObject item : list.subList(validStart, validStop)) { + if (item.equals(o)) { + return i; + } + i++; + } + } catch (ConcurrentModificationException ex) { + throw Py.ValueError(message); } - i++; } throw Py.ValueError(message); } Modified: branches/newlist/src/org/python/core/PyTuple.java =================================================================== --- branches/newlist/src/org/python/core/PyTuple.java 2009-04-11 05:58:57 UTC (rev 6216) +++ branches/newlist/src/org/python/core/PyTuple.java 2009-04-11 07:02:56 UTC (rev 6217) @@ -541,12 +541,26 @@ @Override public Object[] toArray() { - return array; + Object[] converted = new Object[array.length]; + for (int i = 0; i < array.length; i++) { + converted[i] = array[i].__tojava__(Object.class); + } + return converted; } @Override - public Object[] toArray(Object[] a) { - System.arraycopy(array, 0, a, 0, array.length); - return a; + public Object[] toArray(Object[] converted) { + if (converted.length != array.length) { + converted = new Object[array.length]; + } + for (int i = 0; i < array.length; i++) { + converted[i] = array[i].__tojava__(Object.class); + } + if (array.length < converted.length) { + for (int i = array.length; i < converted.length; i++) { + converted[i] = null; + } + } + return converted; } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2009-04-11 05:59:00
|
Revision: 6216 http://jython.svn.sourceforge.net/jython/?rev=6216&view=rev Author: zyasoft Date: 2009-04-11 05:58:57 +0000 (Sat, 11 Apr 2009) Log Message: ----------- Replaced PyTuple with a new implementation based on on just using a backing array, instead of the code formerly shared with PyList (in PySequenceObjectList). Some bugs remain. Modified Paths: -------------- branches/newlist/src/org/python/core/PyNewTuple.java branches/newlist/src/org/python/core/PyTuple.java Removed Paths: ------------- branches/newlist/Lib/test/test_newlist.py Deleted: branches/newlist/Lib/test/test_newlist.py =================================================================== --- branches/newlist/Lib/test/test_newlist.py 2009-04-11 03:57:01 UTC (rev 6215) +++ branches/newlist/Lib/test/test_newlist.py 2009-04-11 05:58:57 UTC (rev 6216) @@ -1,14 +0,0 @@ -# Check every path through every method of UserList - -from org.python.core import PyNewList as newlist -import unittest -from test import test_support, list_tests - -class NewListTest(list_tests.CommonTest): - type2test = newlist - -def test_main(): - test_support.run_unittest(NewListTest) - -if __name__ == "__main__": - test_main() Modified: branches/newlist/src/org/python/core/PyNewTuple.java =================================================================== --- branches/newlist/src/org/python/core/PyNewTuple.java 2009-04-11 03:57:01 UTC (rev 6215) +++ branches/newlist/src/org/python/core/PyNewTuple.java 2009-04-11 05:58:57 UTC (rev 6216) @@ -16,13 +16,11 @@ /** * A builtin python tuple. */ - @ExposedType(name = "newtuple", base = PyObject.class) public class PyNewTuple extends PySequenceList implements List { + public static final PyType TYPE = PyType.fromClass(PyNewTuple.class); - private final PyObject[] array; - private final List<PyObject> list; private static final PyNewTuple EMPTY_TUPLE = new PyNewTuple(); public PyNewTuple() { @@ -42,11 +40,9 @@ // System.err.println("Initializing from " + Arrays.toString(elements)); if (elements == null) { array = new PyObject[0]; - list = Collections.emptyList(); } else { array = new PyObject[elements.length]; System.arraycopy(elements, 0, array, 0, elements.length); - list = Collections.unmodifiableList(Arrays.asList(array)); } } @@ -63,18 +59,15 @@ } else { array = elements; } - list = Collections.unmodifiableList(Arrays.asList(array)); } public PyNewTuple(PyType subtype, Collection<PyObject> elements) { super(subtype); if (elements == null) { array = new PyObject[0]; - list = Collections.emptyList(); } else { array = new PyObject[elements.size()]; elements.toArray(array); - list = Collections.unmodifiableList(Arrays.asList(array)); } } @@ -82,12 +75,20 @@ // System.err.println("newtuple (no copy):" + Arrays.toString(elements)); return new PyNewTuple(elements, false); } + private volatile List<PyObject> cachedList = null; + private List<PyObject> getList() { + if (cachedList == null) { + cachedList = Arrays.asList(array); + } + return cachedList; + } + @ExposedNew final static PyObject newtuple_new(PyNewWrapper new_, boolean init, PyType subtype, PyObject[] args, String[] keywords) { // System.err.println("newtuple_new"); - ArgParser ap = new ArgParser("newtuple", args, keywords, new String[] { "sequence" }, 0); + ArgParser ap = new ArgParser("newtuple", args, keywords, new String[]{"sequence"}, 0); PyObject S = ap.getPyObject(0, null); // System.err.println("newtuple: new_=" + new_ + ",S=" + S); if (new_.for_type == subtype) { @@ -95,7 +96,7 @@ return EMPTY_TUPLE; } if (S instanceof PyNewTupleDerived) { - return new PyNewTuple(((PyNewTuple)S).getArray()); + return new PyNewTuple(((PyNewTuple) S).getArray()); } if (S instanceof PyNewTuple) { return S; @@ -121,21 +122,20 @@ return fromArrayNoCopy(Py.make_array(iterable)); } - - 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); PyObject[] newArray = new PyObject[n]; PyObject[] array = getArray(); if (step == 1) { - System.arraycopy(array, start, newArray, 0, stop-start); + System.arraycopy(array, start, newArray, 0, stop - start); return fromArrayNoCopy(newArray); } int j = 0; - for (int i=start; j<n; i+=step) { + for (int i = start; j < n; i += step) { newArray[j] = array[i]; j++; } @@ -222,7 +222,7 @@ final PyObject newtuple___add__(PyObject generic_other) { PyNewTuple sum = null; if (generic_other instanceof PyNewTuple) { - PyNewTuple other = (PyNewTuple)generic_other; + PyNewTuple other = (PyNewTuple) generic_other; PyObject[] newArray = new PyObject[array.length + other.array.length]; System.arraycopy(array, 0, newArray, 0, array.length); System.arraycopy(other.array, 0, newArray, array.length, other.array.length); @@ -268,13 +268,13 @@ @ExposedMethod(defaults = "null", doc = BuiltinDocs.tuple___getslice___doc) final PyObject newtuple___getslice__(PyObject s_start, PyObject s_stop, PyObject s_step) { - return seq___getslice__(s_start,s_stop,s_step); + return seq___getslice__(s_start, s_stop, s_step); } @ExposedMethod(doc = BuiltinDocs.tuple___getitem___doc) final PyObject newtuple___getitem__(PyObject index) { PyObject ret = seq___finditem__(index); - if(ret == null) { + if (ret == null) { throw Py.IndexError("index out of range: " + index); } return ret; @@ -311,8 +311,9 @@ } private String subobjRepr(PyObject o) { - if (o == null) + if (o == null) { return "null"; + } return o.__repr__().toString(); } @@ -323,20 +324,22 @@ @ExposedMethod(doc = BuiltinDocs.tuple___repr___doc) final String newtuple___repr__() { StringBuilder buf = new StringBuilder("("); - for (int i = 0; i < array.length-1; i++) { + for (int i = 0; i < array.length - 1; i++) { buf.append(subobjRepr(array[i])); buf.append(", "); } - if (array.length > 0) - buf.append(subobjRepr(array[array.length-1])); - if (array.length == 1) + if (array.length > 0) { + buf.append(subobjRepr(array[array.length - 1])); + } + if (array.length == 1) { buf.append(","); + } buf.append(")"); return buf.toString(); } public List subList(int fromIndex, int toIndex) { - return Collections.unmodifiableList(list.subList(fromIndex, toIndex)); + return Collections.unmodifiableList(getList().subList(fromIndex, toIndex)); } // Make PyNewTuple immutable from the collections interfaces by overriding @@ -344,20 +347,24 @@ // This is how Collections.unmodifiableList() does it. public Iterator iterator() { return new Iterator() { - Iterator i = list.iterator(); + + Iterator i = getList().iterator(); + public void remove() { throw new UnsupportedOperationException(); } + public boolean hasNext() { return i.hasNext(); - } + } + public Object next() { return i.next(); - } + } }; } - public boolean add(Object o){ + public boolean add(Object o) { throw new UnsupportedOperationException(); } @@ -403,15 +410,33 @@ public ListIterator listIterator(final int index) { return new ListIterator() { - ListIterator i = list.listIterator(index); - public boolean hasNext() {return i.hasNext();} - public Object next() {return i.next();} - public boolean hasPrevious() {return i.hasPrevious();} - public Object previous() {return i.previous();} - public int nextIndex() {return i.nextIndex();} - public int previousIndex() {return i.previousIndex();} + ListIterator i = getList().listIterator(index); + public boolean hasNext() { + return i.hasNext(); + } + + public Object next() { + return i.next(); + } + + public boolean hasPrevious() { + return i.hasPrevious(); + } + + public Object previous() { + return i.previous(); + } + + public int nextIndex() { + return i.nextIndex(); + } + + public int previousIndex() { + return i.previousIndex(); + } + public void remove() { throw new UnsupportedOperationException(); } @@ -439,22 +464,38 @@ @Override public boolean contains(Object o) { - return list.contains(o); + PyObject converted = Py.java2py(o); + for (int i = 0; i < array.length; i++) { + if (array[i].equals(converted)) { + return true; + } + } + return false; } @Override public boolean containsAll(Collection c) { - return list.containsAll(c); + if (c instanceof PySequenceList) { + return getList().containsAll(c); + } else { + //XXX: FJW this seems unnecessary. + return getList().containsAll(new PyList(c)); + } } @Override public boolean equals(Object o) { - return list.equals(o); + if (o instanceof PyNewTuple) { + return Arrays.equals(array, ((PyNewTuple) o).array); + } else if (o instanceof List) { // XXX copied from PyList, but... + return o.equals(this); // XXX shouldn't this compare using py2java? + } + return false; } @Override public Object get(int index) { - return list.get(index); + return array[index].__tojava__(Object.class); } @Override @@ -464,17 +505,30 @@ @Override public int indexOf(Object o) { - return list.indexOf(o); + PyObject converted = Py.java2py(o); + for (int i = 0; i < array.length; i++) { + if (array[i].equals(converted)) { + return i; + } + } + return -1; } @Override public boolean isEmpty() { - return list.isEmpty(); + return array.length == 0; } @Override public int lastIndexOf(Object o) { - return list.lastIndexOf(o); + PyObject converted = Py.java2py(o); + int lastIndex = -1; + for (int i = 0; i < array.length; i++) { + if (array[i].equals(converted)) { + lastIndex = i; + } + } + return lastIndex; } @Override @@ -504,12 +558,12 @@ @Override public Object[] toArray() { - return list.toArray(); + return array; } @Override public Object[] toArray(Object[] a) { - return list.toArray(a); + System.arraycopy(array, 0, a, 0, array.length); + return a; } - } Modified: branches/newlist/src/org/python/core/PyTuple.java =================================================================== --- branches/newlist/src/org/python/core/PyTuple.java 2009-04-11 03:57:01 UTC (rev 6215) +++ branches/newlist/src/org/python/core/PyTuple.java 2009-04-11 05:58:57 UTC (rev 6216) @@ -1,6 +1,7 @@ // Copyright (c) Corporation for National Research Initiatives package org.python.core; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Iterator; @@ -15,11 +16,12 @@ /** * A builtin python tuple. */ +@ExposedType(name = "tuple", base = PyObject.class) +public class PyTuple extends PySequenceList implements List { -@ExposedType(name = "tuple", base = PyObject.class) -public class PyTuple extends PySequenceObjectList -{ public static final PyType TYPE = PyType.fromClass(PyTuple.class); + private final PyObject[] array; + private static final PyTuple EMPTY_TUPLE = new PyTuple(); public PyTuple() { this(TYPE, Py.EmptyObjects); @@ -30,25 +32,62 @@ } public PyTuple(PyType subtype, PyObject[] elements) { - super(subtype, elements); + super(subtype); +// System.err.println("Initializing from " + Arrays.toString(elements)); + if (elements == null) { + array = new PyObject[0]; + } else { + array = new PyObject[elements.length]; + System.arraycopy(elements, 0, array, 0, elements.length); + } } + public PyTuple(PyObject[] elements, boolean copy) { + this(TYPE, elements, copy); + } + + public PyTuple(PyType subtype, PyObject[] elements, boolean copy) { + super(subtype); + + if (copy) { + array = new PyObject[elements.length]; + System.arraycopy(elements, 0, array, 0, elements.length); + } else { + array = elements; + } + } + + private static PyTuple fromArrayNoCopy(PyObject[] elements) { +// System.err.println("newtuple (no copy):" + Arrays.toString(elements)); + return new PyTuple(elements, false); + } + private volatile List<PyObject> cachedList = null; + + private List<PyObject> getList() { + if (cachedList == null) { + cachedList = Arrays.asList(array); + } + return cachedList; + } + @ExposedNew final static PyObject tuple_new(PyNewWrapper new_, boolean init, PyType subtype, PyObject[] args, String[] keywords) { - ArgParser ap = new ArgParser("tuple", args, keywords, new String[] { "sequence" }, 0); +// System.err.println("tuple_new"); + ArgParser ap = new ArgParser("newtuple", args, keywords, new String[]{"sequence"}, 0); PyObject S = ap.getPyObject(0, null); +// System.err.println("newtuple: new_=" + new_ + ",S=" + S); if (new_.for_type == subtype) { if (S == null) { - return new PyTuple(); + return EMPTY_TUPLE; } if (S instanceof PyTupleDerived) { - return new PyTuple(((PyTuple)S).getArray()); + return new PyTuple(((PyTuple) S).getArray()); } if (S instanceof PyTuple) { return S; } - return new PyTuple(Py.make_array(S)); + return fromArrayNoCopy(Py.make_array(S)); } else { if (S == null) { return new PyTupleDerived(subtype, Py.EmptyObjects); @@ -66,26 +105,26 @@ * @return a PyTuple containing each item in the iterable */ public static PyTuple fromIterable(PyObject iterable) { - return new PyTuple(Py.make_array(iterable)); + return fromArrayNoCopy(Py.make_array(iterable)); } 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); PyObject[] newArray = new PyObject[n]; - PyObject[] array = getArray(); if (step == 1) { - System.arraycopy(array, start, newArray, 0, stop-start); - return new PyTuple(newArray); + System.arraycopy(array, start, newArray, 0, stop - start); + return fromArrayNoCopy(newArray); } int j = 0; - for (int i=start; j<n; i+=step) { + for (int i = start; j < n; i += step) { newArray[j] = array[i]; j++; } - return new PyTuple(newArray); + return fromArrayNoCopy(newArray); } protected PyObject repeat(int count) { @@ -99,7 +138,7 @@ return this; } if (size == 0) { - return new PyTuple(); + return EMPTY_TUPLE; } } @@ -108,12 +147,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); } - return new PyTuple(newArray); + return fromArrayNoCopy(newArray); } public int __len__() { @@ -124,7 +162,7 @@ final int tuple___len__() { return size(); } - + @ExposedMethod(doc = BuiltinDocs.tuple___contains___doc) final boolean tuple___contains__(PyObject o) { return super.__contains__(o); @@ -168,15 +206,11 @@ final PyObject tuple___add__(PyObject generic_other) { PyTuple sum = null; if (generic_other instanceof PyTuple) { - PyTuple otherTuple = (PyTuple)generic_other; - PyObject[] array = getArray(); - PyObject[] otherArray = otherTuple.getArray(); - int thisLen = size(); - int otherLen = otherTuple.size(); - PyObject[] newArray = new PyObject[thisLen + otherLen]; - System.arraycopy(array, 0, newArray, 0, thisLen); - System.arraycopy(otherArray, 0, newArray, thisLen, otherLen); - sum = new PyTuple(newArray); + PyTuple other = (PyTuple) generic_other; + PyObject[] newArray = new PyObject[array.length + other.array.length]; + System.arraycopy(array, 0, newArray, 0, array.length); + System.arraycopy(other.array, 0, newArray, array.length, other.array.length); + sum = fromArrayNoCopy(newArray); } return sum; } @@ -218,13 +252,13 @@ @ExposedMethod(defaults = "null", doc = BuiltinDocs.tuple___getslice___doc) final PyObject tuple___getslice__(PyObject s_start, PyObject s_stop, PyObject s_step) { - return seq___getslice__(s_start,s_stop,s_step); + return seq___getslice__(s_start, s_stop, s_step); } @ExposedMethod(doc = BuiltinDocs.tuple___getitem___doc) final PyObject tuple___getitem__(PyObject index) { PyObject ret = seq___finditem__(index); - if(ret == null) { + if (ret == null) { throw Py.IndexError("index out of range: " + index); } return ret; @@ -232,7 +266,7 @@ @ExposedMethod(doc = BuiltinDocs.tuple___getnewargs___doc) final PyTuple tuple___getnewargs__() { - return new PyTuple(new PyTuple(list.getArray())); + return new PyTuple(new PyTuple(getArray())); } public PyTuple __getnewargs__() { @@ -251,7 +285,6 @@ int len = size(); int mult = 1000003; int x = 0x345678; - PyObject[] array = getArray(); while (--len >= 0) { y = array[len].hashCode(); x = (x ^ y) * mult; @@ -261,8 +294,9 @@ } private String subobjRepr(PyObject o) { - if (o == null) + if (o == null) { return "null"; + } return o.__repr__().toString(); } @@ -273,22 +307,22 @@ @ExposedMethod(doc = BuiltinDocs.tuple___repr___doc) final String tuple___repr__() { StringBuilder buf = new StringBuilder("("); - PyObject[] array = getArray(); - int arrayLen = size(); - for (int i = 0; i < arrayLen-1; i++) { + for (int i = 0; i < array.length - 1; i++) { buf.append(subobjRepr(array[i])); buf.append(", "); } - if (arrayLen > 0) - buf.append(subobjRepr(array[arrayLen-1])); - if (arrayLen == 1) + if (array.length > 0) { + buf.append(subobjRepr(array[array.length - 1])); + } + if (array.length == 1) { buf.append(","); + } buf.append(")"); return buf.toString(); } public List subList(int fromIndex, int toIndex) { - return Collections.unmodifiableList(list.subList(fromIndex, toIndex)); + return Collections.unmodifiableList(getList().subList(fromIndex, toIndex)); } // Make PyTuple immutable from the collections interfaces by overriding @@ -296,20 +330,24 @@ // This is how Collections.unmodifiableList() does it. public Iterator iterator() { return new Iterator() { - Iterator i = list.iterator(); + + Iterator i = getList().iterator(); + public void remove() { throw new UnsupportedOperationException(); } + public boolean hasNext() { return i.hasNext(); - } + } + public Object next() { return i.next(); - } + } }; } - public boolean add(Object o){ + public boolean add(Object o) { throw new UnsupportedOperationException(); } @@ -355,15 +393,33 @@ public ListIterator listIterator(final int index) { return new ListIterator() { - ListIterator i = list.listIterator(index); - public boolean hasNext() {return i.hasNext();} - public Object next() {return i.next();} - public boolean hasPrevious() {return i.hasPrevious();} - public Object previous() {return i.previous();} - public int nextIndex() {return i.nextIndex();} - public int previousIndex() {return i.previousIndex();} + ListIterator i = getList().listIterator(index); + public boolean hasNext() { + return i.hasNext(); + } + + public Object next() { + return i.next(); + } + + public boolean hasPrevious() { + return i.hasPrevious(); + } + + public Object previous() { + return i.previous(); + } + + public int nextIndex() { + return i.nextIndex(); + } + + public int previousIndex() { + return i.previousIndex(); + } + public void remove() { throw new UnsupportedOperationException(); } @@ -389,4 +445,108 @@ throw Py.TypeError("'tuple' object does not support item assignment"); } + @Override + public boolean contains(Object o) { + PyObject converted = Py.java2py(o); + for (int i = 0; i < array.length; i++) { + if (array[i].equals(converted)) { + return true; + } + } + return false; + } + + @Override + public boolean containsAll(Collection c) { + if (c instanceof PySequenceList) { + return getList().containsAll(c); + } else { + //XXX: FJW this seems unnecessary. + return getList().containsAll(new PyList(c)); + } + } + + @Override + public boolean equals(Object o) { + if (o instanceof PyTuple) { + return Arrays.equals(array, ((PyTuple) o).array); + } else if (o instanceof List) { // XXX copied from PyList, but... + return o.equals(this); // XXX shouldn't this compare using py2java? + } + return false; + } + + @Override + public Object get(int index) { + return array[index].__tojava__(Object.class); + } + + @Override + public PyObject[] getArray() { + return array; + } + + @Override + public int indexOf(Object o) { + PyObject converted = Py.java2py(o); + for (int i = 0; i < array.length; i++) { + if (array[i].equals(converted)) { + return i; + } + } + return -1; + } + + @Override + public boolean isEmpty() { + return array.length == 0; + } + + @Override + public int lastIndexOf(Object o) { + PyObject converted = Py.java2py(o); + int lastIndex = -1; + for (int i = 0; i < array.length; i++) { + if (array[i].equals(converted)) { + lastIndex = i; + } + } + return lastIndex; + } + + @Override + public void pyadd(int index, PyObject element) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public boolean pyadd(PyObject o) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public PyObject pyget(int index) { + return array[index]; + } + + @Override + public void remove(int start, int stop) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public int size() { + return array.length; + } + + @Override + public Object[] toArray() { + return array; + } + + @Override + public Object[] toArray(Object[] a) { + System.arraycopy(array, 0, a, 0, array.length); + return a; + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2009-04-11 03:57:04
|
Revision: 6215 http://jython.svn.sourceforge.net/jython/?rev=6215&view=rev Author: zyasoft Date: 2009-04-11 03:57:01 +0000 (Sat, 11 Apr 2009) Log Message: ----------- Added PyNewTuple, to soon replace PyTuple too Modified Paths: -------------- branches/newlist/CoreExposed.includes branches/newlist/src/org/python/core/PyList.java branches/newlist/src/templates/mappings Added Paths: ----------- branches/newlist/Lib/test/test_newtuple.py branches/newlist/src/org/python/core/PyNewTuple.java branches/newlist/src/org/python/core/PyNewTupleDerived.java branches/newlist/src/templates/newtuple.derived Modified: branches/newlist/CoreExposed.includes =================================================================== --- branches/newlist/CoreExposed.includes 2009-04-11 01:11:10 UTC (rev 6214) +++ branches/newlist/CoreExposed.includes 2009-04-11 03:57:01 UTC (rev 6215) @@ -28,6 +28,7 @@ org/python/core/PyMethodDescr.class org/python/core/PyModule.class org/python/core/PyNewList.class +org/python/core/PyNewTuple.class org/python/core/PyNone.class org/python/core/PyObject.class org/python/core/PyProperty.class Added: branches/newlist/Lib/test/test_newtuple.py =================================================================== --- branches/newlist/Lib/test/test_newtuple.py (rev 0) +++ branches/newlist/Lib/test/test_newtuple.py 2009-04-11 03:57:01 UTC (rev 6215) @@ -0,0 +1,12 @@ +import unittest +from test import test_support, seq_tests +from org.python.core import PyNewTuple as newtuple + +class TupleTest(seq_tests.CommonTest): + type2test = newtuple + +def test_main(): + test_support.run_unittest(TupleTest) + +if __name__=="__main__": + test_main() Modified: branches/newlist/src/org/python/core/PyList.java =================================================================== --- branches/newlist/src/org/python/core/PyList.java 2009-04-11 01:11:10 UTC (rev 6214) +++ branches/newlist/src/org/python/core/PyList.java 2009-04-11 03:57:01 UTC (rev 6215) @@ -70,7 +70,7 @@ } } - public PyList fromList(List list) { + public static PyList fromList(List list) { return new PyList(list, false); } @@ -461,9 +461,8 @@ @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)) { + for (PyObject item : list) { + if (item.equals(o)) { count++; } } @@ -511,11 +510,12 @@ // 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)) { + int i = 0; + for (PyObject item : list.subList(validStart, validStop)) { + if (item.equals(o)) { return i; } + i++; } throw Py.ValueError(message); } Added: branches/newlist/src/org/python/core/PyNewTuple.java =================================================================== --- branches/newlist/src/org/python/core/PyNewTuple.java (rev 0) +++ branches/newlist/src/org/python/core/PyNewTuple.java 2009-04-11 03:57:01 UTC (rev 6215) @@ -0,0 +1,515 @@ +// Copyright (c) Corporation for National Research Initiatives +package org.python.core; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; + +import org.python.expose.ExposedMethod; +import org.python.expose.ExposedNew; +import org.python.expose.ExposedType; +import org.python.expose.MethodType; + +/** + * A builtin python tuple. + */ + +@ExposedType(name = "newtuple", base = PyObject.class) +public class PyNewTuple extends PySequenceList implements List { + public static final PyType TYPE = PyType.fromClass(PyNewTuple.class); + + private final PyObject[] array; + private final List<PyObject> list; + private static final PyNewTuple EMPTY_TUPLE = new PyNewTuple(); + + public PyNewTuple() { + this(TYPE, Py.EmptyObjects); + } + + public PyNewTuple(PyObject... elements) { + this(TYPE, elements); + } + + public PyNewTuple(Collection<PyObject> collection) { + this(TYPE, collection); + } + + public PyNewTuple(PyType subtype, PyObject[] elements) { + super(subtype); +// System.err.println("Initializing from " + Arrays.toString(elements)); + if (elements == null) { + array = new PyObject[0]; + list = Collections.emptyList(); + } else { + array = new PyObject[elements.length]; + System.arraycopy(elements, 0, array, 0, elements.length); + list = Collections.unmodifiableList(Arrays.asList(array)); + } + } + + public PyNewTuple(PyObject[] elements, boolean copy) { + this(TYPE, elements, copy); + } + + public PyNewTuple(PyType subtype, PyObject[] elements, boolean copy) { + super(subtype); + + if (copy) { + array = new PyObject[elements.length]; + System.arraycopy(elements, 0, array, 0, elements.length); + } else { + array = elements; + } + list = Collections.unmodifiableList(Arrays.asList(array)); + } + + public PyNewTuple(PyType subtype, Collection<PyObject> elements) { + super(subtype); + if (elements == null) { + array = new PyObject[0]; + list = Collections.emptyList(); + } else { + array = new PyObject[elements.size()]; + elements.toArray(array); + list = Collections.unmodifiableList(Arrays.asList(array)); + } + } + + private static PyNewTuple fromArrayNoCopy(PyObject[] elements) { +// System.err.println("newtuple (no copy):" + Arrays.toString(elements)); + return new PyNewTuple(elements, false); + } + + @ExposedNew + final static PyObject newtuple_new(PyNewWrapper new_, boolean init, PyType subtype, + PyObject[] args, String[] keywords) { +// System.err.println("newtuple_new"); + ArgParser ap = new ArgParser("newtuple", args, keywords, new String[] { "sequence" }, 0); + PyObject S = ap.getPyObject(0, null); +// System.err.println("newtuple: new_=" + new_ + ",S=" + S); + if (new_.for_type == subtype) { + if (S == null) { + return EMPTY_TUPLE; + } + if (S instanceof PyNewTupleDerived) { + return new PyNewTuple(((PyNewTuple)S).getArray()); + } + if (S instanceof PyNewTuple) { + return S; + } + return fromArrayNoCopy(Py.make_array(S)); + } else { + if (S == null) { + return new PyNewTupleDerived(subtype, Py.EmptyObjects); + } + return new PyNewTupleDerived(subtype, Py.make_array(S)); + } + } + + /** + * Return a new PyNewTuple from an iterable. + * + * Raises a TypeError if the object is not iterable. + * + * @param iterable an iterable PyObject + * @return a PyNewTuple containing each item in the iterable + */ + public static PyNewTuple fromIterable(PyObject iterable) { + return fromArrayNoCopy(Py.make_array(iterable)); + } + + + + protected PyObject getslice(int start, int stop, int step) { + if (step > 0 && stop < start) + stop = start; + int n = sliceLength(start, stop, step); + PyObject[] newArray = new PyObject[n]; + PyObject[] array = getArray(); + + if (step == 1) { + System.arraycopy(array, start, newArray, 0, stop-start); + return fromArrayNoCopy(newArray); + } + int j = 0; + for (int i=start; j<n; i+=step) { + newArray[j] = array[i]; + j++; + } + return fromArrayNoCopy(newArray); + } + + protected PyObject repeat(int count) { + if (count < 0) { + count = 0; + } + int size = size(); + if (size == 0 || count == 1) { + if (getType() == TYPE) { + // Since tuples are immutable, we can return a shared copy in this case + return this; + } + if (size == 0) { + return EMPTY_TUPLE; + } + } + + int newSize = size * count; + if (newSize / size != count) { + 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); + } + return fromArrayNoCopy(newArray); + } + + public int __len__() { + return newtuple___len__(); + } + + @ExposedMethod(doc = BuiltinDocs.tuple___len___doc) + final int newtuple___len__() { + return size(); + } + + @ExposedMethod(doc = BuiltinDocs.tuple___contains___doc) + final boolean newtuple___contains__(PyObject o) { + return super.__contains__(o); + } + + @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.tuple___ne___doc) + final PyObject newtuple___ne__(PyObject o) { + return super.__ne__(o); + } + + @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.tuple___eq___doc) + final PyObject newtuple___eq__(PyObject o) { + return super.__eq__(o); + } + + @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.tuple___gt___doc) + final PyObject newtuple___gt__(PyObject o) { + return super.__gt__(o); + } + + @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.tuple___ge___doc) + final PyObject newtuple___ge__(PyObject o) { + return super.__ge__(o); + } + + @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.tuple___lt___doc) + final PyObject newtuple___lt__(PyObject o) { + return super.__lt__(o); + } + + @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.tuple___le___doc) + final PyObject newtuple___le__(PyObject o) { + return super.__le__(o); + } + + public PyObject __add__(PyObject generic_other) { + return newtuple___add__(generic_other); + } + + @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.tuple___add___doc) + final PyObject newtuple___add__(PyObject generic_other) { + PyNewTuple sum = null; + if (generic_other instanceof PyNewTuple) { + PyNewTuple other = (PyNewTuple)generic_other; + PyObject[] newArray = new PyObject[array.length + other.array.length]; + System.arraycopy(array, 0, newArray, 0, array.length); + System.arraycopy(other.array, 0, newArray, array.length, other.array.length); + sum = fromArrayNoCopy(newArray); + } + return sum; + } + + @Override + public PyObject __mul__(PyObject o) { + return newtuple___mul__(o); + } + + @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.tuple___mul___doc) + final PyObject newtuple___mul__(PyObject o) { + if (!o.isIndex()) { + return null; + } + return repeat(o.asIndex(Py.OverflowError)); + } + + @Override + public PyObject __rmul__(PyObject o) { + return newtuple___rmul__(o); + } + + @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.tuple___rmul___doc) + final PyObject newtuple___rmul__(PyObject o) { + if (!o.isIndex()) { + return null; + } + return repeat(o.asIndex(Py.OverflowError)); + } + + public PyObject __iter__() { + return newtuple___iter__(); + } + + @ExposedMethod(doc = BuiltinDocs.tuple___iter___doc) + public PyObject newtuple___iter__() { + return new PyFastSequenceIter(this); + } + + @ExposedMethod(defaults = "null", doc = BuiltinDocs.tuple___getslice___doc) + final PyObject newtuple___getslice__(PyObject s_start, PyObject s_stop, PyObject s_step) { + return seq___getslice__(s_start,s_stop,s_step); + } + + @ExposedMethod(doc = BuiltinDocs.tuple___getitem___doc) + final PyObject newtuple___getitem__(PyObject index) { + PyObject ret = seq___finditem__(index); + if(ret == null) { + throw Py.IndexError("index out of range: " + index); + } + return ret; + } + + @ExposedMethod(doc = BuiltinDocs.tuple___getnewargs___doc) + final PyTuple newtuple___getnewargs__() { + return new PyTuple(new PyTuple(getArray())); + } + + public PyTuple __getnewargs__() { + return newtuple___getnewargs__(); + } + + public int hashCode() { + return newtuple___hash__(); + } + + @ExposedMethod(doc = BuiltinDocs.tuple___hash___doc) + final int newtuple___hash__() { + // strengthened hash to avoid common collisions. from CPython + // tupleobject.tuplehash. See http://bugs.python.org/issue942952 + int y; + int len = size(); + int mult = 1000003; + int x = 0x345678; + PyObject[] array = getArray(); + while (--len >= 0) { + y = array[len].hashCode(); + x = (x ^ y) * mult; + mult += 82520 + len + len; + } + return x + 97531; + } + + private String subobjRepr(PyObject o) { + if (o == null) + return "null"; + return o.__repr__().toString(); + } + + public String toString() { + return newtuple___repr__(); + } + + @ExposedMethod(doc = BuiltinDocs.tuple___repr___doc) + final String newtuple___repr__() { + StringBuilder buf = new StringBuilder("("); + for (int i = 0; i < array.length-1; i++) { + buf.append(subobjRepr(array[i])); + buf.append(", "); + } + if (array.length > 0) + buf.append(subobjRepr(array[array.length-1])); + if (array.length == 1) + buf.append(","); + buf.append(")"); + return buf.toString(); + } + + public List subList(int fromIndex, int toIndex) { + return Collections.unmodifiableList(list.subList(fromIndex, toIndex)); + } + + // Make PyNewTuple immutable from the collections interfaces by overriding + // all the mutating methods to throw UnsupportedOperationException exception. + // This is how Collections.unmodifiableList() does it. + public Iterator iterator() { + return new Iterator() { + Iterator i = list.iterator(); + public void remove() { + throw new UnsupportedOperationException(); + } + public boolean hasNext() { + return i.hasNext(); + } + public Object next() { + return i.next(); + } + }; + } + + public boolean add(Object o){ + throw new UnsupportedOperationException(); + } + + public boolean remove(Object o) { + throw new UnsupportedOperationException(); + } + + public boolean addAll(Collection coll) { + throw new UnsupportedOperationException(); + } + + public boolean removeAll(Collection coll) { + throw new UnsupportedOperationException(); + } + + public boolean retainAll(Collection coll) { + throw new UnsupportedOperationException(); + } + + public void clear() { + throw new UnsupportedOperationException(); + } + + public Object set(int index, Object element) { + throw new UnsupportedOperationException(); + } + + public void add(int index, Object element) { + throw new UnsupportedOperationException(); + } + + public Object remove(int index) { + throw new UnsupportedOperationException(); + } + + public boolean addAll(int index, Collection c) { + throw new UnsupportedOperationException(); + } + + public ListIterator listIterator() { + return listIterator(0); + } + + public ListIterator listIterator(final int index) { + return new ListIterator() { + ListIterator i = list.listIterator(index); + + public boolean hasNext() {return i.hasNext();} + public Object next() {return i.next();} + public boolean hasPrevious() {return i.hasPrevious();} + public Object previous() {return i.previous();} + public int nextIndex() {return i.nextIndex();} + public int previousIndex() {return i.previousIndex();} + + public void remove() { + throw new UnsupportedOperationException(); + } + + public void set(Object o) { + throw new UnsupportedOperationException(); + } + + public void add(Object o) { + throw new UnsupportedOperationException(); + } + }; + } + + protected String unsupportedopMessage(String op, PyObject o2) { + if (op.equals("+")) { + return "can only concatenate tuple (not \"{2}\") to tuple"; + } + return super.unsupportedopMessage(op, o2); + } + + public void pyset(int index, PyObject value) { + throw Py.TypeError("'tuple' object does not support item assignment"); + } + + @Override + public boolean contains(Object o) { + return list.contains(o); + } + + @Override + public boolean containsAll(Collection c) { + return list.containsAll(c); + } + + @Override + public boolean equals(Object o) { + return list.equals(o); + } + + @Override + public Object get(int index) { + return list.get(index); + } + + @Override + public PyObject[] getArray() { + return array; + } + + @Override + public int indexOf(Object o) { + return list.indexOf(o); + } + + @Override + public boolean isEmpty() { + return list.isEmpty(); + } + + @Override + public int lastIndexOf(Object o) { + return list.lastIndexOf(o); + } + + @Override + public void pyadd(int index, PyObject element) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public boolean pyadd(PyObject o) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public PyObject pyget(int index) { + return array[index]; + } + + @Override + public void remove(int start, int stop) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public int size() { + return array.length; + } + + @Override + public Object[] toArray() { + return list.toArray(); + } + + @Override + public Object[] toArray(Object[] a) { + return list.toArray(a); + } + +} Added: branches/newlist/src/org/python/core/PyNewTupleDerived.java =================================================================== --- branches/newlist/src/org/python/core/PyNewTupleDerived.java (rev 0) +++ branches/newlist/src/org/python/core/PyNewTupleDerived.java 2009-04-11 03:57:01 UTC (rev 6215) @@ -0,0 +1,1162 @@ +/* Generated file, do not modify. See jython/src/templates/gderived.py. */ +package org.python.core; + +import java.io.Serializable; + +public class PyNewTupleDerived extends PyNewTuple implements Slotted { + + public PyObject getSlot(int index) { + return slots[index]; + } + + public void setSlot(int index,PyObject value) { + slots[index]=value; + } + + private PyObject[]slots; + + private PyObject dict; + + public PyObject fastGetDict() { + return dict; + } + + public PyObject getDict() { + return dict; + } + + public void setDict(PyObject newDict) { + if (newDict instanceof PyStringMap||newDict instanceof PyDictionary) { + dict=newDict; + } else { + throw Py.TypeError("__dict__ must be set to a Dictionary "+newDict.getClass().getName()); + } + } + + public void delDict() { + // deleting an object's instance dict makes it grow a new one + dict=new PyStringMap(); + } + + public PyNewTupleDerived(PyType subtype,PyObject[]elements) { + super(subtype,elements); + slots=new PyObject[subtype.getNumSlots()]; + dict=subtype.instDict(); + } + + public PyString __str__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__str__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(); + if (res instanceof PyString) + return(PyString)res; + throw Py.TypeError("__str__"+" returned non-"+"string"+" (type "+res.getType().fastGetName()+")"); + } + return super.__str__(); + } + + public PyString __repr__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__repr__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(); + if (res instanceof PyString) + return(PyString)res; + throw Py.TypeError("__repr__"+" returned non-"+"string"+" (type "+res.getType().fastGetName()+")"); + } + return super.__repr__(); + } + + public PyString __hex__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__hex__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(); + if (res instanceof PyString) + return(PyString)res; + throw Py.TypeError("__hex__"+" returned non-"+"string"+" (type "+res.getType().fastGetName()+")"); + } + return super.__hex__(); + } + + public PyString __oct__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__oct__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(); + if (res instanceof PyString) + return(PyString)res; + throw Py.TypeError("__oct__"+" returned non-"+"string"+" (type "+res.getType().fastGetName()+")"); + } + return super.__oct__(); + } + + public PyFloat __float__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__float__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(); + if (res instanceof PyFloat) + return(PyFloat)res; + throw Py.TypeError("__float__"+" returned non-"+"float"+" (type "+res.getType().fastGetName()+")"); + } + return super.__float__(); + } + + public PyComplex __complex__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__complex__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(); + if (res instanceof PyComplex) + return(PyComplex)res; + throw Py.TypeError("__complex__"+" returned non-"+"complex"+" (type "+res.getType().fastGetName()+")"); + } + return super.__complex__(); + } + + public PyObject __pos__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__pos__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(); + return super.__pos__(); + } + + public PyObject __neg__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__neg__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(); + return super.__neg__(); + } + + public PyObject __abs__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__abs__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(); + return super.__abs__(); + } + + public PyObject __invert__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__invert__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(); + return super.__invert__(); + } + + public PyObject __reduce__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__reduce__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(); + return super.__reduce__(); + } + + public PyObject __add__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__add__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__add__(other); + } + + public PyObject __radd__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__radd__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__radd__(other); + } + + public PyObject __sub__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__sub__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__sub__(other); + } + + public PyObject __rsub__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__rsub__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__rsub__(other); + } + + public PyObject __mul__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__mul__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__mul__(other); + } + + public PyObject __rmul__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__rmul__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__rmul__(other); + } + + public PyObject __div__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__div__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__div__(other); + } + + public PyObject __rdiv__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__rdiv__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__rdiv__(other); + } + + public PyObject __floordiv__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__floordiv__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__floordiv__(other); + } + + public PyObject __rfloordiv__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__rfloordiv__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__rfloordiv__(other); + } + + public PyObject __truediv__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__truediv__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__truediv__(other); + } + + public PyObject __rtruediv__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__rtruediv__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__rtruediv__(other); + } + + public PyObject __mod__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__mod__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__mod__(other); + } + + public PyObject __rmod__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__rmod__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__rmod__(other); + } + + public PyObject __divmod__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__divmod__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__divmod__(other); + } + + public PyObject __rdivmod__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__rdivmod__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__rdivmod__(other); + } + + public PyObject __rpow__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__rpow__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__rpow__(other); + } + + public PyObject __lshift__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__lshift__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__lshift__(other); + } + + public PyObject __rlshift__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__rlshift__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__rlshift__(other); + } + + public PyObject __rshift__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__rshift__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__rshift__(other); + } + + public PyObject __rrshift__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__rrshift__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__rrshift__(other); + } + + public PyObject __and__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__and__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__and__(other); + } + + public PyObject __rand__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__rand__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__rand__(other); + } + + public PyObject __or__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__or__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__or__(other); + } + + public PyObject __ror__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__ror__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__ror__(other); + } + + public PyObject __xor__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__xor__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__xor__(other); + } + + public PyObject __rxor__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__rxor__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__rxor__(other); + } + + public PyObject __lt__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__lt__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__lt__(other); + } + + public PyObject __le__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__le__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__le__(other); + } + + public PyObject __gt__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__gt__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__gt__(other); + } + + public PyObject __ge__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__ge__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__ge__(other); + } + + public PyObject __eq__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__eq__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__eq__(other); + } + + public PyObject __ne__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__ne__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__ne__(other); + } + + public PyObject __iadd__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__iadd__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__iadd__(other); + } + + public PyObject __isub__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__isub__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__isub__(other); + } + + public PyObject __imul__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__imul__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__imul__(other); + } + + public PyObject __idiv__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__idiv__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__idiv__(other); + } + + public PyObject __ifloordiv__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__ifloordiv__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__ifloordiv__(other); + } + + public PyObject __itruediv__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__itruediv__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__itruediv__(other); + } + + public PyObject __imod__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__imod__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__imod__(other); + } + + public PyObject __ipow__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__ipow__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__ipow__(other); + } + + public PyObject __ilshift__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__ilshift__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__ilshift__(other); + } + + public PyObject __irshift__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__irshift__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__irshift__(other); + } + + public PyObject __iand__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__iand__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__iand__(other); + } + + public PyObject __ior__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__ior__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__ior__(other); + } + + public PyObject __ixor__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__ixor__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__ixor__(other); + } + + public PyObject __int__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__int__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(); + if (res instanceof PyInteger||res instanceof PyLong) + return(PyObject)res; + throw Py.TypeError("__int__"+" should return an integer"); + } + return super.__int__(); + } + + public PyObject __long__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__long__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(); + if (res instanceof PyLong||res instanceof PyInteger) + return res; + throw Py.TypeError("__long__"+" returned non-"+"long"+" (type "+res.getType().fastGetName()+")"); + } + return super.__long__(); + } + + public int hashCode() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__hash__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(); + if (res instanceof PyInteger) { + return((PyInteger)res).getValue(); + } else + if (res instanceof PyLong) { + return((PyLong)res).getValue().intValue(); + } + throw Py.TypeError("__hash__ should return a int"); + } + if (self_type.lookup("__eq__")!=null||self_type.lookup("__cmp__")!=null) { + throw Py.TypeError(String.format("unhashable type: '%.200s'",getType().fastGetName())); + } + return super.hashCode(); + } + + public PyUnicode __unicode__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__unicode__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(); + if (res instanceof PyUnicode) + return(PyUnicode)res; + if (res instanceof PyString) + return new PyUnicode((PyString)res); + throw Py.TypeError("__unicode__"+" should return a "+"unicode"); + } + return super.__unicode__(); + } + + public int __cmp__(PyObject other) { + PyType self_type=getType(); + PyType[]where_type=new PyType[1]; + PyObject impl=self_type.lookup_where("__cmp__",where_type); + // Full Compatibility with CPython __cmp__: + // If the derived type don't override __cmp__, the + // *internal* super().__cmp__ should be called, not the + // exposed one. The difference is that the exposed __cmp__ + // throws a TypeError if the argument is an instance of the same type. + if (impl==null||where_type[0]==TYPE||Py.isSubClass(TYPE,where_type[0])) { + return super.__cmp__(other); + } + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) { + return-2; + } + int c=res.asInt(); + return c<0?-1:c>0?1:0; + } + + public boolean __nonzero__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__nonzero__"); + if (impl==null) { + impl=self_type.lookup("__len__"); + if (impl==null) + return super.__nonzero__(); + } + PyObject o=impl.__get__(this,self_type).__call__(); + Class c=o.getClass(); + if (c!=PyInteger.class&&c!=PyBoolean.class) { + throw Py.TypeError(String.format("__nonzero__ should return bool or int, returned %s",self_type.getName())); + } + return o.__nonzero__(); + } + + public boolean __contains__(PyObject o) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__contains__"); + if (impl==null) + return super.__contains__(o); + return impl.__get__(this,self_type).__call__(o).__nonzero__(); + } + + public int __len__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__len__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(); + if (res instanceof PyInteger) + return((PyInteger)res).getValue(); + throw Py.TypeError("__len__ should return a int"); + } + return super.__len__(); + } + + public PyObject __iter__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__iter__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(); + impl=self_type.lookup("__getitem__"); + if (impl==null) + return super.__iter__(); + return new PySequenceIter(this); + } + + public PyObject __iternext__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("next"); + if (impl!=null) { + try { + return impl.__get__(this,self_type).__call__(); + } catch (PyException exc) { + if (Py.matchException(exc,Py.StopIteration)) + return null; + throw exc; + } + } + return super.__iternext__(); // ??? + } + + public PyObject __finditem__(PyObject key) { // ??? + PyType self_type=getType(); + PyObject impl=self_type.lookup("__getitem__"); + if (impl!=null) + try { + return impl.__get__(this,self_type).__call__(key); + } catch (PyException exc) { + if (Py.matchException(exc,Py.LookupError)) + return null; + throw exc; + } + return super.__finditem__(key); + } + + public PyObject __finditem__(int key) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__getitem__"); + if (impl!=null) + try { + return impl.__get__(this,self_type).__call__(new PyInteger(key)); + } catch (PyException exc) { + if (Py.matchException(exc,Py.LookupError)) + return null; + throw exc; + } + return super.__finditem__(key); + } + + public PyObject __getitem__(PyObject key) { + // Same as __finditem__, without swallowing LookupErrors. This allows + // __getitem__ implementations written in Python to raise custom + // exceptions (such as subclasses of KeyError). + // + // We are forced to duplicate the code, instead of defining __finditem__ + // in terms of __getitem__. That's because PyObject defines __getitem__ + // in terms of __finditem__. Therefore, we would end with an infinite + // loop when self_type.lookup("__getitem__") returns null: + // + // __getitem__ -> super.__getitem__ -> __finditem__ -> __getitem__ + // + // By duplicating the (short) lookup and call code, we are safe, because + // the call chains will be: + // + // __finditem__ -> super.__finditem__ + // + // __getitem__ -> super.__getitem__ -> __finditem__ -> super.__finditem__ + + PyType self_type=getType(); + PyObject impl=self_type.lookup("__getitem__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(key); + return super.__getitem__(key); + } + + public void __setitem__(PyObject key,PyObject value) { // ??? + PyType self_type=getType(); + PyObject impl=self_type.lookup("__setitem__"); + if (impl!=null) { + impl.__get__(this,self_type).__call__(key,value); + return; + } + super.__setitem__(key,value); + } + + public PyObject __getslice__(PyObject start,PyObject stop,PyObject step) { // ??? + if (step!=null) { + return __getitem__(new PySlice(start,stop,step)); + } + PyType self_type=getType(); + PyObject impl=self_type.lookup("__getslice__"); + if (impl!=null) { + PyObject[]indices=PySlice.indices2(this,start,stop); + return impl.__get__(this,self_type).__call__(indices[0],indices[1]); + } + return super.__getslice__(start,stop,step); + } + + public void __setslice__(PyObject start,PyObject stop,PyObject step,PyObject value) { + if (step!=null) { + __setitem__(new PySlice(start,stop,step),value); + return; + } + PyType self_type=getType(); + PyObject impl=self_type.lookup("__setslice__"); + if (impl!=null) { + PyObject[]indices=PySlice.indices2(this,start,stop); + impl.__get__(this,self_type).__call__(indices[0],indices[1],value); + return; + } + super.__setslice__(start,stop,step,value); + } + + public void __delslice__(PyObject start,PyObject stop,PyObject step) { + if (step!=null) { + __delitem__(new PySlice(start,stop,step)); + return; + } + PyType self_type=getType(); + PyObject impl=self_type.lookup("__delslice__"); + if (impl!=null) { + PyObject[]indices=PySlice.indices2(this,start,stop); + impl.__get__(this,self_type).__call__(indices[0],indices[1]); + return; + } + super.__delslice__(start,stop,step); + } + + public void __delitem__(PyObject key) { // ??? + PyType self_type=getType(); + PyObject impl=self_type.lookup("__delitem__"); + if (impl!=null) { + impl.__get__(this,self_type).__call__(key); + return; + } + super.__delitem__(key); + } + + public PyObject __call__(PyObject args[],String keywords[]) { + ThreadState ts=Py.getThreadState(); + if (ts.recursion_depth++>ts.systemState.getrecursionlimit()) + throw Py.RuntimeError("maximum __call__ recursion depth exceeded"); + try { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__call__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(args,keywords); + return super.__call__(args,keywords); + } finally { + --ts.recursion_depth; + } + } + + public PyObject __findattr_ex__(String name) { + PyType self_type=getType(); + // TODO: We should speed this up. As the __getattribute__ slot almost never + // changes, it is a good candidate for caching, as PyClass does with + // __getattr__. See #1102. + PyObject getattribute=self_type.lookup("__getattribute__"); + PyString py_name=null; + PyException firstAttributeError=null; + try { + if (getattribute!=null) { + py_name=PyString.fromInterned(name); + return getattribute.__get__(this,self_type).__call__(py_name); + } else { + Py.Warning(String.format("__getattribute__ not found on type %s",self_type.getName())); + PyObject ret=super.__findattr_ex__(name); + if (ret!=null) { + return ret; + } // else: pass through to __getitem__ invocation + } + } catch (PyException e) { + if (!Py.matchException(e,Py.AttributeError)) { + throw e; + } else { + firstAttributeError=e; // saved to avoid swallowing custom AttributeErrors + // and pass through to __getattr__ invocation. + } + } + PyObject getattr=self_type.lookup("__getattr__"); + if (getattr!=null) { + if (py_name==null) { + py_name=PyString.fromInterned(name); + } + return getattr.__get__(this,self_type).__call__(py_name); + } + if (firstAttributeError!=null) { + throw firstAttributeError; + } + return null; + } + + public void __setattr__(String name,PyObject value) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__setattr__"); + if (impl!=null) { + impl.__get__(this,self_type).__call__(PyString.fromInterned(name),value); + return; + } + super.__setattr__(name,value); + } + + public void __delattr__(String name) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__delattr__"); + if (impl!=null) { + impl.__get__(this,self_type).__call__(PyString.fromInterned(name)); + return; + } + super.__delattr__(name); + } + + public PyObject __get__(PyObject obj,PyObject type) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__get__"); + if (impl!=null) { + if (obj==null) + obj=Py.None; + if (type==null) + type=Py.None; + return impl.__get__(this,self_type).__call__(obj,type); + } + return super.__get__(obj,type); + } + + public void __set__(PyObject obj,PyObject value) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__set__"); + if (impl!=null) { + impl.__get__(this,self_type).__call__(obj,value); + return; + } + super.__set__(obj,value); + } + + public void __delete__(PyObject obj) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__delete__"); + if (impl!=null) { + impl.__get__(this,self_type).__call__(obj); + return; + } + super.__delete__(obj); + } + + public PyObject __pow__(PyObject other,PyObject modulo) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__pow__"); + if (impl!=null) { + PyObject res; + if (modulo==null) { + res=impl.__get__(this,self_type).__call__(other); + } else { + res=impl.__get__(this,self_type).__call__(other,modulo); + } + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__pow__(other,modulo); + } + + public void dispatch__init__(PyType type,PyObject[]args,String[]keywords) { + PyType self_type=getType(); + if (self_type.isSubType(type)) { + PyObject impl=self_type.lookup("__init__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(args,keywords); + if (res!=Py.None) { + throw Py.TypeError(String.format("__init__() should return None, not '%.200s'",res.getType().fastGetName())); + } + proxyInit(); + } + } + } + + public PyObject __index__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__index__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(); + if (res instanceof PyInteger||res instanceof PyLong) { + return res; + } + throw Py.TypeError(String.format("__index__ returned non-(int,long) (type %s)",res.getType().fastGetName())); + } + return super.__index__(); + } + + public Object __tojava__(Class c) { + // If we are not being asked by the "default" conversion to java, then + // we can provide this as the result, as long as it is a instance of the + // specified class. Without this, derived.__tojava__(PyObject.class) + // would broke. (And that's not pure speculation: PyReflectedFunction's + // ReflectedArgs asks for things like that). + if ((c!=Object.class)&&(c!=Serializable.class)&&(c.isInstance(this))) { + return this; + } + // Otherwise, we call the derived __tojava__, if it exists: + PyType self_type=getType(); + PyObject impl=self_type.lookup("__tojava__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(Py.java2py(c)).__tojava__(Object.class); + return super.__tojava__(c); + } + + public Object __coerce_ex__(PyObject o) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__coerce__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(o); + if (res==Py.NotImplemented) + return Py.None; + if (!(res instanceof PyTuple)) + throw Py.TypeError("__coerce__ didn't return a 2-tuple"); + return((PyTuple)res).getArray(); + } + return super.__coerce_ex__(o); + } + + public String toString() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__repr__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(); + if (!(res instanceof PyString)) + throw Py.TypeError("__repr__ returned non-string (type "+res.getType().fastGetName()+")"); + return((PyString)res).toString(); + } + return super.toString(); + } + +} Modified: branches/newlist/src/templates/mappings =================================================================== --- branches/newlist/src/templates/mappings 2009-04-11 01:11:10 UTC (rev 6214) +++ branches/newlist/src/templates/mappings 2009-04-11 03:57:01 UTC (rev 6215) @@ -26,6 +26,7 @@ local.derived:org.python.modules.thread.PyLocalDerived module.derived:org.python.core.PyModuleDerived newlist.derived:org.python.core.PyNewListDerived +newtuple.derived:org.python.core.PyNewTupleDerived object.derived:org.python.core.PyObjectDerived partial.derived:org.python.... [truncated message content] |
From: <pj...@us...> - 2009-04-11 01:11:11
|
Revision: 6214 http://jython.svn.sourceforge.net/jython/?rev=6214&view=rev Author: pjenvey Date: 2009-04-11 01:11:10 +0000 (Sat, 11 Apr 2009) Log Message: ----------- fix test_sax when running under a path name with spaces Modified Paths: -------------- trunk/jython/Lib/test/test_sax.py Modified: trunk/jython/Lib/test/test_sax.py =================================================================== --- trunk/jython/Lib/test/test_sax.py 2009-04-11 00:25:21 UTC (rev 6213) +++ trunk/jython/Lib/test/test_sax.py 2009-04-11 01:11:10 UTC (rev 6214) @@ -2,6 +2,7 @@ # regression test for SAX 2.0 # $Id: test_sax.py,v 1.13 2004/03/20 07:46:04 fdrake Exp $ +import urllib from xml.sax import handler, make_parser, ContentHandler, \ SAXException, SAXReaderNotAvailable, SAXParseException try: @@ -13,7 +14,7 @@ XMLFilterBase, Location from xml.sax.xmlreader import InputSource, AttributesImpl, AttributesNSImpl from cStringIO import StringIO -from test.test_support import verbose, TestFailed, findfile +from test.test_support import is_jython, verbose, TestFailed, findfile # ===== Utilities @@ -472,28 +473,17 @@ parser.setContentHandler(xmlgen) testfile = findfile("test.xml") parser.parse(testfile) - #In Jython, the system id is a URL with forward slashes, and under Windows - #findfile returns a path with backslashes, so replace the backslashes with - #forward - import os - if os.name == 'java': + if is_jython: + # In Jython, the system id is a URL with forward slashes, and + # under Windows findfile returns a path with backslashes, so + # replace the backslashes with forward testfile = testfile.replace('\\', '/') - t = xmlgen.location.getSystemId().endswith(testfile) and \ + # XXX: may not match getSystemId when the filename contains funky + # characters (like ':') + expected = urllib.quote(testfile) + return xmlgen.location.getSystemId().endswith(expected) and \ xmlgen.location.getPublicId() is None - if not t: - print 'os.name: %s' % os.name - print 'systemId (%s): %s publicId (%s): %s' % \ - (type(xmlgen.location.getSystemId()), xmlgen.location.getSystemId(), - type(xmlgen.location.getPublicId()), xmlgen.location.getPublicId()) - content = open(testfile).read() - print '------- contents of %s ---------' % testfile - print content - print '------- end contents of %s ---------' % testfile - print '------- repr ---------' - print repr(content) - print '------- end repr ---------' - return t # =========================================================================== This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2009-04-11 00:25:30
|
Revision: 6213 http://jython.svn.sourceforge.net/jython/?rev=6213&view=rev Author: pjenvey Date: 2009-04-11 00:25:21 +0000 (Sat, 11 Apr 2009) Log Message: ----------- o reapply test_cmd_line workaround o fix error handling of bad -c/-m command lines Modified Paths: -------------- trunk/jython/Lib/test/test_cmd_line.py trunk/jython/src/org/python/util/jython.java trunk/jython/src/shell/jython Modified: trunk/jython/Lib/test/test_cmd_line.py =================================================================== --- trunk/jython/Lib/test/test_cmd_line.py 2009-04-11 00:24:56 UTC (rev 6212) +++ trunk/jython/Lib/test/test_cmd_line.py 2009-04-11 00:25:21 UTC (rev 6213) @@ -50,7 +50,8 @@ self.assertTrue('usage' in self.start_python('-h')) def test_version(self): - version = 'Python %d.%d' % sys.version_info[:2] + prefix = 'J' if test.test_support.is_jython else 'P' + version = prefix + 'ython %d.%d' % sys.version_info[:2] self.assertTrue(self.start_python('-V').startswith(version)) def test_run_module(self): Modified: trunk/jython/src/org/python/util/jython.java =================================================================== --- trunk/jython/src/org/python/util/jython.java 2009-04-11 00:24:56 UTC (rev 6212) +++ trunk/jython/src/org/python/util/jython.java 2009-04-11 00:25:21 UTC (rev 6213) @@ -259,6 +259,7 @@ interp.exec(opts.command); } catch (Throwable t) { Py.printException(t); + System.exit(1); } } @@ -273,7 +274,7 @@ } catch (Throwable t) { Py.printException(t); interp.cleanup(); - System.exit(0); + System.exit(-1); } } } @@ -407,8 +408,20 @@ Options.importSite = false; } else if (arg.equals("-c")) { - command = args[++index]; - if (!fixInteractive) interactive = false; + if (arg.length() > 2) { + command = arg.substring(2); + } + else if ((index + 1) < args.length) { + command = args[++index]; + } else { + System.err.println("Argument expected for the -c option"); + System.err.print(jython.usageHeader); + System.err.println("Try `jython -h' for more information."); + return false; + } + if (!fixInteractive) { + interactive = false; + } index++; break; } Modified: trunk/jython/src/shell/jython =================================================================== --- trunk/jython/src/shell/jython 2009-04-11 00:24:56 UTC (rev 6212) +++ trunk/jython/src/shell/jython 2009-04-11 00:25:21 UTC (rev 6213) @@ -117,9 +117,13 @@ fi ;; # Match switches that take an argument - -c|-C|-jar|-Q|-W) - python_args=("${python_args[@]}" "$1" "$2") - shift + -c|-C|-m|-jar|-Q|-W) + if [ $# = 1 ]; then + python_args=("${python_args[@]}" "$1") + else + python_args=("${python_args[@]}" "$1" "$2") + shift + fi; ;; # Match -Dprop=val type args -D*) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2009-04-11 00:25:05
|
Revision: 6212 http://jython.svn.sourceforge.net/jython/?rev=6212&view=rev Author: pjenvey Date: 2009-04-11 00:24:56 +0000 (Sat, 11 Apr 2009) Log Message: ----------- from: http://svn.python.org/projects/python/branches/release25-maint/Lib/test/test_cmd_line.py@54387 Modified Paths: -------------- trunk/jython/Lib/test/test_cmd_line.py Modified: trunk/jython/Lib/test/test_cmd_line.py =================================================================== --- trunk/jython/Lib/test/test_cmd_line.py 2009-04-10 23:09:34 UTC (rev 6211) +++ trunk/jython/Lib/test/test_cmd_line.py 2009-04-11 00:24:56 UTC (rev 6212) @@ -1,4 +1,3 @@ -# from CPython 2.4, changing Python to Jython in test_version import test.test_support, unittest import sys @@ -7,14 +6,20 @@ class CmdLineTest(unittest.TestCase): def start_python(self, cmd_line): - outfp, infp = popen2.popen4('%s %s' % (sys.executable, cmd_line)) + outfp, infp = popen2.popen4('"%s" %s' % (sys.executable, cmd_line)) infp.close() data = outfp.read() outfp.close() + # try to cleanup the child so we don't appear to leak when running + # with regrtest -R. This should be a no-op on Windows. + popen2._cleanup() return data - def exit_code(self, cmd_line): - return subprocess.call([sys.executable, cmd_line], stderr=subprocess.PIPE) + def exit_code(self, *args): + cmd_line = [sys.executable] + cmd_line.extend(args) + return subprocess.call(cmd_line, stdout=subprocess.PIPE, + stderr=subprocess.PIPE) def test_directories(self): self.assertNotEqual(self.exit_code('.'), 0) @@ -22,7 +27,7 @@ def verify_valid_flag(self, cmd_line): data = self.start_python(cmd_line) - self.assertTrue(data == '' or data.endswith('\n'), repr(data)) + self.assertTrue(data == '' or data.endswith('\n')) self.assertTrue('Traceback' not in data) def test_environment(self): @@ -42,16 +47,47 @@ self.verify_valid_flag('-S') def test_usage(self): - result = self.start_python('-h') - self.assertTrue('usage' in result, repr(result)) + self.assertTrue('usage' in self.start_python('-h')) def test_version(self): - version = 'Jython %d.%d' % sys.version_info[:2] - result = self.start_python('-V') - self.assertTrue(result.startswith(version), repr(result)) + version = 'Python %d.%d' % sys.version_info[:2] + self.assertTrue(self.start_python('-V').startswith(version)) + def test_run_module(self): + # Test expected operation of the '-m' switch + # Switch needs an argument + self.assertNotEqual(self.exit_code('-m'), 0) + # Check we get an error for a nonexistent module + self.assertNotEqual( + self.exit_code('-m', 'fnord43520xyz'), + 0) + # Check the runpy module also gives an error for + # a nonexistent module + self.assertNotEqual( + self.exit_code('-m', 'runpy', 'fnord43520xyz'), + 0) + # All good if module is located and run successfully + self.assertEqual( + self.exit_code('-m', 'timeit', '-n', '1'), + 0) + + def test_run_code(self): + # Test expected operation of the '-c' switch + # Switch needs an argument + self.assertNotEqual(self.exit_code('-c'), 0) + # Check we get an error for an uncaught exception + self.assertNotEqual( + self.exit_code('-c', 'raise Exception'), + 0) + # All good if execution is successful + self.assertEqual( + self.exit_code('-c', 'pass'), + 0) + + def test_main(): test.test_support.run_unittest(CmdLineTest) + test.test_support.reap_children() if __name__ == "__main__": test_main() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2009-04-10 23:09:53
|
Revision: 6211 http://jython.svn.sourceforge.net/jython/?rev=6211&view=rev Author: pjenvey Date: 2009-04-10 23:09:34 +0000 (Fri, 10 Apr 2009) Log Message: ----------- feeble attempt of debugging why these fail on hudson's all job Modified Paths: -------------- trunk/jython/Lib/test/test_cmd_line.py trunk/jython/Lib/test/test_sax.py Modified: trunk/jython/Lib/test/test_cmd_line.py =================================================================== --- trunk/jython/Lib/test/test_cmd_line.py 2009-04-10 21:12:28 UTC (rev 6210) +++ trunk/jython/Lib/test/test_cmd_line.py 2009-04-10 23:09:34 UTC (rev 6211) @@ -42,11 +42,13 @@ self.verify_valid_flag('-S') def test_usage(self): - self.assertTrue('usage' in self.start_python('-h')) + result = self.start_python('-h') + self.assertTrue('usage' in result, repr(result)) def test_version(self): version = 'Jython %d.%d' % sys.version_info[:2] - self.assertTrue(self.start_python('-V').startswith(version)) + result = self.start_python('-V') + self.assertTrue(result.startswith(version), repr(result)) def test_main(): test.test_support.run_unittest(CmdLineTest) Modified: trunk/jython/Lib/test/test_sax.py =================================================================== --- trunk/jython/Lib/test/test_sax.py 2009-04-10 21:12:28 UTC (rev 6210) +++ trunk/jython/Lib/test/test_sax.py 2009-04-10 23:09:34 UTC (rev 6211) @@ -479,8 +479,21 @@ if os.name == 'java': testfile = testfile.replace('\\', '/') - return xmlgen.location.getSystemId().endswith(testfile) and \ + t = xmlgen.location.getSystemId().endswith(testfile) and \ xmlgen.location.getPublicId() is None + if not t: + print 'os.name: %s' % os.name + print 'systemId (%s): %s publicId (%s): %s' % \ + (type(xmlgen.location.getSystemId()), xmlgen.location.getSystemId(), + type(xmlgen.location.getPublicId()), xmlgen.location.getPublicId()) + content = open(testfile).read() + print '------- contents of %s ---------' % testfile + print content + print '------- end contents of %s ---------' % testfile + print '------- repr ---------' + print repr(content) + print '------- end repr ---------' + return t # =========================================================================== This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2009-04-10 21:12:34
|
Revision: 6210 http://jython.svn.sourceforge.net/jython/?rev=6210&view=rev Author: pjenvey Date: 2009-04-10 21:12:28 +0000 (Fri, 10 Apr 2009) Log Message: ----------- ClassType shouldn't go through object's lookup machinery at all. not even __class__ or __call__ should show up in its lookup Modified Paths: -------------- trunk/jython/src/org/python/core/PyClass.java Modified: trunk/jython/src/org/python/core/PyClass.java =================================================================== --- trunk/jython/src/org/python/core/PyClass.java 2009-04-10 19:21:01 UTC (rev 6209) +++ trunk/jython/src/org/python/core/PyClass.java 2009-04-10 21:12:28 UTC (rev 6210) @@ -1,10 +1,7 @@ // Copyright (c) Corporation for National Research Initiatives package org.python.core; -import org.python.expose.ExposedGet; -import org.python.expose.ExposedMethod; import org.python.expose.ExposedNew; -import org.python.expose.ExposedSet; import org.python.expose.ExposedType; /** @@ -16,16 +13,12 @@ public static final PyType TYPE = PyType.fromClass(PyClass.class); /** Holds the namespace for this class */ - @ExposedGet public PyObject __dict__; /** The base classes of this class */ - @ExposedGet public PyTuple __bases__; /** The name of this class */ - @ExposedGet - @ExposedSet public String __name__; // Store these methods for performance optimization. These are only used by PyInstance @@ -112,14 +105,70 @@ @Override public PyObject __findattr_ex__(String name) { + if (name == "__dict__") { + return __dict__; + } + if (name == "__bases__") { + return __bases__; + } + if (name == "__name__") { + return Py.newString(__name__); + } + PyObject result = lookup(name); if (result == null) { - return super.__findattr_ex__(name); + return result; } return result.__get__(null, this); } @Override + public void __setattr__(String name, PyObject value) { + if (name == "__dict__") { + setDict(value); + return; + } else if (name == "__bases__") { + setBases(value); + return; + } else if (name == "__name__") { + setName(value); + return; + } else if (name == "__getattr__") { + __getattr__ = value; + return; + } else if (name == "__setattr__") { + __setattr__ = value; + return; + } else if (name == "__delattr__") { + __delattr__ = value; + return; + } else if (name == "__tojava__") { + __tojava__ = value; + return; + } else if (name == "__del__") { + __del__ = value; + return; + } else if (name == "__contains__") { + __contains__ = value; + return; + } + + if (value == null) { + try { + __dict__.__delitem__(name); + } catch (PyException pye) { + noAttributeError(name); + } + } + __dict__.__setitem__(name, value); + } + + @Override + public void __delattr__(String name) { + __setattr__(name, null); + } + + @Override public void __rawdir__(PyDictionary accum) { mergeClassDict(accum, this); } @@ -135,11 +184,6 @@ @Override public PyObject __call__(PyObject[] args, String[] keywords) { - return classobj___call__(args, keywords); - } - - @ExposedMethod - final PyObject classobj___call__(PyObject[] args, String[] keywords) { PyInstance inst; if (__del__ == null) { inst = new PyInstance(this); @@ -151,6 +195,11 @@ return inst; } + @Override + public boolean isCallable() { + return true; + } + /* PyClass's are compared based on __name__ */ @Override public int __cmp__(PyObject other) { @@ -204,17 +253,20 @@ return false; } - @ExposedSet(name = "__dict__") public void setDict(PyObject value) { - if (!(value instanceof PyStringMap || value instanceof PyDictionary)) { + if (value == null || !(value instanceof PyStringMap || value instanceof PyDictionary)) { throw Py.TypeError("__dict__ must be a dictionary object"); } __dict__ = value; } - @ExposedSet(name = "__bases__") - public void setBases(PyTuple value) { - for (PyObject base : value.getArray()) { + public void setBases(PyObject value) { + if (value == null || !(value instanceof PyTuple)) { + throw Py.TypeError("__bases__ must be a tuple object"); + } + + PyTuple bases = (PyTuple)value; + for (PyObject base : bases.getArray()) { if (!(base instanceof PyClass)) { throw Py.TypeError("__bases__ items must be classes"); } @@ -222,6 +274,17 @@ throw Py.TypeError("a __bases__ item causes an inheritance cycle"); } } - __bases__ = value; + __bases__ = bases; } + + public void setName(PyObject value) { + if (value == null || !Py.isInstance(value, PyString.TYPE)) { + throw Py.TypeError("__name__ must be a string object"); + } + String name = value.toString(); + if (name.contains("\u0000")) { + throw Py.TypeError("__name__ must not contain null bytes"); + } + __name__ = name; + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2009-04-10 19:21:04
|
Revision: 6209 http://jython.svn.sourceforge.net/jython/?rev=6209&view=rev Author: pjenvey Date: 2009-04-10 19:21:01 +0000 (Fri, 10 Apr 2009) Log Message: ----------- cleanup class __module__ setup: o type/ClassType must default a __module__ (instead of Py.makeClass) for when types are constructed via type(name, bases, dict) o simplify the compiler's __module__ setup as it should just fail fast Modified Paths: -------------- trunk/jython/Lib/test/test_class_jy.py trunk/jython/src/org/python/compiler/Module.java trunk/jython/src/org/python/core/Py.java trunk/jython/src/org/python/core/PyClass.java trunk/jython/src/org/python/core/PyFrame.java trunk/jython/src/org/python/core/PyType.java trunk/jython/src/org/python/core/imp.java Modified: trunk/jython/Lib/test/test_class_jy.py =================================================================== --- trunk/jython/Lib/test/test_class_jy.py 2009-04-10 15:36:58 UTC (rev 6208) +++ trunk/jython/Lib/test/test_class_jy.py 2009-04-10 19:21:01 UTC (rev 6209) @@ -17,17 +17,24 @@ self.assertEqual(str.__module__, '__builtin__') class Foo: pass - self.assertEqual(Foo.__module__, __name__) - self.assertEqual(str(Foo), '%s.Foo' % __name__) - self.assert_(repr(Foo).startswith('<class %s.Foo at' % __name__)) - foo = Foo() - self.assert_(str(foo).startswith('<%s.Foo instance at' % __name__)) + Fu = types.ClassType('Fu', (), {}) + for cls in Foo, Fu: + self.assert_('__module__' in cls.__dict__) + self.assertEqual(cls.__module__, __name__) + self.assertEqual(str(cls), '%s.%s' % (__name__, cls.__name__)) + self.assert_(repr(cls).startswith('<class %s.%s at' % + (__name__, cls.__name__))) + obj = cls() + self.assert_(str(obj).startswith('<%s.%s instance at' % + (__name__, cls.__name__))) class Bar(object): pass class Baz(Object): pass - for cls in Bar, Baz: + Bang = type('Bang', (), {}) + for cls in Bar, Baz, Bang: + self.assert_('__module__' in cls.__dict__) self.assertEqual(cls.__module__, __name__) self.assertEqual(str(cls), "<class '%s.%s'>" % (__name__, cls.__name__)) self.assertEqual(repr(cls), "<class '%s.%s'>" % (__name__, cls.__name__)) Modified: trunk/jython/src/org/python/compiler/Module.java =================================================================== --- trunk/jython/src/org/python/compiler/Module.java 2009-04-10 15:36:58 UTC (rev 6208) +++ trunk/jython/src/org/python/compiler/Module.java 2009-04-10 19:21:01 UTC (rev 6209) @@ -438,25 +438,14 @@ CodeCompiler compiler = new CodeCompiler(this, printResults); if (classBody) { - Label label_got_name = new Label(); - int module_tmp = c.getLocal("org/python/core/PyObject"); - + // Set the class's __module__ to __name__. fails when there's no __name__ c.aload(1); c.ldc("__module__"); - c.invokevirtual("org/python/core/PyFrame", "getname_or_null", "(" + $str + ")" + $pyObj); - c.dup(); - c.ifnonnull(label_got_name); - c.pop(); c.aload(1); c.ldc("__name__"); - c.invokevirtual("org/python/core/PyFrame", "getname_or_null", "(" + $str + ")" + $pyObj); + c.invokevirtual("org/python/core/PyFrame", "getname", "(" + $str + ")" + $pyObj); - c.label(label_got_name); - c.astore(module_tmp); - c.aload(1); - c.ldc("__module__"); - c.aload(module_tmp); c.invokevirtual("org/python/core/PyFrame", "setlocal", "(" + $str + $pyObj + ")V"); } Modified: trunk/jython/src/org/python/core/Py.java =================================================================== --- trunk/jython/src/org/python/core/Py.java 2009-04-10 15:36:58 UTC (rev 6208) +++ trunk/jython/src/org/python/core/Py.java 2009-04-10 19:21:01 UTC (rev 6209) @@ -1566,14 +1566,6 @@ * @return a new Python Class PyObject */ public static PyObject makeClass(String name, PyObject[] bases, PyObject dict) { - PyFrame frame = getFrame(); - if (dict.__finditem__("__module__") == null) { - PyObject module = frame.getglobal("__name__"); - if (module != null) { - dict.__setitem__("__module__", module); - } - } - PyObject metaclass = dict.__finditem__("__metaclass__"); if (metaclass == null) { @@ -1584,7 +1576,7 @@ metaclass = base.getType(); } } else { - PyObject globals = frame.f_globals; + PyObject globals = getFrame().f_globals; if (globals != null) { metaclass = globals.__finditem__("__metaclass__"); } Modified: trunk/jython/src/org/python/core/PyClass.java =================================================================== --- trunk/jython/src/org/python/core/PyClass.java 2009-04-10 15:36:58 UTC (rev 6208) +++ trunk/jython/src/org/python/core/PyClass.java 2009-04-10 19:21:01 UTC (rev 6209) @@ -55,10 +55,8 @@ if (!(dict instanceof PyStringMap || dict instanceof PyDictionary)) { throw Py.TypeError("PyClass_New: dict must be a dictionary"); } - if (dict.__finditem__("__doc__") == null) { - dict.__setitem__("__doc__", Py.None); - } - findModule(dict); + PyType.ensureDoc(dict); + PyType.ensureModule(dict); if (!(bases instanceof PyTuple)) { throw Py.TypeError("PyClass_New: bases must be a tuple"); @@ -94,19 +92,6 @@ __contains__ = lookup("__contains__"); } - private static void findModule(PyObject dict) { - PyObject module = dict.__finditem__("__module__"); - if (module == null || module == Py.None) { - PyFrame f = Py.getFrame(); - if (f != null) { - PyObject nm = f.f_globals.__finditem__("__name__"); - if (nm != null) { - dict.__setitem__("__module__", nm); - } - } - } - } - PyObject lookup(String name) { PyObject result = __dict__.__finditem__(name); if (result == null && __bases__ != null) { Modified: trunk/jython/src/org/python/core/PyFrame.java =================================================================== --- trunk/jython/src/org/python/core/PyFrame.java 2009-04-10 15:36:58 UTC (rev 6208) +++ trunk/jython/src/org/python/core/PyFrame.java 2009-04-10 19:21:01 UTC (rev 6209) @@ -276,20 +276,6 @@ throw Py.NameError(String.format(NAME_ERROR_MSG, index)); } - public PyObject getname_or_null(String index) { - PyObject ret; - if (f_locals == null || f_locals == f_globals) { - ret = doGetglobal(index); - } else { - ret = f_locals.__finditem__(index); - if (ret != null) { - return ret; - } - ret = doGetglobal(index); - } - return ret; - } - public PyObject getglobal(String index) { PyObject ret = doGetglobal(index); if (ret != null) { Modified: trunk/jython/src/org/python/core/PyType.java =================================================================== --- trunk/jython/src/org/python/core/PyType.java 2009-04-10 15:36:58 UTC (rev 6208) +++ trunk/jython/src/org/python/core/PyType.java 2009-04-10 19:21:01 UTC (rev 6209) @@ -374,10 +374,8 @@ dict.__setitem__("__new__", new PyStaticMethod(new_)); } - // NOTE: __module__ is already guaranteed by Py.makeClass - if (dict.__finditem__("__doc__") == null) { - dict.__setitem__("__doc__", Py.None); - } + ensureDoc(dict); + ensureModule(dict); // Calculate method resolution order mro_internal(); @@ -396,6 +394,37 @@ } } + /** + * Ensure dict contains a __doc__. + * + * @param dict a PyObject mapping + */ + public static void ensureDoc(PyObject dict) { + if (dict.__finditem__("__doc__") == null) { + dict.__setitem__("__doc__", Py.None); + } + } + + /** + * Ensure dict contains a __module__, retrieving it from the current frame if it + * doesn't exist. + * + * @param dict a PyObject mapping + */ + public static void ensureModule(PyObject dict) { + if (dict.__finditem__("__module__") != null) { + return; + } + PyFrame frame = Py.getFrame(); + if (frame == null) { + return; + } + PyObject name = frame.f_globals.__finditem__("__name__"); + if (name != null) { + dict.__setitem__("__module__", name); + } + } + private static PyObject invoke_new_(PyObject new_, PyType type, boolean init, PyObject[] args, String[] keywords) { PyObject newobj; Modified: trunk/jython/src/org/python/core/imp.java =================================================================== --- trunk/jython/src/org/python/core/imp.java 2009-04-10 15:36:58 UTC (rev 6208) +++ trunk/jython/src/org/python/core/imp.java 2009-04-10 19:21:01 UTC (rev 6209) @@ -20,7 +20,7 @@ private static final String UNKNOWN_SOURCEFILE = "<unknown>"; - public static final int APIVersion = 19; + public static final int APIVersion = 20; public static final int NO_MTIME = -1; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <am...@us...> - 2009-04-10 15:37:02
|
Revision: 6208 http://jython.svn.sourceforge.net/jython/?rev=6208&view=rev Author: amak Date: 2009-04-10 15:36:58 +0000 (Fri, 10 Apr 2009) Log Message: ----------- Creating a poll object cache for MS Windows, because of a problem on that platform with creating too many Selectors. http://bugs.jython.org/issue1291 Modified Paths: -------------- trunk/jython/Lib/select.py Modified: trunk/jython/Lib/select.py =================================================================== --- trunk/jython/Lib/select.py 2009-04-10 13:45:27 UTC (rev 6207) +++ trunk/jython/Lib/select.py 2009-04-10 15:36:58 UTC (rev 6208) @@ -7,10 +7,11 @@ import java.nio.channels.Selector from java.nio.channels.SelectionKey import OP_ACCEPT, OP_CONNECT, OP_WRITE, OP_READ +import errno +import os +import Queue import socket -import errno - class error(Exception): pass ALL = None @@ -144,10 +145,18 @@ except java.lang.Exception, jlx: raise _map_exception(jlx) - def close(self): + def _deregister_all(self): try: for k in self.selector.keys(): k.cancel() + # Keys are not actually removed from the selector until the next select operation. + self.selector.selectNow() + except java.lang.Exception, jlx: + raise _map_exception(jlx) + + def close(self): + try: + self._deregister_all() self.selector.close() except java.lang.Exception, jlx: raise _map_exception(jlx) @@ -165,10 +174,48 @@ return 0 return int(floatvalue * 1000) # Convert to milliseconds +# This cache for poll objects is required because of a bug in java on MS Windows +# http://bugs.jython.org/issue1291 + +class poll_object_cache: + + def __init__(self): + self.is_windows = os.get_os_type() == 'nt' + if self.is_windows: + self.poll_object_queue = Queue.Queue() + import atexit + atexit.register(self.finalize) + + def get_poll_object(self): + if not self.is_windows: + return poll() + try: + return self.poll_object_queue.get(False) + except Queue.Empty: + return poll() + + def release_poll_object(self, pobj): + if self.is_windows: + pobj._deregister_all() + self.poll_object_queue.put(pobj) + else: + pobj.close() + + def finalize(self): + if self.is_windows: + while True: + try: + p = self.poll_object_queue.get(False) + p.close() + except Queue.Empty: + return + +_poll_object_cache = poll_object_cache() + def native_select(read_fd_list, write_fd_list, outofband_fd_list, timeout=None): timeout = _calcselecttimeoutvalue(timeout) # First create a poll object to do the actual watching. - pobj = poll() + pobj = _poll_object_cache.get_poll_object() try: registered_for_read = {} # Check the read list @@ -192,10 +239,7 @@ write_ready_list.append(fd) return read_ready_list, write_ready_list, oob_ready_list finally: - # Need to close the poll object no matter what happened - # If it is left open, it may still have references to sockets - # That were registered before any exceptions occurred - pobj.close() + _poll_object_cache.release_poll_object(pobj) select = native_select This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2009-04-10 13:46:00
|
Revision: 6207 http://jython.svn.sourceforge.net/jython/?rev=6207&view=rev Author: fwierzbicki Date: 2009-04-10 13:45:27 +0000 (Fri, 10 Apr 2009) Log Message: ----------- Merged revisions 6161-6167,6169-6175,6177-6185,6187-6189,6193-6194,6198,6203-6205 via svnmerge from https://jython.svn.sourceforge.net/svnroot/jython/trunk/jython ........ r6161 | pjenvey | 2009-04-04 21:53:25 -0400 (Sat, 04 Apr 2009) | 2 lines convert PyClass to exposed annotations ........ r6162 | pjenvey | 2009-04-04 22:32:50 -0400 (Sat, 04 Apr 2009) | 2 lines remove the _new.classobj hack now that PyClass __new__ does the right thing ........ r6163 | pjenvey | 2009-04-04 23:18:39 -0400 (Sat, 04 Apr 2009) | 2 lines remove stop_at_java cruft -- remnants of old Java integration ........ r6164 | thobes | 2009-04-05 13:22:40 -0400 (Sun, 05 Apr 2009) | 2 lines Fix for http://bugs.jython.org/issue1110 ........ r6165 | fwierzbicki | 2009-04-05 14:29:58 -0400 (Sun, 05 Apr 2009) | 9 lines Fix for http://bugs.jython.org/issue1264 'is not' test exhibits incorrect behaviour when wrapping Java objects Included is a reshuffling of the patch test so that a regression test can pick it up. It isn't perfect since I had to use raw "assert" statements in the python, but it is run by the Java unit test IdentityTest. Thanks to Geoffrey French for the patch. ........ r6166 | pjenvey | 2009-04-05 14:42:28 -0400 (Sun, 05 Apr 2009) | 2 lines fix ant javatest regrtest (the buildbot) unnecessarily compiling twice ........ r6167 | fwierzbicki | 2009-04-05 15:15:19 -0400 (Sun, 05 Apr 2009) | 2 lines remove sys.path appends that where not needed. ........ r6169 | pjenvey | 2009-04-05 18:13:33 -0400 (Sun, 05 Apr 2009) | 2 lines fix dictproxy equality checks ........ r6170 | pjenvey | 2009-04-05 19:21:56 -0400 (Sun, 05 Apr 2009) | 2 lines type shouldn't actually have a __str__ ........ r6171 | pjenvey | 2009-04-05 21:03:47 -0400 (Sun, 05 Apr 2009) | 1 line make exposed methods final ........ r6172 | pjenvey | 2009-04-05 21:38:32 -0400 (Sun, 05 Apr 2009) | 2 lines convert PyInstance to exposed annotations. fixes its binop rule problems (#1197) ........ r6173 | pjenvey | 2009-04-05 22:35:31 -0400 (Sun, 05 Apr 2009) | 3 lines allow comparison of builtin methods, fixes test_descr.methodwrapper minus its implementation detail ........ r6174 | pjenvey | 2009-04-05 23:12:07 -0400 (Sun, 05 Apr 2009) | 3 lines avoid __tojava__ conversion to int in ldexp so it'll acutally raise OverflowErors. helps out SymPy ........ r6175 | pjenvey | 2009-04-05 23:19:30 -0400 (Sun, 05 Apr 2009) | 1 line fix complex.__nonzero__ ........ r6177 | fwierzbicki | 2009-04-06 19:52:09 -0400 (Mon, 06 Apr 2009) | 2 lines Added some missed contributors to ACKNOWLEDGMENTS file. ........ r6178 | fwierzbicki | 2009-04-06 20:33:46 -0400 (Mon, 06 Apr 2009) | 2 lines Added missing committers to Acknowledgments file. ........ r6179 | pjenvey | 2009-04-06 20:37:45 -0400 (Mon, 06 Apr 2009) | 2 lines these now match CPythonLib's ........ r6180 | pjenvey | 2009-04-06 21:49:15 -0400 (Mon, 06 Apr 2009) | 1 line shadow sys.platform ........ r6181 | pjenvey | 2009-04-06 21:52:46 -0400 (Mon, 06 Apr 2009) | 2 lines str/unicode don't need __unicode__, and having it actually confuses some code ........ r6182 | zyasoft | 2009-04-06 22:23:13 -0400 (Mon, 06 Apr 2009) | 2 lines Changed my name to "Jim Baker" ........ r6183 | fwierzbicki | 2009-04-06 22:32:17 -0400 (Mon, 06 Apr 2009) | 3 lines By Jim Baker's suggestion (I agree of course!) specifically acknowledging Alan's contribution of the long standing external project Modjy. ........ r6184 | fwierzbicki | 2009-04-06 22:44:38 -0400 (Mon, 06 Apr 2009) | 2 lines Adding the committer Leonardo Soto. ........ r6185 | pjenvey | 2009-04-07 01:48:59 -0400 (Tue, 07 Apr 2009) | 2 lines parser string input shouldn't go through universal newlines mode ........ r6187 | pjenvey | 2009-04-07 21:14:13 -0400 (Tue, 07 Apr 2009) | 4 lines 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 ........ r6188 | pjenvey | 2009-04-07 21:23:13 -0400 (Tue, 07 Apr 2009) | 3 lines o reapply our compiler workarounds from r4114 o support compiler.compile via builtin compile ........ r6189 | pjenvey | 2009-04-07 21:27:07 -0400 (Tue, 07 Apr 2009) | 3 lines relax the Windows platform matching, don't bother special casing ce fixes #1162 ........ r6193 | pjenvey | 2009-04-08 21:26:22 -0400 (Wed, 08 Apr 2009) | 1 line rearrange per coding standards ........ r6194 | pjenvey | 2009-04-09 21:09:14 -0400 (Thu, 09 Apr 2009) | 2 lines remove 2.3 workarounds ........ r6198 | pjenvey | 2009-04-09 23:32:30 -0400 (Thu, 09 Apr 2009) | 5 lines o cleanup/refactor PyType.newType and add __weakref__ support fixes #1200 o actually encode unicode slot names o ensure __doc__ on all types ........ r6203 | pjenvey | 2009-04-10 00:27:38 -0400 (Fri, 10 Apr 2009) | 1 line test_pyexpat is fail ........ r6204 | pjenvey | 2009-04-10 01:01:22 -0400 (Fri, 10 Apr 2009) | 3 lines fix a new GlobalRef being tracked (reaped) for every getweakrefcount/getweakrefs query ........ r6205 | pjenvey | 2009-04-10 02:33:04 -0400 (Fri, 10 Apr 2009) | 2 lines use the types module instead of the pending deprecation new, whitespace ........ Modified Paths: -------------- branches/newlist/ACKNOWLEDGMENTS branches/newlist/CoreExposed.includes branches/newlist/Lib/compiler/pycodegen.py branches/newlist/Lib/compiler/transformer.py branches/newlist/Lib/new.py branches/newlist/Lib/os.py branches/newlist/Lib/subprocess.py branches/newlist/Lib/test/regrtest.py branches/newlist/Lib/test/test_builtin_jy.py branches/newlist/Lib/test/test_class_jy.py branches/newlist/Lib/test/test_codeop_jy.py branches/newlist/Lib/test/test_complex_jy.py branches/newlist/Lib/test/test_descr.py branches/newlist/Lib/test/test_descr_jy.py branches/newlist/Lib/test/test_dictproxy_jy.py branches/newlist/Lib/test/test_slots_jy.py branches/newlist/Misc/make_binops.py branches/newlist/NEWS branches/newlist/build.xml branches/newlist/src/org/python/core/ParserFacade.java branches/newlist/src/org/python/core/Py.java branches/newlist/src/org/python/core/PyBuiltinMethod.java branches/newlist/src/org/python/core/PyClass.java branches/newlist/src/org/python/core/PyComplex.java branches/newlist/src/org/python/core/PyDictProxy.java branches/newlist/src/org/python/core/PyInstance.java branches/newlist/src/org/python/core/PyObject.java branches/newlist/src/org/python/core/PyString.java branches/newlist/src/org/python/core/PySystemState.java branches/newlist/src/org/python/core/PyType.java branches/newlist/src/org/python/core/PyUnicode.java branches/newlist/src/org/python/modules/Setup.java branches/newlist/src/org/python/modules/_weakref/GlobalRef.java branches/newlist/src/org/python/modules/_weakref/ReferenceType.java branches/newlist/src/org/python/modules/_weakref/WeakrefModule.java branches/newlist/src/org/python/modules/math.java Added Paths: ----------- branches/newlist/tests/java/org/python/tests/identity/ branches/newlist/tests/java/org/python/tests/identity/IdentityObject.java branches/newlist/tests/java/org/python/tests/identity/IdentityTest.java branches/newlist/tests/python/ branches/newlist/tests/python/identity_test.py Removed Paths: ------------- branches/newlist/Lib/test/cfgparser.1 branches/newlist/Lib/test/test_doctest.py branches/newlist/src/org/python/modules/_newmodule.java branches/newlist/tests/java/org/python/tests/identity/IdentityObject.java branches/newlist/tests/java/org/python/tests/identity/IdentityTest.java branches/newlist/tests/python/identity_test.py Property Changed: ---------------- branches/newlist/ Property changes on: branches/newlist ___________________________________________________________________ Modified: svnmerge-integrated - /branches/modjy:1-6074 /branches/pbcvm:1-6045 /trunk/jython:1-6159 + /branches/modjy:1-6074 /branches/pbcvm:1-6045 /trunk/jython:1-6206 Modified: branches/newlist/ACKNOWLEDGMENTS =================================================================== --- branches/newlist/ACKNOWLEDGMENTS 2009-04-10 12:51:05 UTC (rev 6206) +++ branches/newlist/ACKNOWLEDGMENTS 2009-04-10 13:45:27 UTC (rev 6207) @@ -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: @@ -66,6 +68,20 @@ Nathan Franzen Aleks Totic Randolph Brown + Geoffrey French + Tobias Ivarsson + Lino Mastrodomenico + S\xE9bastien Boisg\xE9rault + Jim Baker + Charlie Groves + Otmar Humbel + Philip Jenvey + Nicholas Riley + Frank Wierzbicki + Khalid Zuberi + Sean McGrath + Clark Updike + Leonardo Soto Local Variables: mode: indented-text Modified: branches/newlist/CoreExposed.includes =================================================================== --- branches/newlist/CoreExposed.includes 2009-04-10 12:51:05 UTC (rev 6206) +++ branches/newlist/CoreExposed.includes 2009-04-10 13:45:27 UTC (rev 6207) @@ -6,6 +6,7 @@ org/python/core/PyBoolean.class org/python/core/PyBuiltinCallable.class org/python/core/PyCell.class +org/python/core/PyClass.class org/python/core/PyClassMethod.class org/python/core/PyClassMethodDescr.class org/python/core/PyComplex.class @@ -19,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: branches/newlist/Lib/compiler/pycodegen.py =================================================================== --- branches/newlist/Lib/compiler/pycodegen.py 2009-04-10 12:51:05 UTC (rev 6206) +++ branches/newlist/Lib/compiler/pycodegen.py 2009-04-10 13:45:27 UTC (rev 6207) @@ -3,17 +3,19 @@ import marshal import struct import sys -import types 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_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) +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? @@ -49,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: @@ -121,8 +127,7 @@ f.write(self.getPycHeader()) marshal.dump(self.code, f) - #MAGIC = imp.get_magic() - MAGIC = None + MAGIC = None if is_jython else imp.get_magic() def getPycHeader(self): # compile.c uses marshal to write a long directly, with @@ -207,8 +212,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 +222,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 +376,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 +390,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 +404,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 +538,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 +636,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 +827,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 +893,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 +908,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 +1049,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 +1153,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 +1342,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 +1374,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 +1384,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 +1407,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 +1470,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: branches/newlist/Lib/compiler/transformer.py =================================================================== --- branches/newlist/Lib/compiler/transformer.py 2009-04-10 12:51:05 UTC (rev 6206) +++ branches/newlist/Lib/compiler/transformer.py 2009-04-10 13:45:27 UTC (rev 6207) @@ -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,21 @@ # 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 symbol import token import sys +if not sys.platform.startswith('java'): + import parser -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 +71,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 +90,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 +121,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 +166,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 +195,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 +278,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 +301,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 +318,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 +354,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 +383,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 +429,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 +482,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 +491,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 +506,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 +519,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 +533,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 +571,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 +606,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 +640,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 +660,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 +672,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 +700,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 +716,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 +764,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 +835,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 +872,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 +943,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 +987,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 +1024,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 +1040,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 +1056,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 +1068,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 +1078,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 +1097,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 +1122,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 +1132,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 +1142,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_... [truncated message content] |
From: <fwi...@us...> - 2009-04-10 12:51:15
|
Revision: 6206 http://jython.svn.sourceforge.net/jython/?rev=6206&view=rev Author: fwierzbicki Date: 2009-04-10 12:51:05 +0000 (Fri, 10 Apr 2009) Log Message: ----------- Initialized merge tracking via "svnmerge" with revisions "1-6159" from https://jython.svn.sourceforge.net/svnroot/jython/trunk/jython Property Changed: ---------------- branches/newlist/ Property changes on: branches/newlist ___________________________________________________________________ Modified: svnmerge-integrated - /branches/modjy:1-6074 /branches/pbcvm:1-6045 + /branches/modjy:1-6074 /branches/pbcvm:1-6045 /trunk/jython:1-6159 This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2009-04-10 06:33:05
|
Revision: 6205 http://jython.svn.sourceforge.net/jython/?rev=6205&view=rev Author: pjenvey Date: 2009-04-10 06:33:04 +0000 (Fri, 10 Apr 2009) Log Message: ----------- use the types module instead of the pending deprecation new, whitespace Modified Paths: -------------- trunk/jython/Lib/test/test_class_jy.py Modified: trunk/jython/Lib/test/test_class_jy.py =================================================================== --- trunk/jython/Lib/test/test_class_jy.py 2009-04-10 05:01:22 UTC (rev 6204) +++ trunk/jython/Lib/test/test_class_jy.py 2009-04-10 06:33:04 UTC (rev 6205) @@ -4,7 +4,7 @@ Made for Jython """ import __builtin__ -import new +import types import unittest from java.lang import Object from test import test_support @@ -34,7 +34,6 @@ self.assert_(str(Bar()).startswith('<%s.Bar object at' % __name__)) self.assert_(str(Baz()).startswith("org.python.proxies.%s$Baz" % __name__)) - def test_builtin_attributes(self): for attr, val in dict(__name__='foo', __module__='bar', __dict__={}, __flags__=1, __base__=object, @@ -119,7 +118,7 @@ pass def hello(self): return 'hello' - Bar = new.classobj('Bar', (Foo,), dict(hello=hello)) + Bar = types.ClassType('Bar', (Foo,), dict(hello=hello)) self.assert_(type(Bar), type) self.assert_(issubclass(Bar, Foo)) self.assert_(hasattr(Bar, 'hello')) @@ -183,8 +182,8 @@ def __getitem__(self, key): raise IndexError, "Fraid not" self.assertRaises(IndexError, A().__getitem__, 'b') - + class ClassNamelessModuleTestCase(unittest.TestCase): def setUp(self): @@ -306,15 +305,12 @@ class JavaClassNamingTestCase(unittest.TestCase): - """ - Tests for PyJavaClass naming. - """ + """Tests for PyJavaClass naming.""" + def test_java_class_name(self): - """ - The __name__ and __module__ attributes of Java classes should be set - according to the same convention that Python uses. - """ + # The __name__ and __module__ attributes of Java classes should + # be set according to the same convention that Python uses. from java.lang import String self.assertEqual(String.__name__, "String") self.assertEqual(String.__module__, "java.lang") @@ -323,7 +319,9 @@ module_name = __name__ class ClassDefinesDunderModule(unittest.TestCase): + """Verifies http://bugs.jython.org/issue1022 is fixed""" + def test_dundermodule_in_classdef(self): class Foo: self.assertEqual(__module__, module_name) @@ -359,17 +357,17 @@ # type.__str__ previously broke this self.assertEqual(str(Bar), 'foo') - + def test_main(): - test_support.run_unittest(ClassGeneralTestCase, - ClassNamelessModuleTestCase, - BrokenNameTestCase, - ClassLocalsTestCase, - IsDescendentTestCase, - JavaClassNamingTestCase, - ClassDefinesDunderModule, - ClassMetaclassRepr, - ) + test_support.run_unittest( + ClassGeneralTestCase, + ClassNamelessModuleTestCase, + BrokenNameTestCase, + ClassLocalsTestCase, + IsDescendentTestCase, + JavaClassNamingTestCase, + ClassDefinesDunderModule, + ClassMetaclassRepr) if __name__ == "__main__": This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2009-04-10 05:01:35
|
Revision: 6204 http://jython.svn.sourceforge.net/jython/?rev=6204&view=rev Author: pjenvey Date: 2009-04-10 05:01:22 +0000 (Fri, 10 Apr 2009) Log Message: ----------- fix a new GlobalRef being tracked (reaped) for every getweakrefcount/getweakrefs query Modified Paths: -------------- trunk/jython/src/org/python/core/PyType.java trunk/jython/src/org/python/modules/_weakref/GlobalRef.java trunk/jython/src/org/python/modules/_weakref/WeakrefModule.java Modified: trunk/jython/src/org/python/core/PyType.java =================================================================== --- trunk/jython/src/org/python/core/PyType.java 2009-04-10 04:27:38 UTC (rev 6203) +++ trunk/jython/src/org/python/core/PyType.java 2009-04-10 05:01:22 UTC (rev 6204) @@ -18,7 +18,7 @@ import org.python.expose.ExposedSet; import org.python.expose.ExposedType; import org.python.expose.TypeBuilder; -import org.python.modules._weakref.GlobalRef; +import org.python.modules._weakref.WeakrefModule; import org.python.util.Generic; /** @@ -325,7 +325,7 @@ @Override public Object invokeGet(PyObject obj) { - PyList weakrefs = GlobalRef.newInstance(obj).refs(); + PyList weakrefs = WeakrefModule.getweakrefs(obj); switch (weakrefs.size()) { case 0: return Py.None; Modified: trunk/jython/src/org/python/modules/_weakref/GlobalRef.java =================================================================== --- trunk/jython/src/org/python/modules/_weakref/GlobalRef.java 2009-04-10 04:27:38 UTC (rev 6203) +++ trunk/jython/src/org/python/modules/_weakref/GlobalRef.java 2009-04-10 05:01:22 UTC (rev 6204) @@ -110,6 +110,12 @@ return new PyList(list); } + /** + * Create a new tracked GlobalRef. + * + * @param object a PyObject to reference + * @return a new tracked GlobalRef + */ public static GlobalRef newInstance(PyObject object) { GlobalRef ref = objects.get(new GlobalRef(object)); if (ref == null) { @@ -120,6 +126,28 @@ } /** + * Return the number of references to the specified PyObject. + * + * @param object a PyObject + * @return an int reference count + */ + public static int getCount(PyObject object) { + GlobalRef ref = objects.get(new GlobalRef(object)); + return ref == null ? 0 : ref.count(); + } + + /** + * Return a list of references to the specified PyObject. + * + * @param object a PyObject + * @return a PyList of references. may be empty + */ + public static PyList getRefs(PyObject object) { + GlobalRef ref = objects.get(new GlobalRef(object)); + return ref == null ? new PyList() : ref.refs(); + } + + /** * Allow GlobalRef's to be used as hashtable keys. */ public boolean equals(Object o) { Modified: trunk/jython/src/org/python/modules/_weakref/WeakrefModule.java =================================================================== --- trunk/jython/src/org/python/modules/_weakref/WeakrefModule.java 2009-04-10 04:27:38 UTC (rev 6203) +++ trunk/jython/src/org/python/modules/_weakref/WeakrefModule.java 2009-04-10 05:01:22 UTC (rev 6204) @@ -55,20 +55,12 @@ } } - public static int getweakrefcount(PyObject o) { - GlobalRef ref = GlobalRef.newInstance(o); - if (ref == null) { - return 0; - } - return ref.count(); + public static int getweakrefcount(PyObject object) { + return GlobalRef.getCount(object); } - public static PyList getweakrefs(PyObject o) { - GlobalRef ref = GlobalRef.newInstance(o); - if (ref == null) { - return new PyList(); - } - return ref.refs(); + public static PyList getweakrefs(PyObject object) { + return GlobalRef.getRefs(object); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2009-04-10 04:27:51
|
Revision: 6203 http://jython.svn.sourceforge.net/jython/?rev=6203&view=rev Author: pjenvey Date: 2009-04-10 04:27:38 +0000 (Fri, 10 Apr 2009) Log Message: ----------- test_pyexpat is fail Modified Paths: -------------- trunk/jython/Lib/test/regrtest.py Modified: trunk/jython/Lib/test/regrtest.py =================================================================== --- trunk/jython/Lib/test/regrtest.py 2009-04-10 04:01:54 UTC (rev 6202) +++ trunk/jython/Lib/test/regrtest.py 2009-04-10 04:27:38 UTC (rev 6203) @@ -1424,7 +1424,6 @@ test_plistlib test_poll test_pty - test_pyexpat test_resource test_rgbimg test_scriptpackages @@ -1477,6 +1476,7 @@ test_multibytecodec_support test_peepholer test_pyclbr + test_pyexpat test_stringprep test_threadsignals test_transformer This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2009-04-10 04:01:56
|
Revision: 6202 http://jython.svn.sourceforge.net/jython/?rev=6202&view=rev Author: fwierzbicki Date: 2009-04-10 04:01:54 +0000 (Fri, 10 Apr 2009) Log Message: ----------- Added null check. Modified Paths: -------------- branches/newlist/src/org/python/core/PyList.java Modified: branches/newlist/src/org/python/core/PyList.java =================================================================== --- branches/newlist/src/org/python/core/PyList.java 2009-04-10 03:57:54 UTC (rev 6201) +++ branches/newlist/src/org/python/core/PyList.java 2009-04-10 04:01:54 UTC (rev 6202) @@ -134,7 +134,7 @@ value = new PyList((PySequence) value); } setsliceIterator(start, stop, step, value.asIterable().iterator()); - } else if (!(value instanceof List)) { + } else if (value != null && !(value instanceof List)) { //XXX: can we avoid copying here? Needed to pass test_userlist value = new PyList(value); setsliceIterator(start, stop, step, value.asIterable().iterator()); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2009-04-10 03:57:56
|
Revision: 6201 http://jython.svn.sourceforge.net/jython/?rev=6201&view=rev Author: fwierzbicki Date: 2009-04-10 03:57:54 +0000 (Fri, 10 Apr 2009) Log Message: ----------- Pesimistic copy of list if not a PySequence and not a java.util.List. Needed to pass test_userlist, but feels like we could avoid sometimes, but how to check? Modified Paths: -------------- branches/newlist/src/org/python/core/PyList.java Modified: branches/newlist/src/org/python/core/PyList.java =================================================================== --- branches/newlist/src/org/python/core/PyList.java 2009-04-10 03:53:07 UTC (rev 6200) +++ branches/newlist/src/org/python/core/PyList.java 2009-04-10 03:57:54 UTC (rev 6201) @@ -135,6 +135,8 @@ } setsliceIterator(start, stop, step, value.asIterable().iterator()); } else if (!(value instanceof List)) { + //XXX: can we avoid copying here? Needed to pass test_userlist + value = new PyList(value); setsliceIterator(start, stop, step, value.asIterable().iterator()); } else { System.err.println("List"); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2009-04-10 03:53:10
|
Revision: 6200 http://jython.svn.sourceforge.net/jython/?rev=6200&view=rev Author: fwierzbicki Date: 2009-04-10 03:53:07 +0000 (Fri, 10 Apr 2009) Log Message: ----------- Fix ClassCastException when not PySequence and not List. Modified Paths: -------------- branches/newlist/src/org/python/core/PyList.java Modified: branches/newlist/src/org/python/core/PyList.java =================================================================== --- branches/newlist/src/org/python/core/PyList.java 2009-04-10 03:49:01 UTC (rev 6199) +++ branches/newlist/src/org/python/core/PyList.java 2009-04-10 03:53:07 UTC (rev 6200) @@ -129,11 +129,13 @@ if (stop < start) { stop = start; } - if ((value instanceof PySequence) || (!(value instanceof List))) { + if (value instanceof PySequence) { if (value == this) { // copy value = new PyList((PySequence) value); } setsliceIterator(start, stop, step, value.asIterable().iterator()); + } else if (!(value instanceof List)) { + setsliceIterator(start, stop, step, value.asIterable().iterator()); } else { System.err.println("List"); List valueList = (List) value.__tojava__(List.class); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2009-04-10 03:49:09
|
Revision: 6199 http://jython.svn.sourceforge.net/jython/?rev=6199&view=rev Author: zyasoft Date: 2009-04-10 03:49:01 +0000 (Fri, 10 Apr 2009) Log Message: ----------- Added back logic for PyList.equals(List) Modified Paths: -------------- branches/newlist/src/org/python/core/PyList.java Modified: branches/newlist/src/org/python/core/PyList.java =================================================================== --- branches/newlist/src/org/python/core/PyList.java 2009-04-10 03:32:30 UTC (rev 6198) +++ branches/newlist/src/org/python/core/PyList.java 2009-04-10 03:49:01 UTC (rev 6199) @@ -775,6 +775,8 @@ public boolean equals(Object o) { if (o instanceof PyList) { return (((PyList) o).list.equals(list)); + } else if(o instanceof List) { // XXX copied from PyList, but... + return o.equals(this); // XXX shouldn't this compare using py2java? } return false; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2009-04-10 03:32:37
|
Revision: 6198 http://jython.svn.sourceforge.net/jython/?rev=6198&view=rev Author: pjenvey Date: 2009-04-10 03:32:30 +0000 (Fri, 10 Apr 2009) Log Message: ----------- o cleanup/refactor PyType.newType and add __weakref__ support fixes #1200 o actually encode unicode slot names o ensure __doc__ on all types Modified Paths: -------------- trunk/jython/Lib/test/test_descr.py trunk/jython/Lib/test/test_slots_jy.py trunk/jython/src/org/python/core/PyType.java trunk/jython/src/org/python/modules/_weakref/ReferenceType.java Modified: trunk/jython/Lib/test/test_descr.py =================================================================== --- trunk/jython/Lib/test/test_descr.py 2009-04-10 03:14:42 UTC (rev 6197) +++ trunk/jython/Lib/test/test_descr.py 2009-04-10 03:32:30 UTC (rev 6198) @@ -1346,9 +1346,6 @@ a.foo = 42 vereq(a.__dict__, {"foo": 42}) - # XXX: Jython doesn't support __weakref__ - return - class W(object): __slots__ = ["__weakref__"] a = W() @@ -3442,11 +3439,7 @@ if verbose: print "Testing dict-proxy iterkeys..." keys = [ key for key in C.__dict__.iterkeys() ] keys.sort() - if is_jython: - # XXX: It should include __doc__, but no __weakref__ (for now) - vereq(keys, ['__dict__', '__module__', 'meth']) - else: - vereq(keys, ['__dict__', '__doc__', '__module__', '__weakref__', 'meth']) + vereq(keys, ['__dict__', '__doc__', '__module__', '__weakref__', 'meth']) def dictproxyitervalues(): class C(object): @@ -3454,8 +3447,7 @@ pass if verbose: print "Testing dict-proxy itervalues..." values = [ values for values in C.__dict__.itervalues() ] - # XXX: See dictproxyiterkeys - vereq(len(values), is_jython and 3 or 5) + vereq(len(values), 5) def dictproxyiteritems(): class C(object): @@ -3464,10 +3456,7 @@ if verbose: print "Testing dict-proxy iteritems..." keys = [ key for (key, value) in C.__dict__.iteritems() ] keys.sort() - if is_jython: - vereq(keys, ['__dict__', '__module__', 'meth']) - else: - vereq(keys, ['__dict__', '__doc__', '__module__', '__weakref__', 'meth']) + vereq(keys, ['__dict__', '__doc__', '__module__', '__weakref__', 'meth']) def funnynew(): if verbose: print "Testing __new__ returning something unexpected..." Modified: trunk/jython/Lib/test/test_slots_jy.py =================================================================== --- trunk/jython/Lib/test/test_slots_jy.py 2009-04-10 03:14:42 UTC (rev 6197) +++ trunk/jython/Lib/test/test_slots_jy.py 2009-04-10 03:32:30 UTC (rev 6198) @@ -124,9 +124,20 @@ self.assertEqual(baz.__dict__, {'bar': 'hello bar'}) +class SlottedWithWeakrefTestCase(unittest.TestCase): + + def test_subclass_oldstyle(self): + class OldBase: + pass + class Foo(OldBase, object): + __slots__ = '__dict__' + self.assert_(hasattr(Foo, '__weakref__')) + + def test_main(): test_support.run_unittest(SlottedTestCase, - SlottedWithDictTestCase) + SlottedWithDictTestCase, + SlottedWithWeakrefTestCase) if __name__ == '__main__': Modified: trunk/jython/src/org/python/core/PyType.java =================================================================== --- trunk/jython/src/org/python/core/PyType.java 2009-04-10 03:14:42 UTC (rev 6197) +++ trunk/jython/src/org/python/core/PyType.java 2009-04-10 03:32:30 UTC (rev 6198) @@ -1,3 +1,4 @@ +/* Copyright (c) Jython Developers */ package org.python.core; import java.io.Serializable; @@ -17,6 +18,7 @@ import org.python.expose.ExposedSet; import org.python.expose.ExposedType; import org.python.expose.TypeBuilder; +import org.python.modules._weakref.GlobalRef; import org.python.util.Generic; /** @@ -27,7 +29,10 @@ public static PyType TYPE = fromClass(PyType.class); - /** The type's name. builtin types include their fully qualified name, e.g.: time.struct_time. */ + /** + * The type's name. builtin types include their fully qualified name, e.g.: + * time.struct_time. + */ protected String name; /** __base__, the direct base type or null. */ @@ -46,8 +51,8 @@ private long tp_flags; /** - * The Java Class instances of this type will be represented as, or null if it's determined by a - * base type. + * The Java Class instances of this type will be represented as, or null if it's + * determined by a base type. */ protected Class<?> underlying_class; @@ -64,12 +69,15 @@ /** Whether this type allows subclassing. */ private boolean isBaseType = true; + /** Whether this type has a __dict__. */ + protected boolean needs_userdict; + + /** Whether this type has a __weakref__ slot (however all types are weakrefable). */ + protected boolean needs_weakref; + /** Whether finalization is required for this type's instances (implements __del__). */ private boolean needs_finalizer; - /** Whether this type's instances require a __dict__. */ - protected boolean needs_userdict; - /** The number of __slots__ defined. */ private int numSlots; @@ -86,9 +94,9 @@ super(subtype); } + private PyType() { + } - private PyType() {} - /** * Creates the PyType instance for type itself. The argument just exists to make the constructor * distinct. @@ -100,33 +108,31 @@ @ExposedNew public static PyObject type___new__(PyNewWrapper new_, boolean init, PyType subtype, PyObject[] args, String[] keywords) { + // Special case: type(x) should return x.getType() if (args.length == 1 && keywords.length == 0) { return args[0].getType(); } + // If that didn't trigger, we need 3 arguments. but ArgParser below may give a msg + // saying type() needs exactly 3. if (args.length + keywords.length != 3) { - throw Py.TypeError("type() takes exactly 1 or 3 arguments"); + throw Py.TypeError("type() takes 1 or 3 arguments"); } ArgParser ap = new ArgParser("type()", args, keywords, "name", "bases", "dict"); String name = ap.getString(0); - PyObject bases = ap.getPyObject(1); - - if (!(bases instanceof PyTuple)) { - throw Py.TypeError("type(): bases must be tuple"); - } + PyTuple bases = (PyTuple)ap.getPyObjectByType(1, PyTuple.TYPE); PyObject dict = ap.getPyObject(2); if (!(dict instanceof PyDictionary || dict instanceof PyStringMap)) { - throw Py.TypeError("type(): dict must be dict"); + throw Py.TypeError("type(): argument 3 must be dict, not " + dict.getType()); } - return newType(new_, subtype, name, (PyTuple)bases, dict); + return newType(new_, subtype, name, bases, dict); } public static PyObject newType(PyNewWrapper new_, PyType metatype, String name, PyTuple bases, PyObject dict) { - PyType object_type = fromClass(PyObject.class); + PyObject[] tmpBases = bases.getArray(); + PyType winner = findMostDerivedMetatype(tmpBases, metatype); - PyObject[] bases_list = bases.getArray(); - PyType winner = findMostDerivedMetatype(bases_list, metatype); if (winner != metatype) { PyObject winner_new_ = winner.lookup("__new__"); if (winner_new_ != null && winner_new_ != new_) { @@ -136,166 +142,146 @@ } metatype = winner; } - // Use PyType as the metaclass for Python subclasses of Java classes rather than PyJavaType. - // Using PyJavaType as metaclass exposes the java.lang.Object methods on the type, which - // doesn't make sense for python subclasses. + + // Use PyType as the metaclass for Python subclasses of Java classes rather than + // PyJavaType. Using PyJavaType as metaclass exposes the java.lang.Object methods + // on the type, which doesn't make sense for python subclasses. if (metatype == PyType.fromClass(Class.class)) { metatype = TYPE; } - if (bases_list.length == 0) { - bases_list = new PyObject[] {object_type}; - } - List<Class<?>> interfaces = Generic.list(); - Class<?> baseClass = null; - for (PyObject base : bases_list) { - if (!(base instanceof PyType)) { - continue; - } - Class<?> proxy = ((PyType)base).getProxyType(); - if (proxy == null) { - continue; - } - if (proxy.isInterface()) { - interfaces.add(proxy); - } else { - if (baseClass != null) { - throw Py.TypeError("no multiple inheritance for Java classes: " - + proxy.getName() + " and " + baseClass.getName()); - } - baseClass = proxy; - } - } - Class<?> proxyClass = null; - if (baseClass != null || interfaces.size() != 0) { - String proxyName = name; - PyObject module = dict.__finditem__("__module__"); - if (module != null) { - proxyName = module.toString() + "$" + proxyName; - } - proxyClass = MakeProxies.makeProxy(baseClass, interfaces, name, proxyName, dict); - PyType proxyType = PyType.fromClass(proxyClass); - List<PyObject> cleanedBases = Generic.list(); - boolean addedProxyType = false; - for (PyObject base : bases_list) { - if (!(base instanceof PyType)) { - cleanedBases.add(base); - continue; - } - Class<?> proxy = ((PyType)base).getProxyType(); - if (proxy == null) { - cleanedBases.add(base);// non-proxy types go straight into our lookup - } else { - if (!(base instanceof PyJavaType)) { - // python subclasses of proxy types need to be added as a base so their - // version of methods will show up - cleanedBases.add(base); - } else if (!addedProxyType) { - // Only add a single Java type, since everything's going to go through the - // proxy type - cleanedBases.add(proxyType); - addedProxyType = true; - } - } - } - bases_list = cleanedBases.toArray(new PyObject[cleanedBases.size()]); - } - PyType newtype; + PyType type; if (new_.for_type == metatype || metatype == PyType.fromClass(Class.class)) { - newtype = new PyType(); // XXX set metatype + // XXX: set metatype + type = new PyType(); } else { - newtype = new PyTypeDerived(metatype); + type = new PyTypeDerived(metatype); } - if (proxyClass != null) { - newtype.javaProxy = proxyClass; - } + if (dict instanceof PyStringMap) { dict = ((PyStringMap)dict).copy(); } else { dict = ((PyDictionary)dict).copy(); } - if (dict.__finditem__("__module__") == null) { - PyFrame frame = Py.getFrame(); - if (frame != null) { - PyObject globals = frame.f_globals; - PyObject modname; - if ((modname = globals.__finditem__("__name__")) != null) { - dict.__setitem__("__module__", modname); - } - } - } - // XXX also __doc__ __module__ + type.name = name; + type.bases = tmpBases.length == 0 ? new PyObject[] {PyObject.TYPE} : tmpBases; + type.dict = dict; + type.tp_flags = Py.TPFLAGS_HEAPTYPE | Py.TPFLAGS_BASETYPE; - newtype.dict = dict; - newtype.name = name; - newtype.base = best_base(bases_list); - newtype.numSlots = newtype.base.numSlots; - newtype.bases = bases_list; + // immediately setup the javaProxy if applicable. may modify bases + List<Class<?>> interfaces = Generic.list(); + Class<?> baseProxyClass = getJavaLayout(type.bases, interfaces); + type.setupProxy(baseProxyClass, interfaces); - if (!newtype.base.isBaseType) { + PyType base = type.base = best_base(type.bases); + if (!base.isBaseType) { throw Py.TypeError(String.format("type '%.100s' is not an acceptable base type", - newtype.base.name)); + base.name)); } + type.createAllSlots(!base.needs_userdict, !base.needs_weakref); + type.ensureAttributes(); + + for (PyObject cur : type.bases) { + if (cur instanceof PyType) + ((PyType)cur).attachSubclass(type); + } + + return type; + } + + /** + * Create all slots and related descriptors. + * + * @param mayAddDict whether a __dict__ descriptor is allowed on this type + * @param mayAddWeak whether a __weakref__ descriptor is allowed on this type + */ + private void createAllSlots(boolean mayAddDict, boolean mayAddWeak) { + numSlots = base.numSlots; + boolean wantDict = false; + boolean wantWeak = false; PyObject slots = dict.__finditem__("__slots__"); - boolean needsDictDescr = false; + if (slots == null) { - newtype.needs_userdict = true; - // a dict descriptor is required if base doesn't already provide a dict - needsDictDescr = !newtype.base.needs_userdict; + wantDict = mayAddDict; + wantWeak = mayAddWeak; } else { - // have slots, but may inherit a dict - newtype.needs_userdict = newtype.base.needs_userdict; + if (slots instanceof PyString) { + slots = new PyTuple(slots); + } - if (slots instanceof PyString) { - addSlot(newtype, slots); - } else { - for (PyObject slotname : slots.asIterable()) { - addSlot(newtype, slotname); + // Check for valid slot names and create them. Handle two special cases + for (PyObject slot : slots.asIterable()) { + String slotName = confirmIdentifier(slot); + + if (slotName.equals("__dict__")) { + if (!mayAddDict || wantDict) { + throw Py.TypeError("__dict__ slot disallowed: we already got one"); + } + wantDict = true; + } else if (slotName.equals("__weakref__")) { + if (!mayAddWeak || wantWeak) { + throw Py.TypeError("__weakref__ slot disallowed: we already got one"); + } + wantWeak = true; + } else { + slotName = mangleName(name, slotName); + if (dict.__finditem__(slotName) == null) { + dict.__setitem__(slotName, new PySlot(this, slotName, numSlots++)); + } } } - if (!newtype.base.needs_userdict && newtype.needs_userdict) { - // base doesn't provide dict but addSlot found the __dict__ slot - needsDictDescr = true; - } else if (bases_list.length > 0 && !newtype.needs_userdict) { - // secondary bases may provide dict - for (PyObject base : bases_list) { - if (base == newtype.base) { + // Secondary bases may provide weakrefs or dict + if (bases.length > 1 + && ((mayAddDict && !wantDict) || (mayAddWeak && !wantWeak))) { + for (PyObject base : bases) { + if (base == this.base) { // Skip primary base continue; } + if (base instanceof PyClass) { - // Classic base class provides dict - newtype.needs_userdict = true; - needsDictDescr = true; + // Classic base class provides both + if (mayAddDict && !wantDict) { + wantDict = true; + } + if (mayAddWeak && !wantWeak) { + wantWeak = true; + } break; } - PyType tmpType = (PyType)base; - if (tmpType.needs_userdict) { - newtype.needs_userdict = true; - needsDictDescr = true; + + PyType baseType = (PyType)base; + if (mayAddDict && !wantDict && baseType.needs_userdict) { + wantDict = true; + } + if (mayAddWeak && !wantWeak && baseType.needs_weakref) { + wantWeak = true; + } + if ((!mayAddDict || wantDict) && (!mayAddWeak || wantWeak)) { // Nothing more to check break; } } } } - - newtype.tp_flags = Py.TPFLAGS_HEAPTYPE | Py.TPFLAGS_BASETYPE; - - // special case __new__, if function => static method - PyObject tmp = dict.__finditem__("__new__"); - if (tmp != null && tmp instanceof PyFunction) { // XXX java functions? - dict.__setitem__("__new__", new PyStaticMethod(tmp)); + + if (wantDict) { + createDictSlot(); } + if (wantWeak) { + createWeakrefSlot(); + } + needs_finalizer = lookup("__del__") != null; + } - newtype.mro_internal(); - // __dict__ descriptor - if (needsDictDescr && dict.__finditem__("__dict__") == null) { - dict.__setitem__("__dict__", new PyDataDescr(newtype, "__dict__", PyObject.class) { - + /** + * Create the __dict__ descriptor. + */ + private void createDictSlot() { + dict.__setitem__("__dict__", new PyDataDescr(this, "__dict__", PyObject.class) { @Override public Object invokeGet(PyObject obj) { return obj.getDict(); @@ -321,16 +307,93 @@ obj.delDict(); } }); + needs_userdict = true; + } + + /** + * Create the __weakref__ descriptor. + */ + private void createWeakrefSlot() { + dict.__setitem__("__weakref__", new PyDataDescr(this, "__weakref__", PyObject.class) { + private static final String writeMsg = + "attribute '%s' of '%s' objects is not writable"; + + private void notWritable(PyObject obj) { + throw Py.AttributeError(String.format(writeMsg, "__weakref__", + obj.getType().fastGetName())); + } + + @Override + public Object invokeGet(PyObject obj) { + PyList weakrefs = GlobalRef.newInstance(obj).refs(); + switch (weakrefs.size()) { + case 0: + return Py.None; + case 1: + return weakrefs.pyget(0); + default: + return weakrefs; + + } + } + + @Override + public boolean implementsDescrSet() { + return true; + } + + @Override + public void invokeSet(PyObject obj, Object value) { + // XXX: Maybe have PyDataDescr do notWritable() for us + notWritable(obj); + } + + @Override + public boolean implementsDescrDelete() { + return true; + } + + @Override + public void invokeDelete(PyObject obj) { + notWritable(obj); + } + }); + needs_weakref = true; + } + + /** + * Setup this type's special attributes. + */ + private void ensureAttributes() { + inheritSpecial(); + + // special case __new__, if function => static method + PyObject new_ = dict.__finditem__("__new__"); + // XXX: java functions? + if (new_ != null && new_ instanceof PyFunction) { + dict.__setitem__("__new__", new PyStaticMethod(new_)); } - newtype.fillHasSetAndDelete(); - newtype.needs_finalizer = newtype.lookup("__del__") != null; + // NOTE: __module__ is already guaranteed by Py.makeClass + if (dict.__finditem__("__doc__") == null) { + dict.__setitem__("__doc__", Py.None); + } - for (PyObject cur : bases_list) { - if (cur instanceof PyType) - ((PyType)cur).attachSubclass(newtype); + // Calculate method resolution order + mro_internal(); + fillHasSetAndDelete(); + } + + /** + * Inherit special attributes from the dominant base. + */ + private void inheritSpecial() { + if (!needs_userdict && base.needs_userdict) { + needs_userdict = true; } - return newtype; + if (!needs_weakref && base.needs_weakref) { + needs_weakref = true; + } } private static PyObject invoke_new_(PyObject new_, PyType type, boolean init, PyObject[] args, @@ -436,6 +499,89 @@ return base.getLayout(); } + /** + * Get the most parent Java proxy Class from bases, tallying any encountered Java + * interfaces. + * + * @param bases array of base Jython classes + * @param interfaces List for collecting interfaces to + * @return base Java proxy Class + * @raises Py.TypeError if multiple Java inheritance was attempted + */ + private static Class<?> getJavaLayout(PyObject[] bases, List<Class<?>> interfaces) { + Class<?> baseProxy = null; + + for (PyObject base : bases) { + if (!(base instanceof PyType)) { + continue; + } + Class<?> proxy = ((PyType)base).getProxyType(); + if (proxy == null) { + continue; + } + if (proxy.isInterface()) { + interfaces.add(proxy); + } else { + if (baseProxy != null) { + String msg = "no multiple inheritance for Java classes: %s and %s"; + throw Py.TypeError(String.format(msg, proxy.getName(), baseProxy.getName())); + } + baseProxy = proxy; + } + } + + return baseProxy; + } + + /** + * Setup the javaProxy for this type. + * + * @param baseProxyClass this type's base proxyClass + * @param interfaces a list of Java interfaces in bases + */ + private void setupProxy(Class<?> baseProxyClass, List<Class<?>> interfaces) { + if (baseProxyClass == null && interfaces.size() == 0) { + // javaProxy not applicable + return; + } + + String proxyName = name; + PyObject module = dict.__finditem__("__module__"); + if (module != null) { + proxyName = module.toString() + "$" + proxyName; + } + Class<?> proxyClass = MakeProxies.makeProxy(baseProxyClass, interfaces, name, proxyName, + dict); + javaProxy = proxyClass; + + PyType proxyType = PyType.fromClass(proxyClass); + List<PyObject> cleanedBases = Generic.list(); + boolean addedProxyType = false; + for (PyObject base : bases) { + if (!(base instanceof PyType)) { + cleanedBases.add(base); + continue; + } + Class<?> proxy = ((PyType)base).getProxyType(); + if (proxy == null) { + // non-proxy types go straight into our lookup + cleanedBases.add(base); + } else { + if (!(base instanceof PyJavaType)) { + // python subclasses of proxy types need to be added as a base so their + // version of methods will show up + cleanedBases.add(base); + } else if (!addedProxyType) { + // Only add a single Java type, since everything's going to go through the + // proxy type + cleanedBases.add(proxyType); + addedProxyType = true; + } + } + } + bases = cleanedBases.toArray(new PyObject[cleanedBases.size()]); + } + //XXX: needs __doc__ @ExposedGet(name = "__base__") public PyObject getBase() { @@ -828,20 +974,6 @@ return winner; } - private static void addSlot(PyType newtype, PyObject slotname) { - confirmIdentifier(slotname); - String slotstring = mangleName(newtype.name, slotname.toString()); - if (slotstring.equals("__dict__")) { - if (newtype.base.needs_userdict || newtype.needs_userdict) { - throw Py.TypeError("__dict__ slot disallowed: we already got one"); - } - newtype.needs_userdict = true; - } else if (newtype.dict.__finditem__(slotstring) == null) { - newtype.dict.__setitem__(slotstring, new PySlot(newtype, slotstring, - newtype.numSlots++)); - } - } - public boolean isSubType(PyType supertype) { if (mro != null) { for (PyObject base : mro) { @@ -1342,22 +1474,28 @@ //XXX: consider pulling this out into a generally accessible place // I bet this is duplicated more or less in other places. - private static void confirmIdentifier(PyObject o) { + private static String confirmIdentifier(PyObject obj) { + String identifier; + if (!(obj instanceof PyString)) { + throw Py.TypeError(String.format("__slots__ items must be strings, not '%.200s'", + obj.getType().fastGetName())); + } else if (obj instanceof PyUnicode) { + identifier = ((PyUnicode)obj).encode(); + } else { + identifier = obj.toString(); + } + String msg = "__slots__ must be identifiers"; - if (o == Py.None) { - throw Py.TypeError(msg); - } - String identifier = o.toString(); - if (identifier == null || identifier.length() < 1 + if (identifier.length() == 0 || (!Character.isLetter(identifier.charAt(0)) && identifier.charAt(0) != '_')) { throw Py.TypeError(msg); } - char[] chars = identifier.toCharArray(); - for (int i = 0; i < chars.length; i++) { - if (!Character.isLetterOrDigit(chars[i]) && chars[i] != '_') { + for (char c : identifier.toCharArray()) { + if (!Character.isLetterOrDigit(c) && c != '_') { throw Py.TypeError(msg); } } + return identifier; } //XXX: copied from CodeCompiler.java and changed variable names. Modified: trunk/jython/src/org/python/modules/_weakref/ReferenceType.java =================================================================== --- trunk/jython/src/org/python/modules/_weakref/ReferenceType.java 2009-04-10 03:14:42 UTC (rev 6197) +++ trunk/jython/src/org/python/modules/_weakref/ReferenceType.java 2009-04-10 03:32:30 UTC (rev 6198) @@ -35,7 +35,10 @@ GlobalRef gref = GlobalRef.newInstance(ob); if (new_.for_type == subtype) { - // XXX: Reject types that aren't weak referenceable + // NOTE: CPython disallows weakrefs to many builtin types (e.g. dict, list) + // and would check weakrefability here. We aren't as strict since the JVM can + // weakref anything. Our types' needs_weakref flag only refers to whether it + // has a __weakref__ descriptor, not weakrefability if (callback == null) { ReferenceType ret = (ReferenceType)gref.find(ReferenceType.class); if (ret != null) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2009-04-10 03:15:00
|
Revision: 6197 http://jython.svn.sourceforge.net/jython/?rev=6197&view=rev Author: zyasoft Date: 2009-04-10 03:14:42 +0000 (Fri, 10 Apr 2009) Log Message: ----------- Removed unnecessary import. Modified Paths: -------------- branches/newlist/Lib/test/test_javalist.py Modified: branches/newlist/Lib/test/test_javalist.py =================================================================== --- branches/newlist/Lib/test/test_javalist.py 2009-04-10 03:05:14 UTC (rev 6196) +++ branches/newlist/Lib/test/test_javalist.py 2009-04-10 03:14:42 UTC (rev 6197) @@ -1,4 +1,3 @@ -from test_support import * from javatests import ListTest class PyListTest(ListTest): This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2009-04-10 03:05:16
|
Revision: 6196 http://jython.svn.sourceforge.net/jython/?rev=6196&view=rev Author: fwierzbicki Date: 2009-04-10 03:05:14 +0000 (Fri, 10 Apr 2009) Log Message: ----------- properly handle setslice. Modified Paths: -------------- branches/newlist/src/org/python/core/PyList.java Modified: branches/newlist/src/org/python/core/PyList.java =================================================================== --- branches/newlist/src/org/python/core/PyList.java 2009-04-10 02:22:11 UTC (rev 6195) +++ branches/newlist/src/org/python/core/PyList.java 2009-04-10 03:05:14 UTC (rev 6196) @@ -155,14 +155,27 @@ } 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); + if(step == 1) { + List<PyObject> copy = new ArrayList<PyObject>(); + copy.addAll(this.list.subList(0, start)); + if (iter != null) { + while (iter.hasNext()) { + copy.add(iter.next()); + } } + copy.addAll(this.list.subList(stop, this.list.size())); + this.list.clear(); + this.list.addAll(copy); + } else { + 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); + } + } } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2009-04-10 02:22:12
|
Revision: 6195 http://jython.svn.sourceforge.net/jython/?rev=6195&view=rev Author: fwierzbicki Date: 2009-04-10 02:22:11 +0000 (Fri, 10 Apr 2009) Log Message: ----------- Throw exception on mutate during sort. Modified Paths: -------------- branches/newlist/src/org/python/core/PyComparator.java branches/newlist/src/org/python/core/PyList.java Modified: branches/newlist/src/org/python/core/PyComparator.java =================================================================== --- branches/newlist/src/org/python/core/PyComparator.java 2009-04-10 01:09:14 UTC (rev 6194) +++ branches/newlist/src/org/python/core/PyComparator.java 2009-04-10 02:22:11 UTC (rev 6195) @@ -4,11 +4,13 @@ public class PyComparator implements Comparator<PyObject> { - protected PyObject cmp = null; - protected PyObject key = null; + protected PyList list; + protected PyObject cmp; + protected PyObject key; protected boolean reverse = false; - PyComparator(PyObject cmp, PyObject key, boolean reverse) { + PyComparator(PyList list, PyObject cmp, PyObject key, boolean reverse) { + this.list = list; this.cmp = cmp; this.key = key; this.reverse = reverse; @@ -32,6 +34,9 @@ if (reverse) { return -result; } + if (this.list.gListAllocatedStatus >= 0) { + throw Py.ValueError("list modified during sort"); + } return result; } Modified: branches/newlist/src/org/python/core/PyList.java =================================================================== --- branches/newlist/src/org/python/core/PyList.java 2009-04-10 01:09:14 UTC (rev 6194) +++ branches/newlist/src/org/python/core/PyList.java 2009-04-10 02:22:11 UTC (rev 6195) @@ -680,12 +680,11 @@ sort(Py.None, Py.None, Py.False); } - public void sort(PyObject cmp, PyObject key, PyObject reverse) { - //System.out.println("sorting with " + cmp + ":" + key + ":" + reverse); - //MergeState ms = new MergeState(this, cmp, key, reverse.__nonzero__()); - //ms.sort(); - PyComparator c = new PyComparator(cmp, key, reverse.__nonzero__()); + public synchronized void sort(PyObject cmp, PyObject key, PyObject reverse) { + gListAllocatedStatus = -1; + PyComparator c = new PyComparator(this, cmp, key, reverse.__nonzero__()); Collections.sort(list, c); + gListAllocatedStatus = __len__(); } public int hashCode() { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2009-04-10 01:09:40
|
Revision: 6194 http://jython.svn.sourceforge.net/jython/?rev=6194&view=rev Author: pjenvey Date: 2009-04-10 01:09:14 +0000 (Fri, 10 Apr 2009) Log Message: ----------- remove 2.3 workarounds Modified Paths: -------------- trunk/jython/Lib/test/test_descr.py Modified: trunk/jython/Lib/test/test_descr.py =================================================================== --- trunk/jython/Lib/test/test_descr.py 2009-04-09 01:26:22 UTC (rev 6193) +++ trunk/jython/Lib/test/test_descr.py 2009-04-10 01:09:14 UTC (rev 6194) @@ -697,15 +697,13 @@ class _instance(object): pass class M2(object): - # XXX: Jython 2.3 - #@staticmethod + @staticmethod def __new__(cls, name, bases, dict): self = object.__new__(cls) self.name = name self.bases = bases self.dict = dict return self - __new__ = staticmethod(__new__) def __call__(self): it = _instance() # Early binding of methods @@ -2196,11 +2194,9 @@ aProp = property(lambda self: "foo") class Sub(Base): - # XXX: Jython 2.3 - #@classmethod + @classmethod def test(klass): return super(Sub,klass).aProp - test = classmethod(test) veris(Sub.test(), Base.aProp) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2009-04-09 01:26:43
|
Revision: 6193 http://jython.svn.sourceforge.net/jython/?rev=6193&view=rev Author: pjenvey Date: 2009-04-09 01:26:22 +0000 (Thu, 09 Apr 2009) Log Message: ----------- rearrange per coding standards Modified Paths: -------------- trunk/jython/src/org/python/core/PyObject.java Modified: trunk/jython/src/org/python/core/PyObject.java =================================================================== --- trunk/jython/src/org/python/core/PyObject.java 2009-04-08 21:45:35 UTC (rev 6192) +++ trunk/jython/src/org/python/core/PyObject.java 2009-04-09 01:26:22 UTC (rev 6193) @@ -7,6 +7,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; + import org.python.expose.ExposedDelete; import org.python.expose.ExposedGet; import org.python.expose.ExposedMethod; @@ -24,26 +25,62 @@ public static final PyType TYPE = PyType.fromClass(PyObject.class); - //XXX: in CPython object.__new__ has a doc string... - @ExposedNew - @ExposedMethod(doc = BuiltinDocs.object___init___doc) - final void object___init__(PyObject[] args, String[] keywords) { -// XXX: attempted fix for object(foo=1), etc -// XXX: this doesn't work for metaclasses, for some reason -// if (args.length > 0) { -// throw Py.TypeError("default __new__ takes no parameters"); -// } + /** The type of this object. */ + PyType objtype; + /** + * An underlying Java instance that this object is wrapping or is a subclass + * of. Anything attempting to use the proxy should go through {@link #getJavaProxy()} + * which ensures that it's initialized. + */ + protected Object javaProxy; + + /** Primitives classes their wrapper classes. */ + private static final Map<Class<?>, Class<?>> primitiveMap = Generic.map(); + + static { + primitiveMap.put(Character.TYPE, Character.class); + primitiveMap.put(Boolean.TYPE, Boolean.class); + primitiveMap.put(Byte.TYPE, Byte.class); + primitiveMap.put(Short.TYPE, Short.class); + primitiveMap.put(Integer.TYPE, Integer.class); + primitiveMap.put(Long.TYPE, Long.class); + primitiveMap.put(Float.TYPE, Float.class); + primitiveMap.put(Double.TYPE, Double.class); } + public PyObject(PyType objtype) { + this.objtype = objtype; + } + /** - * An underlying Java instance that this object is wrapping or is a subclass of. Anything - * attempting to use the proxy should go through {@link #getJavaProxy()} which ensures that it's - * initialized. + * The standard constructor for a <code>PyObject</code>. It will set the <code>objtype</code> + * field to correspond to the specific subclass of <code>PyObject</code> being instantiated. + **/ + public PyObject() { + objtype = PyType.fromClass(getClass()); + } + + /** + * Creates the PyObject for the base type. The argument only exists to make the constructor + * distinct. */ - protected Object javaProxy; + PyObject(boolean ignored) { + objtype = (PyType)this; + } - PyType objtype; + //XXX: in CPython object.__new__ has a doc string... + @ExposedNew + @ExposedMethod(doc = BuiltinDocs.object___init___doc) + final void object___init__(PyObject[] args, String[] keywords) { + // XXX: attempted fix for object(foo=1), etc + // XXX: this doesn't work for metaclasses, for some reason + /* + if (args.length > 0) { + throw Py.TypeError("default __new__ takes no parameters"); + } + */ + } @ExposedGet(name = "__class__") public PyType getType() { @@ -82,27 +119,7 @@ return Py.None; } - public PyObject(PyType objtype) { - this.objtype = objtype; - } - /** - * Creates the PyObject for the base type. The argument only exists to make the constructor - * distinct. - */ - PyObject(boolean ignored) { - objtype = (PyType)this; - } - - /** - * The standard constructor for a <code>PyObject</code>. It will set the <code>objtype</code> - * field to correspond to the specific subclass of <code>PyObject</code> being instantiated. - **/ - public PyObject() { - objtype = PyType.fromClass(getClass()); - } - - /** * Dispatch __init__ behavior */ public void dispatch__init__(PyType type, PyObject[] args, String[] keywords) {} @@ -288,18 +305,6 @@ return javaProxy; } - private static final Map<Class<?>, Class<?>> primitiveMap = Generic.map(); - static { - primitiveMap.put(Character.TYPE, Character.class); - primitiveMap.put(Boolean.TYPE, Boolean.class); - primitiveMap.put(Byte.TYPE, Byte.class); - primitiveMap.put(Short.TYPE, Short.class); - primitiveMap.put(Integer.TYPE, Integer.class); - primitiveMap.put(Long.TYPE, Long.class); - primitiveMap.put(Float.TYPE, Float.class); - primitiveMap.put(Double.TYPE, Double.class); - } - /** * The basic method to override when implementing a callable object. * This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |