[pypy-svn] r25531 - in pypy/dist/pypy: annotation objspace/std objspace/std/test

stephan at codespeak.net stephan at codespeak.net
Sat Apr 8 00:14:41 CEST 2006


Author: stephan
Date: Sat Apr  8 00:14:38 2006
New Revision: 25531

Modified:
   pypy/dist/pypy/annotation/classdef.py
   pypy/dist/pypy/objspace/std/complexobject.py
   pypy/dist/pypy/objspace/std/complextype.py
   pypy/dist/pypy/objspace/std/model.py
   pypy/dist/pypy/objspace/std/objspace.py
   pypy/dist/pypy/objspace/std/setobject.py
   pypy/dist/pypy/objspace/std/settype.py
   pypy/dist/pypy/objspace/std/test/test_complexobject.py
Log:
complex numbers are now considered working: tests are passed and pypy-c can be 
created. 
set implementation is nearly done. To test set/frozenset, enable WITHSET in
objspace/std/model.py


Modified: pypy/dist/pypy/annotation/classdef.py
==============================================================================
--- pypy/dist/pypy/annotation/classdef.py	(original)
+++ pypy/dist/pypy/annotation/classdef.py	Sat Apr  8 00:14:38 2006
@@ -277,6 +277,12 @@
     def generalize_attr(self, attr, s_value=None):
         # if the attribute exists in a superclass, generalize there,
         # as imposed by invariant (I)
+        #start debug
+        #if self.name.endswith('W_Root') and attr == 'setdata':
+        #    print 'NAME:',self.name
+        #    import pdb
+        #    pdb.set_trace()
+        #stop debug
         for clsdef in self.getmro():
             if attr in clsdef.attrs:
                 clsdef._generalize_attr(attr, s_value)

Modified: pypy/dist/pypy/objspace/std/complexobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/complexobject.py	(original)
+++ pypy/dist/pypy/objspace/std/complexobject.py	Sat Apr  8 00:14:38 2006
@@ -254,6 +254,11 @@
 def coerce__Complex_Complex(space, w_complex1, w_complex2):
     return space.newtuple([w_complex1, w_complex2])
 
+def complex_conjugate__Complex(space, w_self):
+    #w_real = space.call_function(space.w_float,space.wrap(w_self.realval))
+    #w_imag = space.call_function(space.w_float,space.wrap(-w_self.imagval))
+    return space.newcomplex(w_self.realval,-w_self.imagval)
+
 app = gateway.applevel(""" 
     import math
     def possint(f):
@@ -281,4 +286,5 @@
 repr__Complex = app.interphook('repr__Complex') 
 str__Complex = app.interphook('str__Complex') 
 
-register_all(vars())
+from pypy.objspace.std import complextype
+register_all(vars(), complextype)

Modified: pypy/dist/pypy/objspace/std/complextype.py
==============================================================================
--- pypy/dist/pypy/objspace/std/complextype.py	(original)
+++ pypy/dist/pypy/objspace/std/complextype.py	Sat Apr  8 00:14:38 2006
@@ -1,14 +1,20 @@
 from pypy.interpreter.error import OperationError
 from pypy.interpreter import gateway
 from pypy.objspace.std.strutil import interp_string_to_float, ParseStringError
+from pypy.objspace.std.objspace import register_all
 from pypy.objspace.std.noneobject import W_NoneObject
 from pypy.objspace.std.stdtypedef import GetSetProperty, StdTypeDef, newmethod
+from pypy.objspace.std.stdtypedef import StdObjSpaceMultiMethod
 
 # ERRORCODES
 
 ERR_WRONG_SECOND = "complex() can't take second arg if first is a string"
 ERR_MALFORMED = "complex() arg is a malformed string"
 
+complex_conjugate = StdObjSpaceMultiMethod('conjugate', 1)
+
+register_all(vars(),globals())
+
 def _split_complex(s):
     slen = len(s)
     if slen == 0:
@@ -122,10 +128,10 @@
         raise OperationError(space.w_TypeError, space.wrap(ERR_WRONG_SECOND))
     # if arguments can be cast to a float, do it
     try:
-        w_real = cast_float(space,w_real)
+        w_real = space.call_function(space.w_float,w_real) 
     except:pass
     try:
-        w_imag = cast_float(space,w_imag)
+        w_imag = space.call_function(space.w_float,w_imag) 
     except:pass
 
     # test for '__complex__' attribute and get result of
@@ -175,18 +181,9 @@
         return cnum
     else:
         return None
-
-def cast_float(num):
-    return float(num)
 """, filename=__file__)
 
 extract_complex = app.interphook('extract_complex')
-cast_float = app.interphook('cast_float')
-
-def descr_conjugate(space, w_self):
-    assert space.is_true(space.isinstance(w_self, space.w_complex))
-    from pypy.objspace.std.complexobject import W_ComplexObject
-    return W_ComplexObject(space,w_self.realval, -w_self.imagval)
 
 def complexwprop(name):
     def fget(space, w_obj):
@@ -205,5 +202,6 @@
     __new__ = newmethod(descr__new__),
     real = complexwprop('realval'),
     imag = complexwprop('imagval'),
-    conjugate = newmethod(descr_conjugate)
     )
+
+complex_typedef.registermethods(globals())

Modified: pypy/dist/pypy/objspace/std/model.py
==============================================================================
--- pypy/dist/pypy/objspace/std/model.py	(original)
+++ pypy/dist/pypy/objspace/std/model.py	Sat Apr  8 00:14:38 2006
@@ -8,7 +8,6 @@
 import pypy.interpreter.pycode
 import pypy.interpreter.special
 
-WITHCOMPLEX = False
 WITHSET = False
 
 class StdTypeModel:
