[pypy-svn] r30484 - in pypy/dist/pypy/objspace/std: . test

arigo at codespeak.net arigo at codespeak.net
Mon Jul 24 22:35:54 CEST 2006


Author: arigo
Date: Mon Jul 24 22:35:49 2006
New Revision: 30484

Modified:
   pypy/dist/pypy/objspace/std/booltype.py
   pypy/dist/pypy/objspace/std/objspace.py
   pypy/dist/pypy/objspace/std/test/test_boolobject.py
   pypy/dist/pypy/objspace/std/test/test_typeobject.py
Log:
A minor bug on 'bool' (heh :-)

An optimization for the translated PyPy to avoid making all the
user subclasses for types that are not allowed to be subclassed
anyway.


Modified: pypy/dist/pypy/objspace/std/booltype.py
==============================================================================
--- pypy/dist/pypy/objspace/std/booltype.py	(original)
+++ pypy/dist/pypy/objspace/std/booltype.py	Mon Jul 24 22:35:49 2006
@@ -5,6 +5,7 @@
 
 
 def descr__new__(space, w_booltype, w_obj=None):
+    space.w_bool.check_user_subclass(w_booltype)
     if space.is_true(w_obj):
         return space.w_True
     else:

Modified: pypy/dist/pypy/objspace/std/objspace.py
==============================================================================
--- pypy/dist/pypy/objspace/std/objspace.py	(original)
+++ pypy/dist/pypy/objspace/std/objspace.py	Mon Jul 24 22:35:49 2006
@@ -427,11 +427,17 @@
         w_type = self.gettypeobject(cls.typedef)
         if self.is_w(w_type, w_subtype):
             instance =  instantiate(cls)
-        else:
+        elif cls.typedef.acceptable_as_base_class:
+            # the purpose of the above check is to avoid the code below
+            # to be annotated at all for 'cls' if it is not necessary
             w_subtype = w_type.check_user_subclass(w_subtype)
             subcls = get_unique_interplevel_subclass(cls, w_subtype.hasdict, w_subtype.nslots != 0, w_subtype.needsdel, w_subtype.weakrefable)
             instance = instantiate(subcls)
             instance.user_setup(self, w_subtype, w_subtype.nslots)
+        else:
+            raise OperationError(self.w_TypeError,
+                self.wrap("%s.__new__(%s): only for the type %s" % (
+                    w_type.name, w_subtype.getname(self, '?'), w_type.name)))
         assert isinstance(instance, cls)
         return instance
     allocate_instance._annspecialcase_ = "specialize:arg(1)"

Modified: pypy/dist/pypy/objspace/std/test/test_boolobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/test/test_boolobject.py	(original)
+++ pypy/dist/pypy/objspace/std/test/test_boolobject.py	Mon Jul 24 22:35:49 2006
@@ -38,4 +38,10 @@
         assert True & True is True
         assert True ^ True is False
         assert False ^ False is False
-        assert True ^ False is True
\ No newline at end of file
+        assert True ^ False is True
+
+    def test_new(self):
+        assert bool.__new__(bool, "hi") is True
+        assert bool.__new__(bool, "") is False
+        raises(TypeError, bool.__new__, int)
+        raises(TypeError, bool.__new__, 42)

Modified: pypy/dist/pypy/objspace/std/test/test_typeobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/test/test_typeobject.py	(original)
+++ pypy/dist/pypy/objspace/std/test/test_typeobject.py	Mon Jul 24 22:35:49 2006
@@ -1,4 +1,6 @@
 import autopath
+from pypy.objspace.std.objspace import *
+from pypy.objspace.std.stdtypedef import *
 
 ##class TestSpecialMultimethodCode(testit.TestCase):
 
@@ -53,6 +55,29 @@
 ##                               w(-1.5))
 
 
+class TestTypeObject:
+
+    def test_not_acceptable_as_base_class(self):
+        space = self.space
+        class W_Stuff(W_Object):
+            pass
+        def descr__new__(space, w_subtype):
+            return space.allocate_instance(W_Stuff, w_subtype)
+        W_Stuff.typedef = StdTypeDef("stuff",
+                                     __new__ = newmethod(descr__new__))
+        W_Stuff.typedef.acceptable_as_base_class = False
+        w_stufftype = space.gettypeobject(W_Stuff.typedef)
+        space.appexec([w_stufftype], """(stufftype):
+            x = stufftype.__new__(stufftype)
+            assert type(x) is stufftype
+            raises(TypeError, stufftype.__new__)
+            raises(TypeError, stufftype.__new__, int)
+            raises(TypeError, stufftype.__new__, 42)
+            raises(TypeError, stufftype.__new__, stufftype, 511)
+            raises(TypeError, type, 'sub', (stufftype,), {})
+        """)
+
+
 class AppTestTypeObject:
     def test_bases(self):
         assert int.__bases__ == (object,)



More information about the Pypy-commit mailing list