[pypy-svn] r46447 - in pypy/dist/pypy/annotation: . test

arigo at codespeak.net arigo at codespeak.net
Mon Sep 10 19:39:29 CEST 2007


Author: arigo
Date: Mon Sep 10 19:39:28 2007
New Revision: 46447

Modified:
   pypy/dist/pypy/annotation/binaryop.py
   pypy/dist/pypy/annotation/builtin.py
   pypy/dist/pypy/annotation/model.py
   pypy/dist/pypy/annotation/test/test_annrpython.py
   pypy/dist/pypy/annotation/unaryop.py
Log:
Start of an attempt to support simple weakrefs directly in RPython.


Modified: pypy/dist/pypy/annotation/binaryop.py
==============================================================================
--- pypy/dist/pypy/annotation/binaryop.py	(original)
+++ pypy/dist/pypy/annotation/binaryop.py	Mon Sep 10 19:39:28 2007
@@ -11,7 +11,7 @@
 from pypy.annotation.model import SomeTuple, SomeImpossibleValue, s_ImpossibleValue
 from pypy.annotation.model import SomeInstance, SomeBuiltin, SomeIterator
 from pypy.annotation.model import SomePBC, SomeSlice, SomeFloat, s_None
-from pypy.annotation.model import SomeExternalObject
+from pypy.annotation.model import SomeExternalObject, SomeWeakRef
 from pypy.annotation.model import SomeAddress, SomeTypedAddressAccess
 from pypy.annotation.model import SomeWeakGcAddress
 from pypy.annotation.model import SomeCTypesObject
@@ -867,6 +867,16 @@
 
 
 #_________________________________________
+# weakrefs
+
+class __extend__(pairtype(SomeWeakRef, SomeWeakRef)):
+    def union((s_wrf1, s_wrf2)):
+        basedef = s_wrf1.classdef.commonbase(s_wrf2.classdef)
+        if basedef is None:
+            return SomeObject()
+        return SomeWeakRef(basedef)
+
+#_________________________________________
 # memory addresses
 
 class __extend__(pairtype(SomeAddress, SomeAddress)):

Modified: pypy/dist/pypy/annotation/builtin.py
==============================================================================
--- pypy/dist/pypy/annotation/builtin.py	(original)
+++ pypy/dist/pypy/annotation/builtin.py	Mon Sep 10 19:39:28 2007
@@ -8,7 +8,7 @@
 from pypy.annotation.model import SomeUnicodeCodePoint, SomeAddress
 from pypy.annotation.model import SomeFloat, SomeWeakGcAddress, unionof
 from pypy.annotation.model import SomePBC, SomeInstance, SomeDict
-from pypy.annotation.model import SomeExternalObject
+from pypy.annotation.model import SomeExternalObject, SomeWeakRef
 from pypy.annotation.model import annotation_to_lltype, lltype_to_annotation, ll_to_annotation
 from pypy.annotation.model import add_knowntypedata
 from pypy.annotation.model import s_ImpossibleValue
@@ -569,6 +569,18 @@
 BUILTIN_ANALYZERS[ootype.ooidentityhash] = ooidentityhash
 
 #________________________________
+# weakrefs
+
+import weakref
+
+def weakref_ref(s_obj):
+    if not isinstance(s_obj, SomeInstance):
+        raise Exception("cannot take a weakref to %r" % (s_obj,))
+    return SomeWeakRef(s_obj.classdef)
+
+BUILTIN_ANALYZERS[weakref.ref] = weakref_ref
+
+#________________________________
 # non-gc objects
 
 def robjmodel_free_non_gc_object(obj):

Modified: pypy/dist/pypy/annotation/model.py
==============================================================================
--- pypy/dist/pypy/annotation/model.py	(original)
+++ pypy/dist/pypy/annotation/model.py	Mon Sep 10 19:39:28 2007
@@ -492,6 +492,17 @@
 s_ImpossibleValue = SomeImpossibleValue()
 
 # ____________________________________________________________
+# weakrefs
+
+class SomeWeakRef(SomeObject):
+    immutable = True
+    def __init__(self, classdef):
+        self.classdef = classdef
+
+    def can_be_none(self):
+        return False
+
+# ____________________________________________________________
 # memory addresses
 
 from pypy.rpython.lltypesystem import llmemory

Modified: pypy/dist/pypy/annotation/test/test_annrpython.py
==============================================================================
--- pypy/dist/pypy/annotation/test/test_annrpython.py	(original)
+++ pypy/dist/pypy/annotation/test/test_annrpython.py	Mon Sep 10 19:39:28 2007
@@ -2804,6 +2804,31 @@
         s = a.build_types(f, [int])
         assert isinstance(s, annmodel.SomeInteger)
 
+    def test_weakref(self):
+        import weakref
+
+        class A:
+            pass
+        class B(A):
+            pass
+        class C(A):
+            pass
+
+        def f(n):
+            if n:
+                b = B()
+                b.hello = 42
+                r = weakref.ref(b)
+            else:
+                c = C()
+                c.hello = 64
+                r = weakref.ref(c)
+            return r().hello
+        a = self.RPythonAnnotator()
+        s = a.build_types(f, [int])
+        assert isinstance(s, annmodel.SomeInteger)
+        assert not s.is_constant()
+
 
 def g(n):
     return [0,1,2,n]

Modified: pypy/dist/pypy/annotation/unaryop.py
==============================================================================
--- pypy/dist/pypy/annotation/unaryop.py	(original)
+++ pypy/dist/pypy/annotation/unaryop.py	Mon Sep 10 19:39:28 2007
@@ -7,7 +7,7 @@
      SomeDict, SomeUnicodeCodePoint, SomeTuple, SomeImpossibleValue, \
      SomeInstance, SomeBuiltin, SomeFloat, SomeIterator, SomePBC, \
      SomeExternalObject, SomeTypedAddressAccess, SomeAddress, \
-     SomeCTypesObject, s_ImpossibleValue, s_Bool, \
+     SomeCTypesObject, SomeWeakRef, s_ImpossibleValue, s_Bool, \
      unionof, set, missing_operation, add_knowntypedata, HarmlesslyBlocked, \
      SomeGenericCallable
 from pypy.annotation.bookkeeper import getbookkeeper
@@ -751,6 +751,13 @@
         return entry.compute_result_annotation(*args_s)
 
 #_________________________________________
+# weakrefs
+
+class __extend__(SomeWeakRef):
+    def simple_call(s_wrf):
+        return SomeInstance(s_wrf.classdef, can_be_None=True)
+
+#_________________________________________
 # memory addresses
 
 from pypy.rpython.memory import lladdress



More information about the Pypy-commit mailing list