@@ -21,8 +20,7 @@
             from pypy.objspace.std.booltype   import bool_typedef
             from pypy.objspace.std.inttype    import int_typedef
             from pypy.objspace.std.floattype  import float_typedef
-            if WITHCOMPLEX:
-                from pypy.objspace.std.complextype  import complex_typedef
+            from pypy.objspace.std.complextype  import complex_typedef
             if WITHSET:
                 from pypy.objspace.std.settype import set_typedef
                 from pypy.objspace.std.settype import frozenset_typedef
@@ -48,8 +46,7 @@
         from pypy.objspace.std import boolobject
         from pypy.objspace.std import intobject
         from pypy.objspace.std import floatobject
-        if WITHCOMPLEX:
-            from pypy.objspace.std import complexobject
+        from pypy.objspace.std import complexobject
         if WITHSET:
             from pypy.objspace.std import setobject
         from pypy.objspace.std import tupleobject
@@ -90,11 +87,11 @@
             pypy.interpreter.pycode.PyCode: [],
             pypy.interpreter.special.Ellipsis: [],
             }
-        if WITHCOMPLEX:
-            self.typeorder[complexobject.W_ComplexObject] = []
+        self.typeorder[complexobject.W_ComplexObject] = []
         if WITHSET:
             self.typeorder[setobject.W_SetObject] = []
             self.typeorder[setobject.W_FrozensetObject] = []
+            self.typeorder[setobject.W_SetIterObject] = []
         for type in self.typeorder:
             self.typeorder[type].append((type, None))
 
@@ -111,36 +108,25 @@
             (intobject.W_IntObject,     boolobject.delegate_Bool2Int),
             (longobject.W_LongObject,   longobject.delegate_Bool2Long),
             (floatobject.W_FloatObject, floatobject.delegate_Bool2Float),
-            #(complexobject.W_ComplexObject, complexobject.delegate_Bool2Complex),
+            (complexobject.W_ComplexObject, complexobject.delegate_Bool2Complex),
             ]
         self.typeorder[intobject.W_IntObject] += [
             (longobject.W_LongObject,   longobject.delegate_Int2Long),
             (floatobject.W_FloatObject, floatobject.delegate_Int2Float),
-            #(complexobject.W_ComplexObject, complexobject.delegate_Int2Complex),
+            (complexobject.W_ComplexObject, complexobject.delegate_Int2Complex),
             ]
         self.typeorder[longobject.W_LongObject] += [
             (floatobject.W_FloatObject, floatobject.delegate_Long2Float),
-            #(complexobject.W_ComplexObject, complexobject.delegate_Long2Complex),
+            (complexobject.W_ComplexObject, 
+                    complexobject.delegate_Long2Complex),
             ]
         self.typeorder[floatobject.W_FloatObject] += [
-            #(complexobject.W_ComplexObject, complexobject.delegate_Float2Complex),
+            (complexobject.W_ComplexObject, 
+                    complexobject.delegate_Float2Complex),
             ]
         self.typeorder[stringobject.W_StringObject] += [
          (unicodeobject.W_UnicodeObject, unicodeobject.delegate_String2Unicode),
             ]
-        if WITHCOMPLEX:
-            self.typeorder[boolobject.W_BoolObject] += [
-                (complexobject.W_ComplexObject, complexobject.delegate_Bool2Complex),
-                ]
-            self.typeorder[intobject.W_IntObject] += [
-                (complexobject.W_ComplexObject, complexobject.delegate_Int2Complex),
-                ]
-            self.typeorder[longobject.W_LongObject] += [
-                (complexobject.W_ComplexObject, complexobject.delegate_Long2Complex),
-                ]
-            self.typeorder[floatobject.W_FloatObject] += [
-                (complexobject.W_ComplexObject, complexobject.delegate_Float2Complex),
-                ]
 
         # put W_Root everywhere
         self.typeorder[W_Root] = []

Modified: pypy/dist/pypy/objspace/std/objspace.py
==============================================================================
--- pypy/dist/pypy/objspace/std/objspace.py	(original)
+++ pypy/dist/pypy/objspace/std/objspace.py	Sat Apr  8 00:14:38 2006
@@ -7,7 +7,7 @@
 from pypy.tool.cache import Cache 
 from pypy.tool.sourcetools import func_with_new_name
 from pypy.objspace.std.model import W_Object, UnwrapError
-from pypy.objspace.std.model import WITHCOMPLEX, WITHSET
+from pypy.objspace.std.model import WITHSET
 from pypy.objspace.std.model import W_ANY, StdObjSpaceMultiMethod, StdTypeModel
 from pypy.objspace.std.multimethod import FailedToImplement
 from pypy.objspace.descroperation import DescrOperation
@@ -292,21 +292,7 @@
                                        self.wrap(x.stop),
                                        self.wrap(x.step))
         if isinstance(x, complex):
-            if WITHCOMPLEX:
-                return W_ComplexObject(self, x.real, x.imag)
-            else:
-                c = self.builtin.get('complex') 
-                return self.call_function(c,
-                                          self.wrap(x.real), 
-                                          self.wrap(x.imag))
-        # SD disable for native complex
-        #if isinstance(x, complex):
-            # XXX is this right?   YYY no, this is wrong right now  (CT)
-            # ZZZ hum, seems necessary for complex literals in co_consts (AR)
-            # c = self.builtin.get('complex') 
-        #    return self.call_function(c,
-        #                              self.wrap(x.real), 
-        #                              self.wrap(x.imag))
+            return W_ComplexObject(self, x.real, x.imag)
 
         if isinstance(x, set):
             if WITHSET:
@@ -359,10 +345,8 @@
     def newfloat(self, floatval):
         return W_FloatObject(self, floatval)
 
