[pypy-svn] r78276 - in pypy/trunk/pypy/objspace/std: . test

cfbolz at codespeak.net cfbolz at codespeak.net
Mon Oct 25 18:46:34 CEST 2010


Author: cfbolz
Date: Mon Oct 25 18:46:32 2010
New Revision: 78276

Modified:
   pypy/trunk/pypy/objspace/std/test/test_typeobject.py
   pypy/trunk/pypy/objspace/std/typeobject.py
Log:
(cfbolz, arigo): you learn something new every day. in cpython you can add slots
that exist as methods as well and it "works": the slot is mostly ignored.


Modified: pypy/trunk/pypy/objspace/std/test/test_typeobject.py
==============================================================================
--- pypy/trunk/pypy/objspace/std/test/test_typeobject.py	(original)
+++ pypy/trunk/pypy/objspace/std/test/test_typeobject.py	Mon Oct 25 18:46:32 2010
@@ -1085,4 +1085,12 @@
 
         assert b == 1
 
-        
+    def test_slots_with_method_in_class(self):
+        # this works in cpython...
+        class A(object):
+            __slots__ = ["f"]
+            def f(self, x):
+                return x + 1
+        a = A()
+        assert a.f(1) == 2
+

Modified: pypy/trunk/pypy/objspace/std/typeobject.py
==============================================================================
--- pypy/trunk/pypy/objspace/std/typeobject.py	(original)
+++ pypy/trunk/pypy/objspace/std/typeobject.py	Mon Oct 25 18:46:32 2010
@@ -561,9 +561,11 @@
     slot_name = _mangle(slot_name, w_self.name)
     # Force interning of slot names.
     slot_name = space.str_w(space.new_interned_str(slot_name))
-    member = Member(w_self.nslots, slot_name, w_self)
-    w_self.dict_w[slot_name] = space.wrap(member)
-    w_self.nslots += 1
+    if slot_name not in w_self.dict_w:
+        # in cpython it is ignored less, but we probably don't care
+        member = Member(w_self.nslots, slot_name, w_self)
+        w_self.dict_w[slot_name] = space.wrap(member)
+        w_self.nslots += 1
 
 def create_dict_slot(w_self):
     if not w_self.hasdict:



More information about the Pypy-commit mailing list