From: <zy...@us...> - 2009-04-07 13:10:03
|
Revision: 6186 http://jython.svn.sourceforge.net/jython/?rev=6186&view=rev Author: zyasoft Date: 2009-04-07 13:09:48 +0000 (Tue, 07 Apr 2009) Log Message: ----------- Added testing of newlist (derived from test_userlist), and some of these tests now pass. Modified Paths: -------------- branches/newlist/CoreExposed.includes branches/newlist/src/org/python/core/PyNewList.java branches/newlist/src/templates/README.txt branches/newlist/src/templates/mappings Added Paths: ----------- branches/newlist/Lib/test/test_newlist.py Modified: branches/newlist/CoreExposed.includes =================================================================== --- branches/newlist/CoreExposed.includes 2009-04-07 05:48:59 UTC (rev 6185) +++ branches/newlist/CoreExposed.includes 2009-04-07 13:09:48 UTC (rev 6186) @@ -25,6 +25,7 @@ org/python/core/PyMethod.class org/python/core/PyMethodDescr.class org/python/core/PyModule.class +org/python/core/PyNewList.class org/python/core/PyNone.class org/python/core/PyObject.class org/python/core/PyProperty.class Added: branches/newlist/Lib/test/test_newlist.py =================================================================== --- branches/newlist/Lib/test/test_newlist.py (rev 0) +++ branches/newlist/Lib/test/test_newlist.py 2009-04-07 13:09:48 UTC (rev 6186) @@ -0,0 +1,60 @@ +# Check every path through every method of UserList + +from org.python.core import PyNewList as UserList +import unittest +from test import test_support, list_tests + +class UserListTest(list_tests.CommonTest): + type2test = UserList + + def test_getslice(self): + super(UserListTest, self).test_getslice() + l = [0, 1, 2, 3, 4] + u = self.type2test(l) + for i in range(-3, 6): + self.assertEqual(u[:i], l[:i]) + self.assertEqual(u[i:], l[i:]) + for j in xrange(-3, 6): + self.assertEqual(u[i:j], l[i:j]) + + def test_add_specials(self): + u = UserList("spam") + u2 = u + "eggs" + self.assertEqual(u2, list("spameggs")) + + def test_radd_specials(self): + u = UserList("eggs") + u2 = "spam" + u + self.assertEqual(u2, list("spameggs")) + u2 = u.__radd__(UserList("spam")) + self.assertEqual(u2, list("spameggs")) + + def test_iadd(self): + super(UserListTest, self).test_iadd() + u = [0, 1] + u += UserList([0, 1]) + self.assertEqual(u, [0, 1, 0, 1]) + + def test_mixedcmp(self): + u = self.type2test([0, 1]) + self.assertEqual(u, [0, 1]) + self.assertNotEqual(u, [0]) + self.assertNotEqual(u, [0, 2]) + + def test_mixedadd(self): + u = self.type2test([0, 1]) + self.assertEqual(u + [], u) + self.assertEqual(u + [2], [0, 1, 2]) + + def test_getitemoverwriteiter(self): + # Verify that __getitem__ overrides *are* recognized by __iter__ + class T(self.type2test): + def __getitem__(self, key): + return str(key) + '!!!' + self.assertEqual(iter(T((1,2))).next(), "0!!!") + +def test_main(): + test_support.run_unittest(UserListTest) + +if __name__ == "__main__": + test_main() Modified: branches/newlist/src/org/python/core/PyNewList.java =================================================================== --- branches/newlist/src/org/python/core/PyNewList.java 2009-04-07 05:48:59 UTC (rev 6185) +++ branches/newlist/src/org/python/core/PyNewList.java 2009-04-07 13:09:48 UTC (rev 6186) @@ -10,6 +10,7 @@ import org.python.util.Generic; import java.util.Collection; +import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.ListIterator; @@ -65,7 +66,7 @@ public PyNewList(PyObject o) { this(TYPE); for (PyObject item : o.asIterable()) { - append(item); + list.add(item); } } @@ -86,8 +87,8 @@ @ExposedNew @ExposedMethod(doc = BuiltinDocs.list___init___doc) - final void list___init__(PyObject[] args, String[] kwds) { - ArgParser ap = new ArgParser("list", args, kwds, new String[] {"sequence"}, 0); + final void newlist___init__(PyObject[] args, String[] kwds) { + ArgParser ap = new ArgParser("newlist", args, kwds, new String[] {"sequence"}, 0); PyObject seq = ap.getPyObject(0, null); clear(); if(seq == null) { @@ -104,11 +105,11 @@ @Override public int __len__() { - return list___len__(); + return newlist___len__(); } @ExposedMethod(doc = BuiltinDocs.list___len___doc) - final int list___len__() { + final int newlist___len__() { return size(); } @@ -127,12 +128,17 @@ if(stop < start) { stop = start; } - if ((value instanceof PySequence) || (!(value instanceof List))) { + if (value instanceof PySequenceList) { + setsliceIterator(start, stop, step, ((PySequenceList)value).listIterator()); + } + else if ((value instanceof PySequence) || (!(value instanceof List))) { + System.err.println("PySequence"); if (value == this) { // copy value = new PyNewList((PySequence)value); } setsliceIterator(start, stop, step, value.asIterable().iterator()); } else { + System.err.println("List"); List valueList = (List)value.__tojava__(List.class); if(valueList != null && valueList != Py.NoConversion) { setsliceList(start, stop, step, valueList); @@ -146,32 +152,28 @@ ((ArrayList) list).ensureCapacity(start + n); } ListIterator src = value.listIterator(); - if (step == 1) { - ListIterator<PyObject> dest = list.subList(start, stop).listIterator(); - while (dest.hasNext() && src.hasNext()) { - dest.set(Py.java2py(src.next())); - } - } else { - for (int j = start; j < stop && src.hasNext(); j += step) { - set(j, src.next()); - } + for (int j = start; src.hasNext(); j += step) { + set(j, src.next()); } } protected void setsliceIterator(int start, int stop, int step, Iterator<PyObject> iter) { int n = sliceLength(start, stop, step); + int size = list.size(); + int delta = start > size ? n : start - size + n; if (list instanceof ArrayList) { - ((ArrayList) list).ensureCapacity(start + n); + ((ArrayList) list).ensureCapacity(size + delta); } - if (step == 1) { - ListIterator<PyObject> dest = list.subList(start, stop).listIterator(); - while (dest.hasNext() && iter.hasNext()) { - dest.set(iter.next()); + System.err.println("setsliceIterator: start=" + start + ",stop=" + stop + ",step=" + step + ",n=" + n + ",delta=" + delta); + + for (int j = start; iter.hasNext(); j += step) { + System.err.print(this); + if (j > size) { + list.add(iter.next()); + } else { + list.set(j, iter.next()); } - } else { - for (int i = 0, j = start; i < n && iter.hasNext(); i++, j += step) { - set(j, iter.next()); - } + System.err.println("-> " + this); } } @@ -195,42 +197,42 @@ } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___ne___doc) - final PyObject list___ne__(PyObject o) { + final PyObject newlist___ne__(PyObject o) { return seq___ne__(o); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___eq___doc) - final PyObject list___eq__(PyObject o) { + final PyObject newlist___eq__(PyObject o) { return seq___eq__(o); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___lt___doc) - final PyObject list___lt__(PyObject o) { + final PyObject newlist___lt__(PyObject o) { return seq___lt__(o); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___le___doc) - final PyObject list___le__(PyObject o) { + final PyObject newlist___le__(PyObject o) { return seq___le__(o); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___gt___doc) - final PyObject list___gt__(PyObject o) { + final PyObject newlist___gt__(PyObject o) { return seq___gt__(o); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___ge___doc) - final PyObject list___ge__(PyObject o) { + final PyObject newlist___ge__(PyObject o) { return seq___ge__(o); } @Override public PyObject __imul__(PyObject o) { - return list___imul__(o); + return newlist___imul__(o); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___imul___doc) - final PyObject list___imul__(PyObject o) { + final PyObject newlist___imul__(PyObject o) { if (!o.isIndex()) { return null; } @@ -264,11 +266,11 @@ @Override public PyObject __mul__(PyObject o) { - return list___mul__(o); + return newlist___mul__(o); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___mul___doc) - final PyObject list___mul__(PyObject o) { + final PyObject newlist___mul__(PyObject o) { if (!o.isIndex()) { return null; } @@ -277,11 +279,11 @@ @Override public PyObject __rmul__(PyObject o) { - return list___rmul__(o); + return newlist___rmul__(o); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___rmul___doc) - final PyObject list___rmul__(PyObject o) { + final PyObject newlist___rmul__(PyObject o) { if (!o.isIndex()) { return null; } @@ -290,11 +292,11 @@ @Override public PyObject __add__(PyObject o) { - return list___add__(o); + return newlist___add__(o); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___add___doc) - final PyObject list___add__(PyObject o) { + final PyObject newlist___add__(PyObject o) { PyNewList sum = null; if (o instanceof PySequenceList && !(o instanceof PyTuple)) { if (o instanceof PyNewList) { @@ -310,7 +312,7 @@ if(oList != Py.NoConversion && oList != null) { List otherList = (List) oList; sum = new PyNewList(); - sum.list_extend(this); + sum.newlist_extend(this); for(Iterator i = otherList.iterator(); i.hasNext();) { sum.add(i.next()); } @@ -321,12 +323,12 @@ @Override public PyObject __radd__(PyObject o) { - return list___radd__(o); + return newlist___radd__(o); } //XXX: needs __doc__ @ExposedMethod(type = MethodType.BINARY) - final PyObject list___radd__(PyObject o) { + final PyObject newlist___radd__(PyObject o) { // Support adding java.util.List, but prevent adding PyTuple. // 'o' should never be a PyNewList since __add__ is defined. PyNewList sum = null; @@ -343,22 +345,22 @@ } @ExposedMethod(doc = BuiltinDocs.list___contains___doc) - final boolean list___contains__(PyObject o) { + final boolean newlist___contains__(PyObject o) { return object___contains__(o); } @ExposedMethod(doc = BuiltinDocs.list___delitem___doc) - final void list___delitem__(PyObject index) { + final void newlist___delitem__(PyObject index) { seq___delitem__(index); } @ExposedMethod(doc = BuiltinDocs.list___setitem___doc) - final void list___setitem__(PyObject o, PyObject def) { + final void newlist___setitem__(PyObject o, PyObject def) { seq___setitem__(o, def); } @ExposedMethod(doc = BuiltinDocs.list___getitem___doc) - final PyObject list___getitem__(PyObject o) { + final PyObject newlist___getitem__(PyObject o) { PyObject ret = seq___finditem__(o); if(ret == null) { throw Py.IndexError("index out of range: " + o); @@ -368,26 +370,26 @@ @Override public PyObject __iter__() { - return list___iter__(); + return newlist___iter__(); } @ExposedMethod(doc = BuiltinDocs.list___iter___doc) - public PyObject list___iter__() { + public PyObject newlist___iter__() { return new PyFastSequenceIter(this); } @ExposedMethod(defaults = "null", doc = BuiltinDocs.list___getslice___doc) - final PyObject list___getslice__(PyObject start, PyObject stop, PyObject step) { + final PyObject newlist___getslice__(PyObject start, PyObject stop, PyObject step) { return seq___getslice__(start, stop, step); } @ExposedMethod(defaults = "null", doc = BuiltinDocs.list___setslice___doc) - final void list___setslice__(PyObject start, PyObject stop, PyObject step, PyObject value) { + final void newlist___setslice__(PyObject start, PyObject stop, PyObject step, PyObject value) { seq___setslice__(start, stop, step, value); } @ExposedMethod(defaults = "null", doc = BuiltinDocs.list___delslice___doc) - final void list___delslice__(PyObject start, PyObject stop, PyObject step) { + final void newlist___delslice__(PyObject start, PyObject stop, PyObject step) { seq___delslice__(start, stop, step); } @@ -400,12 +402,12 @@ } public String toString() { - return list_toString(); + return newlist_toString(); } //XXX: needs __doc__ @ExposedMethod(names = "__repr__") - final String list_toString() { + final String newlist_toString() { ThreadState ts = Py.getThreadState(); if(!ts.enterRepr(this)) { return "[...]"; @@ -432,11 +434,11 @@ * the element to add. */ public void append(PyObject o) { - list_append(o); + newlist_append(o); } @ExposedMethod(doc = BuiltinDocs.list_append_doc) - final void list_append(PyObject o) { + final void newlist_append(PyObject o) { pyadd(o); gListAllocatedStatus = __len__(); } @@ -448,11 +450,11 @@ * the argument to test for. Testing is done with the <code>==</code> operator. */ public int count(PyObject o) { - return list_count(o); + return newlist_count(o); } @ExposedMethod(doc = BuiltinDocs.list_count_doc) - final int list_count(PyObject o) { + final int newlist_count(PyObject o) { int count = 0; PyObject[] array = getArray(); for(int i = 0, n = size(); i < n; i++) { @@ -474,29 +476,29 @@ } public int index(PyObject o, int start) { - return list_index(o, start, size()); + return newlist_index(o, start, size()); } public int index(PyObject o, int start, int stop) { - return list_index(o, start, stop); + return newlist_index(o, start, stop); } @ExposedMethod(defaults = {"null", "null"}, doc = BuiltinDocs.list_index_doc) - final int list_index(PyObject o, PyObject start, PyObject stop) { + final int newlist_index(PyObject o, PyObject start, PyObject stop) { int startInt = start == null ? 0 : PySlice.calculateSliceIndex(start); int stopInt = stop == null ? size() : PySlice.calculateSliceIndex(stop); - return list_index(o, startInt, stopInt); + return newlist_index(o, startInt, stopInt); } - final int list_index(PyObject o, int start, int stop) { + final int newlist_index(PyObject o, int start, int stop) { return _index(o, "list.index(x): x not in list", start, stop); } - final int list_index(PyObject o, int start) { + final int newlist_index(PyObject o, int start) { return _index(o, "list.index(x): x not in list", start, size()); } - final int list_index(PyObject o) { + final int newlist_index(PyObject o) { return _index(o, "list.index(x): x not in list", 0, size()); } @@ -523,11 +525,11 @@ * the element to insert. */ public void insert(int index, PyObject o) { - list_insert(index, o); + newlist_insert(index, o); } @ExposedMethod(doc = BuiltinDocs.list_insert_doc) - final void list_insert(int index, PyObject o) { + final void newlist_insert(int index, PyObject o) { if(index < 0) { index = Math.max(0, size() + index); } @@ -547,11 +549,11 @@ * the element to search for and remove. */ public void remove(PyObject o) { - list_remove(o); + newlist_remove(o); } @ExposedMethod(doc = BuiltinDocs.list_remove_doc) - final void list_remove(PyObject o) { + final void newlist_remove(PyObject o) { del(_index(o, "list.remove(x): x not in list", 0, size())); gListAllocatedStatus = __len__(); } @@ -562,20 +564,12 @@ * this side effect. */ public void reverse() { - list_reverse(); + newlist_reverse(); } @ExposedMethod(doc = BuiltinDocs.list_reverse_doc) - final void list_reverse() { - PyObject tmp; - int n = size(); - PyObject[] array = getArray(); - int j = n - 1; - for(int i = 0; i < n / 2; i++, j--) { - tmp = array[i]; - array[i] = array[j]; - array[j] = tmp; - } + final void newlist_reverse() { + Collections.reverse(list); gListAllocatedStatus = __len__(); } @@ -593,11 +587,11 @@ * the index of the element to remove and return. */ public PyObject pop(int n) { - return list_pop(n); + return newlist_pop(n); } @ExposedMethod(defaults = "-1", doc = BuiltinDocs.list_pop_doc) - final PyObject list_pop(int n) { + final PyObject newlist_pop(int n) { int length = size(); if(length == 0) { throw Py.IndexError("pop from empty list"); @@ -608,8 +602,7 @@ if(n < 0 || n >= length) { throw Py.IndexError("pop index out of range"); } - PyObject v = pyget(n); - setslice(n, n + 1, 1, Py.EmptyTuple); + PyObject v = list.remove(n); return v; } @@ -621,23 +614,33 @@ * the sequence of items to append to the list. */ public void extend(PyObject o) { - list_extend(o); + newlist_extend(o); } @ExposedMethod(doc = BuiltinDocs.list_extend_doc) - final void list_extend(PyObject o) { - int length = size(); - setslice(length, length, 1, o); + final void newlist_extend(PyObject o) { + if (o instanceof PyNewList) { + list.addAll(((PyNewList)o).list); + } else if (o instanceof PySequenceObjectList) { + PyObject other[] = ((PySequenceObjectList)o).getArray(); + for (int i = 0; i < other.length; i++) { + list.add(other[i]); + } + } else { + for (PyObject item : o.asIterable()) { + list.add(item); + } + } gListAllocatedStatus = __len__(); } @Override public PyObject __iadd__(PyObject o) { - return list___iadd__(o); + return newlist___iadd__(o); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___iadd___doc) - final PyObject list___iadd__(PyObject o) { + final PyObject newlist___iadd__(PyObject o) { PyType oType = o.getType(); if (oType == TYPE || oType == PyTuple.TYPE || this == o) { extend(fastSequence(o, "argument must be iterable")); @@ -676,7 +679,7 @@ @ExposedMethod(doc = BuiltinDocs.list_sort_doc) - final void list_sort(PyObject[] args, String[] kwds) { + final void newlist_sort(PyObject[] args, String[] kwds) { ArgParser ap = new ArgParser("list", args, kwds, new String[]{"cmp", "key", "reverse"}, 0); PyObject cmp = ap.getPyObject(0, Py.None); PyObject key = ap.getPyObject(1, Py.None); @@ -698,11 +701,11 @@ } public int hashCode() { - return list___hash__(); + return newlist___hash__(); } @ExposedMethod(doc = BuiltinDocs.list___hash___doc) - final int list___hash__() { + final int newlist___hash__() { throw Py.TypeError(String.format("unhashable type: '%.200s'", getType().fastGetName())); } @@ -745,7 +748,8 @@ add(element); } } - return c.size() > 0; } + return c.size() > 0; + } @Override public void clear() { Modified: branches/newlist/src/templates/README.txt =================================================================== --- branches/newlist/src/templates/README.txt 2009-04-07 05:48:59 UTC (rev 6185) +++ branches/newlist/src/templates/README.txt 2009-04-07 13:09:48 UTC (rev 6186) @@ -1,17 +1,24 @@ -Some classes have generated code to enable their usage within Jython. Each -such file will have a generated section that is created with the gexpose.py -script. For the PyInteger class it is created thus: +Derived classes (classes that allow for user extension) are created as follows: - python gexpose.py int.expose ../../jython/src/org/python/core/PyInteger.java +1. Create a template file xxx.derived +2. Modify mappings, which associates a template with a specific class in + the source tree to be generated +3. Run (with CPython) gderived.py against the the template file -For each class there is an xxx.expose file describing what should be exposed. +Example: creating a derivable version of int -In addition there is an xxxDerived.java class that is completely generated -with the script gderived.py. For the PyInteger class it is created thus: +from the file int.derived: - python gderived.py int.derived >../../jython/src/org/python/core/PyIntegerDerived.java + base_class: PyInteger + want_dict: true + ctr: int v + incl: object -There is an ant target to generate these automatically. See the template -target in the top-level build file, or the org.python.util.TemplateAntTask -ant task. In the future, the template generation will be linked into the -main build targets. +from mappings, the relevant entry (please keep sorted): + + int.derived:org.python.core.PyIntegerDerived + +To generate the source of the class, src/org/python/core/PyInteger.java: + + python gderived.py int.derived + Modified: branches/newlist/src/templates/mappings =================================================================== --- branches/newlist/src/templates/mappings 2009-04-07 05:48:59 UTC (rev 6185) +++ branches/newlist/src/templates/mappings 2009-04-07 13:09:48 UTC (rev 6186) @@ -25,6 +25,7 @@ long.derived:org.python.core.PyLongDerived local.derived:org.python.modules.thread.PyLocalDerived module.derived:org.python.core.PyModuleDerived +newlist.derived:org.python.core.PyNewListDerived object.derived:org.python.core.PyObjectDerived partial.derived:org.python.modules._functools.PyPartialDerived property.derived:org.python.core.PyPropertyDerived This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2009-04-08 06:48:27
|
Revision: 6190 http://jython.svn.sourceforge.net/jython/?rev=6190&view=rev Author: zyasoft Date: 2009-04-08 06:48:21 +0000 (Wed, 08 Apr 2009) Log Message: ----------- Fixed setslice/getslice problems. Modified Paths: -------------- branches/newlist/Lib/test/test_newlist.py branches/newlist/src/org/python/core/PyNewList.java Modified: branches/newlist/Lib/test/test_newlist.py =================================================================== --- branches/newlist/Lib/test/test_newlist.py 2009-04-08 01:27:07 UTC (rev 6189) +++ branches/newlist/Lib/test/test_newlist.py 2009-04-08 06:48:21 UTC (rev 6190) @@ -1,60 +1,14 @@ # Check every path through every method of UserList -from org.python.core import PyNewList as UserList +from org.python.core import PyNewList as newlist import unittest from test import test_support, list_tests -class UserListTest(list_tests.CommonTest): - type2test = UserList +class NewListTest(list_tests.CommonTest): + type2test = newlist - def test_getslice(self): - super(UserListTest, self).test_getslice() - l = [0, 1, 2, 3, 4] - u = self.type2test(l) - for i in range(-3, 6): - self.assertEqual(u[:i], l[:i]) - self.assertEqual(u[i:], l[i:]) - for j in xrange(-3, 6): - self.assertEqual(u[i:j], l[i:j]) - - def test_add_specials(self): - u = UserList("spam") - u2 = u + "eggs" - self.assertEqual(u2, list("spameggs")) - - def test_radd_specials(self): - u = UserList("eggs") - u2 = "spam" + u - self.assertEqual(u2, list("spameggs")) - u2 = u.__radd__(UserList("spam")) - self.assertEqual(u2, list("spameggs")) - - def test_iadd(self): - super(UserListTest, self).test_iadd() - u = [0, 1] - u += UserList([0, 1]) - self.assertEqual(u, [0, 1, 0, 1]) - - def test_mixedcmp(self): - u = self.type2test([0, 1]) - self.assertEqual(u, [0, 1]) - self.assertNotEqual(u, [0]) - self.assertNotEqual(u, [0, 2]) - - def test_mixedadd(self): - u = self.type2test([0, 1]) - self.assertEqual(u + [], u) - self.assertEqual(u + [2], [0, 1, 2]) - - def test_getitemoverwriteiter(self): - # Verify that __getitem__ overrides *are* recognized by __iter__ - class T(self.type2test): - def __getitem__(self, key): - return str(key) + '!!!' - self.assertEqual(iter(T((1,2))).next(), "0!!!") - def test_main(): - test_support.run_unittest(UserListTest) + test_support.run_unittest(NewListTest) if __name__ == "__main__": test_main() Modified: branches/newlist/src/org/python/core/PyNewList.java =================================================================== --- branches/newlist/src/org/python/core/PyNewList.java 2009-04-08 01:27:07 UTC (rev 6189) +++ branches/newlist/src/org/python/core/PyNewList.java 2009-04-08 06:48:21 UTC (rev 6190) @@ -16,7 +16,7 @@ import java.util.ListIterator; @ExposedType(name = "newlist", base = PyObject.class) -public class PyNewList extends PySequenceList implements List{ +public class PyNewList extends PySequenceList implements List { public static final PyType TYPE = PyType.fromClass(PyNewList.class); protected final List<PyObject> list; @@ -75,12 +75,13 @@ } private static List<PyObject> listify(Iterator<PyObject> iter) { - List<PyObject> list = Generic.list(); - while (iter.hasNext()) { + List<PyObject> list = Generic.list(); + while (iter.hasNext()) { list.add(iter.next()); - } - return list; + } + return list; } + public PyNewList(Iterator<PyObject> iter) { this(TYPE, listify(iter)); } @@ -88,14 +89,14 @@ @ExposedNew @ExposedMethod(doc = BuiltinDocs.list___init___doc) final void newlist___init__(PyObject[] args, String[] kwds) { - ArgParser ap = new ArgParser("newlist", args, kwds, new String[] {"sequence"}, 0); + ArgParser ap = new ArgParser("newlist", args, kwds, new String[]{"sequence"}, 0); PyObject seq = ap.getPyObject(0, null); clear(); - if(seq == null) { + if (seq == null) { return; } - if(seq instanceof PyNewList) { - list.addAll((PyNewList)seq); // don't convert + if (seq instanceof PyNewList) { + list.addAll((PyNewList) seq); // don't convert } else { for (PyObject item : seq.asIterable()) { append(item); @@ -125,22 +126,18 @@ @Override protected void setslice(int start, int stop, int step, PyObject value) { - if(stop < start) { + if (stop < start) { stop = start; } - if (value instanceof PySequenceList) { - setsliceIterator(start, stop, step, ((PySequenceList)value).listIterator()); - } - else if ((value instanceof PySequence) || (!(value instanceof List))) { - System.err.println("PySequence"); + if ((value instanceof PySequence) || (!(value instanceof List))) { if (value == this) { // copy - value = new PyNewList((PySequence)value); + value = new PyNewList((PySequence) value); } setsliceIterator(start, stop, step, value.asIterable().iterator()); } else { System.err.println("List"); - List valueList = (List)value.__tojava__(List.class); - if(valueList != null && valueList != Py.NoConversion) { + List valueList = (List) value.__tojava__(List.class); + if (valueList != null && valueList != Py.NoConversion) { setsliceList(start, stop, step, valueList); } } @@ -158,26 +155,17 @@ } protected void setsliceIterator(int start, int stop, int step, Iterator<PyObject> iter) { - int n = sliceLength(start, stop, step); int size = list.size(); - int delta = start > size ? n : start - size + n; - if (list instanceof ArrayList) { - ((ArrayList) list).ensureCapacity(size + delta); - } - System.err.println("setsliceIterator: start=" + start + ",stop=" + stop + ",step=" + step + ",n=" + n + ",delta=" + delta); - for (int j = start; iter.hasNext(); j += step) { - System.err.print(this); - if (j > size) { - list.add(iter.next()); + PyObject item = iter.next(); + if (j >= size) { + list.add(item); } else { - list.set(j, iter.next()); + list.set(j, item); } - System.err.println("-> " + this); } } - @Override protected PyObject repeat(int count) { if (count < 0) { @@ -254,7 +242,7 @@ int newSize = size * count; if (list instanceof ArrayList) { - ((ArrayList)list).ensureCapacity(newSize); + ((ArrayList) list).ensureCapacity(newSize); } List oldList = new ArrayList<PyObject>(list); for (int i = 1; i < count; i++) { @@ -300,20 +288,20 @@ PyNewList sum = null; if (o instanceof PySequenceList && !(o instanceof PyTuple)) { if (o instanceof PyNewList) { - List oList = ((PyNewList)o).list; + List oList = ((PyNewList) o).list; List newList = new ArrayList(list.size() + oList.size()); newList.addAll(list); newList.addAll(oList); sum = fromList(newList); } - } else if(!(o instanceof PySequenceList)) { + } else if (!(o instanceof PySequenceList)) { // also support adding java lists (but not PyTuple!) Object oList = o.__tojava__(List.class); - if(oList != Py.NoConversion && oList != null) { + if (oList != Py.NoConversion && oList != null) { List otherList = (List) oList; sum = new PyNewList(); sum.newlist_extend(this); - for(Iterator i = otherList.iterator(); i.hasNext();) { + for (Iterator i = otherList.iterator(); i.hasNext();) { sum.add(i.next()); } } @@ -332,7 +320,7 @@ // Support adding java.util.List, but prevent adding PyTuple. // 'o' should never be a PyNewList since __add__ is defined. PyNewList sum = null; - if(o instanceof PySequence) { + if (o instanceof PySequence) { return null; } Object oList = o.__tojava__(List.class); @@ -362,7 +350,7 @@ @ExposedMethod(doc = BuiltinDocs.list___getitem___doc) final PyObject newlist___getitem__(PyObject o) { PyObject ret = seq___finditem__(o); - if(ret == null) { + if (ret == null) { throw Py.IndexError("index out of range: " + o); } return ret; @@ -395,7 +383,7 @@ @Override protected String unsupportedopMessage(String op, PyObject o2) { - if(op.equals("+")) { + if (op.equals("+")) { return "can only concatenate list (not \"{2}\") to list"; } return super.unsupportedopMessage(op, o2); @@ -409,19 +397,19 @@ @ExposedMethod(names = "__repr__") final String newlist_toString() { ThreadState ts = Py.getThreadState(); - if(!ts.enterRepr(this)) { + if (!ts.enterRepr(this)) { return "[...]"; } StringBuilder buf = new StringBuilder("["); int length = size(); - PyObject[] array = getArray(); - for(int i = 0; i < length - 1; i++) { - buf.append((array[i]).__repr__().toString()); - buf.append(", "); + int i = 0; + for (PyObject item : list) { + buf.append(item.__repr__().toString()); + if (i < length - 1) { + buf.append(", "); + } + i++; } - if(length > 0) { - buf.append((array[length - 1]).__repr__().toString()); - } buf.append("]"); ts.exitRepr(this); return buf.toString(); @@ -457,8 +445,8 @@ final int newlist_count(PyObject o) { int count = 0; PyObject[] array = getArray(); - for(int i = 0, n = size(); i < n; i++) { - if(array[i].equals(o)) { + for (int i = 0, n = size(); i < n; i++) { + if (array[i].equals(o)) { count++; } } @@ -507,8 +495,8 @@ int validStop = boundToSequence(stop); int validStart = boundToSequence(start); PyObject[] array = getArray(); - for(int i = validStart; i < validStop && i < size(); i++) { - if(array[i].equals(o)) { + for (int i = validStart; i < validStop && i < size(); i++) { + if (array[i].equals(o)) { return i; } } @@ -530,10 +518,10 @@ @ExposedMethod(doc = BuiltinDocs.list_insert_doc) final void newlist_insert(int index, PyObject o) { - if(index < 0) { + if (index < 0) { index = Math.max(0, size() + index); } - if(index > size()) { + if (index > size()) { index = size(); } pyadd(index, o); @@ -593,13 +581,13 @@ @ExposedMethod(defaults = "-1", doc = BuiltinDocs.list_pop_doc) final PyObject newlist_pop(int n) { int length = size(); - if(length == 0) { + if (length == 0) { throw Py.IndexError("pop from empty list"); } - if(n < 0) { + if (n < 0) { n += length; } - if(n < 0 || n >= length) { + if (n < 0 || n >= length) { throw Py.IndexError("pop index out of range"); } PyObject v = list.remove(n); @@ -620,9 +608,9 @@ @ExposedMethod(doc = BuiltinDocs.list_extend_doc) final void newlist_extend(PyObject o) { if (o instanceof PyNewList) { - list.addAll(((PyNewList)o).list); + list.addAll(((PyNewList) o).list); } else if (o instanceof PySequenceObjectList) { - PyObject other[] = ((PySequenceObjectList)o).getArray(); + PyObject other[] = ((PySequenceObjectList) o).getArray(); for (int i = 0; i < other.length; i++) { list.add(other[i]); } @@ -671,13 +659,10 @@ * @param compare * the comparison function. */ - - /** + /** * Sort the items of the list in place. Items is compared with the normal relative comparison * operators. */ - - @ExposedMethod(doc = BuiltinDocs.list_sort_doc) final void newlist_sort(PyObject[] args, String[] kwds) { ArgParser ap = new ArgParser("list", args, kwds, new String[]{"cmp", "key", "reverse"}, 0); @@ -696,7 +681,7 @@ } public void sort(PyObject cmp, PyObject key, PyObject reverse) { - MergeState ms = new MergeState(new PyList((Collection)this), cmp, key, reverse.__nonzero__()); + MergeState ms = new MergeState(new PyList((Collection) this), cmp, key, reverse.__nonzero__()); ms.sort(); } @@ -713,7 +698,7 @@ public PyTuple __getnewargs__() { return new PyTuple(new PyTuple(getArray())); } - + @Override public void add(int index, Object element) { pyadd(index, Py.java2py(element)); @@ -765,8 +750,7 @@ public boolean containsAll(Collection c) { if (c instanceof PySequenceList) { return list.containsAll(c); - } - else { + } else { return list.containsAll(new PyList(c)); } } @@ -774,7 +758,7 @@ @Override public boolean equals(Object o) { if (o instanceof PyNewList) { - return (((PyNewList)o).list.equals(list)); + return (((PyNewList) o).list.equals(list)); } return false; } @@ -783,7 +767,7 @@ public Object get(int index) { return list.get(index).__tojava__(Object.class); } - + /** @deprecated */ @Override public PyObject[] getArray() { @@ -814,7 +798,7 @@ @Override public ListIterator listIterator() { return list.listIterator(); -} + } @Override public ListIterator listIterator(int index) { @@ -855,8 +839,7 @@ public boolean removeAll(Collection c) { if (c instanceof PySequenceList) { return list.removeAll(c); - } - else { + } else { return list.removeAll(new PyNewList(c)); } } @@ -865,8 +848,7 @@ public boolean retainAll(Collection c) { if (c instanceof PySequenceList) { return list.retainAll(c); - } - else { + } else { return list.retainAll(new PyNewList(c)); } } @@ -897,20 +879,17 @@ } protected PyObject getslice(int start, int stop, int step) { - if(step > 0 && stop < start) { + if (step > 0 && stop < start) { stop = start; } int n = sliceLength(start, stop, step); List newList; - if(step == 1) { + if (step == 1) { newList = new ArrayList<PyObject>(list.subList(start, stop)); - } - else { + } else { newList = new ArrayList<PyObject>(n); - int j = 0; - for(int i = start; j < n; i += step) { - newList.set(j, list.get(i)); - j++; + for (int i = start, j = 0; j < n; i += step, j++) { + newList.add(list.get(i)); } } return fromList(newList); @@ -920,5 +899,4 @@ public boolean remove(Object o) { return list.remove(Py.java2py(o)); } - } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <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: <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: <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-16 03:11:58
|
Revision: 6229 http://jython.svn.sourceforge.net/jython/?rev=6229&view=rev Author: zyasoft Date: 2009-04-16 03:11:53 +0000 (Thu, 16 Apr 2009) Log Message: ----------- Merged revisions 6208-6214,6218-6221,6224-6226 via svnmerge from https://jython.svn.sourceforge.net/svnroot/jython/trunk/jython ........ r6208 | amak | 2009-04-10 09:36:58 -0600 (Fri, 10 Apr 2009) | 2 lines 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 ........ r6209 | pjenvey | 2009-04-10 13:21:01 -0600 (Fri, 10 Apr 2009) | 5 lines 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 ........ r6210 | pjenvey | 2009-04-10 15:12:28 -0600 (Fri, 10 Apr 2009) | 3 lines ClassType shouldn't go through object's lookup machinery at all. not even __class__ or __call__ should show up in its lookup ........ r6211 | pjenvey | 2009-04-10 17:09:34 -0600 (Fri, 10 Apr 2009) | 1 line feeble attempt of debugging why these fail on hudson's all job ........ r6212 | pjenvey | 2009-04-10 18:24:56 -0600 (Fri, 10 Apr 2009) | 3 lines from: http://svn.python.org/projects/python/branches/release25-maint/Lib/test/test_cmd_line.py@54387 ........ r6213 | pjenvey | 2009-04-10 18:25:21 -0600 (Fri, 10 Apr 2009) | 3 lines o reapply test_cmd_line workaround o fix error handling of bad -c/-m command lines ........ r6214 | pjenvey | 2009-04-10 19:11:10 -0600 (Fri, 10 Apr 2009) | 1 line fix test_sax when running under a path name with spaces ........ r6218 | amak | 2009-04-11 12:26:24 -0600 (Sat, 11 Apr 2009) | 1 line Adding a readme.txt so that the WEB-INF/lib directory doesn't end getting omitted up by the installer. ........ r6219 | pjenvey | 2009-04-11 14:49:13 -0600 (Sat, 11 Apr 2009) | 1 line cleanup ........ r6220 | pjenvey | 2009-04-11 15:27:36 -0600 (Sat, 11 Apr 2009) | 1 line encode unicode printed to file objects per the file's encoding ........ r6221 | pjenvey | 2009-04-11 18:21:44 -0600 (Sat, 11 Apr 2009) | 1 line restore the original encoding of SyntaxError text ........ r6224 | fwierzbicki | 2009-04-14 15:16:03 -0600 (Tue, 14 Apr 2009) | 6 lines Applied patch from http://bugs.jython.org/issue1271: Bean property accessors in derived class overide methods in base class. I altered his tests so that they are easily runnable by "ant javatest". Thanks to Geoffrey French for the fix. Also fixed a bad comment in IdentityTest.java. ........ r6225 | fwierzbicki | 2009-04-14 16:50:45 -0600 (Tue, 14 Apr 2009) | 3 lines Policy file that disallows writing to the filesystem to test GAE security patch. ........ r6226 | fwierzbicki | 2009-04-14 17:00:40 -0600 (Tue, 14 Apr 2009) | 2 lines Lame shell script to test jython with policy that disallows file writes. ........ Modified Paths: -------------- branches/newlist/Lib/os.py branches/newlist/Lib/select.py branches/newlist/Lib/test/test_class_jy.py branches/newlist/Lib/test/test_cmd_line.py branches/newlist/Lib/test/test_sax.py branches/newlist/NEWS branches/newlist/src/org/python/compiler/Module.java branches/newlist/src/org/python/core/ParserFacade.java branches/newlist/src/org/python/core/Py.java branches/newlist/src/org/python/core/PyClass.java branches/newlist/src/org/python/core/PyFrame.java branches/newlist/src/org/python/core/PyJavaType.java branches/newlist/src/org/python/core/PyType.java branches/newlist/src/org/python/core/StdoutWrapper.java branches/newlist/src/org/python/core/imp.java branches/newlist/src/org/python/util/jython.java branches/newlist/src/shell/jython branches/newlist/tests/java/org/python/tests/identity/IdentityTest.java branches/newlist/tests/java/org/python/tests/props/BeanPropertyTest.java Added Paths: ----------- branches/newlist/Demo/modjy_webapp/WEB-INF/lib/readme.txt branches/newlist/tests/policy/ branches/newlist/tests/policy/nowrite.policy branches/newlist/tests/policy/run.sh branches/newlist/tests/python/prop_test.py Removed Paths: ------------- branches/newlist/tests/policy/nowrite.policy branches/newlist/tests/policy/run.sh Property Changed: ---------------- branches/newlist/ Property changes on: branches/newlist ___________________________________________________________________ Modified: svnmerge-integrated - /branches/modjy:1-6074 /branches/pbcvm:1-6045 /trunk/jython:1-6206 + /branches/modjy:1-6074 /branches/pbcvm:1-6045 /trunk/jython:1-6228 Copied: branches/newlist/Demo/modjy_webapp/WEB-INF/lib/readme.txt (from rev 6226, trunk/jython/Demo/modjy_webapp/WEB-INF/lib/readme.txt) =================================================================== --- branches/newlist/Demo/modjy_webapp/WEB-INF/lib/readme.txt (rev 0) +++ branches/newlist/Demo/modjy_webapp/WEB-INF/lib/readme.txt 2009-04-16 03:11:53 UTC (rev 6229) @@ -0,0 +1 @@ +The jython.jar file should be placed in this directory. Modified: branches/newlist/Lib/os.py =================================================================== --- branches/newlist/Lib/os.py 2009-04-15 06:18:52 UTC (rev 6228) +++ branches/newlist/Lib/os.py 2009-04-16 03:11:53 UTC (rev 6229) @@ -1080,7 +1080,6 @@ return _posix.isatty(fileno) if not isinstance(fileno, IOBase): - print fileno raise TypeError('a file descriptor is required') return fileno.isatty() Modified: branches/newlist/Lib/select.py =================================================================== --- branches/newlist/Lib/select.py 2009-04-15 06:18:52 UTC (rev 6228) +++ branches/newlist/Lib/select.py 2009-04-16 03:11:53 UTC (rev 6229) @@ -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 Modified: branches/newlist/Lib/test/test_class_jy.py =================================================================== --- branches/newlist/Lib/test/test_class_jy.py 2009-04-15 06:18:52 UTC (rev 6228) +++ branches/newlist/Lib/test/test_class_jy.py 2009-04-16 03:11:53 UTC (rev 6229) @@ -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: branches/newlist/Lib/test/test_cmd_line.py =================================================================== --- branches/newlist/Lib/test/test_cmd_line.py 2009-04-15 06:18:52 UTC (rev 6228) +++ branches/newlist/Lib/test/test_cmd_line.py 2009-04-16 03:11:53 UTC (rev 6229) @@ -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): @@ -45,11 +50,45 @@ self.assertTrue('usage' in self.start_python('-h')) def test_version(self): - version = 'Jython %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): + # 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() Modified: branches/newlist/Lib/test/test_sax.py =================================================================== --- branches/newlist/Lib/test/test_sax.py 2009-04-15 06:18:52 UTC (rev 6228) +++ branches/newlist/Lib/test/test_sax.py 2009-04-16 03:11:53 UTC (rev 6229) @@ -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,14 +473,16 @@ 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('\\', '/') - return 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 Modified: branches/newlist/NEWS =================================================================== --- branches/newlist/NEWS 2009-04-15 06:18:52 UTC (rev 6228) +++ branches/newlist/NEWS 2009-04-16 03:11:53 UTC (rev 6229) @@ -2,6 +2,7 @@ Jython 2.5.0 rc 1 Bugs fixed (new numbering due to move to Roundup) + - [ 1271 ] Bean property accessors in derived class overide methods in base class - [ 1264 ] 'is not' test exhibits incorrect behaviour when wrapping Java objects - [ 1295 ] Setting a write-only bean property causes a NPE - [ 1272 ] ASTList ClassCastException Modified: branches/newlist/src/org/python/compiler/Module.java =================================================================== --- branches/newlist/src/org/python/compiler/Module.java 2009-04-15 06:18:52 UTC (rev 6228) +++ branches/newlist/src/org/python/compiler/Module.java 2009-04-16 03:11:53 UTC (rev 6229) @@ -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: branches/newlist/src/org/python/core/ParserFacade.java =================================================================== --- branches/newlist/src/org/python/core/ParserFacade.java 2009-04-15 06:18:52 UTC (rev 6228) +++ branches/newlist/src/org/python/core/ParserFacade.java 2009-04-16 03:11:53 UTC (rev 6229) @@ -44,7 +44,7 @@ private ParserFacade() {} - private static String getLine(BufferedReader reader, int line) { + private static String getLine(ExpectedEncodingBufferedReader reader, int line) { if (reader == null) { return ""; } @@ -53,7 +53,14 @@ for (int i = 0; i < line; i++) { text = reader.readLine(); } - return text == null ? text : text + "\n"; + if (text == null) { + return text; + } + if (reader.encoding != null) { + // restore the original encoding + text = new PyUnicode(text).encode(reader.encoding); + } + return text + "\n"; } catch (IOException ioe) { } return text; @@ -80,7 +87,7 @@ line = node.getLine(); col = node.getCharPositionInLine(); } - String text=getLine(reader, line); + String text= getLine(reader, line); String msg = e.getMessage(); if (e.getType() == Py.IndentationError) { return new PyIndentationError(msg, line, col, text, filename); Modified: branches/newlist/src/org/python/core/Py.java =================================================================== --- branches/newlist/src/org/python/core/Py.java 2009-04-15 06:18:52 UTC (rev 6228) +++ branches/newlist/src/org/python/core/Py.java 2009-04-16 03:11:53 UTC (rev 6229) @@ -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: branches/newlist/src/org/python/core/PyClass.java =================================================================== --- branches/newlist/src/org/python/core/PyClass.java 2009-04-15 06:18:52 UTC (rev 6228) +++ branches/newlist/src/org/python/core/PyClass.java 2009-04-16 03:11:53 UTC (rev 6229) @@ -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 @@ -55,10 +48,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 +85,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) { @@ -127,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); } @@ -150,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); @@ -166,6 +195,11 @@ return inst; } + @Override + public boolean isCallable() { + return true; + } + /* PyClass's are compared based on __name__ */ @Override public int __cmp__(PyObject other) { @@ -219,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"); } @@ -237,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; + } } Modified: branches/newlist/src/org/python/core/PyFrame.java =================================================================== --- branches/newlist/src/org/python/core/PyFrame.java 2009-04-15 06:18:52 UTC (rev 6228) +++ branches/newlist/src/org/python/core/PyFrame.java 2009-04-16 03:11:53 UTC (rev 6229) @@ -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: branches/newlist/src/org/python/core/PyJavaType.java =================================================================== --- branches/newlist/src/org/python/core/PyJavaType.java 2009-04-15 06:18:52 UTC (rev 6228) +++ branches/newlist/src/org/python/core/PyJavaType.java 2009-04-16 03:11:53 UTC (rev 6229) @@ -407,7 +407,8 @@ // If one of our superclasses has something defined for this name, check if its a bean // property, and if so, try to fill in any gaps in our property from there - PyObject superForName = lookup(prop.__name__); + PyObject fromType[] = new PyObject[] { null }; + PyObject superForName = lookup_where(prop.__name__, fromType); if (superForName instanceof PyBeanProperty) { PyBeanProperty superProp = ((PyBeanProperty)superForName); // If it has a set method and we don't, take it regardless. If the types don't line @@ -426,6 +427,12 @@ // If the parent bean is hiding a static field, we need it as well. prop.field = superProp.field; } + } else if (superForName != null && fromType[0] != this && !(superForName instanceof PyBeanEvent)) { + // There is already an entry for this name + // It came from a type which is not @this; it came from a superclass + // It is not a bean event + // Do not override methods defined in superclass + continue; } // If the return types on the set and get methods for a property don't agree, the get // method takes precedence Modified: branches/newlist/src/org/python/core/PyType.java =================================================================== --- branches/newlist/src/org/python/core/PyType.java 2009-04-15 06:18:52 UTC (rev 6228) +++ branches/newlist/src/org/python/core/PyType.java 2009-04-16 03:11:53 UTC (rev 6229) @@ -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: branches/newlist/src/org/python/core/StdoutWrapper.java =================================================================== --- branches/newlist/src/org/python/core/StdoutWrapper.java 2009-04-15 06:18:52 UTC (rev 6228) +++ branches/newlist/src/org/python/core/StdoutWrapper.java 2009-04-16 03:11:53 UTC (rev 6229) @@ -105,8 +105,14 @@ file.write(" "); file.softspace = false; } - PyString string = o.__str__(); - String s = string.toString(); + + String s; + if (o instanceof PyUnicode && file.encoding != null) { + s = ((PyUnicode)o).encode(file.encoding, "strict"); + } else { + s = o.__str__().toString(); + } + int len = s.length(); file.write(s); if (o instanceof PyString) { Modified: branches/newlist/src/org/python/core/imp.java =================================================================== --- branches/newlist/src/org/python/core/imp.java 2009-04-15 06:18:52 UTC (rev 6228) +++ branches/newlist/src/org/python/core/imp.java 2009-04-16 03:11:53 UTC (rev 6229) @@ -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; Modified: branches/newlist/src/org/python/util/jython.java =================================================================== --- branches/newlist/src/org/python/util/jython.java 2009-04-15 06:18:52 UTC (rev 6228) +++ branches/newlist/src/org/python/util/jython.java 2009-04-16 03:11:53 UTC (rev 6229) @@ -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: branches/newlist/src/shell/jython =================================================================== --- branches/newlist/src/shell/jython 2009-04-15 06:18:52 UTC (rev 6228) +++ branches/newlist/src/shell/jython 2009-04-16 03:11:53 UTC (rev 6229) @@ -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*) Modified: branches/newlist/tests/java/org/python/tests/identity/IdentityTest.java =================================================================== --- branches/newlist/tests/java/org/python/tests/identity/IdentityTest.java 2009-04-15 06:18:52 UTC (rev 6228) +++ branches/newlist/tests/java/org/python/tests/identity/IdentityTest.java 2009-04-16 03:11:53 UTC (rev 6229) @@ -18,7 +18,6 @@ } public void testReadonly() { - //This used to cause an NPE see http://bugs.jython.org/issue1295 interp.execfile("tests/python/identity_test.py"); } } Modified: branches/newlist/tests/java/org/python/tests/props/BeanPropertyTest.java =================================================================== --- branches/newlist/tests/java/org/python/tests/props/BeanPropertyTest.java 2009-04-15 06:18:52 UTC (rev 6228) +++ branches/newlist/tests/java/org/python/tests/props/BeanPropertyTest.java 2009-04-16 03:11:53 UTC (rev 6229) @@ -20,25 +20,8 @@ interp.exec("from org.python.tests.props import Readonly;Readonly().a = 'test'"); } - //This test is for http://bugs.jython.org/issue1271 - public void testBaseProp() { - /* - interp.exec("from org.python.tests.props import PropShadow"); - interp.exec("a = PropShadow.Derived()"); - interp.exec("assert a.foo() == 1, 'a'"); - interp.exec("assert a.bar() == 2, 'b'"); - */ + public void testShadowing() { + interp.execfile("tests/python/prop_test.py"); } - //This test is for http://bugs.jython.org/issue1271 - public void testDerivedProp() { - /* - interp.exec("from org.python.tests.props import PropShadow"); - interp.exec("b = PropShadow.Derived()"); - interp.exec("assert b.getBaz() == 4, 'c'"); - interp.exec("assert b.getFoo() == 3, 'd'"); - interp.exec("assert b.foo() == 1, 'e'"); - interp.exec("assert b.foo() == 1, 'f'"); - */ - } } Deleted: branches/newlist/tests/policy/nowrite.policy =================================================================== --- trunk/jython/tests/policy/nowrite.policy 2009-04-14 23:00:40 UTC (rev 6226) +++ branches/newlist/tests/policy/nowrite.policy 2009-04-16 03:11:53 UTC (rev 6229) @@ -1,3 +0,0 @@ -grant { - permission java.util.PropertyPermission "*", "read"; -}; Copied: branches/newlist/tests/policy/nowrite.policy (from rev 6226, trunk/jython/tests/policy/nowrite.policy) =================================================================== --- branches/newlist/tests/policy/nowrite.policy (rev 0) +++ branches/newlist/tests/policy/nowrite.policy 2009-04-16 03:11:53 UTC (rev 6229) @@ -0,0 +1,3 @@ +grant { + permission java.util.PropertyPermission "*", "read"; +}; Deleted: branches/newlist/tests/policy/run.sh =================================================================== --- trunk/jython/tests/policy/run.sh 2009-04-14 23:00:40 UTC (rev 6226) +++ branches/newlist/tests/policy/run.sh 2009-04-16 03:11:53 UTC (rev 6229) @@ -1 +0,0 @@ -java -Djava.security.manager -Djava.security.policy=nowrite.policy -classpath ../../dist/jython.jar org.python.util.jython $@ Copied: branches/newlist/tests/policy/run.sh (from rev 6226, trunk/jython/tests/policy/run.sh) =================================================================== --- branches/newlist/tests/policy/run.sh (rev 0) +++ branches/newlist/tests/policy/run.sh 2009-04-16 03:11:53 UTC (rev 6229) @@ -0,0 +1 @@ +java -Djava.security.manager -Djava.security.policy=nowrite.policy -classpath ../../dist/jython.jar org.python.util.jython $@ Copied: branches/newlist/tests/python/prop_test.py (from rev 6226, trunk/jython/tests/python/prop_test.py) =================================================================== --- branches/newlist/tests/python/prop_test.py (rev 0) +++ branches/newlist/tests/python/prop_test.py 2009-04-16 03:11:53 UTC (rev 6229) @@ -0,0 +1,16 @@ +from org.python.tests.identity import IdentityObject + +#This test is for http://bugs.jython.org/issue1271 +from org.python.tests.props import PropShadow + +a = PropShadow.Derived() +assert a.foo() == 1, 'a' +assert a.bar() == 2, 'b' + +from org.python.tests.props import PropShadow +b = PropShadow.Derived() +assert b.getBaz() == 4, 'c' +assert b.baz == 4, 'e' +assert b.getFoo() == 3, 'd' +assert b.foo() == 1, 'f' + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2009-04-18 02:53:03
|
Revision: 6240 http://jython.svn.sourceforge.net/jython/?rev=6240&view=rev Author: zyasoft Date: 2009-04-18 02:53:00 +0000 (Sat, 18 Apr 2009) Log Message: ----------- Removed helper and transition to newlist classes and any corresponding references. Modified Paths: -------------- branches/newlist/CoreExposed.includes branches/newlist/src/org/python/core/Py.java branches/newlist/src/org/python/core/PyList.java branches/newlist/src/templates/mappings Removed 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/org/python/core/PyObjectArray.java branches/newlist/src/org/python/core/PyObjectList.java branches/newlist/src/org/python/core/PySequenceObjectList.java Modified: branches/newlist/CoreExposed.includes =================================================================== --- branches/newlist/CoreExposed.includes 2009-04-18 02:31:16 UTC (rev 6239) +++ branches/newlist/CoreExposed.includes 2009-04-18 02:53:00 UTC (rev 6240) @@ -27,8 +27,6 @@ org/python/core/PyMethod.class 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 Deleted: branches/newlist/Lib/test/test_newtuple.py =================================================================== --- branches/newlist/Lib/test/test_newtuple.py 2009-04-18 02:31:16 UTC (rev 6239) +++ branches/newlist/Lib/test/test_newtuple.py 2009-04-18 02:53:00 UTC (rev 6240) @@ -1,12 +0,0 @@ -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/Py.java =================================================================== --- branches/newlist/src/org/python/core/Py.java 2009-04-18 02:31:16 UTC (rev 6239) +++ branches/newlist/src/org/python/core/Py.java 2009-04-18 02:53:00 UTC (rev 6240) @@ -21,6 +21,8 @@ import org.python.antlr.base.mod; import com.kenai.constantine.platform.Errno; +import java.util.ArrayList; +import java.util.List; import org.python.compiler.Module; import org.python.core.adapter.ClassicPyObjectAdapter; import org.python.core.adapter.ExtensiblePyObjectAdapter; @@ -1932,13 +1934,12 @@ } catch (PyException exc) { } - PyObjectArray objs = new PyObjectArray(n); + List<PyObject> objs = new ArrayList<PyObject>(n); for (PyObject item : o.asIterable()) { objs.add(item); } - // Cut back if guess was too large. - objs.trimToSize(); - return (PyObject[]) objs.getArray(); + PyObject dest[] = new PyObject[0]; + return (objs.toArray(dest)); } } /** @deprecated */ Modified: branches/newlist/src/org/python/core/PyList.java =================================================================== --- branches/newlist/src/org/python/core/PyList.java 2009-04-18 02:31:16 UTC (rev 6239) +++ branches/newlist/src/org/python/core/PyList.java 2009-04-18 02:53:00 UTC (rev 6240) @@ -651,11 +651,6 @@ final void list_extend(PyObject o) { if (o instanceof PyList) { list.addAll(((PyList) o).list); - } else if (o instanceof PySequenceObjectList) { - PyObject other[] = ((PySequenceObjectList) o).getArray(); - for (int i = 0; i < other.length; i++) { - list.add(other[i]); - } } else { for (PyObject item : o.asIterable()) { list.add(item); Deleted: branches/newlist/src/org/python/core/PyNewTuple.java =================================================================== --- branches/newlist/src/org/python/core/PyNewTuple.java 2009-04-18 02:31:16 UTC (rev 6239) +++ branches/newlist/src/org/python/core/PyNewTuple.java 2009-04-18 02:53:00 UTC (rev 6240) @@ -1,569 +0,0 @@ -// 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 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]; - } else { - array = new PyObject[elements.length]; - System.arraycopy(elements, 0, array, 0, elements.length); - } - } - - 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; - } - } - - public PyNewTuple(PyType subtype, Collection<PyObject> elements) { - super(subtype); - if (elements == null) { - array = new PyObject[0]; - } else { - array = new PyObject[elements.size()]; - elements.toArray(array); - } - } - - private static PyNewTuple fromArrayNoCopy(PyObject[] elements) { -// 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); - 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(getList().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 = 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) { - 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 = 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(); - } - - 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) { - 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 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 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; - } -} Deleted: branches/newlist/src/org/python/core/PyNewTupleDerived.java =================================================================== --- branches/newlist/src/org/python/core/PyNewTupleDerived.java 2009-04-18 02:31:16 UTC (rev 6239) +++ branches/newlist/src/org/python/core/PyNewTupleDerived.java 2009-04-18 02:53:00 UTC (rev 6240) @@ -1,1162 +0,0 @@ -/* 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)&&... [truncated message content] |
From: <zy...@us...> - 2009-04-18 23:28:34
|
Revision: 6242 http://jython.svn.sourceforge.net/jython/?rev=6242&view=rev Author: zyasoft Date: 2009-04-18 23:28:20 +0000 (Sat, 18 Apr 2009) Log Message: ----------- Merged revisions 6230,6232-6236,6241 via svnmerge from https://jython.svn.sourceforge.net/svnroot/jython/trunk/jython ........ r6230 | pjenvey | 2009-04-15 23:48:35 -0600 (Wed, 15 Apr 2009) | 1 line make BadPickleGet an actual exception ........ r6232 | pjenvey | 2009-04-16 17:59:40 -0600 (Thu, 16 Apr 2009) | 1 line cleanup/coding standards ........ r6233 | pjenvey | 2009-04-16 19:35:16 -0600 (Thu, 16 Apr 2009) | 2 lines fix LineBufferedWriter.write's result, cleanup ........ r6234 | pjenvey | 2009-04-16 19:38:24 -0600 (Thu, 16 Apr 2009) | 3 lines rename eof_fodder7 to match other SyntaxError'ing tests. avoids test_compiler from randomly importing it ........ r6235 | pjenvey | 2009-04-16 21:44:35 -0600 (Thu, 16 Apr 2009) | 3 lines o fix memoization with cyclic structures: http://bugs.python.org/issue998998 o cleanup persistent_id/load handling, allow persistent_load as a list ........ r6236 | fwierzbicki | 2009-04-17 08:06:01 -0600 (Fri, 17 Apr 2009) | 2 lines More tinkering to get a useful policy file for GAE testing. ........ r6241 | amak | 2009-04-18 07:06:43 -0600 (Sat, 18 Apr 2009) | 3 lines For the moment, we do not actually support IPV6. Before we can support it, we need to establish how such support can be adequately tested. Having the has_ipv6 flag as 1/True is misleading to the users. ........ Modified Paths: -------------- branches/newlist/Lib/socket.py branches/newlist/Lib/test/test_cpickle_jy.py branches/newlist/Lib/test/test_eof_jy.py branches/newlist/src/org/python/core/io/BufferedWriter.java branches/newlist/src/org/python/core/io/LineBufferedWriter.java branches/newlist/src/org/python/core/io/StreamIO.java branches/newlist/src/org/python/core/io/UniversalIOWrapper.java branches/newlist/src/org/python/modules/cPickle.java branches/newlist/tests/policy/nowrite.policy branches/newlist/tests/policy/run.sh Added Paths: ----------- branches/newlist/Lib/test/badsyntax_eof1.py Removed Paths: ------------- branches/newlist/Lib/test/eof_fodder7.py Property Changed: ---------------- branches/newlist/ Property changes on: branches/newlist ___________________________________________________________________ Modified: svnmerge-integrated - /branches/modjy:1-6074 /branches/pbcvm:1-6045 /trunk/jython:1-6228 + /branches/modjy:1-6074 /branches/pbcvm:1-6045 /trunk/jython:1-6241 Modified: branches/newlist/Lib/socket.py =================================================================== --- branches/newlist/Lib/socket.py 2009-04-18 13:06:43 UTC (rev 6241) +++ branches/newlist/Lib/socket.py 2009-04-18 23:28:20 UTC (rev 6242) @@ -484,10 +484,11 @@ else: return self._do_receive_nio(0, num_bytes, flags) +# For now, we DO NOT have complete IPV6 support. +has_ipv6 = False + # Name and address functions - -has_ipv6 = 1 - + def _gethostbyaddr(name): # This is as close as I can get; at least the types are correct... addresses = java.net.InetAddress.getAllByName(gethostbyname(name)) Copied: branches/newlist/Lib/test/badsyntax_eof1.py (from rev 6241, trunk/jython/Lib/test/badsyntax_eof1.py) =================================================================== --- branches/newlist/Lib/test/badsyntax_eof1.py (rev 0) +++ branches/newlist/Lib/test/badsyntax_eof1.py 2009-04-18 23:28:20 UTC (rev 6242) @@ -0,0 +1,5 @@ +def hi(): + pass + +def bye(): + hi( Deleted: branches/newlist/Lib/test/eof_fodder7.py =================================================================== --- branches/newlist/Lib/test/eof_fodder7.py 2009-04-18 13:06:43 UTC (rev 6241) +++ branches/newlist/Lib/test/eof_fodder7.py 2009-04-18 23:28:20 UTC (rev 6242) @@ -1,5 +0,0 @@ -def hi(): - pass - -def bye(): - hi( Modified: branches/newlist/Lib/test/test_cpickle_jy.py =================================================================== --- branches/newlist/Lib/test/test_cpickle_jy.py 2009-04-18 13:06:43 UTC (rev 6241) +++ branches/newlist/Lib/test/test_cpickle_jy.py 2009-04-18 23:28:20 UTC (rev 6242) @@ -7,13 +7,34 @@ import unittest from test import test_support +class MyClass(object): + pass + + class CPickleTestCase(unittest.TestCase): def test_zero_long(self): self.assertEqual(cPickle.loads(cPickle.dumps(0L, 2)), 0L) self.assertEqual(cPickle.dumps(0L, 2), pickle.dumps(0L, 2)) + def test_cyclic_memoize(self): + # http://bugs.python.org/issue998998 - cPickle shouldn't fail + # this, though pickle.py still does + m = MyClass() + m2 = MyClass() + s = set([m]) + m.foo = set([m2]) + m2.foo = s + + s2 = cPickle.loads(cPickle.dumps(s)) + self.assertEqual(len(s2), 1) + m3 = iter(s2).next() + self.assertEqual(len(m3.foo), 1) + m4 = iter(m3.foo).next() + self.assertEqual(m4.foo, s2) + + def test_main(): test_support.run_unittest(CPickleTestCase) Modified: branches/newlist/Lib/test/test_eof_jy.py =================================================================== --- branches/newlist/Lib/test/test_eof_jy.py 2009-04-18 13:06:43 UTC (rev 6241) +++ branches/newlist/Lib/test/test_eof_jy.py 2009-04-18 23:28:20 UTC (rev 6242) @@ -47,7 +47,7 @@ def test_trailing_paren(self): try: - import eof_fodder7 + import badsyntax_eof1 except SyntaxError, cause: self.assertEquals(cause.lineno, 5) Modified: branches/newlist/src/org/python/core/io/BufferedWriter.java =================================================================== --- branches/newlist/src/org/python/core/io/BufferedWriter.java 2009-04-18 13:06:43 UTC (rev 6241) +++ branches/newlist/src/org/python/core/io/BufferedWriter.java 2009-04-18 23:28:20 UTC (rev 6242) @@ -53,10 +53,11 @@ int totalToWrite = total - toBuffer; int count = totalToWrite; + ByteBuffer[] bulk = new ByteBuffer[] {buffer, bytes}; // Prepare the buffer for writing buffer.flip(); while (count > 0) { - count -= rawIO.write(new ByteBuffer[] {buffer, bytes}); + count -= rawIO.write(bulk); } // Prepare the buffer for buffering buffer.clear(); Modified: branches/newlist/src/org/python/core/io/LineBufferedWriter.java =================================================================== --- branches/newlist/src/org/python/core/io/LineBufferedWriter.java 2009-04-18 13:06:43 UTC (rev 6241) +++ branches/newlist/src/org/python/core/io/LineBufferedWriter.java 2009-04-18 23:28:20 UTC (rev 6242) @@ -23,7 +23,7 @@ /** {@inheritDoc} */ public int write(ByteBuffer bytes) { - int written = 0; + int size = bytes.remaining(); while (bytes.hasRemaining()) { byte next = bytes.get(); @@ -37,11 +37,10 @@ } if (next == LF_BYTE) { - written += buffer.position(); flush(); } } - return written; + return size; } } Modified: branches/newlist/src/org/python/core/io/StreamIO.java =================================================================== --- branches/newlist/src/org/python/core/io/StreamIO.java 2009-04-18 13:06:43 UTC (rev 6241) +++ branches/newlist/src/org/python/core/io/StreamIO.java 2009-04-18 23:28:20 UTC (rev 6242) @@ -16,8 +16,8 @@ import java.nio.channels.ReadableByteChannel; import java.nio.channels.WritableByteChannel; +import org.python.core.Py; import org.python.core.imp; -import org.python.core.Py; /** * Raw I/O implementation for simple streams. @@ -82,9 +82,6 @@ * Construct a StreamIO for the given write channel. * * @param writeChannel a WritableByteChannel - * @param isatty boolean whether this io object is a tty device - * @param closefd boolean whether the underlying file is closed on - * close() (defaults to True) */ public StreamIO(WritableByteChannel writeChannel) { this(writeChannel, true); @@ -160,56 +157,62 @@ /** Unwrap one or more nested FilterInputStreams. */ private static FileDescriptor getInputFileDescriptor(InputStream stream) throws IOException { - if (stream == null) - return null; - if (stream instanceof FileInputStream) - return ((FileInputStream)stream).getFD(); - if (stream instanceof FilterInputStream) { - Field inField = null; - try { - inField = FilterInputStream.class.getDeclaredField("in"); - inField.setAccessible(true); - return getInputFileDescriptor((InputStream)inField.get(stream)); - } catch (Exception e) { - } finally { - if (inField != null && inField.isAccessible()) - inField.setAccessible(false); - } - } - return null; + if (stream == null) { + return null; + } + if (stream instanceof FileInputStream) { + return ((FileInputStream)stream).getFD(); + } + if (stream instanceof FilterInputStream) { + Field inField = null; + try { + inField = FilterInputStream.class.getDeclaredField("in"); + inField.setAccessible(true); + return getInputFileDescriptor((InputStream)inField.get(stream)); + } catch (Exception e) { + // XXX: masking other exceptions + } finally { + if (inField != null && inField.isAccessible()) + inField.setAccessible(false); + } + } + return null; } /** Unwrap one or more nested FilterOutputStreams. */ private static FileDescriptor getOutputFileDescriptor(OutputStream stream) throws IOException { - if (stream == null) - return null; - if (stream instanceof FileOutputStream) - return ((FileOutputStream)stream).getFD(); - if (stream instanceof FilterOutputStream) { - Field outField = null; - try { - outField = FilterOutputStream.class.getDeclaredField("out"); - outField.setAccessible(true); - return getOutputFileDescriptor((OutputStream)outField.get(stream)); - } catch (Exception e) { - } finally { - if (outField != null && outField.isAccessible()) - outField.setAccessible(false); - } - } - return null; + if (stream == null) { + return null; + } + if (stream instanceof FileOutputStream) { + return ((FileOutputStream)stream).getFD(); + } + if (stream instanceof FilterOutputStream) { + Field outField = null; + try { + outField = FilterOutputStream.class.getDeclaredField("out"); + outField.setAccessible(true); + return getOutputFileDescriptor((OutputStream)outField.get(stream)); + } catch (Exception e) { + // XXX: masking other exceptions + } finally { + if (outField != null && outField.isAccessible()) + outField.setAccessible(false); + } + } + return null; } /** {@inheritDoc} */ - public boolean isatty() { checkClosed(); FileDescriptor fd; try { - if ( ((fd = getInputFileDescriptor(inputStream)) == null) && - ((fd = getOutputFileDescriptor(outputStream)) == null)) - return false; + if ((fd = getInputFileDescriptor(inputStream)) == null + && (fd = getOutputFileDescriptor(outputStream)) == null) { + return false; + } } catch (IOException e) { return false; } Modified: branches/newlist/src/org/python/core/io/UniversalIOWrapper.java =================================================================== --- branches/newlist/src/org/python/core/io/UniversalIOWrapper.java 2009-04-18 13:06:43 UTC (rev 6241) +++ branches/newlist/src/org/python/core/io/UniversalIOWrapper.java 2009-04-18 23:28:20 UTC (rev 6242) @@ -3,7 +3,6 @@ import java.nio.ByteBuffer; import java.util.EnumSet; -import java.util.Iterator; import org.python.core.Py; import org.python.core.PyObject; @@ -189,7 +188,6 @@ byte[] readaheadArray; int readaheadPos; int interimBuilderPos; - String line; do { readaheadArray = readahead.array(); @@ -313,12 +311,12 @@ if (size == 0) { return Py.None; } else if (size == 1) { - Newline newline = (Newline)newlineTypes.iterator().next(); + Newline newline = newlineTypes.iterator().next(); return new PyString(newline.getValue()); } - int i = 0; PyObject[] newlines = new PyObject[size]; + int i = 0; for (Newline newline : newlineTypes) { newlines[i++] = new PyString(newline.getValue()); } Modified: branches/newlist/src/org/python/modules/cPickle.java =================================================================== --- branches/newlist/src/org/python/modules/cPickle.java 2009-04-18 13:06:43 UTC (rev 6241) +++ branches/newlist/src/org/python/modules/cPickle.java 2009-04-18 23:28:20 UTC (rev 6242) @@ -384,11 +384,8 @@ public static PyObject PicklingError; public static PyObject UnpickleableError; public static PyObject UnpicklingError; + public static PyObject BadPickleGet; - public static final PyString BadPickleGet = - new PyString("cPickle.BadPickleGet"); - - final static char MARK = '('; final static char STOP = '.'; final static char POP = '0'; @@ -510,6 +507,7 @@ PicklingError = Py.makeClass("PicklingError", PickleError, exceptionNamespace()); UnpickleableError = Py.makeClass("UnpickleableError", PicklingError, _UnpickleableError()); UnpicklingError = Py.makeClass("UnpicklingError", PickleError, exceptionNamespace()); + BadPickleGet = Py.makeClass("BadPickleGet", UnpicklingError, exceptionNamespace()); } public static PyObject exceptionNamespace() { @@ -772,14 +770,8 @@ private void save(PyObject object, boolean pers_save) { - if (!pers_save) { - if (persistent_id != null) { - PyObject pid = persistent_id.__call__(object); - if (pid != Py.None) { - save_pers(pid); - return; - } - } + if (!pers_save && persistent_id != null && save_pers(object, persistent_id)) { + return; } int d = get_id(object); @@ -803,12 +795,8 @@ if (save_type(object, t)) return; - if (inst_persistent_id != null) { - PyObject pid = inst_persistent_id.__call__(object); - if (pid != Py.None) { - save_pers(pid); - return; - } + if (!pers_save && inst_persistent_id != null && save_pers(object, inst_persistent_id)) { + return; } if (Py.isSubClass(t, PyType.TYPE)) { @@ -861,13 +849,20 @@ "Second element of tupe returned by " + reduce.__repr__() + " must be a tuple"); } - save_reduce(callable, arg_tup, state, listitems, dictitems, putMemo(d, object)); - + save_reduce(callable, arg_tup, state, listitems, dictitems, object); } - final private void save_pers(PyObject pid) { + final private boolean save_pers(PyObject object, PyObject pers_func) { + PyObject pid = pers_func.__call__(object); + if (pid == Py.None) { + return false; + } + if (protocol == 0) { + if (!Py.isInstance(pid, PyString.TYPE)) { + throw new PyException(PicklingError, "persistent id must be string"); + } file.write(PERSID); file.write(pid.toString()); file.write("\n"); @@ -875,10 +870,12 @@ save(pid, true); file.write(BINPERSID); } + return true; } final private void save_reduce(PyObject callable, PyObject arg_tup, - PyObject state, PyObject listitems, PyObject dictitems, int memoId) + PyObject state, PyObject listitems, PyObject dictitems, + PyObject object) { PyObject callableName = callable.__findattr__("__name__"); if(protocol >= 2 && callableName != null @@ -896,7 +893,10 @@ save(arg_tup); file.write(REDUCE); } - put(memoId); + + // Memoize + put(putMemo(get_id(object), object)); + if (listitems != Py.None) { batch_appends(listitems); } @@ -1699,17 +1699,30 @@ final private void load_persid() { - String pid = file.readlineNoNl(); - push(persistent_load.__call__(new PyString(pid))); + load_persid(new PyString(file.readlineNoNl())); } final private void load_binpersid() { - PyObject pid = pop(); - push(persistent_load.__call__(pid)); + load_persid(pop()); } + final private void load_persid(PyObject pid) { + if (persistent_load == null) { + throw new PyException(UnpicklingError, + "A load persistent id instruction was encountered,\n" + + "but no persistent_load function was specified."); + } + if (persistent_load instanceof PyList) { + ((PyList)persistent_load).append(pid); + } else { + pid = persistent_load.__call__(pid); + } + push(pid); + } + + final private void load_none() { push(Py.None); } @@ -2067,16 +2080,18 @@ final private void load_get() { String py_str = file.readlineNoNl(); PyObject value = memo.get(py_str); - if (value == null) + if (value == null) { throw new PyException(BadPickleGet, py_str); + } push(value); } final private void load_binget() { String py_key = String.valueOf((int)file.read(1).charAt(0)); PyObject value = memo.get(py_key); - if (value == null) + if (value == null) { throw new PyException(BadPickleGet, py_key); + } push(value); } @@ -2084,8 +2099,9 @@ int i = read_binint(); String py_key = String.valueOf(i); PyObject value = memo.get(py_key); - if (value == null) + if (value == null) { throw new PyException(BadPickleGet, py_key); + } push(value); } Modified: branches/newlist/tests/policy/nowrite.policy =================================================================== --- branches/newlist/tests/policy/nowrite.policy 2009-04-18 13:06:43 UTC (rev 6241) +++ branches/newlist/tests/policy/nowrite.policy 2009-04-18 23:28:20 UTC (rev 6242) @@ -1,3 +1,8 @@ grant { + permission java.lang.RuntimePermission "createClassLoader"; + permission java.lang.RuntimePermission "getProtectionDomain"; permission java.util.PropertyPermission "*", "read"; + permission java.io.FilePermission "<<ALL FILES>>", "read"; + + permission java.lang.RuntimePermission "accessDeclaredMembers"; }; Modified: branches/newlist/tests/policy/run.sh =================================================================== --- branches/newlist/tests/policy/run.sh 2009-04-18 13:06:43 UTC (rev 6241) +++ branches/newlist/tests/policy/run.sh 2009-04-18 23:28:20 UTC (rev 6242) @@ -1 +1 @@ -java -Djava.security.manager -Djava.security.policy=nowrite.policy -classpath ../../dist/jython.jar org.python.util.jython $@ +java -Djava.security.manager -Djava.security.policy=nowrite.policy -classpath ../../dist/jython.jar org.python.util.jython -Dpython.home=../../dist -Dpython.cachedir.skip=true $@ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |