[pypy-svn] r10626 - in pypy/dist/pypy: objspace/std translator translator/test

pedronis at codespeak.net pedronis at codespeak.net
Thu Apr 14 18:36:56 CEST 2005


Author: pedronis
Date: Thu Apr 14 18:36:56 2005
New Revision: 10626

Modified:
   pypy/dist/pypy/objspace/std/objspace.py
   pypy/dist/pypy/translator/ann_override.py
   pypy/dist/pypy/translator/test/test_annrpython.py
Log:
special case instatiate, also a reminder that generators need to do something special for it

also special cased and singled out the code for wrapping builtin exceptions in StdObjSpace.wrap -> 
wrap_exception_cls

specialize by location allocate_instance and added assert isinstance(.,_) (with test for this kind of annotation 
setting), it should allow the various descr__new__ code to call the appropriate __init__ without trouble





Modified: pypy/dist/pypy/objspace/std/objspace.py
==============================================================================
--- pypy/dist/pypy/objspace/std/objspace.py	(original)
+++ pypy/dist/pypy/objspace/std/objspace.py	Thu Apr 14 18:36:56 2005
@@ -197,9 +197,8 @@
             return w_result
         # anything below this line is implicitly XXX'ed
         if isinstance(x, type(Exception)) and issubclass(x, Exception):
-            if hasattr(self, 'w_' + x.__name__):
-                w_result = getattr(self, 'w_' + x.__name__)
-                assert isinstance(w_result, W_TypeObject)
+            w_result = self.wrap_exception_cls(x)
+            if w_result is not None:
                 return w_result
         from fake import fake_type
         if isinstance(x, type):
@@ -209,6 +208,13 @@
         return ft(self, x)
     wrap._specialize_ = "argtypes"
 
+    def wrap_exception_cls(x):
+        """NOT_RPYTHON"""
+        if hasattr(self, 'w_' + x.__name__):
+            w_result = getattr(self, 'w_' + x.__name__)            
+            return w_result
+        return None
+        
     def unwrap(self, w_obj):
         if isinstance(w_obj, BaseWrappable):
             return w_obj
@@ -262,13 +268,16 @@
         user-defined type, without actually __init__ializing the instance."""
         w_type = self.gettypeobject(cls.typedef)
         if self.is_true(self.is_(w_type, w_subtype)):
-            return instantiate(cls)
+            instance =  instantiate(cls)
         else:
             w_type.check_user_subclass(w_subtype)
             subcls = get_unique_interplevel_subclass(cls, w_subtype.hasdict, w_subtype.nslots != 0)
             instance = instantiate(subcls)
             instance.user_setup(self, w_subtype, w_subtype.nslots)
-            return instance
+        assert isinstance(instance, cls)
+        return instance
+    allocate_instance._specialize_ = "location"
+            
 
     def unpacktuple(self, w_tuple, expected_length=None):
         assert isinstance(w_tuple, W_TupleObject)

Modified: pypy/dist/pypy/translator/ann_override.py
==============================================================================
--- pypy/dist/pypy/translator/ann_override.py	(original)
+++ pypy/dist/pypy/translator/ann_override.py	Thu Apr 14 18:36:56 2005
@@ -6,6 +6,8 @@
 from pypy.interpreter import pyframe
 from pypy.objspace.std import fake
 from pypy.module.sys2 import state as sys_state
+import pypy.interpreter.typedef as itypedef
+from pypy.objspace.std.objspace import StdObjSpace
 
 def hole(*args):
     return annmodel.SomeImpossibleValue(benign=True)
@@ -14,6 +16,15 @@
     bk = getbookkeeper()
     return bk.immutablevalue(None)
 
+def instantiate(cls):
+    clsdef = getbookkeeper().getclassdef(itypedef.W_Root)
+    return annmodel.SomeInstance(clsdef)
+
+def wrap_exception_cls(x):
+    import pypy.std.objspace.std.typeobject as typeobject
+    clsdef = getbookkeeper().getclassdef(typeobject.W_TypeObject)
+    return annmodel.SomeInstance(clsdef, can_be_None=True)
+
 pypy_overrides = {}
 
 def install(tgt, override):
@@ -25,3 +36,5 @@
 install(error.OperationError.record_interpreter_traceback, ignore)
 install(sys_state.pypy_getudir, ignore)
 install(fake.wrap_exception, hole)
+install(itypedef.instantiate, instantiate)
+install(StdObjSpace.wrap_exception_cls, wrap_exception_cls)

Modified: pypy/dist/pypy/translator/test/test_annrpython.py
==============================================================================
--- pypy/dist/pypy/translator/test/test_annrpython.py	(original)
+++ pypy/dist/pypy/translator/test/test_annrpython.py	Thu Apr 14 18:36:56 2005
@@ -782,7 +782,30 @@
         a = RPythonAnnotator()
         s = a.build_types(g, [])
         assert s.const == True
-        
+
+    def test_alloc_like(self):
+        class C1(object):
+            pass
+        class C2(object):
+            pass
+
+        def inst(cls):
+            return cls()
+
+        def alloc(cls):
+            i = inst(cls)
+            assert isinstance(i, cls)
+            return i
+        alloc._specialize_ = "location"
+
+        def f():
+            c1 = alloc(C1)
+            c2 = alloc(C2)
+            return c1,c2
+        a = RPythonAnnotator()
+        s = a.build_types(f, [])
+        assert s.items[0].knowntype == C1
+        assert s.items[1].knowntype == C2
 
 
 def g(n):



More information about the Pypy-commit mailing list