-    # SD needed for complex
-    if WITHCOMPLEX:
-        def newcomplex(self, realval, imagval):
-            return W_ComplexObject(self, realval, imagval)
+    def newcomplex(self, realval, imagval):
+        return W_ComplexObject(self, realval, imagval)
 
     if WITHSET:
         def newset(self, list_w):

Modified: pypy/dist/pypy/objspace/std/setobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/setobject.py	(original)
+++ pypy/dist/pypy/objspace/std/setobject.py	Sat Apr  8 00:14:38 2006
@@ -5,114 +5,422 @@
 from pypy.rpython.objectmodel import r_dict
 from pypy.interpreter import gateway
 
-def _set_init(w_self, space, wrappeditems):
-    W_Object.__init__(w_self, space)
-    w_self.data = data = r_dict(space.eq_w, space.hash_w)
-    if space.is_true(space.isinstance(wrappeditems, space.w_frozenset)):
-        data.update(wrappeditems.data)
-    elif space.is_true(space.isinstance(wrappeditems, space.w_set)):
-        data.update(wrappeditems.data)
-    else:
-        iterable_w = space.unpackiterable(wrappeditems)
-        for w_item in iterable_w:
-            w_self.data[w_item] = space.w_True
-
-
-class W_SetObject(W_Object):
-    from pypy.objspace.std.settype import set_typedef as typedef
+class W_BaseSetObject(W_Object):
 
     def __init__(w_self, space, wrappeditems):
-        _set_init(w_self, space, wrappeditems)
+        W_Object.__init__(w_self, space)
+        w_self.setdata = setdata = r_dict(space.eq_w, space.hash_w)
+        iterable_w = space.unpackiterable(wrappeditems)
+        for w_item in iterable_w:
+            w_self.setdata[w_item] = None
 
     def __repr__(w_self):
         """representation for debugging purposes"""
-        reprlist = [repr(w_item) for w_item in w_self.data.keys()]
+        reprlist = [repr(w_item) for w_item in w_self.setdata.keys()]
         return "<%s(%s)>" % (w_self.__class__.__name__, ', '.join(reprlist))
 
-class W_FrozensetObject(W_Object):
-    from pypy.objspace.std.settype import frozenset_typedef as typedef
+class W_SetObject(W_BaseSetObject):
+    from pypy.objspace.std.settype import set_typedef as typedef
 
-    def __init__(w_self, space, wrappeditems):
-        _set_init(w_self, space, wrappeditems)
+class W_FrozensetObject(W_BaseSetObject):
+    from pypy.objspace.std.settype import frozenset_typedef as typedef
 
 registerimplementation(W_SetObject)
 registerimplementation(W_FrozensetObject)
 
