Index: PyTuple.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/PyTuple.java,v retrieving revision 2.12 diff -u -r2.12 PyTuple.java --- PyTuple.java 23 Feb 2005 04:26:21 -0000 2.12 +++ PyTuple.java 10 Mar 2005 02:27:39 -0000 @@ -40,14 +40,281 @@ public PyObject[] list; /** Internal use only. Do not call this method explicit. */ - public static void classDictInit(PyObject dict) { - PySequence.classDictInit(dict); - dict.__setitem__("__len__", new TupleFunctions("__len__", 3, 0)); - dict.__setitem__("__add__", new TupleFunctions("__add__", 15, 1)); - // hide these from Python! - dict.__setitem__("classDictInit", null); - dict.__setitem__("toString", null); - dict.__setitem__("hashCode", null); + public static void classDictInit(PyObject dict)throws PyIgnoreMethodTag {} + /* type info */ + + public static final String exposed_name="tuple"; + + public static void typeSetup(PyObject dict,PyType.Newstyle marker) { + class exposed___ne__ extends PyBuiltinFunctionNarrow { + + private PyTuple self; + + public PyObject getSelf() { + return self; + } + + exposed___ne__(PyTuple self,PyBuiltinFunction.Info info) { + super(info); + this.self=self; + } + + public PyBuiltinFunction makeBound(PyObject self) { + return new exposed___ne__((PyTuple)self,info); + } + + public PyObject __call__(PyObject arg0) { + PyObject ret=self.tuple___ne__(arg0); + if (ret==null) + return Py.NotImplemented; + return ret; + } + + public PyObject inst_call(PyObject gself,PyObject arg0) { + PyTuple self=(PyTuple)gself; + PyObject ret=self.tuple___ne__(arg0); + if (ret==null) + return Py.NotImplemented; + return ret; + } + + } + dict.__setitem__("__ne__",new PyMethodDescr("__ne__",PyTuple.class,1,1,new exposed___ne__(null,null))); + class exposed___eq__ extends PyBuiltinFunctionNarrow { + + private PyTuple self; + + public PyObject getSelf() { + return self; + } + + exposed___eq__(PyTuple self,PyBuiltinFunction.Info info) { + super(info); + this.self=self; + } + + public PyBuiltinFunction makeBound(PyObject self) { + return new exposed___eq__((PyTuple)self,info); + } + + public PyObject __call__(PyObject arg0) { + PyObject ret=self.tuple___eq__(arg0); + if (ret==null) + return Py.NotImplemented; + return ret; + } + + public PyObject inst_call(PyObject gself,PyObject arg0) { + PyTuple self=(PyTuple)gself; + PyObject ret=self.tuple___eq__(arg0); + if (ret==null) + return Py.NotImplemented; + return ret; + } + + } + dict.__setitem__("__eq__",new PyMethodDescr("__eq__",PyTuple.class,1,1,new exposed___eq__(null,null))); + class exposed___contains__ extends PyBuiltinFunctionNarrow { + + private PyTuple self; + + public PyObject getSelf() { + return self; + } + + exposed___contains__(PyTuple self,PyBuiltinFunction.Info info) { + super(info); + this.self=self; + } + + public PyBuiltinFunction makeBound(PyObject self) { + return new exposed___contains__((PyTuple)self,info); + } + + public PyObject __call__(PyObject arg0) { + return Py.newBoolean(self.tuple___contains__(arg0)); + } + + public PyObject inst_call(PyObject gself,PyObject arg0) { + PyTuple self=(PyTuple)gself; + return Py.newBoolean(self.tuple___contains__(arg0)); + } + + } + dict.__setitem__("__contains__",new PyMethodDescr("__contains__",PyTuple.class,1,1,new exposed___contains__(null,null))); + class exposed___len__ extends PyBuiltinFunctionNarrow { + + private PyTuple self; + + public PyObject getSelf() { + return self; + } + + exposed___len__(PyTuple self,PyBuiltinFunction.Info info) { + super(info); + this.self=self; + } + + public PyBuiltinFunction makeBound(PyObject self) { + return new exposed___len__((PyTuple)self,info); + } + + public PyObject __call__() { + return Py.newInteger(self.tuple___len__()); + } + + public PyObject inst_call(PyObject gself) { + PyTuple self=(PyTuple)gself; + return Py.newInteger(self.tuple___len__()); + } + + } + dict.__setitem__("__len__",new PyMethodDescr("__len__",PyTuple.class,0,0,new exposed___len__(null,null))); + class exposed___add__ extends PyBuiltinFunctionNarrow { + + private PyTuple self; + + public PyObject getSelf() { + return self; + } + + exposed___add__(PyTuple self,PyBuiltinFunction.Info info) { + super(info); + this.self=self; + } + + public PyBuiltinFunction makeBound(PyObject self) { + return new exposed___add__((PyTuple)self,info); + } + + public PyObject __call__(PyObject arg0) { + return self.tuple___add__(arg0); + } + + public PyObject inst_call(PyObject gself,PyObject arg0) { + PyTuple self=(PyTuple)gself; + return self.tuple___add__(arg0); + } + + } + dict.__setitem__("__add__",new PyMethodDescr("__add__",PyTuple.class,1,1,new exposed___add__(null,null))); + class exposed___mul__ extends PyBuiltinFunctionNarrow { + + private PyTuple self; + + public PyObject getSelf() { + return self; + } + + exposed___mul__(PyTuple self,PyBuiltinFunction.Info info) { + super(info); + this.self=self; + } + + public PyBuiltinFunction makeBound(PyObject self) { + return new exposed___mul__((PyTuple)self,info); + } + + public PyObject __call__(PyObject arg0) { + return self.tuple___mul__(arg0); + } + + public PyObject inst_call(PyObject gself,PyObject arg0) { + PyTuple self=(PyTuple)gself; + return self.tuple___mul__(arg0); + } + + } + dict.__setitem__("__mul__",new PyMethodDescr("__mul__",PyTuple.class,1,1,new exposed___mul__(null,null))); + class exposed___rmul__ extends PyBuiltinFunctionNarrow { + + private PyTuple self; + + public PyObject getSelf() { + return self; + } + + exposed___rmul__(PyTuple self,PyBuiltinFunction.Info info) { + super(info); + this.self=self; + } + + public PyBuiltinFunction makeBound(PyObject self) { + return new exposed___rmul__((PyTuple)self,info); + } + + public PyObject __call__(PyObject arg0) { + return self.tuple___rmul__(arg0); + } + + public PyObject inst_call(PyObject gself,PyObject arg0) { + PyTuple self=(PyTuple)gself; + return self.tuple___rmul__(arg0); + } + + } + dict.__setitem__("__rmul__",new PyMethodDescr("__rmul__",PyTuple.class,1,1,new exposed___rmul__(null,null))); + class exposed___hash__ extends PyBuiltinFunctionNarrow { + + private PyTuple self; + + public PyObject getSelf() { + return self; + } + + exposed___hash__(PyTuple self,PyBuiltinFunction.Info info) { + super(info); + this.self=self; + } + + public PyBuiltinFunction makeBound(PyObject self) { + return new exposed___hash__((PyTuple)self,info); + } + + public PyObject __call__() { + return Py.newInteger(self.tuple_hashCode()); + } + + public PyObject inst_call(PyObject gself) { + PyTuple self=(PyTuple)gself; + return Py.newInteger(self.tuple_hashCode()); + } + + } + dict.__setitem__("__hash__",new PyMethodDescr("__hash__",PyTuple.class,0,0,new exposed___hash__(null,null))); + class exposed___repr__ extends PyBuiltinFunctionNarrow { + + private PyTuple self; + + public PyObject getSelf() { + return self; + } + + exposed___repr__(PyTuple self,PyBuiltinFunction.Info info) { + super(info); + this.self=self; + } + + public PyBuiltinFunction makeBound(PyObject self) { + return new exposed___repr__((PyTuple)self,info); + } + + public PyObject __call__() { + return new PyString(self.tuple_toString()); + } + + public PyObject inst_call(PyObject gself) { + PyTuple self=(PyTuple)gself; + return new PyString(self.tuple_toString()); + } + + } + dict.__setitem__("__repr__",new PyMethodDescr("__repr__",PyTuple.class,0,0,new exposed___repr__(null,null))); + dict.__setitem__("__new__",new PyNewWrapper(PyTuple.class,"__new__",-1,-1) { + public PyObject new_impl(boolean init,PyType subtype,PyObject[]args,String[]keywords) { + return tuple_new(this,init,subtype,args,keywords); + } + }); } public PyTuple() { @@ -58,6 +325,24 @@ list = elements; } + final static PyObject tuple_new(PyObject new_, boolean init, PyType subtype, + PyObject[] args, String[] keywords) { + ArgParser ap = new ArgParser("tuple", args, keywords, new String[] { "S" }, 0); + PyObject S = ap.getPyObject(0, null); + if (S == null) + return new PyTuple(); + if (S instanceof PyTuple) + return S; + PyObject iter = S.__iter__(); + PyObject [] a = new PyObject[S.__len__()]; + int i = 0; + for (PyObject item = null; (item = iter.__iternext__()) != null; ) { + a[i] = item; + i++; + } + return new PyTuple(a); + } + public String safeRepr() throws PyIgnoreMethodTag { return "'tuple' object"; } @@ -94,10 +379,30 @@ } public int __len__() { + return tuple___len__(); + } + + final int tuple___len__() { return list.length; } + final boolean tuple___contains__(PyObject o) { + return super.__contains__(o); + } + + final PyObject tuple___ne__(PyObject o) { + return super.__ne__(o); + } + + final PyObject tuple___eq__(PyObject o) { + return super.__eq__(o); + } + public PyObject __add__(PyObject generic_other) { + return tuple___add__(generic_other); + } + + final PyObject tuple___add__(PyObject generic_other) { if (generic_other instanceof PyTuple) { PyTuple other = (PyTuple)generic_other; PyObject new_list[] = new PyObject[list.length+other.list.length]; @@ -110,7 +415,25 @@ return null; } + final PyObject tuple___mul__(PyObject o) { + if (!(o instanceof PyInteger || o instanceof PyLong)) + throw Py.TypeError("can't multiply sequence to non-int"); + int count = o.__int__().getValue(); + return repeat(count); + } + + final PyObject tuple___rmul__(PyObject o) { + if (!(o instanceof PyInteger || o instanceof PyLong)) + throw Py.TypeError("can't multiply sequence to non-int"); + int count = o.__int__().getValue(); + return repeat(count); + } + public int hashCode() { + return tuple_hashCode(); + } + + final int tuple_hashCode() { int x, y; int len = list.length; x = 0x345678; @@ -145,6 +468,10 @@ } public String toString() { + return tuple_toString(); + } + + final String tuple_toString() { StringBuffer buf = new StringBuffer("("); for (int i = 0; i < list.length-1; i++) { buf.append(subobjRepr(list[i])); Index: __builtin__.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/__builtin__.java,v retrieving revision 2.63 diff -u -r2.63 __builtin__.java --- __builtin__.java 1 Mar 2005 04:06:03 -0000 2.63 +++ __builtin__.java 10 Mar 2005 02:28:23 -0000 @@ -115,6 +115,7 @@ dict.__setitem__("complex", PyType.fromClass(PyComplex.class)); dict.__setitem__("dict", PyType.fromClass(PyDictionary.class)); dict.__setitem__("list", PyType.fromClass(PyList.class)); + dict.__setitem__("tuple", PyType.fromClass(PyTuple.class)); dict.__setitem__("property", PyType.fromClass(PyProperty.class)); dict.__setitem__("staticmethod", PyType.fromClass(PyStaticMethod.class)); @@ -140,7 +141,6 @@ dict.__setitem__("globals", new BuiltinFunctions("globals", 4, 0)); dict.__setitem__("hash", new BuiltinFunctions("hash", 5, 1)); dict.__setitem__("cmp", new BuiltinFunctions("cmp", 6, 2)); - dict.__setitem__("tuple", new BuiltinFunctions("tuple", 8, 1)); dict.__setitem__("apply", new BuiltinFunctions("apply", 9, 2, 3)); dict.__setitem__("isinstance", new BuiltinFunctions("isinstance", 10, 2));