Index: src/org/python/core/PyDictionary.java =================================================================== --- src/org/python/core/PyDictionary.java (revision 3803) +++ src/org/python/core/PyDictionary.java (working copy) @@ -626,7 +626,7 @@ } } - final void dict_init(PyObject[] args,String[] kwds) { + protected void dict_init(PyObject[] args,String[] kwds) { int nargs = args.length - kwds.length; if (nargs > 1) throw PyBuiltinFunction.DefaultInfo.unexpectedCall( @@ -785,10 +785,13 @@ return dict___eq__(ob_other); } - final PyObject dict___eq__(PyObject ob_other) { - if (ob_other.getType() != getType()) + final PyObject dict___eq__(PyObject ob_other) { + PyType thisType = getType(); + PyType otherType = ob_other.getType(); + if (otherType != thisType && !thisType.isSubType(otherType) + && !otherType.isSubType(thisType)) { return null; - + } PyDictionary other = (PyDictionary)ob_other; int an = table.size(); int bn = other.table.size(); @@ -855,9 +858,12 @@ } final int dict___cmp__(PyObject ob_other) { - if (ob_other.getType() != getType()) + PyType thisType = getType(); + PyType otherType = ob_other.getType(); + if (otherType != thisType && !thisType.isSubType(otherType) + && !otherType.isSubType(thisType)) { return -2; - + } PyDictionary other = (PyDictionary)ob_other; int an = table.size(); int bn = other.table.size(); Index: src/org/python/core/PyException.java =================================================================== --- src/org/python/core/PyException.java (revision 3803) +++ src/org/python/core/PyException.java (working copy) @@ -37,16 +37,17 @@ (!(value instanceof PyInstance && __builtin__.isinstance(value, type)))) { - //System.out.println("value: "+value); if (value instanceof PyTuple) { - value = ((PyClass)type).__call__( - ((PyTuple)value).getArray()); + if (type == Py.KeyError) { + value = ((PyClass)type).__call__(new PyObject[] {value}); + } else { + value = ((PyClass)type).__call__(((PyTuple)value).getArray()); + } } else { if (value == Py.None) { value = ((PyClass)type).__call__(Py.EmptyObjects); } else { - value = ((PyClass)type).__call__( - new PyObject[] {value}); + value = ((PyClass)type).__call__(new PyObject[] {value}); } } } Index: src/org/python/core/exceptions.java =================================================================== --- src/org/python/core/exceptions.java (revision 3803) +++ src/org/python/core/exceptions.java (working copy) @@ -269,7 +269,10 @@ ArgParser ap = new ArgParser("__init__", arg, kws, "self", "args"); PyObject self = ap.getPyObject(0); PyObject args = ap.getList(1); - + + if (arg.length == 2) { + self.__setattr__("message", ap.getPyObject(1)); + } self.__setattr__("args", args); } Index: src/org/python/core/Py.java =================================================================== --- src/org/python/core/Py.java (revision 3803) +++ src/org/python/core/Py.java (working copy) @@ -162,7 +162,10 @@ public static PyException KeyError(String message) { return new PyException(Py.KeyError, message); } - + public static PyException KeyError(PyObject key) { + return new PyException(Py.KeyError, key); + } + public static PyObject AssertionError; public static PyException AssertionError(String message) { return new PyException(Py.AssertionError, message); Index: src/org/python/modules/Setup.java =================================================================== --- src/org/python/modules/Setup.java (revision 3803) +++ src/org/python/modules/Setup.java (working copy) @@ -56,6 +56,7 @@ "_random:org.python.modules.random.RandomModule", "cmath", "itertools", - "zipimport:org.python.modules.zipimport.zipimport" + "zipimport:org.python.modules.zipimport.zipimport", + "collections:org.python.modules.collections.Collections" }; } Index: src/org/python/modules/collections/PyDequeDerived.java =================================================================== --- src/org/python/modules/collections/PyDequeDerived.java (revision 0) +++ src/org/python/modules/collections/PyDequeDerived.java (revision 0) @@ -0,0 +1,955 @@ +package org.python.modules.collections; +import org.python.core.*; + +public class PyDequeDerived extends PyDeque 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 PyDequeDerived(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); + } + } + +} Index: src/org/python/modules/collections/PyDefaultDict.java =================================================================== --- src/org/python/modules/collections/PyDefaultDict.java (revision 0) +++ src/org/python/modules/collections/PyDefaultDict.java (revision 0) @@ -0,0 +1,283 @@ +/** + * PyDefaultDict.java - This is a subclass of the builtin dict(PyDictionary) class. + * It supports one additional method __missing__ and adds one writable instance + * variable default_factory. The remaining functionality is the same as for the dict + * class. + * + * collections.defaultdict([default_factory[, ...]]) - returns a new dictionary-like + * object. The first argument provides the initial value for the default_factory + * attribute; it defaults to None. All remaining arguments are treated the same as + * if they were passed to the dict constructor, including keyword arguments. + * + * @author Mehendran T (mehendran@gmail.com) + * Novell Software Development (I) Pvt. Ltd + * @created Tue 18-Sep-2007 21:09:09 + */ +package org.python.modules.collections; + +import org.python.core.PyBuiltinFunction; +import org.python.core.PyBuiltinMethod; +import org.python.core.PyBuiltinMethodNarrow; +import org.python.core.PyException; +import org.python.core.PyMethodDescr; +import org.python.core.PyGetSetDescr; +import org.python.core.PyNewWrapper; +import org.python.core.PyDictionary; +import org.python.core.PyObject; +import org.python.core.PyType; +import org.python.core.Py; +import org.python.core.PyTuple; +import java.util.Hashtable; +import org.python.core.PyString; +import org.python.core.PyFunction; +import org.python.core.__builtin__; + +public class PyDefaultDict extends PyDictionary { + + //~ BEGIN GENERATED REGION -- DO NOT EDIT SEE gexpose.py + /* type info */ + + public static final String exposed_name="defaultdict"; + + public static void typeSetup(PyObject dict,PyType.Newstyle marker) { + dict.__setitem__("default_factory",new PyGetSetDescr("default_factory",PyDefaultDict.class,"getDefaultFactory","setDefaultFactory",null)); + class exposed___getitem__ extends PyBuiltinMethodNarrow { + + exposed___getitem__(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed___getitem__(self,info); + } + + public PyObject __call__(PyObject arg0) { + return((PyDefaultDict)self).defaultdict___getitem__(arg0); + } + + } + dict.__setitem__("__getitem__",new PyMethodDescr("__getitem__",PyDefaultDict.class,1,1,new exposed___getitem__(null,null))); + class exposed___missing__ extends PyBuiltinMethodNarrow { + + exposed___missing__(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed___missing__(self,info); + } + + public PyObject __call__(PyObject arg0) { + return((PyDefaultDict)self).defaultdict___missing__(arg0); + } + + } + dict.__setitem__("__missing__",new PyMethodDescr("__missing__",PyDefaultDict.class,1,1,new exposed___missing__(null,null))); + class exposed___reduce__ extends PyBuiltinMethodNarrow { + + exposed___reduce__(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed___reduce__(self,info); + } + + public PyObject __call__() { + return((PyDefaultDict)self).defaultdict___reduce__(); + } + + } + dict.__setitem__("__reduce__",new PyMethodDescr("__reduce__",PyDefaultDict.class,0,0,new exposed___reduce__(null,null))); + class exposed_copy extends PyBuiltinMethodNarrow { + + exposed_copy(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed_copy(self,info); + } + + public PyObject __call__() { + return((PyDefaultDict)self).defaultdict_copy(); + } + + } + dict.__setitem__("copy",new PyMethodDescr("copy",PyDefaultDict.class,0,0,new exposed_copy(null,null))); + class exposed___copy__ extends PyBuiltinMethodNarrow { + + exposed___copy__(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed___copy__(self,info); + } + + public PyObject __call__() { + return((PyDefaultDict)self).defaultdict___copy__(); + } + + } + dict.__setitem__("__copy__",new PyMethodDescr("__copy__",PyDefaultDict.class,0,0,new exposed___copy__(null,null))); + class exposed___repr__ extends PyBuiltinMethodNarrow { + + exposed___repr__(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed___repr__(self,info); + } + + public PyObject __call__() { + return new PyString(((PyDefaultDict)self).defaultdict_toString()); + } + + } + dict.__setitem__("__repr__",new PyMethodDescr("__repr__",PyDefaultDict.class,0,0,new exposed___repr__(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) { + ((PyDefaultDict)self).defaultdict_init(args,keywords); + return Py.None; + } + + } + dict.__setitem__("__init__",new PyMethodDescr("__init__",PyDefaultDict.class,-1,-1,new exposed___init__(null,null))); + dict.__setitem__("__new__",new PyNewWrapper(PyDefaultDict.class,"__new__",-1,-1) { + + public PyObject new_impl(boolean init,PyType subtype,PyObject[]args,String[]keywords) { + PyDefaultDict newobj; + if (for_type==subtype) { + newobj=new PyDefaultDict(); + if (init) + newobj.defaultdict_init(args,keywords); + } else { + newobj=new PyDefaultDictDerived(subtype); + } + return newobj; + } + + }); + } + //~ END GENERATED REGION -- DO NOT EDIT SEE gexpose.py + + private static final PyType DEFAULTDICT_TYPE = PyType.fromClass(PyDefaultDict.class); + + /** + * This attribute is used by the __missing__ method; it is initialized from + * the first argument to the constructor, if present, or to None, if absent. + */ + private PyObject default_factory = Py.None; + + public PyDefaultDict() { + this(DEFAULTDICT_TYPE); + } + + public PyDefaultDict(PyType subtype) { + super(subtype); + } + + final void defaultdict_init(PyObject[] args, String[] kwds) { + int nargs = args.length - kwds.length; + if (nargs != 0) { + default_factory = args[0]; + if (default_factory.__findattr__("__call__") == null) { + throw Py.TypeError("first argument must be callable"); + } + PyObject newargs[] = new PyObject[args.length - 1]; + System.arraycopy(args, 1, newargs, 0, newargs.length); + dict_init(newargs , kwds); + } + } + + public PyObject getDefaultFactory() { + return default_factory; + } + + public void setDefaultFactory(PyObject value) { + default_factory = value; + } + + final PyObject defaultdict___getitem__(PyObject key) { + PyObject val = super.__finditem__(key); + if (val == null) { + val = defaultdict___missing__(key); + } + return val; + } + + public PyObject __finditem__(PyObject key) { + return defaultdict___getitem__(key); + } + + /** + * This method is called by the __getitem__ method of the dict class when + * the requested key is not found; whatever it returns or raises is then + * returned or raised by __getitem__. + */ + final PyObject defaultdict___missing__(PyObject key) { + if (default_factory == Py.None) { + throw Py.KeyError(key); + } + PyObject value = default_factory.__call__(); + if (value == null) { + return value; + } + __setitem__(key, value); + return value; + } + + public PyObject __reduce__() { + return defaultdict___reduce__(); + } + + final PyObject defaultdict___reduce__() { + PyTuple args = null; + if (default_factory == Py.None) { + args = new PyTuple(); + } else { + PyObject[] ob = {default_factory}; + args = new PyTuple(ob); + } + return new PyTuple(new PyObject[]{this.getType(), args, Py.None, + Py.None, this.items()}); + } + + public PyDictionary copy() { + return defaultdict_copy(); + } + + final PyDefaultDict defaultdict_copy() { + return defaultdict___copy__(); + } + + final PyDefaultDict defaultdict___copy__() { + PyDefaultDict ob = new PyDefaultDict(); + ob.default_factory = default_factory; + ob.table = (Hashtable)table.clone(); + return ob; + } + + public String toString() { + return defaultdict_toString(); + } + + final String defaultdict_toString() { + return "defaultdict(" + default_factory +", " + super.toString() + ")"; + } +} Index: src/org/python/modules/collections/PyDefaultDictDerived.java =================================================================== --- src/org/python/modules/collections/PyDefaultDictDerived.java (revision 0) +++ src/org/python/modules/collections/PyDefaultDictDerived.java (revision 0) @@ -0,0 +1,955 @@ +package org.python.modules.collections; +import org.python.core.*; + +public class PyDefaultDictDerived extends PyDefaultDict 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 PyDefaultDictDerived(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); + } + } + +} Index: src/org/python/modules/collections/PyDeque.java =================================================================== --- src/org/python/modules/collections/PyDeque.java (revision 0) +++ src/org/python/modules/collections/PyDeque.java (revision 0) @@ -0,0 +1,976 @@ +/** + * PyDeque.java - This class implements the functionalities of Deque data structure. + * Deques are a generalization of stacks and queues (the name is pronounced “deck” + * and is short for “double-ended queue”). Deques support thread-safe, memory + * efficient appends and pops from either side of the deque with approximately the + * same O(1) performance in either direction. + * + * Though list objects support similar operations, they are optimized for fast + * fixed-length operations and incur O(n) memory movement costs for pop(0) and + * insert(0, v) operations which change both the size and position of the underlying + * data representation. + * + * collections.deque([iterable]) - returns a new deque object initialized left-to-right + * (using append()) with data from iterable. If iterable is not specified, the new + * deque is empty. + * + * @author Mehendran T (mehendran@gmail.com) + * Novell Software Development (I) Pvt. Ltd + * @created Mon 10-Sep-2007 19:54:27 + */ +package org.python.modules.collections; + +import org.python.core.PyIterator; +import org.python.core.PyObject; +import org.python.core.PySequenceIter; +import org.python.core.PyTuple; +import org.python.core.PyType; +import org.python.core.Py; +import org.python.core.PyException; +import org.python.core.PyInteger; +import org.python.core.PyLong; +import org.python.core.PyNewWrapper; +import org.python.core.PyMethodDescr; +import org.python.core.PyBuiltinFunction; +import org.python.core.PyBuiltinMethod; +import org.python.core.PyBuiltinMethodNarrow; +import org.python.core.PyString; +import org.python.core.ThreadState; + +public class PyDeque extends PyObject { + + //~ BEGIN GENERATED REGION -- DO NOT EDIT SEE gexpose.py + /* type info */ + + public static final String exposed_name="deque"; + + public static void typeSetup(PyObject dict,PyType.Newstyle marker) { + class exposed_append extends PyBuiltinMethodNarrow { + + exposed_append(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed_append(self,info); + } + + public PyObject __call__(PyObject arg0) { + ((PyDeque)self).deque_append(arg0); + return Py.None; + } + + } + dict.__setitem__("append",new PyMethodDescr("append",PyDeque.class,1,1,new exposed_append(null,null))); + class exposed_appendleft extends PyBuiltinMethodNarrow { + + exposed_appendleft(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed_appendleft(self,info); + } + + public PyObject __call__(PyObject arg0) { + ((PyDeque)self).deque_appendleft(arg0); + return Py.None; + } + + } + dict.__setitem__("appendleft",new PyMethodDescr("appendleft",PyDeque.class,1,1,new exposed_appendleft(null,null))); + class exposed_pop extends PyBuiltinMethodNarrow { + + exposed_pop(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed_pop(self,info); + } + + public PyObject __call__() { + return((PyDeque)self).deque_pop(); + } + + } + dict.__setitem__("pop",new PyMethodDescr("pop",PyDeque.class,0,0,new exposed_pop(null,null))); + class exposed_popleft extends PyBuiltinMethodNarrow { + + exposed_popleft(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed_popleft(self,info); + } + + public PyObject __call__() { + return((PyDeque)self).deque_popleft(); + } + + } + dict.__setitem__("popleft",new PyMethodDescr("popleft",PyDeque.class,0,0,new exposed_popleft(null,null))); + class exposed_extend extends PyBuiltinMethodNarrow { + + exposed_extend(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed_extend(self,info); + } + + public PyObject __call__(PyObject arg0) { + ((PyDeque)self).deque_extend(arg0); + return Py.None; + } + + } + dict.__setitem__("extend",new PyMethodDescr("extend",PyDeque.class,1,1,new exposed_extend(null,null))); + class exposed_extendleft extends PyBuiltinMethodNarrow { + + exposed_extendleft(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed_extendleft(self,info); + } + + public PyObject __call__(PyObject arg0) { + ((PyDeque)self).deque_extendleft(arg0); + return Py.None; + } + + } + dict.__setitem__("extendleft",new PyMethodDescr("extendleft",PyDeque.class,1,1,new exposed_extendleft(null,null))); + class exposed_remove extends PyBuiltinMethodNarrow { + + exposed_remove(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed_remove(self,info); + } + + public PyObject __call__(PyObject arg0) { + ((PyDeque)self).deque_remove(arg0); + return Py.None; + } + + } + dict.__setitem__("remove",new PyMethodDescr("remove",PyDeque.class,1,1,new exposed_remove(null,null))); + class exposed_clear extends PyBuiltinMethodNarrow { + + exposed_clear(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed_clear(self,info); + } + + public PyObject __call__() { + ((PyDeque)self).deque_clear(); + return Py.None; + } + + } + dict.__setitem__("clear",new PyMethodDescr("clear",PyDeque.class,0,0,new exposed_clear(null,null))); + class exposed_rotate extends PyBuiltinMethodNarrow { + + exposed_rotate(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed_rotate(self,info); + } + + public PyObject __call__(PyObject arg0) { + ((PyDeque)self).deque_rotate(arg0); + return Py.None; + } + + public PyObject __call__() { + ((PyDeque)self).deque_rotate(); + return Py.None; + } + + } + dict.__setitem__("rotate",new PyMethodDescr("rotate",PyDeque.class,0,1,new exposed_rotate(null,null))); + class exposed___getitem__ extends PyBuiltinMethodNarrow { + + exposed___getitem__(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed___getitem__(self,info); + } + + public PyObject __call__(PyObject arg0) { + return((PyDeque)self).deque___getitem__(arg0); + } + + } + dict.__setitem__("__getitem__",new PyMethodDescr("__getitem__",PyDeque.class,1,1,new exposed___getitem__(null,null))); + class exposed___setitem__ extends PyBuiltinMethodNarrow { + + exposed___setitem__(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed___setitem__(self,info); + } + + public PyObject __call__(PyObject arg0,PyObject arg1) { + ((PyDeque)self).deque___setitem__(arg0,arg1); + return Py.None; + } + + } + dict.__setitem__("__setitem__",new PyMethodDescr("__setitem__",PyDeque.class,2,2,new exposed___setitem__(null,null))); + class exposed___delitem__ extends PyBuiltinMethodNarrow { + + exposed___delitem__(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed___delitem__(self,info); + } + + public PyObject __call__(PyObject arg0) { + ((PyDeque)self).deque___delitem__(arg0); + return Py.None; + } + + } + dict.__setitem__("__delitem__",new PyMethodDescr("__delitem__",PyDeque.class,1,1,new exposed___delitem__(null,null))); + class exposed___len__ extends PyBuiltinMethodNarrow { + + exposed___len__(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed___len__(self,info); + } + + public PyObject __call__() { + return Py.newInteger(((PyDeque)self).deque___len__()); + } + + } + dict.__setitem__("__len__",new PyMethodDescr("__len__",PyDeque.class,0,0,new exposed___len__(null,null))); + class exposed___repr__ extends PyBuiltinMethodNarrow { + + exposed___repr__(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed___repr__(self,info); + } + + public PyObject __call__() { + return new PyString(((PyDeque)self).deque_toString()); + } + + } + dict.__setitem__("__repr__",new PyMethodDescr("__repr__",PyDeque.class,0,0,new exposed___repr__(null,null))); + class exposed___iter__ extends PyBuiltinMethodNarrow { + + exposed___iter__(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed___iter__(self,info); + } + + public PyObject __call__() { + return((PyDeque)self).deque___iter__(); + } + + } + dict.__setitem__("__iter__",new PyMethodDescr("__iter__",PyDeque.class,0,0,new exposed___iter__(null,null))); + class exposed___eq__ extends PyBuiltinMethodNarrow { + + exposed___eq__(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed___eq__(self,info); + } + + public PyObject __call__(PyObject arg0) { + PyObject ret=((PyDeque)self).deque___eq__(arg0); + if (ret==null) + return Py.NotImplemented; + return ret; + } + + } + dict.__setitem__("__eq__",new PyMethodDescr("__eq__",PyDeque.class,1,1,new exposed___eq__(null,null))); + class exposed___ne__ extends PyBuiltinMethodNarrow { + + exposed___ne__(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed___ne__(self,info); + } + + public PyObject __call__(PyObject arg0) { + PyObject ret=((PyDeque)self).deque___ne__(arg0); + if (ret==null) + return Py.NotImplemented; + return ret; + } + + } + dict.__setitem__("__ne__",new PyMethodDescr("__ne__",PyDeque.class,1,1,new exposed___ne__(null,null))); + class exposed___lt__ extends PyBuiltinMethodNarrow { + + exposed___lt__(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed___lt__(self,info); + } + + public PyObject __call__(PyObject arg0) { + PyObject ret=((PyDeque)self).deque___lt__(arg0); + if (ret==null) + return Py.NotImplemented; + return ret; + } + + } + dict.__setitem__("__lt__",new PyMethodDescr("__lt__",PyDeque.class,1,1,new exposed___lt__(null,null))); + class exposed___le__ extends PyBuiltinMethodNarrow { + + exposed___le__(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed___le__(self,info); + } + + public PyObject __call__(PyObject arg0) { + PyObject ret=((PyDeque)self).deque___le__(arg0); + if (ret==null) + return Py.NotImplemented; + return ret; + } + + } + dict.__setitem__("__le__",new PyMethodDescr("__le__",PyDeque.class,1,1,new exposed___le__(null,null))); + class exposed___gt__ extends PyBuiltinMethodNarrow { + + exposed___gt__(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed___gt__(self,info); + } + + public PyObject __call__(PyObject arg0) { + PyObject ret=((PyDeque)self).deque___gt__(arg0); + if (ret==null) + return Py.NotImplemented; + return ret; + } + + } + dict.__setitem__("__gt__",new PyMethodDescr("__gt__",PyDeque.class,1,1,new exposed___gt__(null,null))); + class exposed___ge__ extends PyBuiltinMethodNarrow { + + exposed___ge__(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed___ge__(self,info); + } + + public PyObject __call__(PyObject arg0) { + PyObject ret=((PyDeque)self).deque___ge__(arg0); + if (ret==null) + return Py.NotImplemented; + return ret; + } + + } + dict.__setitem__("__ge__",new PyMethodDescr("__ge__",PyDeque.class,1,1,new exposed___ge__(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__() { + return new PyInteger(((PyDeque)self).deque_hashCode()); + } + + } + dict.__setitem__("__hash__",new PyMethodDescr("__hash__",PyDeque.class,0,0,new exposed___hash__(null,null))); + class exposed___reduce__ extends PyBuiltinMethodNarrow { + + exposed___reduce__(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed___reduce__(self,info); + } + + public PyObject __call__() { + return((PyDeque)self).deque___reduce__(); + } + + } + dict.__setitem__("__reduce__",new PyMethodDescr("__reduce__",PyDeque.class,0,0,new exposed___reduce__(null,null))); + class exposed___copy__ extends PyBuiltinMethodNarrow { + + exposed___copy__(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed___copy__(self,info); + } + + public PyObject __call__() { + return((PyDeque)self).deque___copy__(); + } + + } + dict.__setitem__("__copy__",new PyMethodDescr("__copy__",PyDeque.class,0,0,new exposed___copy__(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) { + ((PyDeque)self).deque_init(args,keywords); + return Py.None; + } + + } + dict.__setitem__("__init__",new PyMethodDescr("__init__",PyDeque.class,-1,-1,new exposed___init__(null,null))); + dict.__setitem__("__new__",new PyNewWrapper(PyDeque.class,"__new__",-1,-1) { + + public PyObject new_impl(boolean init,PyType subtype,PyObject[]args,String[]keywords) { + PyDeque newobj; + if (for_type==subtype) { + newobj=new PyDeque(); + if (init) + newobj.deque_init(args,keywords); + } else { + newobj=new PyDequeDerived(subtype); + } + return newobj; + } + + }); + } + //~ END GENERATED REGION -- DO NOT EDIT SEE gexpose.py + + private static final PyType DEQUE_TYPE = PyType.fromClass(PyDeque.class); + private int size = 0; + private Node header = new Node(null, null, null); + + public PyDeque() { + this(DEQUE_TYPE); + } + + public PyDeque(PyType subType) { + super(subType); + header.left = header.right = header; + } + + final void deque_init(PyObject[] args, String[] kwds) { + if (kwds.length > 0) { + throw Py.TypeError("deque() does not take keyword arguments"); + } + int nargs = args.length; + if (nargs > 1) { + throw PyBuiltinFunction.DefaultInfo.unexpectedCall(nargs, false, exposed_name, 0, 1); + } + if (nargs == 0) { + return; + } + PyObject data = args[0]; + + if (data.__findattr__("__iter__") != null) { + deque_extend(data); + } else { + try { + deque_extend(new PySequenceIter(data)); + } catch (PyException e) { + if(Py.matchException(e, Py.AttributeError)) { + throw Py.TypeError("'" + data.getType().fastGetName() + + "' object is not iterable"); + } + } + } + } + + /** + * Add obj to the right side of the deque. + */ + final void deque_append(PyObject obj) { + addBefore(obj, header); + } + + /** + * Add obj to the left side of the deque. + */ + final void deque_appendleft(PyObject obj) { + addBefore(obj, header.right); + } + + private Node addBefore(PyObject obj, Node node) { + Node newNode = new Node(obj, node, node.left); + newNode.left.right = newNode; + newNode.right.left = newNode; + size++; + return newNode; + } + + /** + * Remove all elements from the deque leaving it with length 0. + */ + final void deque_clear() { + Node node = header.right; + while (node != header) { + Node right = node.right; + node.left = null; + node.right = null; + node.data = null; + node = right; + } + header.right = header.left = header; + size = 0; + } + + /** + * Extend the right side of the deque by appending elements from the + * iterable argument. + */ + final void deque_extend(PyObject iterable) { + PyObject iter = iterable.__iter__(); + for (PyObject tmp = null; (tmp = iter.__iternext__()) != null; ) { + deque_append(tmp); + } + } + + /** + * Extend the left side of the deque by appending elements from iterable. + * Note, the series of left appends results in reversing the order of + * elements in the iterable argument. + */ + final void deque_extendleft(PyObject iterable) { + PyObject iter = iterable.__iter__(); + for (PyObject tmp = null; (tmp = iter.__iternext__()) != null; ) { + deque_appendleft(tmp); + } + } + + /** + * Remove and return an element from the right side of the deque. If no + * elements are present, raises an IndexError. + */ + final PyObject deque_pop() { + return removeNode(header.left); + } + + /** + * Remove and return an element from the left side of the deque. If no + * elements are present, raises an IndexError. + */ + final PyObject deque_popleft() { + return removeNode(header.right); + } + + private PyObject removeNode(Node node) { + if (node == header) { + throw Py.IndexError("pop from an empty deque"); + } + PyObject obj = node.data; + node.left.right = node.right; + node.right.left = node.left; + node.right = null; + node.left = null; + node.data = null; + size--; + return obj; + } + + /** + * Removed the first occurrence of value. If not found, raises a + * ValueError. + */ + final PyObject deque_remove(PyObject value) { + int n = size; + Node tmp = header.right; + boolean match = false; + for (int i = 0; i < n; i++) { + if (tmp.data.equals(value)) { + match = true; + } + if (n != size) { + throw Py.IndexError("deque mutated during remove()."); + } + if (match) { + return removeNode(tmp); + } + tmp = tmp.right; + } + throw Py.ValueError("deque.remove(x): x not in deque"); + } + + /** + * Rotate the deque n steps to the right. If n is negative, rotate to the + * left. Rotating one step to the right is equivalent to: d.appendleft(d.pop()). + */ + final void deque_rotate(PyObject stepsobj) { + if (size == 0) { + return; + } + + int n = 0; + if (stepsobj instanceof PyInteger || stepsobj instanceof PyLong) { + n = ((PyInteger)stepsobj.__int__()).getValue(); + } else { + throw Py.TypeError("an integer is required"); + } + + int halfsize = (size + 1) >> 1; + if (n > halfsize || n < -halfsize) { + n %= size; + if (n > halfsize) { + n -= size; + } else if (n < -halfsize) { + n += size; + } + } + + //rotate right + for (int i = 0; i < n; i++) { + deque_appendleft(deque_pop()); + } + //rotate left + for (int i = 0; i > n; i--) { + deque_append(deque_popleft()); + } + } + + /** + * Rotate the deque one step to the right. + * Rotating one step to the right is equivalent to: d.appendleft(d.pop()). + */ + final void deque_rotate() { + deque_rotate(Py.One); + } + + public String toString() { + return deque_toString(); + } + + final String deque_toString() { + ThreadState ts = Py.getThreadState(); + if (!ts.enterRepr(this)) { + return "[...]"; + } + String name = getType().fastGetName(); + StringBuffer buf = new StringBuffer(name).append("(["); + for (Node tmp = header.right; tmp != header; tmp = tmp.right) { + buf.append(tmp.data.__repr__().toString()); + if (tmp.right != header) { + buf.append(", "); + } + } + buf.append("])"); + ts.exitRepr(this); + return buf.toString(); + } + + public int __len__() { + return deque___len__(); + } + + final int deque___len__() { + return size; + } + + public PyObject __finditem__(PyObject key) { + try { + return deque___getitem__(key); + } catch (PyException pe) { + if (Py.matchException(pe, Py.KeyError)) { + return null; + } + throw pe; + } + } + + final PyObject deque___getitem__(PyObject index) { + return getNode(index).data; + } + + public void __setitem__(PyObject index, PyObject value) { + deque___setitem__(index, value); + } + + final void deque___setitem__(PyObject index, PyObject value) { + Node node = getNode(index).right; + removeNode(node.left); + addBefore(value, node); + } + + public void __delitem__(PyObject key) { + deque___delitem__(key); + } + + final void deque___delitem__(PyObject key) { + removeNode(getNode(key)); + } + + private Node getNode(PyObject index) { + int pos = 0; + if (index instanceof PyInteger || index instanceof PyLong) { + pos = ((PyInteger)index.__int__()).getValue(); + } else { + throw Py.TypeError("an integer is required"); + } + + if (pos < 0) { + pos += size; + } + if (pos < 0 || pos >= size) { + throw Py.IndexError("index out of range: " + index); + } + + Node tmp = header; + if (pos < (size >> 1)) { + for (int i = 0; i <= pos; i++) { + tmp = tmp.right; + } + } else { + for (int i = size - 1; i >= pos; i--) { + tmp = tmp.left; + } + } + return tmp; + } + + public PyObject __iter__() { + return deque___iter__(); + } + + final PyObject deque___iter__() { + return new PyDequeIter(); + } + + public synchronized PyObject __eq__(PyObject o) { + return deque___eq__(o); + } + + final synchronized PyObject deque___eq__(PyObject o) { + if (!(getType() == o.getType()) && !(getType().isSubType(o.getType()))) { + return null; + } + int tl = __len__(); + int ol = o.__len__(); + if (tl != ol) { + return Py.False; + } + int i = cmp(this, tl, o, ol); + return (i < 0) ? Py.True : Py.False; + } + + public synchronized PyObject __ne__(PyObject o) { + return deque___ne__(o); + } + + final synchronized PyObject deque___ne__(PyObject o) { + if (!(getType() == o.getType()) && !(getType().isSubType(o.getType()))) { + return null; + } + int tl = __len__(); + int ol = o.__len__(); + if (tl != ol) { + return Py.True; + } + int i = cmp(this, tl, o, ol); + return (i < 0) ? Py.False : Py.True; + } + + public synchronized PyObject __lt__(PyObject o) { + return deque___lt__(o); + } + + final synchronized PyObject deque___lt__(PyObject o) { + if (!(getType() == o.getType()) && !(getType().isSubType(o.getType()))) { + return null; + } + int i = cmp(this, -1, o, -1); + if (i < 0) { + return (i == -1) ? Py.True : Py.False; + } + return __finditem__(i)._lt(o.__finditem__(i)); + } + + public synchronized PyObject __le__(PyObject o) { + return deque___le__(o); + } + + final synchronized PyObject deque___le__(PyObject o) { + if (!(getType() == o.getType()) && !(getType().isSubType(o.getType()))) { + return null; + } + int i = cmp(this, -1, o, -1); + if (i < 0) { + return (i == -1 || i == -2) ? Py.True : Py.False; + } + return __finditem__(i)._le(o.__finditem__(i)); + } + + public synchronized PyObject __gt__(PyObject o) { + return deque___gt__(o); + } + + final synchronized PyObject deque___gt__(PyObject o) { + if (!(getType() == o.getType()) && !(getType().isSubType(o.getType()))) { + return null; + } + int i = cmp(this, -1, o, -1); + if (i < 0) { + return (i == -3) ? Py.True : Py.False; + } + return __finditem__(i)._gt(o.__finditem__(i)); + } + + public synchronized PyObject __ge__(PyObject o) { + return deque___ge__(o); + } + + final synchronized PyObject deque___ge__(PyObject o) { + if (!(getType() == o.getType()) && !(getType().isSubType(o.getType()))) { + return null; + } + int i = cmp(this, -1, o, -1); + if (i < 0) { + return (i == -3 || i == -2) ? Py.True : Py.False; + } + return __finditem__(i)._ge(o.__finditem__(i)); + } + + // Return value >= 0 is the index where the sequences differs. + // -1: reached the end of o1 without a difference + // -2: reached the end of both seqeunces without a difference + // -3: reached the end of o2 without a difference + protected static int cmp(PyObject o1, int ol1, PyObject o2, int ol2) { + if (ol1 < 0) { + ol1 = o1.__len__(); + } + if (ol2 < 0) { + ol2 = o2.__len__(); + } + int i = 0; + for ( ; i < ol1 && i < ol2; i++) { + if (!o1.__getitem__(i)._eq(o2.__getitem__(i)).__nonzero__()) { + return i; + } + } + if (ol1 == ol2) { + return -2; + } + return (ol1 < ol2) ? -1 : -3; + } + + public int hashCode() { + return deque_hashCode(); + } + + final int deque_hashCode() { + throw Py.TypeError("deque objects are unhashable"); + } + + public PyObject __reduce__() { + return deque___reduce__(); + } + + final PyObject deque___reduce__() { + return new PyTuple(new PyObject [] { + this.getType(), + Py.EmptyTuple, + Py.None, + this.deque___iter__() + }); + } + + final PyObject deque___copy__() { + PyDeque pd = (PyDeque)this.getType().__call__(); + pd.deque_extend(this); + return pd; + } + + private static class Node { + private Node left; + private Node right; + private PyObject data; + + Node(PyObject data, Node right, Node left) { + this.data = data; + this.right = right; + this.left = left; + } + } + + private class PyDequeIter extends PyIterator { + + private Node lastReturned = header; + private int itersize; + + public PyDequeIter() { + itersize = size; + } + + public PyObject __iternext__() { + if (itersize != size) { + throw Py.RuntimeError("deque changed size during iteration"); + } + if (lastReturned.right != header) { + lastReturned = lastReturned.right; + return lastReturned.data; + } + return null; + } + } +} Index: src/org/python/modules/collections/Collections.java =================================================================== --- src/org/python/modules/collections/Collections.java (revision 0) +++ src/org/python/modules/collections/Collections.java (revision 0) @@ -0,0 +1,23 @@ +/** + * Collections.java - This module adds the ability to use high performance data + * structures. + * - deque: ordered collection accessible from endpoints only + * - defaultdict: dict subclass with a default value factory + * + * @author Mehendran T (mehendran@gmail.com) + * Novell Software Development (I) Pvt. Ltd + * @created Fri 07-Sep-2007 18:50:20 + */ +package org.python.modules.collections; + +import org.python.core.ClassDictInit; +import org.python.core.Py; +import org.python.core.PyObject; + +public class Collections implements ClassDictInit { + + public static void classDictInit(PyObject dict) { + dict.__setitem__("deque", Py.java2py(PyDeque.class)); + dict.__setitem__("defaultdict", Py.java2py(PyDefaultDict.class)); + } +} Index: src/templates/mappings =================================================================== --- src/templates/mappings (revision 3803) +++ src/templates/mappings (working copy) @@ -62,3 +62,7 @@ unicode.expose:org.python.core.PyUnicode zipimporter.derived:org.python.modules.zipimport.zipimporterDerived zipimporter.expose:org.python.modules.zipimport.zipimporter +deque.expose:org.python.modules.collections.PyDeque +deque.derived:org.python.modules.collections.PyDequeDerived +defaultdict.expose:org.python.modules.collections.PyDefaultDict +defaultdict.derived:org.python.modules.collections.PyDefaultDictDerived Index: src/templates/deque.derived =================================================================== --- src/templates/deque.derived (revision 0) +++ src/templates/deque.derived (revision 0) @@ -0,0 +1,4 @@ +base_class: PyDeque +want_dict: true +ctr: +incl: object Index: src/templates/defaultdict.expose =================================================================== --- src/templates/defaultdict.expose (revision 0) +++ src/templates/defaultdict.expose (revision 0) @@ -0,0 +1,19 @@ +# setup +type_name: defaultdict +type_class: PyDefaultDict + +# getsets +expose_getset: default_factory getDefaultFactory setDefaultFactory + +# exposed methods +expose_meth: __getitem__ o +expose_meth: __missing__ o +expose_meth: __reduce__ +expose_meth: copy +expose_meth: __copy__ +expose_meth: __repr__ + `sdeleg`(toString); +expose_wide_meth: __init__ -1 -1 + `vdeleg`(init); + `void; +expose_new_mutable: Index: src/templates/deque.expose =================================================================== --- src/templates/deque.expose (revision 0) +++ src/templates/deque.expose (revision 0) @@ -0,0 +1,59 @@ +# setup +type_name: deque +type_class: PyDeque +# exposed methods +expose_meth: :- append o +expose_meth: :- appendleft o +expose_meth: pop +expose_meth: popleft +expose_meth: :- extend o +expose_meth: :- extendleft o +expose_meth: :- remove o +expose_meth: :- clear +expose_meth: :- rotate o? +expose_meth: __getitem__ o +expose_meth: :- __setitem__ o o +expose_meth: :- __delitem__ o +expose_meth: :i __len__ +expose_meth: __repr__ + return new PyString(((`typ)self).deque_toString()); +expose_meth: __iter__ +expose_meth: __eq__ o + PyObject ret = ((`typ)self).deque___eq__(`arg0); + if (ret == null) + return Py.NotImplemented; + return ret; +expose_meth: __ne__ o + PyObject ret = ((`typ)self).deque___ne__(`arg0); + if (ret == null) + return Py.NotImplemented; + return ret; +expose_meth: __lt__ o + PyObject ret = ((`typ)self).deque___lt__(`arg0); + if (ret == null) + return Py.NotImplemented; + return ret; +expose_meth: __le__ o + PyObject ret = ((`typ)self).deque___le__(`arg0); + if (ret == null) + return Py.NotImplemented; + return ret; +expose_meth: __gt__ o + PyObject ret = ((`typ)self).deque___gt__(`arg0); + if (ret == null) + return Py.NotImplemented; + return ret; +expose_meth: __ge__ o + PyObject ret = ((`typ)self).deque___ge__(`arg0); + if (ret == null) + return Py.NotImplemented; + return ret; +expose_meth: __hash__ + return new PyInteger(((`typ)self).deque_hashCode()); +expose_meth: __reduce__ +#expose_meth: __reduce_ex__ i? +expose_meth: __copy__ +expose_wide_meth: __init__ -1 -1 + `vdeleg`(init); + `void; +expose_new_mutable: Index: src/templates/defaultdict.derived =================================================================== --- src/templates/defaultdict.derived (revision 0) +++ src/templates/defaultdict.derived (revision 0) @@ -0,0 +1,4 @@ +base_class: PyDefaultDict +want_dict: true +ctr: +incl: object Index: CPythonLib/test/test_defaultdict.py =================================================================== --- CPythonLib/test/test_defaultdict.py (revision 0) +++ CPythonLib/test/test_defaultdict.py (revision 0) @@ -0,0 +1,149 @@ +"""Unit tests for collections.defaultdict.""" + +import os +import copy +import tempfile +import unittest +from test import test_support + +from collections import defaultdict + +def foobar(): + return list + +class TestDefaultDict(unittest.TestCase): + + def test_basic(self): + d1 = defaultdict() + self.assertEqual(d1.default_factory, None) + d1.default_factory = list + d1[12].append(42) + self.assertEqual(d1, {12: [42]}) + d1[12].append(24) + self.assertEqual(d1, {12: [42, 24]}) + d1[13] + d1[14] + self.assertEqual(d1, {12: [42, 24], 13: [], 14: []}) + self.assert_(d1[12] is not d1[13] is not d1[14]) + d2 = defaultdict(list, foo=1, bar=2) + self.assertEqual(d2.default_factory, list) + self.assertEqual(d2, {"foo": 1, "bar": 2}) + self.assertEqual(d2["foo"], 1) + self.assertEqual(d2["bar"], 2) + self.assertEqual(d2[42], []) + self.assert_("foo" in d2) + self.assert_("foo" in d2.keys()) + self.assert_("bar" in d2) + self.assert_("bar" in d2.keys()) + self.assert_(42 in d2) + self.assert_(42 in d2.keys()) + self.assert_(12 not in d2) + self.assert_(12 not in d2.keys()) + d2.default_factory = None + self.assertEqual(d2.default_factory, None) + try: + d2[15] + except KeyError, err: + self.assertEqual(err.args, (15,)) + else: + self.fail("d2[15] didn't raise KeyError") + self.assertRaises(TypeError, defaultdict, 1) + + def test_missing(self): + d1 = defaultdict() + self.assertRaises(KeyError, d1.__missing__, 42) + d1.default_factory = list + self.assertEqual(d1.__missing__(42), []) + + def test_repr(self): + d1 = defaultdict() + self.assertEqual(d1.default_factory, None) + self.assertEqual(repr(d1), "defaultdict(None, {})") + d1[11] = 41 + self.assertEqual(repr(d1), "defaultdict(None, {11: 41})") + d2 = defaultdict(int) + self.assertEqual(d2.default_factory, int) + d2[12] = 42 + self.assertEqual(repr(d2), "defaultdict(, {12: 42})") + def foo(): return 43 + d3 = defaultdict(foo) + self.assert_(d3.default_factory is foo) + d3[13] + self.assertEqual(repr(d3), "defaultdict(%s, {13: 43})" % repr(foo)) + + def test_print(self): + d1 = defaultdict() + def foo(): return 42 + d2 = defaultdict(foo, {1: 2}) + # NOTE: We can't use tempfile.[Named]TemporaryFile since this + # code must exercise the tp_print C code, which only gets + # invoked for *real* files. + tfn = tempfile.mktemp() + try: + f = open(tfn, "w+") + try: + print >>f, d1 + print >>f, d2 + f.seek(0) + self.assertEqual(f.readline(), repr(d1) + "\n") + self.assertEqual(f.readline(), repr(d2) + "\n") + finally: + f.close() + finally: + os.remove(tfn) + + def test_copy(self): + d1 = defaultdict() + d2 = d1.copy() + self.assertEqual(type(d2), defaultdict) + self.assertEqual(d2.default_factory, None) + self.assertEqual(d2, {}) + d1.default_factory = list + d3 = d1.copy() + self.assertEqual(type(d3), defaultdict) + self.assertEqual(d3.default_factory, list) + self.assertEqual(d3, {}) + d1[42] + d4 = d1.copy() + self.assertEqual(type(d4), defaultdict) + self.assertEqual(d4.default_factory, list) + self.assertEqual(d4, {42: []}) + d4[12] + self.assertEqual(d4, {42: [], 12: []}) + + def test_shallow_copy(self): + d1 = defaultdict(foobar, {1: 1}) + d2 = copy.copy(d1) + self.assertEqual(d2.default_factory, foobar) + self.assertEqual(d2, d1) + d1.default_factory = list + d2 = copy.copy(d1) + self.assertEqual(d2.default_factory, list) + self.assertEqual(d2, d1) + + def test_deep_copy(self): + d1 = defaultdict(foobar, {1: [1]}) + d2 = copy.deepcopy(d1) + self.assertEqual(d2.default_factory, foobar) + self.assertEqual(d2, d1) + self.assert_(d1[1] is not d2[1]) + d1.default_factory = list + d2 = copy.deepcopy(d1) + self.assertEqual(d2.default_factory, list) + self.assertEqual(d2, d1) + + def test_keyerror_without_factory(self): + d1 = defaultdict() + try: + d1[(1,)] + except KeyError, err: + self.assertEqual(err.message, (1,)) + else: + self.fail("expected KeyError") + + +def test_main(): + test_support.run_unittest(TestDefaultDict) + +if __name__ == "__main__": + test_main() Index: CPythonLib/test/test_deque.py =================================================================== --- CPythonLib/test/test_deque.py (revision 0) +++ CPythonLib/test/test_deque.py (revision 0) @@ -0,0 +1,651 @@ +from collections import deque +import unittest +from test import test_support#, seq_tests +from weakref import proxy +import copy +import cPickle as pickle +from cStringIO import StringIO +import random +import os + +BIG = 100000 + +def fail(): + raise SyntaxError + yield 1 + +class BadCmp: + def __eq__(self, other): + raise RuntimeError + +class MutateCmp: + def __init__(self, deque, result): + self.deque = deque + self.result = result + def __eq__(self, other): + self.deque.clear() + return self.result + +class TestBasic(unittest.TestCase): + + def test_basics(self): + d = deque(xrange(100)) + d.__init__(xrange(100, 200)) + for i in xrange(200, 400): + d.append(i) + for i in reversed(xrange(-200, 0)): + d.appendleft(i) + self.assertEqual(list(d), range(-200, 400)) + self.assertEqual(len(d), 600) + + left = [d.popleft() for i in xrange(250)] + self.assertEqual(left, range(-200, 50)) + self.assertEqual(list(d), range(50, 400)) + + right = [d.pop() for i in xrange(250)] + right.reverse() + self.assertEqual(right, range(150, 400)) + self.assertEqual(list(d), range(50, 150)) + + def test_comparisons(self): + d = deque('xabc'); d.popleft() + for e in [d, deque('abc'), deque('ab'), deque(), list(d)]: + self.assertEqual(d==e, type(d)==type(e) and list(d)==list(e)) + self.assertEqual(d!=e, not(type(d)==type(e) and list(d)==list(e))) + + args = map(deque, ('', 'a', 'b', 'ab', 'ba', 'abc', 'xba', 'xabc', 'cba')) + for x in args: + for y in args: + self.assertEqual(x == y, list(x) == list(y), (x,y)) + self.assertEqual(x != y, list(x) != list(y), (x,y)) + self.assertEqual(x < y, list(x) < list(y), (x,y)) + self.assertEqual(x <= y, list(x) <= list(y), (x,y)) + self.assertEqual(x > y, list(x) > list(y), (x,y)) + self.assertEqual(x >= y, list(x) >= list(y), (x,y)) + self.assertEqual(cmp(x,y), cmp(list(x),list(y)), (x,y)) + + def test_extend(self): + d = deque('a') + self.assertRaises(TypeError, d.extend, 1) + d.extend('bcd') + self.assertEqual(list(d), list('abcd')) + + def test_extendleft(self): + d = deque('a') + self.assertRaises(TypeError, d.extendleft, 1) + d.extendleft('bcd') + self.assertEqual(list(d), list(reversed('abcd'))) + d = deque() + d.extendleft(range(1000)) + self.assertEqual(list(d), list(reversed(range(1000)))) + self.assertRaises(SyntaxError, d.extendleft, fail()) + + def test_getitem(self): + n = 200 + d = deque(xrange(n)) + l = range(n) + for i in xrange(n): + d.popleft() + l.pop(0) + if random.random() < 0.5: + d.append(i) + l.append(i) + for j in xrange(1-len(l), len(l)): + assert d[j] == l[j] + + d = deque('superman') + self.assertEqual(d[0], 's') + self.assertEqual(d[-1], 'n') + d = deque() + self.assertRaises(IndexError, d.__getitem__, 0) + self.assertRaises(IndexError, d.__getitem__, -1) + + def test_setitem(self): + n = 200 + d = deque(xrange(n)) + for i in xrange(n): + d[i] = 10 * i + self.assertEqual(list(d), [10*i for i in xrange(n)]) + l = list(d) + for i in xrange(1-n, 0, -1): + d[i] = 7*i + l[i] = 7*i + self.assertEqual(list(d), l) + + def test_delitem(self): + n = 500 # O(n**2) test, don't make this too big + d = deque(xrange(n)) + self.assertRaises(IndexError, d.__delitem__, -n-1) + self.assertRaises(IndexError, d.__delitem__, n) + for i in xrange(n): + self.assertEqual(len(d), n-i) + j = random.randrange(-len(d), len(d)) + val = d[j] + self.assert_(val in d) + del d[j] + self.assert_(val not in d) + self.assertEqual(len(d), 0) + + def test_rotate(self): + s = tuple('abcde') + n = len(s) + + d = deque(s) + d.rotate(1) # verify rot(1) + self.assertEqual(''.join(d), 'eabcd') + + d = deque(s) + d.rotate(-1) # verify rot(-1) + self.assertEqual(''.join(d), 'bcdea') + d.rotate() # check default to 1 + self.assertEqual(tuple(d), s) + + for i in xrange(n*3): + d = deque(s) + e = deque(d) + d.rotate(i) # check vs. rot(1) n times + for j in xrange(i): + e.rotate(1) + self.assertEqual(tuple(d), tuple(e)) + d.rotate(-i) # check that it works in reverse + self.assertEqual(tuple(d), s) + e.rotate(n-i) # check that it wraps forward + self.assertEqual(tuple(e), s) + + for i in xrange(n*3): + d = deque(s) + e = deque(d) + d.rotate(-i) + for j in xrange(i): + e.rotate(-1) # check vs. rot(-1) n times + self.assertEqual(tuple(d), tuple(e)) + d.rotate(i) # check that it works in reverse + self.assertEqual(tuple(d), s) + e.rotate(i-n) # check that it wraps backaround + self.assertEqual(tuple(e), s) + + d = deque(s) + e = deque(s) + e.rotate(BIG+17) # verify on long series of rotates + dr = d.rotate + for i in xrange(BIG+17): + dr() + self.assertEqual(tuple(d), tuple(e)) + + self.assertRaises(TypeError, d.rotate, 'x') # Wrong arg type + self.assertRaises(TypeError, d.rotate, 1, 10) # Too many args + + d = deque() + d.rotate() # rotate an empty deque + self.assertEqual(d, deque()) + + def test_len(self): + d = deque('ab') + self.assertEqual(len(d), 2) + d.popleft() + self.assertEqual(len(d), 1) + d.pop() + self.assertEqual(len(d), 0) + self.assertRaises(IndexError, d.pop) + self.assertEqual(len(d), 0) + d.append('c') + self.assertEqual(len(d), 1) + d.appendleft('d') + self.assertEqual(len(d), 2) + d.clear() + self.assertEqual(len(d), 0) + + def test_underflow(self): + d = deque() + self.assertRaises(IndexError, d.pop) + self.assertRaises(IndexError, d.popleft) + + def test_clear(self): + d = deque(xrange(100)) + self.assertEqual(len(d), 100) + d.clear() + self.assertEqual(len(d), 0) + self.assertEqual(list(d), []) + d.clear() # clear an emtpy deque + self.assertEqual(list(d), []) + + def test_remove(self): + d = deque('abcdefghcij') + d.remove('c') + self.assertEqual(d, deque('abdefghcij')) + d.remove('c') + self.assertEqual(d, deque('abdefghij')) + self.assertRaises(ValueError, d.remove, 'c') + self.assertEqual(d, deque('abdefghij')) + + # Handle comparison errors + d = deque(['a', 'b', BadCmp(), 'c']) + e = deque(d) + self.assertRaises(RuntimeError, d.remove, 'c') + for x, y in zip(d, e): + # verify that original order and values are retained. + self.assert_(x is y) + + # Handle evil mutator + for match in (True, False): + d = deque(['ab']) + d.extend([MutateCmp(d, match), 'c']) + self.assertRaises(IndexError, d.remove, 'c') + self.assertEqual(d, deque()) + + def test_repr(self): + d = deque(xrange(200)) + e = eval(repr(d)) + self.assertEqual(list(d), list(e)) + d.append(d) + self.assert_('...' in repr(d)) + + def test_print(self): + d = deque(xrange(200)) + d.append(d) + try: + fo = open(test_support.TESTFN, "wb") + print >> fo, d, + fo.close() + fo = open(test_support.TESTFN, "rb") + self.assertEqual(fo.read(), repr(d)) + finally: + fo.close() + os.remove(test_support.TESTFN) + + def test_init(self): + self.assertRaises(TypeError, deque, 'abc', 2); + self.assertRaises(TypeError, deque, 1); + + def test_hash(self): + self.assertRaises(TypeError, hash, deque('abc')) + + def test_long_steadystate_queue_popleft(self): + for size in (0, 1, 2, 100, 1000): + d = deque(xrange(size)) + append, pop = d.append, d.popleft + for i in xrange(size, BIG): + append(i) + x = pop() + if x != i - size: + self.assertEqual(x, i-size) + self.assertEqual(list(d), range(BIG-size, BIG)) + + def test_long_steadystate_queue_popright(self): + for size in (0, 1, 2, 100, 1000): + d = deque(reversed(xrange(size))) + append, pop = d.appendleft, d.pop + for i in xrange(size, BIG): + append(i) + x = pop() + if x != i - size: + self.assertEqual(x, i-size) + self.assertEqual(list(reversed(list(d))), range(BIG-size, BIG)) + + def test_big_queue_popleft(self): + pass + d = deque() + append, pop = d.append, d.popleft + for i in xrange(BIG): + append(i) + for i in xrange(BIG): + x = pop() + if x != i: + self.assertEqual(x, i) + + def test_big_queue_popright(self): + d = deque() + append, pop = d.appendleft, d.pop + for i in xrange(BIG): + append(i) + for i in xrange(BIG): + x = pop() + if x != i: + self.assertEqual(x, i) + + def test_big_stack_right(self): + d = deque() + append, pop = d.append, d.pop + for i in xrange(BIG): + append(i) + for i in reversed(xrange(BIG)): + x = pop() + if x != i: + self.assertEqual(x, i) + self.assertEqual(len(d), 0) + + def test_big_stack_left(self): + d = deque() + append, pop = d.appendleft, d.popleft + for i in xrange(BIG): + append(i) + for i in reversed(xrange(BIG)): + x = pop() + if x != i: + self.assertEqual(x, i) + self.assertEqual(len(d), 0) + + def test_roundtrip_iter_init(self): + d = deque(xrange(200)) + e = deque(d) + self.assertNotEqual(id(d), id(e)) + self.assertEqual(list(d), list(e)) + + def test_pickle(self): + d = deque(xrange(200)) + for i in (0, 1, 2): + s = pickle.dumps(d, i) + e = pickle.loads(s) + self.assertNotEqual(id(d), id(e)) + self.assertEqual(list(d), list(e)) + + def test_pickle_recursive(self): + d = deque('abc') + d.append(d) + for i in (0, 1, 2): + e = pickle.loads(pickle.dumps(d, i)) + self.assertNotEqual(id(d), id(e)) + self.assertEqual(id(e), id(e[-1])) + + def test_deepcopy(self): + mut = [10] + d = deque([mut]) + e = copy.deepcopy(d) + self.assertEqual(list(d), list(e)) + mut[0] = 11 + self.assertNotEqual(id(d), id(e)) + self.assertNotEqual(list(d), list(e)) + + def test_copy(self): + mut = [10] + d = deque([mut]) + e = copy.copy(d) + self.assertEqual(list(d), list(e)) + mut[0] = 11 + self.assertNotEqual(id(d), id(e)) + self.assertEqual(list(d), list(e)) + + def test_reversed(self): + for s in ('abcd', xrange(2000)): + self.assertEqual(list(reversed(deque(s))), list(reversed(s))) + + def test_gc_doesnt_blowup(self): + import gc + # This used to assert-fail in deque_traverse() under a debug + # build, or run wild with a NULL pointer in a release build. + d = deque() + for i in xrange(100): + d.append(1) + gc.collect() + +class TestVariousIteratorArgs(unittest.TestCase): + + def test_constructor(self): + for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)): + for g in (seq_tests.Sequence, seq_tests.IterFunc, + seq_tests.IterGen, seq_tests.IterFuncStop, + seq_tests.itermulti, seq_tests.iterfunc): + self.assertEqual(list(deque(g(s))), list(g(s))) + self.assertRaises(TypeError, deque, seq_tests.IterNextOnly(s)) + self.assertRaises(TypeError, deque, seq_tests.IterNoNext(s)) + self.assertRaises(ZeroDivisionError, deque, seq_tests.IterGenExc(s)) + + def test_iter_with_altered_data(self): + d = deque('abcdefg') + it = iter(d) + d.pop() + self.assertRaises(RuntimeError, it.next) + + def test_runtime_error_on_empty_deque(self): + d = deque() + it = iter(d) + d.append(10) + self.assertRaises(RuntimeError, it.next) + +class Deque(deque): + pass + +class DequeWithBadIter(deque): + def __iter__(self): + raise TypeError + +class TestSubclass(unittest.TestCase): + + def test_basics(self): + d = Deque(xrange(100)) + d.__init__(xrange(100, 200)) + for i in xrange(200, 400): + d.append(i) + for i in reversed(xrange(-200, 0)): + d.appendleft(i) + self.assertEqual(list(d), range(-200, 400)) + self.assertEqual(len(d), 600) + + left = [d.popleft() for i in xrange(250)] + self.assertEqual(left, range(-200, 50)) + self.assertEqual(list(d), range(50, 400)) + + right = [d.pop() for i in xrange(250)] + right.reverse() + self.assertEqual(right, range(150, 400)) + self.assertEqual(list(d), range(50, 150)) + + d.clear() + self.assertEqual(len(d), 0) + + def test_copy_pickle(self): + + d = Deque('abc') + + e = d.__copy__() + self.assertEqual(type(d), type(e)) + self.assertEqual(list(d), list(e)) + + e = Deque(d) + self.assertEqual(type(d), type(e)) + self.assertEqual(list(d), list(e)) + + s = pickle.dumps(d) + e = pickle.loads(s) + self.assertNotEqual(id(d), id(e)) + self.assertEqual(type(d), type(e)) + self.assertEqual(list(d), list(e)) + + def test_pickle(self): + d = Deque('abc') + d.append(d) + + e = pickle.loads(pickle.dumps(d)) + self.assertNotEqual(id(d), id(e)) + self.assertEqual(type(d), type(e)) + dd = d.pop() + ee = e.pop() + self.assertEqual(id(e), id(ee)) + self.assertEqual(d, e) + + d.x = d + e = pickle.loads(pickle.dumps(d)) + self.assertEqual(id(e), id(e.x)) + + d = DequeWithBadIter('abc') + self.assertRaises(TypeError, pickle.dumps, d) + + def test_weakref(self): + d = deque('gallahad') + p = proxy(d) + self.assertEqual(str(p), str(d)) + d = None + self.assertRaises(ReferenceError, str, p) + + def test_strange_subclass(self): + class X(deque): + def __iter__(self): + return iter([]) + d1 = X([1,2,3]) + d2 = X([4,5,6]) + d1 == d2 # not clear if this is supposed to be True or False, + # but it used to give a SystemError + + +class SubclassWithKwargs(deque): + def __init__(self, newarg=1): + deque.__init__(self) + +class TestSubclassWithKwargs(unittest.TestCase): + def test_subclass_with_kwargs(self): + # SF bug #1486663 -- this used to erroneously raise a TypeError + SubclassWithKwargs(newarg=1) + +#============================================================================== + +libreftest = """ +Example from the Library Reference: Doc/lib/libcollections.tex + +>>> from collections import deque +>>> d = deque('ghi') # make a new deque with three items +>>> for elem in d: # iterate over the deque's elements +... print elem.upper() +G +H +I +>>> d.append('j') # add a new entry to the right side +>>> d.appendleft('f') # add a new entry to the left side +>>> d # show the representation of the deque +deque(['f', 'g', 'h', 'i', 'j']) +>>> d.pop() # return and remove the rightmost item +'j' +>>> d.popleft() # return and remove the leftmost item +'f' +>>> list(d) # list the contents of the deque +['g', 'h', 'i'] +>>> d[0] # peek at leftmost item +'g' +>>> d[-1] # peek at rightmost item +'i' +>>> list(reversed(d)) # list the contents of a deque in reverse +['i', 'h', 'g'] +>>> 'h' in d # search the deque +True +>>> d.extend('jkl') # add multiple elements at once +>>> d +deque(['g', 'h', 'i', 'j', 'k', 'l']) +>>> d.rotate(1) # right rotation +>>> d +deque(['l', 'g', 'h', 'i', 'j', 'k']) +>>> d.rotate(-1) # left rotation +>>> d +deque(['g', 'h', 'i', 'j', 'k', 'l']) +>>> deque(reversed(d)) # make a new deque in reverse order +deque(['l', 'k', 'j', 'i', 'h', 'g']) +>>> d.clear() # empty the deque +>>> d.pop() # cannot pop from an empty deque +Traceback (most recent call last): + File "", line 1, in -toplevel- + d.pop() +IndexError: pop from an empty deque + +>>> d.extendleft('abc') # extendleft() reverses the input order +>>> d +deque(['c', 'b', 'a']) + + + +>>> def delete_nth(d, n): +... d.rotate(-n) +... d.popleft() +... d.rotate(n) +... +>>> d = deque('abcdef') +>>> delete_nth(d, 2) # remove the entry at d[2] +>>> d +deque(['a', 'b', 'd', 'e', 'f']) + + + +>>> def roundrobin(*iterables): +... pending = deque(iter(i) for i in iterables) +... while pending: +... task = pending.popleft() +... try: +... yield task.next() +... except StopIteration: +... continue +... pending.append(task) +... + +>>> for value in roundrobin('abc', 'd', 'efgh'): +... print value +... +a +d +e +b +f +c +g +h + + +>>> def maketree(iterable): +... d = deque(iterable) +... while len(d) > 1: +... pair = [d.popleft(), d.popleft()] +... d.append(pair) +... return list(d) +... +>>> print maketree('abcdefgh') +[[[['a', 'b'], ['c', 'd']], [['e', 'f'], ['g', 'h']]]] + +""" + + +#============================================================================== + +__test__ = {'libreftest' : libreftest} + +def test_main(verbose=None): + import sys + test_classes = ( + TestBasic, + TestVariousIteratorArgs, + TestSubclass, + TestSubclassWithKwargs, + ) + + # As This file uses cPickle and the support is needed from that. + del TestBasic.test_pickle + del TestBasic.test_pickle_recursive + del TestSubclass.test_pickle + del TestSubclass.test_copy_pickle + + # Support is needed from weakref module + del TestSubclass.test_weakref + + # This test case won't work in jython, because jython doen't have 'gc' module + del TestBasic.test_gc_doesnt_blowup + + # This test case will not work as 'seq_tests' module is needed + del TestVariousIteratorArgs.test_constructor + + test_support.run_unittest(*test_classes) + + #Following after the 2 lines will not work: gc module is not exist + if test_support.is_jython: + return + + # verify reference counting + if verbose and hasattr(sys, "gettotalrefcount"): + import gc + counts = [None] * 5 + for i in xrange(len(counts)): + test_support.run_unittest(*test_classes) + gc.collect() + counts[i] = sys.gettotalrefcount() + print counts + + # doctests + from test import test_deque + test_support.run_doctest(test_deque, verbose) + +if __name__ == "__main__": + test_main(verbose=True) Index: CPythonLib/copy.py =================================================================== --- CPythonLib/copy.py (revision 59472) +++ CPythonLib/copy.py (working copy) @@ -14,7 +14,7 @@ class instances). - A shallow copy constructs a new compound object and then (to the - extent possible) inserts *the same objects* into in that the + extent possible) inserts *the same objects* into it that the original contains. - A deep copy constructs a new compound object and then, recursively, @@ -62,16 +62,6 @@ __all__ = ["Error", "copy", "deepcopy"] -import inspect -def _getspecial(cls, name): - for basecls in inspect.getmro(cls): - try: - return basecls.__dict__[name] - except: - pass - else: - return None - def copy(x): """Shallow copy operation on arbitrary Python objects. @@ -84,7 +74,7 @@ if copier: return copier(x) - copier = _getspecial(cls, "__copy__") + copier = getattr(cls, "__copy__", None) if copier: return copier(x) @@ -100,9 +90,6 @@ if reductor: rv = reductor() else: - copier = getattr(x, "__copy__", None) - if copier: - return copier() raise Error("un(shallow)copyable object of type %s" % cls) return _reconstruct(x, rv, 0) @@ -110,44 +97,27 @@ _copy_dispatch = d = {} -def _copy_atomic(x): +def _copy_immutable(x): return x -d[types.NoneType] = _copy_atomic -d[types.IntType] = _copy_atomic -d[types.LongType] = _copy_atomic -d[types.FloatType] = _copy_atomic -d[types.BooleanType] = _copy_atomic -try: - d[types.ComplexType] = _copy_atomic -except AttributeError: - pass -d[types.StringType] = _copy_atomic -try: - d[types.UnicodeType] = _copy_atomic -except AttributeError: - pass -try: - d[types.CodeType] = _copy_atomic -except AttributeError: - pass -d[types.TypeType] = _copy_atomic -d[types.XRangeType] = _copy_atomic -d[types.ClassType] = _copy_atomic -d[types.BuiltinFunctionType] = _copy_atomic +for t in (type(None), int, long, float, bool, str, tuple, + frozenset, type, xrange, types.ClassType, + types.BuiltinFunctionType, + types.FunctionType): + d[t] = _copy_immutable +for name in ("ComplexType", "UnicodeType", "CodeType"): + t = getattr(types, name, None) + if t is not None: + d[t] = _copy_immutable -def _copy_list(x): - return x[:] -d[types.ListType] = _copy_list +def _copy_with_constructor(x): + return type(x)(x) +for t in (list, dict, set): + d[t] = _copy_with_constructor -def _copy_tuple(x): - return x[:] -d[types.TupleType] = _copy_tuple - -def _copy_dict(x): +def _copy_with_copy_method(x): return x.copy() -d[types.DictionaryType] = _copy_dict if PyStringMap is not None: - d[PyStringMap] = _copy_dict + d[PyStringMap] = _copy_with_copy_method def _copy_inst(x): if hasattr(x, '__copy__'): @@ -176,7 +146,6 @@ See the module's __doc__ string for more info. """ - if memo is None: memo = {} @@ -198,9 +167,9 @@ if issc: y = _deepcopy_atomic(x, memo) else: - copier = _getspecial(cls, "__deepcopy__") + copier = getattr(x, "__deepcopy__", None) if copier: - y = copier(x, memo) + y = copier(memo) else: reductor = dispatch_table.get(cls) if reductor: @@ -214,9 +183,6 @@ if reductor: rv = reductor() else: - copier = getattr(x, "__deepcopy__", None) - if copier: - return copier(memo) raise Error( "un(deep)copyable object of type %s" % cls) y = _reconstruct(x, rv, 1, memo) @@ -229,28 +195,29 @@ def _deepcopy_atomic(x, memo): return x -d[types.NoneType] = _deepcopy_atomic -d[types.IntType] = _deepcopy_atomic -d[types.LongType] = _deepcopy_atomic -d[types.FloatType] = _deepcopy_atomic -d[types.BooleanType] = _deepcopy_atomic +d[type(None)] = _deepcopy_atomic +d[int] = _deepcopy_atomic +d[long] = _deepcopy_atomic +d[float] = _deepcopy_atomic +d[bool] = _deepcopy_atomic try: - d[types.ComplexType] = _deepcopy_atomic -except AttributeError: + d[complex] = _deepcopy_atomic +except NameError: pass -d[types.StringType] = _deepcopy_atomic +d[str] = _deepcopy_atomic try: - d[types.UnicodeType] = _deepcopy_atomic -except AttributeError: + d[unicode] = _deepcopy_atomic +except NameError: pass try: d[types.CodeType] = _deepcopy_atomic except AttributeError: pass -d[types.TypeType] = _deepcopy_atomic -d[types.XRangeType] = _deepcopy_atomic +d[type] = _deepcopy_atomic +d[xrange] = _deepcopy_atomic d[types.ClassType] = _deepcopy_atomic d[types.BuiltinFunctionType] = _deepcopy_atomic +d[types.FunctionType] = _deepcopy_atomic def _deepcopy_list(x, memo): y = [] @@ -258,7 +225,7 @@ for a in x: y.append(deepcopy(a, memo)) return y -d[types.ListType] = _deepcopy_list +d[list] = _deepcopy_list def _deepcopy_tuple(x, memo): y = [] @@ -277,7 +244,7 @@ y = x memo[d] = y return y -d[types.TupleType] = _deepcopy_tuple +d[tuple] = _deepcopy_tuple def _deepcopy_dict(x, memo): y = {} @@ -285,7 +252,7 @@ for key, value in x.iteritems(): y[deepcopy(key, memo)] = deepcopy(value, memo) return y -d[types.DictionaryType] = _deepcopy_dict +d[dict] = _deepcopy_dict if PyStringMap is not None: d[PyStringMap] = _deepcopy_dict