-app = gateway.applevel("""
-    def and__Set_Set(s, o):
-        return s.intersection(o)
+class W_SetIterObject(W_Object):
+    from pypy.objspace.std.settype import setiter_typedef as typedef
+
+    def __init__(w_self, space, setdata):
+        W_Object.__init__(w_self, space)
+        w_self.content = content = setdata
+        w_self.len = len(content)
+        w_self.pos = 0
+        w_self.iterator = w_self.content.iterkeys()
+
+    def next_entry(w_self):
+        for w_key in w_self.iterator:
+            return w_key
+        else:
+            return None
+
+registerimplementation(W_SetIterObject)
+
+def iter__SetIterObject(space, w_setiter):
+    return w_setiter
+
+def next__SetIterObject(space, w_setiter):
+    content = w_setiter.content
+    if content is not None:
+        if w_setiter.len != len(content):
+            w_setiter.len = -1   # Make this error state sticky
+            raise OperationError(space.w_RuntimeError,
+                     space.wrap("dictionary changed size during iteration"))
+        # look for the next entry
+        w_result = w_setiter.next_entry()
+        if w_result is not None:
+            w_setiter.pos += 1
+            return w_result
+        # no more entries
+        w_setiter.content = None
+    raise OperationError(space.w_StopIteration, space.w_None)
+
+def len__SetIterObject(space, w_setiter):
+    content = w_setiter.content
+    if content is None or w_setiter.len == -1:
+        return space.wrap(0)
+    return space.wrap(w_setiter.len - w_setiter.pos)
+
+# some helper functions
+
+def _dict_to_set(space, rpdict):
+    return space.newset(W_SetIterObject(space, rpdict))
+
+def _dict_to_frozenset(space, rpdict):
+    #return space.newfrozenset(space.newtuple(rpdict.keys()))
+    return space.newfrozenset(W_SetIterObject(space, rpdict))
+
+# helper functions for set operation on dicts
+
+def _union_dict(space, ldict, rdict, isupdate):
+    if isupdate:
+        ld = ldict
+    else:
+        ld = ldict.copy()
+    ld.update(rdict)
+    return ld, rdict
+
+def _difference_dict(space, ldict, rdict, isupdate):
+    if isupdate:
+        ld = ldict
+    else:
+        ld = ldict.copy()
+    del_list_w = []
+    for w_key in ld.iterkeys():
+        if w_key in rdict:
+            del_list_w.append(w_key)
+    for w_key in del_list_w:
+        del ld[w_key]
+
+    return ld, rdict
+
+def _intersection_dict(space, ldict, rdict, isupdate):
+    if isupdate:
+        ld = ldict
+    else:
+        ld = ldict.copy()
+    del_list_w = []
+    for w_key in ld.iterkeys():
+        if w_key not in rdict:
+            del_list_w.append(w_key)
+
+    for w_key in del_list_w:
+        del ld[w_key]
+
+    return ld, rdict
+
+
+def _symmetric_difference_dict(space, ldict, rdict, isupdate):
+    if isupdate:
+        ld = ldict
+    else:
+        ld = ldict.copy()
+    del_list_w = []
+    add_list_w = []
+    for w_key in ld.iterkeys():
+        if w_key in rdict:
+            del_list_w.append(w_key)
+
+    for w_key in rdict.iterkeys():
+        if w_key not in ld:
+            add_list_w.append(w_key)
+
+    for w_key in del_list_w:
+        del ld[w_key]
+
+    for w_key in add_list_w:
+        ld[w_key] = None
+
+    return ld, rdict
+
+#end helper functions
+
+def set_update__Set_Set(space, w_left, w_other):
+    """Update a set with the union of itself and another."""
+    ld, rd = w_left.setdata, w_other.setdata
+    new_ld, rd = _union_dict(space, ld, rd, True)
+    return space.w_None
+
+set_update__Set_Frozenset = set_update__Set_Set
+
+def inplace_or__Set_Set(space, w_left, w_other):
+    set_update__Set_Set(space, w_left, w_other)
+    return w_left
+
+inplace_or__Set_Frozenset = inplace_or__Set_Set
+
+def set_add__Set_ANY(space, w_left, w_other):
+    """Add an element to a set.
+
+    This has no effect if the element is already present.
+    """
+    w_left.setdata[w_other] = None
+    return space.w_None
+
+def set_copy__Set(space, w_left):
+    return space.newset(w_left)
+
+def frozenset_copy_Frozenset(space, w_left):
+    return space.newfrozenset(w_left)
+
+def set_clear__Set(space, w_left):
+    w_left.setdata.clear()
+    return space.w_None
+
+def set_difference__Set_Set(space, w_left, w_other):
+    ld, rd = w_left.setdata, w_other.setdata
+    new_ld, rd = _difference_dict(space, ld, rd, False)
+    return _dict_to_set(space, new_ld)
+
+set_difference__Set_Frozenset = set_difference__Set_Set
+sub__Set_Set = set_difference__Set_Set
+sub__Set_Frozenset = set_difference__Set_Set
+
+def frozenset_difference__Frozenset_Set(space, w_left, w_other):
+    ld, rd = w_left.setdata, w_other.setdata
+    new_ld, rd = _difference_dict(space, ld, rd, False)
+    return _dict_to_frozenset(space, new_ld)
+
+frozenset_difference__Frozenset_Frozenset = frozenset_difference__Frozenset_Set
+sub__Frozenset_Set = frozenset_difference__Frozenset_Set
+sub__Frozenset_Frozenset = frozenset_difference__Frozenset_Set
+
+
+def set_difference_update__Set_Set(space, w_left, w_other):
+    ld, rd = w_left.setdata, w_other.setdata
+    new_ld, rd = _difference_dict(space, ld, rd, True)
+    return space.w_None
+
+set_difference_update__Set_Frozenset = set_difference_update__Set_Set
+
+def inplace_sub__Set_Set(space, w_left, w_other):
+    set_difference_update__Set_Set(space, w_left, w_other)
+    return w_left
+
+inplace_sub__Set_Frozenset = inplace_sub__Set_Set
+
+def eq__Set_Set(space, w_left, w_other):
+    if space.is_w(w_left, w_other):
+        return space.w_True
+
+    if len(w_left.setdata) != len(w_other.setdata):
+        return space.w_False
+
+    for w_key in w_left.setdata.iterkeys():
+        if w_key not in w_other.setdata:
+            return space.w_False
+    return space.w_True
+
+eq__Set_Frozenset = eq__Set_Set
+eq__Frozenset_Frozenset = eq__Set_Set
+eq__Frozenset_Set = eq__Set_Set
+
+def contains__Set_ANY(space, w_left, w_other):
+    return space.newbool(w_other in w_left.setdata)
+
+contains__Frozenset_ANY = contains__Set_ANY
+
+def set_issubset__Set_Set(space, w_left, w_other):
+    if space.is_w(w_left, w_other):
+        return space.w_True
+
+    ld, rd = w_left.setdata, w_other.setdata
+    if len(ld) > len(rd):
+        return space.w_False
+
+    for w_key in ld:
+        if w_key not in rd:
+            return space.w_False
+    return space.w_True
+
+set_issubset__Set_Frozenset = set_issubset__Set_Set
+frozenset_issubset__Frozenset_Set = set_issubset__Set_Set
+frozenset_issubset__Frozenset_Frozenset = set_issubset__Set_Set
+
+le__Set_Set = set_issubset__Set_Set
+le__Set_Frozenset = set_issubset__Set_Set
+le__Frozenset_Frozenset = set_issubset__Set_Set
+
+def set_issuperset__Set_Set(space, w_left, w_other):
+    if space.is_w(w_left, w_other):
+        return space.w_True
+
+    ld, rd = w_left.setdata, w_other.setdata
+    if len(ld) < len(rd):
+        return space.w_False
+
+    for w_key in rd:
+        if w_key not in ld:
+            return space.w_False
+    return space.w_True
+
+set_issuperset__Set_Frozenset = set_issuperset__Set_Set
+frozenset_issuperset__Frozenset_Set = set_issuperset__Set_Set
+frozenset_issuperset__Frozenset_Frozenset = set_issuperset__Set_Set
+
+ge__Set_Set = set_issuperset__Set_Set
+ge__Set_Frozenset = set_issuperset__Set_Set
+ge__Frozenset_Frozenset = set_issuperset__Set_Set
+
+def set_discard__Set_ANY(space, w_left, w_item):
+    if w_item in w_left.setdata:
+        del w_left.setdata[w_item]
+    
+def set_remove__Set_ANY(space, w_left, w_item):
+    try:
+        del w_left.setdata[w_item]
+    except KeyError:
+        raise OperationError(space.w_KeyError,
+                space.call_method(w_item,'__repr__'))
+
+def hash__Set(space, w_left):
+    raise OperationError(space.w_TypeError,
+            space.wrap('set objects are unhashable'))
+
+def set_pop__Set(space, w_left):
+    if len(w_left.setdata) == 0:
+        raise OperationError(space.w_KeyError,
+                                space.wrap('pop from an empty set'))
+    w_keys = w_left.setdata.keys()
+    w_value = w_keys[0]
+    del w_left.setdata[w_value]
+
+    return w_value
+
+def set_intersection__Set_Set(space, w_left, w_other):
+    ld, rd = w_left.setdata, w_other.setdata
+    new_ld, rd = _intersection_dict(space, ld, rd, False)
+    return _dict_to_set(space, new_ld)
+
+set_intersection__Set_Frozenset = set_intersection__Set_Set
+and__Set_Set = set_intersection__Set_Set
+and__Set_Frozenset = set_intersection__Set_Set
+
+def frozenset_intersection__Frozenset_Set(space, w_left, w_other):
+    ld, rd = w_left.setdata, w_other.setdata
+    new_ld, rd = _intersection_dict(space, ld, rd, False)
+    return _dict_to_frozenset(space, new_ld)
+
+frozenset_intersection__Frozenset_Frozenset = \
+        frozenset_intersection__Frozenset_Set
+and__Frozenset_Set = frozenset_intersection__Frozenset_Set
+and__Frozenset_Frozenset = frozenset_intersection__Frozenset_Set
+
+def set_intersection_update__Set_Set(space, w_left, w_other):
+    ld, rd = w_left.setdata, w_other.setdata
+    new_ld, rd = _intersection_dict(space, ld, rd, True)
+    return space.w_None
+
+set_intersection_update__Set_Frozenset = set_intersection_update__Set_Set
+
+def inplace_and__Set_Set(space, w_left, w_other):
+    set_intersection_update__Set_Set(space, w_left, w_other)
+    return w_left
+
+inplace_and__Set_Frozenset = inplace_and__Set_Set
+
+def set_symmetric_difference__Set_Set(space, w_left, w_other):
+    ld, rd = w_left.setdata, w_other.setdata
+    new_ld, rd = _symmetric_difference_dict(space, ld, rd, False)
+    return _dict_to_set(space, new_ld)
+
+set_symmetric_difference__Set_Frozenset = set_symmetric_difference__Set_Set
+xor__Set_Set = set_symmetric_difference__Set_Set
+xor__Set_Frozenset = set_symmetric_difference__Set_Set
+
+def frozenset_symmetric_difference__Frozenset_Set(space, w_left, w_other):
+    ld, rd = w_left.setdata, w_other.setdata
+    new_ld, rd = _symmetric_difference_dict(space, ld, rd, False)
+    return _dict_to_frozenset(space, new_ld)
+
+frozenset_symmetric_difference__Frozenset_Frozenset = \
+        frozenset_symmetric_difference__Frozenset_Set
+xor__Frozenset_Set = frozenset_symmetric_difference__Frozenset_Set
+xor__Frozenset_Frozenset = frozenset_symmetric_difference__Frozenset_Set
+
+def set_symmetric_difference_update__Set_Set(space, w_left, w_other):
+    ld, rd = w_left.setdata, w_other.setdata
+    new_ld, rd = _symmetric_difference_dict(space, ld, rd, True)
+    return space.w_None
+
+set_symmetric_difference_update__Set_Frozenset = \
+        set_symmetric_difference_update__Set_Set
+
+def inplace_xor__Set_Set(space, w_left, w_other):
+    set_symmetric_difference_update__Set_Set(space, w_left, w_other)
+    return w_left
+
+inplace_xor__Set_Frozenset = inplace_xor__Set_Set
+
+def set_union__Set_Set(space, w_left, w_other):
+    ld, rd = w_left.setdata, w_other.setdata
+    new_ld, rd = _union_dict(space, ld, rd, False)
+    return _dict_to_set(space, new_ld)
+
+set_union__Set_Frozenset = set_union__Set_Set
+or__Set_Set = set_union__Set_Set
+or__Set_Frozenset = set_union__Set_Set
+
+def frozenset_union__Frozenset_Set(space, w_left, w_other):
+    ld, rd = w_left.setdata, w_other.setdata
+    new_ld, rd = _union_dict(space, ld, rd, False)
+    return _dict_to_frozenset(space, new_ld)
+
+frozenset_union__Frozenset_Frozenset = frozenset_union__Frozenset_Set
+or__Frozenset_Set = frozenset_union__Frozenset_Set
+or__Frozenset_Frozenset = frozenset_union__Frozenset_Set
+
+def len__Set(space, w_left):
+    return space.newint(len(w_left.setdata))
+
+len__Frozenset = len__Set
+
+def iter__Set(space, w_left):
+    return W_SetIterObject(space, w_left.setdata)
 
+iter__Frozenset = iter__Set
+
+def cmp__Set_Set(space, w_left, w_other):
+    raise OperationError(space.w_TypeError,
+            space.wrap('cannot compare sets using cmp()'))
+
+cmp__Set_Frozenset = cmp__Set_Set
+cmp__Frozenset_Frozenset = cmp__Set_Set
+cmp__Frozenset_Set = cmp__Set_Set
+
+app = gateway.applevel("""
     def ne__Set_Set(s, o):
         return not s == o
 
-    def ge__Set_Set(s, o):
-        return s.issuperset(o)
-
     def gt__Set_Set(s, o):
         return s != o and s.issuperset(o)
 
-    def le__Set_Set(s, o):
-        return s.issubset(o)
-
     def lt__Set_Set(s, o):
         return s != o and s.issubset(o)
 
-    def cmp__Set_Set(s, o):
-        raise TypeError('cannot compare sets using cmp()')
-
-    def or__Set_Set(s, o):
-        return s.union(o)
-
-    def xor__Set_Set(s, o):
-        return s.symmetric_difference(o)
-
     def repr__Set(s):
         return 'set(%s)' % [x for x in s]
 
     def repr__Frozenset(s):
         return 'frozenset(%s)' % [x for x in s]
 
-    def sub__Set_Set(s, o):
-        return s.difference(o)
-
-    def isub__Set_Set(s, o):
-        s.difference_update(o)
-        return s
-
-    def ior__Set_Set(s, o):
-        s.update(o)
-        return s
-
-    def iand__Set_Set(s, o):
-        s.intersection_update(o)
-        return s
-
-    def ixor__Set_Set(s, o):
-        s.symmetric_difference_update(o)
-        return s
-
 """, filename=__file__)
 
-and__Set_Set = app.interphook("and__Set_Set")
-and__Set_Frozenset = and__Set_Set
-and__Frozenset_Set = and__Set_Set
-and__Frozenset_Frozenset = and__Set_Set
-
 ne__Set_Set = app.interphook("ne__Set_Set")
 ne__Set_Frozenset = ne__Set_Set
 ne__Frozenset_Set = ne__Set_Set
 ne__Frozenset_Frozenset = ne__Set_Set
 
-ge__Set_Set = app.interphook("ge__Set_Set")
-ge__Set_Frozenset = ge__Set_Set
-ge__Frozenset_Set = ge__Set_Set
-ge__Frozenset_Frozenset = ge__Set_Set
-
-le__Set_Set = app.interphook("le__Set_Set")
-le__Set_Frozenset = le__Set_Set
-le__Frozenset_Set = le__Set_Set
-le__Frozenset_Frozenset = le__Set_Set
-
 gt__Set_Set = app.interphook("gt__Set_Set")
 gt__Set_Frozenset = gt__Set_Set
 gt__Frozenset_Set = gt__Set_Set
@@ -123,39 +431,8 @@
 lt__Frozenset_Set = lt__Set_Set
 lt__Frozenset_Frozenset = lt__Set_Set
 
-cmp__Set_Set = app.interphook("cmp__Set_Set")
-cmp__Set_Frozenset = cmp__Set_Set
-cmp__Frozenset_Frozenset = cmp__Set_Set
-cmp__Frozenset_Set = cmp__Set_Set
-
-or__Set_Set = app.interphook("or__Set_Set")
-or__Set_Frozenset = or__Set_Set
-or__Frozenset_Set = or__Set_Set
-or__Frozenset_Frozenset = or__Set_Set
-
-xor__Set_Set = app.interphook("xor__Set_Set")
-xor__Set_Frozenset = xor__Set_Set
-xor__Frozenset_Set = xor__Set_Set
-xor__Frozenset_Frozenset = xor__Set_Set
-
 repr__Set = app.interphook('repr__Set')
 repr__Frozenset = app.interphook('repr__Frozenset')
 
-sub__Set_Set = app.interphook('sub__Set_Set')
-sub__Set_Frozenset = sub__Set_Set
-sub__Frozenset_Set = sub__Set_Set
-sub__Frozenset_Frozenset = sub__Set_Set
-
-inplace_sub__Set_Set = app.interphook('isub__Set_Set')
-inplace_sub__Set_Frozenset = inplace_sub__Set_Set
-
-inplace_or__Set_Set = app.interphook('ior__Set_Set')
-inplace_or__Set_Frozenset = inplace_or__Set_Set
-
-inplace_and__Set_Set = app.interphook('iand__Set_Set')
-inplace_and__Set_Frozenset = inplace_and__Set_Set
-
-inplace_xor__Set_Set = app.interphook('ixor__Set_Set')
-inplace_xor__Set_Frozenset = inplace_xor__Set_Set
-
-register_all(vars())
+from pypy.objspace.std import settype
+register_all(vars(), settype)

Modified: pypy/dist/pypy/objspace/std/settype.py
==============================================================================
--- pypy/dist/pypy/objspace/std/settype.py	(original)
+++ pypy/dist/pypy/objspace/std/settype.py	Sat Apr  8 00:14:38 2006
@@ -1,11 +1,38 @@
 from pypy.interpreter.error import OperationError
 from pypy.objspace.std.objspace import register_all
 from pypy.objspace.std.stdtypedef import StdTypeDef, newmethod
-from pypy.objspace.std.stdtypedef import StdObjSpaceMultiMethod
+from pypy.objspace.std.stdtypedef import StdObjSpaceMultiMethod as SMM
 from pypy.interpreter.gateway import NoneNotWrapped
 from pypy.interpreter import gateway
 from pypy.objspace.std.model import WITHSET
 
+set_add                         = SMM('add', 2)
+set_clear                       = SMM('clear', 1)
+set_copy                        = SMM('copy', 1)
+set_difference                  = SMM('difference', 2)
+set_difference_update           = SMM('difference_update', 2)
+set_discard                     = SMM('discard', 2)
+set_intersection                = SMM('intersection', 2)
+set_intersection_update         = SMM('intersection_update', 2)
+set_issubset                    = SMM('issubset', 2)
+set_issuperset                  = SMM('issuperset', 2)
+set_pop                         = SMM('pop', 1)
+set_remove                      = SMM('remove', 2)
+set_symmetric_difference        = SMM('symmetric_difference', 2)
+set_symmetric_difference_update = SMM('symmetric_difference_update', 2)
+set_union                       = SMM('union', 2)
+set_update                      = SMM('update', 2)
+
+frozenset_copy                  = SMM('copy', 1)
+frozenset_difference            = SMM('difference', 2)
+frozenset_intersection          = SMM('intersection', 2)
+frozenset_issubset              = SMM('issubset', 2)
+frozenset_issuperset            = SMM('issuperset', 2)
+frozenset_symmetric_difference  = SMM('symmetric_difference', 2)
+frozenset_union                 = SMM('union', 2)
+
+register_all(vars(), globals())
+
 def descr__set__new__(space, w_settype, w_iterable=NoneNotWrapped):
     from pypy.objspace.std.setobject import W_SetObject
     if w_iterable is None:
@@ -30,281 +57,22 @@
 
     return w_obj
 
-# some helper functions
-
-def _extract_data_dict(space, w_left, w_right):
-    assert (space.is_true(space.isinstance(w_left, space.w_set)) or 
-        space.is_true(space.isinstance(w_left, space.w_frozenset)))
-    if not (space.is_true(space.isinstance(w_right, space.w_set)) or 
-            space.is_true(space.isinstance(w_right, space.w_frozenset))):
-        w_right = space.newset(w_right)
-
-    return w_left.data, w_right.data
-
-def _dict_to_set(space, rpdict):
-    return space.newset(space.newtuple(rpdict.keys()))
-
-def _dict_to_frozenset(space, rpdict):
-    return space.newfrozenset(space.newtuple(rpdict.keys()))
-
-# helper functions for set operation on dicts
-
-def _union_dict(space, ldict, rdict, isupdate):
-    if isupdate:
-        ld = ldict
-    else:
-        ld = ldict.copy()
-    ld.update(rdict)
-    return ld, rdict
-
-def _difference_dict(space, ldict, rdict, isupdate):
-    if isupdate:
-        ld = ldict
-    else:
-        ld = ldict.copy()
-    del_list_w = []
-    for w_key in ld.iterkeys():
-        if w_key in rdict:
-            del_list_w.append(w_key)
-    for w_key in del_list_w:
-        del ld[w_key]
-
-    return ld, rdict
-
-def _intersection_dict(space, ldict, rdict, isupdate):
-    if isupdate:
-        ld = ldict
-    else:
-        ld = ldict.copy()
-    del_list_w = []
-    for w_key in ld.iterkeys():
-        if w_key not in rdict:
-            del_list_w.append(w_key)
-
-    for w_key in del_list_w:
-        del ld[w_key]
-
-    return ld, rdict
-
-
-def _symmetric_difference_dict(space, ldict, rdict, isupdate):
-    if isupdate:
-        ld = ldict
-    else:
-        ld = ldict.copy()
-    del_list_w = []
-    add_list_w = []
-    for w_key in ld.iterkeys():
-        if w_key in rdict:
-            del_list_w.append(w_key)
-
-    for w_key in rdict.iterkeys():
-        if w_key not in ld:
-            add_list_w.append(w_key)
-
-    for w_key in del_list_w:
-        del ld[w_key]
-
-    for w_key in add_list_w:
-        ld[w_key] = space.w_True
-
-    return ld, rdict
-
-def descr_update(space, w_self, w_iterable):
-    """Update a set with the union of itself and another."""
-    ld, rd = _extract_data_dict(space, w_self, w_iterable)
-    new_ld, rd = _union_dict(space, ld, rd, True)
-    return space.w_None
-
-def descr_add(space, w_self, w_other):
-    """Add an element to a set.
-
-    This has no effect if the element is already present.
-    """
-
-    w_self.data[w_other] = space.w_True
-
-def descr_copy_s(space, w_self):
-    return space.newset(w_self)
-
-def descr_copy_fs(space, w_self):
-    return space.newfrozenset(w_self)
-
-def descr_clear(space, w_self):
-    w_self.data.clear()
-
-def descr_difference_s(space, w_self, w_other):
-    ld, rd = _extract_data_dict(space, w_self, w_other)
-    new_ld, rd = _difference_dict(space, ld, rd, False)
-    return _dict_to_set(space, new_ld)
-
-def descr_difference_fs(space, w_self, w_other):
-    ld, rd = _extract_data_dict(space, w_self, w_other)
-    new_ld, rd = _difference_dict(space, ld, rd, False)
-    return _dict_to_frozenset(space, new_ld)
-
-
-def descr_difference_update(space, w_self, w_other):
-    ld, rd = _extract_data_dict(space, w_self, w_other)
-    new_ld, rd = _difference_dict(space, ld, rd, True)
-    return space.w_None
-
-def descr__set__eq__(space, w_self, w_other):
-    if space.is_w(w_self, w_other):
-        return space.w_True
-
-    if len(w_self.data) != len(w_other.data):
-        return space.w_False
-
-    for w_key in w_self.data.iterkeys():
-        if w_key not in w_other.data:
-            return space.w_False
-    return space.w_True
-
-def descr__set__contains__(space, w_self, w_other):
-    return space.newbool(w_other in w_self.data)
-
-def descr_issubset(space, w_self, w_other):
-    if space.is_w(w_self, w_other):
-        return space.w_True
-
-    if len(w_self.data) > len(w_other.data):
-        return space.w_False
-
-    for w_key in w_self.data:
-        if w_key not in w_other.data:
-            return space.w_False
-    return space.w_True
-
-def descr_issuperset(space, w_self, w_other):
-    if space.is_w(w_self, w_other):
-        return space.w_True
-
-    if len(w_self.data) < len(w_other.data):
-        return space.w_False
-
-    for w_key in w_other.data:
-        if w_key not in w_self.data:
-            return space.w_False
-    return space.w_True
-
-def descr_discard(space, w_self, w_item):
-    if w_item in w_self.data:
-        del w_self.data[w_item]
-    
-def descr_remove(space, w_self, w_item):
-    try:
-        del w_self.data[w_item]
-    except KeyError:
-        raise OperationError(space.w_KeyError,
-                space.call_method(w_item,'__repr__'))
-
-def descr__set__hash__(space, w_self):
-    raise OperationError(space.w_TypeError,
-            space.wrap('set objects are unhashable'))
-
-def descr_pop(space, w_self):
-    if len(w_self.data) == 0:
-        raise OperationError(space.w_KeyError,
-                                space.wrap('pop from an empty set'))
-    w_keys = w_self.data.keys()
-    w_value = w_keys[0]
-    del w_self.data[w_value]
-
-    return w_value
-
-def descr_intersection_s(space, w_self, w_other):
-    ld, rd = _extract_data_dict(space, w_self, w_other)
-    new_ld, rd = _intersection_dict(space, ld, rd, False)
-    return _dict_to_set(space, new_ld)
-
-def descr_intersection_fs(space, w_self, w_other):
-    ld, rd = _extract_data_dict(space, w_self, w_other)
-    new_ld, rd = _intersection_dict(space, ld, rd, False)
-    return _dict_to_frozenset(space, new_ld)
-
-def descr_intersection_update(space, w_self, w_other):
-    ld, rd = _extract_data_dict(space, w_self, w_other)
-    new_ld, rd = _intersection_dict(space, ld, rd, True)
-    return space.w_None
-
-def descr_symmetric_difference_s(space, w_self, w_other):
-    ld, rd = _extract_data_dict(space, w_self, w_other)
-    new_ld, rd = _symmetric_difference_dict(space, ld, rd, False)
-    return _dict_to_set(space, new_ld)
-
-def descr_symmetric_difference_fs(space, w_self, w_other):
-    ld, rd = _extract_data_dict(space, w_self, w_other)
-    new_ld, rd = _symmetric_difference_dict(space, ld, rd, False)
-    return _dict_to_frozenset(space, new_ld)
-
-def descr_symmetric_difference_update(space, w_self, w_other):
-    ld, rd = _extract_data_dict(space, w_self, w_other)
-    new_ld, rd = _symmetric_difference_dict(space, ld, rd, True)
-    return space.w_None
-
-def descr_union_s(space, w_self, w_other):
-    ld, rd = _extract_data_dict(space, w_self, w_other)
-    new_ld, rd = _union_dict(space, ld, rd, False)
-    return _dict_to_set(space, new_ld)
-
-def descr_union_fs(space, w_self, w_other):
-    ld, rd = _extract_data_dict(space, w_self, w_other)
-    new_ld, rd = _union_dict(space, ld, rd, False)
-    return _dict_to_frozenset(space, new_ld)
-
-def descr__set__len__(space, w_self):
-    return space.newint(len(w_self.data))
-
-def descr__set__iter__(space, w_self):
-    from pypy.objspace.std import iterobject
-    return iterobject.W_SeqIterObject(space, 
-                                        space.newtuple(w_self.data.keys()))
-
 set_typedef = StdTypeDef("set",
     __doc__ = """set(iterable) --> set object
 
 Build an unordered collection.""",
     __new__ = newmethod(descr__set__new__),
-    __eq__ = newmethod(descr__set__eq__),
-    __contains__ = newmethod(descr__set__contains__),
-    __len__ = newmethod(descr__set__len__),
-    __iter__ = newmethod(descr__set__iter__),
-    __hash__ = newmethod(descr__set__hash__),
-    add = newmethod(descr_add),
-    clear = newmethod(descr_clear),
-    copy = newmethod(descr_copy_s),
-    difference = newmethod(descr_difference_s),
-    difference_update = newmethod(descr_difference_update),
-    discard = newmethod(descr_discard),
-    intersection = newmethod(descr_intersection_s),
-    intersection_update = newmethod(descr_intersection_update),
-    issubset = newmethod(descr_issubset),
-    issuperset = newmethod(descr_issuperset),
-    pop = newmethod(descr_pop),
-    remove = newmethod(descr_remove),
-    symmetric_difference = newmethod(descr_symmetric_difference_s),
-    symmetric_difference_update = newmethod(descr_symmetric_difference_update),
-    union = newmethod(descr_union_s),
-    update = newmethod(descr_update),
     )
 
-#set_typedef.registermethods(globals())
+set_typedef.registermethods(globals())
 
 frozenset_typedef = StdTypeDef("frozenset",
     __doc__ = """frozenset(iterable) --> frozenset object
 
 Build an immutable unordered collection.""",
     __new__ = newmethod(descr__frozenset__new__),
-    __eq__ = newmethod(descr__set__eq__),
-    __contains__ = newmethod(descr__set__contains__),
-    __len__ = newmethod(descr__set__len__),
-    __iter__ = newmethod(descr__set__iter__),
-    copy = newmethod(descr_copy_fs),
-    difference = newmethod(descr_difference_fs),
-    intersection = newmethod(descr_intersection_fs),
-    issubset = newmethod(descr_issubset),
-    issuperset = newmethod(descr_issuperset),
-    symmetric_difference = newmethod(descr_symmetric_difference_fs),
-    union = newmethod(descr_union_fs),
     )
+
+frozenset_typedef.registermethods(globals())
+
+setiter_typedef = StdTypeDef("setiterator")

Modified: pypy/dist/pypy/objspace/std/test/test_complexobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/test/test_complexobject.py	(original)
+++ pypy/dist/pypy/objspace/std/test/test_complexobject.py	Sat Apr  8 00:14:38 2006
@@ -1,8 +1,5 @@
 import autopath
 import py
-from pypy.objspace.std.model import WITHCOMPLEX
-if not WITHCOMPLEX:
-    py.test.skip("only works if WITHCOMPLEX is enabled")
 from pypy.objspace.std import complexobject as cobj
 from pypy.objspace.std import complextype as cobjtype
 from pypy.objspace.std.objspace import FailedToImplement



More information about the Pypy-commit mailing list