From: <cg...@us...> - 2007-12-02 07:11:20
|
Revision: 3749 http://jython.svn.sourceforge.net/jython/?rev=3749&view=rev Author: cgroves Date: 2007-12-01 23:11:17 -0800 (Sat, 01 Dec 2007) Log Message: ----------- Commit patch #1798544 from Rajesh Battala to make slice a type and fix bug #1791931. Modified Paths: -------------- trunk/jython/Lib/test/regrtest.py trunk/jython/src/org/python/core/PyInstance.java trunk/jython/src/org/python/core/PySequence.java trunk/jython/src/org/python/core/PySlice.java trunk/jython/src/org/python/core/__builtin__.java trunk/jython/src/templates/mappings Added Paths: ----------- trunk/jython/Lib/test/test_slice.py trunk/jython/src/org/python/core/PySliceDerived.java trunk/jython/src/templates/slice.derived trunk/jython/src/templates/slice.expose Modified: trunk/jython/Lib/test/regrtest.py =================================================================== --- trunk/jython/Lib/test/regrtest.py 2007-12-02 05:22:32 UTC (rev 3748) +++ trunk/jython/Lib/test/regrtest.py 2007-12-02 07:11:17 UTC (rev 3749) @@ -1052,7 +1052,6 @@ test_pyclbr test_quopri test_random - test_slice test_softspace test_syntax test_threaded_import Added: trunk/jython/Lib/test/test_slice.py =================================================================== --- trunk/jython/Lib/test/test_slice.py (rev 0) +++ trunk/jython/Lib/test/test_slice.py 2007-12-02 07:11:17 UTC (rev 3749) @@ -0,0 +1,99 @@ +# tests for slice objects; in particular the indices method. + +import unittest +from test import test_support + +import sys + +class SliceTest(unittest.TestCase): + + def test_constructor(self): + self.assertRaises(TypeError, slice) + self.assertRaises(TypeError, slice, 1, 2, 3, 4) + + def test_repr(self): + self.assertEqual(repr(slice(1, 2, 3)), "slice(1, 2, 3)") + + def test_hash(self): + # Verify clearing of SF bug #800796 + self.assertRaises(TypeError, hash, slice(5)) + self.assertRaises(TypeError, slice(5).__hash__) + + def test_cmp(self): + s1 = slice(1, 2, 3) + s2 = slice(1, 2, 3) + s3 = slice(1, 2, 4) + self.assertEqual(s1, s2) + self.assertNotEqual(s1, s3) + + class Exc(Exception): + pass + + class BadCmp(object): + def __eq__(self, other): + raise Exc + + s1 = slice(BadCmp()) + s2 = slice(BadCmp()) + self.assertRaises(Exc, cmp, s1, s2) + self.assertEqual(s1, s1) + + s1 = slice(1, BadCmp()) + s2 = slice(1, BadCmp()) + self.assertEqual(s1, s1) + self.assertRaises(Exc, cmp, s1, s2) + + s1 = slice(1, 2, BadCmp()) + s2 = slice(1, 2, BadCmp()) + self.assertEqual(s1, s1) + self.assertRaises(Exc, cmp, s1, s2) + + def test_members(self): + s = slice(1) + self.assertEqual(s.start, None) + self.assertEqual(s.stop, 1) + self.assertEqual(s.step, None) + + s = slice(1, 2) + self.assertEqual(s.start, 1) + self.assertEqual(s.stop, 2) + self.assertEqual(s.step, None) + + s = slice(1, 2, 3) + self.assertEqual(s.start, 1) + self.assertEqual(s.stop, 2) + self.assertEqual(s.step, 3) + + class AnyClass: + pass + + obj = AnyClass() + s = slice(obj) + self.assert_(s.stop is obj) + + def test_indices(self): + self.assertEqual(slice(None ).indices(10), (0, 10, 1)) + self.assertEqual(slice(None, None, 2).indices(10), (0, 10, 2)) + self.assertEqual(slice(1, None, 2).indices(10), (1, 10, 2)) + self.assertEqual(slice(None, None, -1).indices(10), (9, -1, -1)) + self.assertEqual(slice(None, None, -2).indices(10), (9, -1, -2)) + self.assertEqual(slice(3, None, -2).indices(10), (3, -1, -2)) + self.assertEqual( + slice(-100, 100 ).indices(10), + slice(None).indices(10) + ) + self.assertEqual( + slice(100, -100, -1).indices(10), + slice(None, None, -1).indices(10) + ) + self.assertEqual(slice(-100L, 100L, 2L).indices(10), (0, 10, 2)) + + self.assertEqual(range(10)[::sys.maxint - 1], [0]) + + self.assertRaises(OverflowError, slice(None).indices, 1L<<100) + +def test_main(): + test_support.run_unittest(SliceTest) + +if __name__ == "__main__": + test_main() Modified: trunk/jython/src/org/python/core/PyInstance.java =================================================================== --- trunk/jython/src/org/python/core/PyInstance.java 2007-12-02 05:22:32 UTC (rev 3748) +++ trunk/jython/src/org/python/core/PyInstance.java 2007-12-02 07:11:17 UTC (rev 3749) @@ -578,9 +578,9 @@ PySlice slice = (PySlice)key; - if (slice.step != Py.None && slice.step != Py.One) { - if (slice.step instanceof PyInteger) { - if (((PyInteger)slice.step).getValue() != 1) { + if (slice.getStep() != Py.None && slice.getStep() != Py.One) { + if (slice.getStep() instanceof PyInteger) { + if (((PyInteger)slice.getStep()).getValue() != 1) { return null; } } else { Modified: trunk/jython/src/org/python/core/PySequence.java =================================================================== --- trunk/jython/src/org/python/core/PySequence.java 2007-12-02 05:22:32 UTC (rev 3748) +++ trunk/jython/src/org/python/core/PySequence.java 2007-12-02 07:11:17 UTC (rev 3749) @@ -218,38 +218,21 @@ return list; } - protected static final int sliceLength(int start, int stop, int step) { + // make step a long in case adding the start, stop and step together overflows an int + protected static final int sliceLength(int start, int stop, long step) { //System.err.println("slice: "+start+", "+stop+", "+step); int ret; - if (step > 0) { - ret = (stop-start+step-1)/step; + if(step > 0) { + ret = (int)((stop - start + step - 1) / step); } else { - ret = (stop-start+step+1)/step; + ret = (int)((stop - start + step + 1) / step); } - if (ret < 0) return 0; + if(ret < 0) { + return 0; + } return ret; } - private static final int getIndex(PyObject index, int defaultValue) { - if (index == Py.None || index == null) - return defaultValue; - if (index instanceof PyLong) { - try { - index = (index.__int__()); - } catch (PyException exc) { - if (Py.matchException(exc, Py.OverflowError)) { - if (new PyLong(0L).__cmp__(index) < 0) - return Integer.MAX_VALUE; - else - return 0; - } - } - } - if (!(index instanceof PyInteger)) - throw Py.TypeError("slice index must be int"); - return ((PyInteger)index).getValue(); - } - protected int fixindex(int index) { int l = __len__(); if (index < 0) @@ -299,51 +282,6 @@ public boolean isMappingType() throws PyIgnoreMethodTag { return false; } public boolean isNumberType() throws PyIgnoreMethodTag { return false; } - protected static final int getStep(PyObject s_step) { - int step = getIndex(s_step, 1); - if (step == 0) { - throw Py.ValueError("slice step cannot be zero"); - } - return step; - } - - protected static final int getStart(PyObject s_start, int step, - int length) - { - int start; - if (step < 0) { - start = getIndex(s_start, length - 1); - if (start < 0) start += length; - if (start < 0) start = -1; - if (start >= length) start = length - 1; - } else { - start = getIndex(s_start, 0); - if (start < 0) start += length; - if (start < 0) start = 0; - if (start >= length) start = length; - } - - return start; - } - - protected static final int getStop(PyObject s_stop, int start, int step, - int length) - { - int stop; - if (step < 0) { - stop = getIndex(s_stop, -1); - if (stop < -1) stop = length+stop; - if (stop < -1) stop = -1; - } else { - stop = getIndex(s_stop, length); - if (stop < 0) stop = length+stop; - if (stop < 0) stop = 0; - } - if (stop > length) stop = length; - - return stop; - } - public synchronized PyObject __getslice__(PyObject s_start, PyObject s_stop, PyObject s_step) { @@ -360,11 +298,8 @@ PyObject s_stop, PyObject s_step) { - int length = __len__(); - int step = getStep(s_step); - int start = getStart(s_start, step, length); - int stop = getStop(s_stop, start, step, length); - return getslice(start, stop, step); + int[] slice = new PySlice(s_start, s_stop, s_step).indices(__len__()); + return getslice(slice[0], slice[1], slice[2]); } public synchronized void __setslice__(PyObject s_start, PyObject s_stop, @@ -379,11 +314,8 @@ final synchronized void seq___setslice__(PyObject s_start, PyObject s_stop, PyObject s_step, PyObject value) { - int length = __len__(); - int step = getStep(s_step); - int start = getStart(s_start, step, length); - int stop = getStop(s_stop, start, step, length); - setslice(start, stop, step, value); + int[] slice = new PySlice(s_start, s_stop, s_step).indices(__len__()); + setslice(slice[0], slice[1], slice[2], value); } public synchronized void __delslice__(PyObject s_start, PyObject s_stop, @@ -394,11 +326,8 @@ final synchronized void seq___delslice__(PyObject s_start, PyObject s_stop, PyObject s_step) { - int length = __len__(); - int step = getStep(s_step); - int start = getStart(s_start, step, length); - int stop = getStop(s_stop, start, step, length); - delRange(start, stop, step); + int[] slice = new PySlice(s_start, s_stop, s_step).indices(__len__()); + delRange(slice[0], slice[1], slice[2]); } public synchronized void __setitem__(int index, PyObject value) { Modified: trunk/jython/src/org/python/core/PySlice.java =================================================================== --- trunk/jython/src/org/python/core/PySlice.java 2007-12-02 05:22:32 UTC (rev 3748) +++ trunk/jython/src/org/python/core/PySlice.java 2007-12-02 07:11:17 UTC (rev 3749) @@ -1,34 +1,278 @@ -// Copyright (c) Corporation for National Research Initiatives +//Copyright (c) Corporation for National Research Initiatives package org.python.core; +import org.apache.tools.zip.AsiExtraField; +import org.python.core.PyObject.ConversionException; /** * A python slice object. */ public class PySlice extends PyObject { - public PyObject start, stop, step; + //~ BEGIN GENERATED REGION -- DO NOT EDIT SEE gexpose.py + /* type info */ + + public static final String exposed_name="slice"; + + public static void typeSetup(PyObject dict,PyType.Newstyle marker) { + dict.__setitem__("start",new PyGetSetDescr("start",PySlice.class,"getStart",null,null)); + dict.__setitem__("stop",new PyGetSetDescr("stop",PySlice.class,"getStop",null,null)); + dict.__setitem__("step",new PyGetSetDescr("step",PySlice.class,"getStep",null,null)); + class exposed_indices extends PyBuiltinMethodNarrow { + + exposed_indices(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed_indices(self,info); + } + + public PyObject __call__(PyObject arg0) { + return((PySlice)self).slice_indices(arg0); + } + + } + dict.__setitem__("indices",new PyMethodDescr("indices",PySlice.class,1,1,new exposed_indices(null,null))); + class exposed___hash__ extends PyBuiltinMethodNarrow { + + exposed___hash__(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed___hash__(self,info); + } + + public PyObject __call__() { + throw Py.TypeError("unhashable type"); + } + + } + dict.__setitem__("__hash__",new PyMethodDescr("__hash__",PySlice.class,0,0,new exposed___hash__(null,null))); + class exposed___init__ extends PyBuiltinMethod { + + exposed___init__(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed___init__(self,info); + } + + public PyObject __call__(PyObject[]args) { + return __call__(args,Py.NoKeywords); + } + + public PyObject __call__(PyObject[]args,String[]keywords) { + ((PySlice)self).slice_init(args,keywords); + return Py.None; + } + + } + dict.__setitem__("__init__",new PyMethodDescr("__init__",PySlice.class,-1,-1,new exposed___init__(null,null))); + dict.__setitem__("__new__",new PyNewWrapper(PySlice.class,"__new__",-1,-1) { + + public PyObject new_impl(boolean init,PyType subtype,PyObject[]args,String[]keywords) { + PySlice newobj; + if (for_type==subtype) { + newobj=new PySlice(); + if (init) + newobj.slice_init(args,keywords); + } else { + newobj=new PySliceDerived(subtype); + } + return newobj; + } + + }); + } + //~ END GENERATED REGION -- DO NOT EDIT SEE gexpose.py + + final void slice_init(PyObject[] args, String[] keywords) { + if(args.length == 0) { + throw Py.TypeError("slice expected at least 1 arguments, got " + args.length); + } else if(args.length > 3) { + throw Py.TypeError("slice expected at most 3 arguments, got " + args.length); + } + ArgParser ap = new ArgParser("slice", args, keywords, "start", "stop", "step"); + if(args.length == 1) { + stop = ap.getPyObject(0); + } else if(args.length == 2) { + start = ap.getPyObject(0); + stop = ap.getPyObject(1); + } else if(args.length == 3) { + start = ap.getPyObject(0); + stop = ap.getPyObject(1); + step = ap.getPyObject(2); + } + } + + private static final PyType SLICE_TYPE = PyType.fromClass(PySlice.class); + public PySlice(PyObject start, PyObject stop, PyObject step) { - if (start == null) start = Py.None; - if (stop == null) stop = Py.None; - if (step == null) step = Py.One; + this(SLICE_TYPE); + if(start != null) { + this.start = start; + } + if(stop != null) { + this.stop = stop; + } + if(step != null) { + this.step = step; + } + } - this.start = start; - this.stop = stop; - this.step = step; + public PySlice(PyType type) { + super(type); } + + public PySlice() { + this(SLICE_TYPE); + } + public final PyObject getStart() { + return start; + } + + public final PyObject getStop() { + return stop; + } + + public final PyObject getStep() { + return step; + } + + public int hashCode() { + return slice_hashCode(); + } + + final int slice_hashCode() { + throw Py.TypeError("unhashable type"); + } + public PyString __str__() { - return new PyString(start.__repr__()+":"+stop.__repr__()+":"+ - step.__repr__()); + return new PyString(getStart().__repr__() + ":" + getStop().__repr__() + ":" + + getStep().__repr__()); } public PyString __repr__() { - return new PyString("slice("+start.__repr__()+", "+ - stop.__repr__()+", "+ - step.__repr__()+")"); + return new PyString("slice(" + getStart().__repr__() + ", " + + getStop().__repr__() + ", " + + getStep().__repr__() + ")"); } - public boolean isSequenceType() { return false; } + public PyObject __eq__(PyObject o) { + if(getType() != o.getType() && !(getType().isSubType(o.getType()))) { + return null; + } + if(this == o) { + return Py.True; + } + PySlice oSlice = (PySlice)o; + if(eq(getStart(), oSlice.getStart()) && eq(getStop(), oSlice.getStop()) + && eq(getStep(), oSlice.getStep())) { + return Py.True; + } + return Py.False; + } + + private static final boolean eq(PyObject o1, PyObject o2) { + return o1._cmp(o2) == 0; + } + + public PyObject __ne__(PyObject o) { + return __eq__(o).__not__(); + } + + public PyObject slice_indices(PyObject len) { + int ilen; + try { + ilen = len.asInt(0); + } catch(ConversionException e) { + throw Py.TypeError("length must be an int"); + } + int[] slice = indices(ilen); + PyInteger[] pyInts = new PyInteger[slice.length]; + for(int i = 0; i < pyInts.length; i++) { + pyInts[i] = Py.newInteger(slice[i]); + } + return new PyTuple(pyInts); + } + + private static int calculateSliceIndex(PyObject v) { + if(v instanceof PyInteger) { + return ((PyInteger)v).getValue(); + } else if(v instanceof PyLong) { + try { + return ((PyInteger)v.__int__()).getValue(); + } catch (PyException exc) { + if (Py.matchException(exc, Py.OverflowError)) { + if (new PyLong(0L).__cmp__(v) < 0) { + return Integer.MAX_VALUE; + }else { + return 0; + } + } + } + } + throw Py.TypeError("slice indices must be integers or None"); + } + + /** + * Calculates the actual indices of a slice with this slice's start, stop + * and step values for a sequence of length <code>len</code>. + * + * @return an array with the start at index 0, stop at index 1 and step and + * index 2. + */ + public int[] indices(int len) { + int[] slice = new int[3]; + if (getStep() == Py.None) { + slice[STEP] = 1; + } else { + slice[STEP] = calculateSliceIndex(getStep()); + if (slice[STEP] == 0) { + throw Py.ValueError("slice step cannot be zero"); + } + } + + if (getStart() == Py.None) { + slice[START] = slice[STEP] < 0 ? len - 1 : 0; + } else { + slice[START] = calculateSliceIndex(getStart()); + if(slice[START] < 0) { + slice[START] += len; + } + if(slice[START] < 0) { + slice[START] = slice[STEP] < 0 ? -1 : 0; + } + if(slice[START] >= len) { + slice[START] = slice[STEP] < 0 ? len - 1 : len; + } + } + if(getStop() == Py.None) { + slice[STOP] = slice[STEP] < 0 ? -1 : len; + } else { + slice[STOP] = calculateSliceIndex(getStop()); + if(slice[STOP] < 0) { + slice[STOP] += len; + } + if(slice[STOP] < 0) { + slice[STOP] = -1; + } + if(slice[STOP] > len) { + slice[STOP] = len; + } + } + return slice; + } + private static final int START = 0, STOP = 1, STEP = 2; + + public PyObject start = Py.None; + + public PyObject stop = Py.None; + + public PyObject step = Py.None; } Added: trunk/jython/src/org/python/core/PySliceDerived.java =================================================================== --- trunk/jython/src/org/python/core/PySliceDerived.java (rev 0) +++ trunk/jython/src/org/python/core/PySliceDerived.java 2007-12-02 07:11:17 UTC (rev 3749) @@ -0,0 +1,954 @@ +package org.python.core; + +public class PySliceDerived extends PySlice 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 PySliceDerived(PyType subtype) { + super(subtype); + 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__"+" should return a "+"string"); + } + 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__"+" should return a "+"string"); + } + 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__"+" should return a "+"string"); + } + 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__"+" should return a "+"string"); + } + 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__"+" should return a "+"float"); + } + return super.__float__(); + } + + public PyLong __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) + return(PyLong)res; + throw Py.TypeError("__long__"+" should return a "+"long"); + } + return super.__long__(); + } + + 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__"+" should return a "+"complex"); + } + 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 __pow__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__pow__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__pow__(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) + return impl.__get__(this,self_type).__call__(other); + return super.__iadd__(other); + } + + public PyObject __isub__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__isub__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(other); + return super.__isub__(other); + } + + public PyObject __imul__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__imul__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(other); + return super.__imul__(other); + } + + public PyObject __idiv__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__idiv__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(other); + return super.__idiv__(other); + } + + public PyObject __ifloordiv__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__ifloordiv__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(other); + return super.__ifloordiv__(other); + } + + public PyObject __itruediv__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__itruediv__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(other); + return super.__itruediv__(other); + } + + public PyObject __imod__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__imod__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(other); + return super.__imod__(other); + } + + public PyObject __ipow__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__ipow__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(other); + return super.__ipow__(other); + } + + public PyObject __ilshift__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__ilshift__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(other); + return super.__ilshift__(other); + } + + public PyObject __irshift__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__irshift__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(other); + return super.__irshift__(other); + } + + public PyObject __iand__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__iand__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(other); + return super.__iand__(other); + } + + public PyObject __ior__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__ior__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(other); + return super.__ior__(other); + } + + public PyObject __ixor__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__ixor__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(other); + 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 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__ should return a string"); + return((PyString)res).toString(); + } + return super.toString(); + } + + 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(); + throw Py.TypeError("__hash__ should return a int"); + } + if (self_type.lookup("__eq__")!=null||self_type.lookup("__cmp__")!=null) + throw Py.TypeError("unhashable type"); + 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(); + PyObject impl=self_type.lookup("__cmp__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res instanceof PyInteger) { + int v=((PyInteger)res).getValue(); + return v<0?-1:v>0?1:0; + } + throw Py.TypeError("__cmp__ should return a int"); + } + return super.__cmp__(other); + } + + 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__(); + } + return impl.__get__(this,self_type).__call__().__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 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) { // ??? + PyType self_type=getType(); + PyObject impl=self_type.lookup("__getslice__"); + if (impl!=null) + try { + return impl.__get__(this,self_type).__call__(start,stop); + } catch (PyException exc) { + if (Py.matchException(exc,Py.LookupError)) + return null; + throw exc; + } + return super.__getslice__(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__(String name) { + PyType self_type=getType(); + PyObject getattribute=self_type.lookup("__getattribute__"); + PyString py_name=null; + try { + if (getattribute!=null) { + return getattribute.__get__(this,self_type).__call__(py_name=new PyString(name)); + } else { + return super.__findattr__(name); + } + } catch (PyException e) { + if (Py.matchException(e,Py.AttributeError)) { + PyObject getattr=self_type.lookup("__getattr__"); + if (getattr!=null) + try { + return getattr.__get__(this,self_type).__call__(py_name!=null?py_name:new PyString(name)); + } catch (PyException e1) { + if (!Py.matchException(e1,Py.AttributeError)) + throw e1; + } + return null; + } + throw e; + } + } + + 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__(new PyString(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__(new PyString(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 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) + impl.__get__(this,self_type).__call__(args,keywords); + } + } + +} Modified: trunk/jython/src/org/python/core/__builtin__.java =================================================================== --- trunk/jython/src/org/python/core/__builtin__.java 2007-12-02 05:22:32 UTC (rev 3748) +++ trunk/jython/src/org/python/core/__builtin__.java 2007-12-02 07:11:17 UTC (rev 3749) @@ -101,8 +101,6 @@ return __builtin__.repr(arg1); case 38: return __builtin__.round(Py.py2double(arg1)); - case 40: - return __builtin__.slice(arg1); case 41: return __builtin__.vars(arg1); case 42: @@ -164,8 +162,6 @@ return __builtin__.reduce(arg1, arg2); case 38: return __builtin__.round(Py.py2double(arg1), Py.py2int(arg2)); - case 40: - return __builtin__.slice(arg1, arg2); case 42: return __builtin__.xrange(Py.py2int(arg1), Py.py2int(arg2)); case 29: @@ -214,8 +210,6 @@ case 39: __builtin__.setattr(arg1, asString(arg2, "setattr(): attribute name must be string"), arg3); return Py.None; - case 40: - return __builtin__.slice(arg1, arg2, arg3); case 42: return __builtin__.xrange(Py.py2int(arg1), Py.py2int(arg2), Py.py2int(arg3)); case 44: @@ -330,6 +324,7 @@ dict.__setitem__("basestring", PyType.fromClass(PyBaseString.class)); dict.__setitem__("file", PyType.fromClass(PyFile.class)); dict.__setitem__("open", PyType.fromClass(PyFile.class)); + dict.__setitem__("slice", PyType.fromClass(PySlice.class)); /* - */ @@ -384,7 +379,6 @@ dict.__setitem__("repr", new BuiltinFunctions("repr", 37, 1)); dict.__setitem__("round", new BuiltinFunctions("round", 38, 1, 2)); dict.__setitem__("setattr", new BuiltinFunctions("setattr", 39, 3)); - dict.__setitem__("slice", new BuiltinFunctions("slice", 40, 1, 3)); dict.__setitem__("vars", new BuiltinFunctions("vars", 41, 0, 1)); dict.__setitem__("xrange", new BuiltinFunctions("xrange", 42, 1, 3)); dict.__setitem__("zip", new BuiltinFunctions("zip", 43, 1, -1)); @@ -1023,18 +1017,6 @@ o.__setattr__(n, v); } - public static PySlice slice(PyObject start, PyObject stop, PyObject step) { - return new PySlice(start, stop, step); - } - - public static PySlice slice(PyObject start, PyObject stop) { - return slice(start, stop, Py.None); - } - - public static PySlice slice(PyObject stop) { - return slice(Py.None, stop, Py.None); - } - public static PyObject sum(PyObject seq, PyObject result) { if (result instanceof PyString) { Modified: trunk/jython/src/templates/mappings =================================================================== --- trunk/jython/src/templates/mappings 2007-12-02 05:22:32 UTC (rev 3748) +++ trunk/jython/src/templates/mappings 2007-12-02 07:11:17 UTC (rev 3749) @@ -45,6 +45,8 @@ set.expose:org.python.modules.sets.PySet #gderived.py doesn't handle derived out of org.python.core #set.derived:org.python.modules.sets.PySetDerived +slice.expose:org.python.core.PySlice +slice.derived:org.python.core.PySliceDerived str.expose:org.python.core.PyString str.derived:org.python.core.PyStringDerived super.derived:org.python.core.PySuperDerived Added: trunk/jython/src/templates/slice.derived =================================================================== --- trunk/jython/src/templates/slice.derived (rev 0) +++ trunk/jython/src/templates/slice.derived 2007-12-02 07:11:17 UTC (rev 3749) @@ -0,0 +1,5 @@ +base_class: PySlice +want_dict: true +ctr: +incl: object + Added: trunk/jython/src/templates/slice.expose =================================================================== --- trunk/jython/src/templates/slice.expose (rev 0) +++ trunk/jython/src/templates/slice.expose 2007-12-02 07:11:17 UTC (rev 3749) @@ -0,0 +1,17 @@ +# setup +type_name: slice +type_class: PySlice + +# getsets +expose_getset: start getStart +expose_getset: stop getStop +expose_getset: step getStep + +# exposed methods +expose_meth: indices o +expose_meth: __hash__ + throw Py.TypeError("unhashable type"); +expose_wide_meth: __init__ -1 -1 + `vdeleg`(init); + `void; +expose_new_mutable: This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |