[pypy-commit] pypy default: Add a way to know if the program can use weakrefs.

arigo noreply at buildbot.pypy.org
Mon Apr 16 18:39:59 CEST 2012


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r54438:e36a83af4ecb
Date: 2012-04-16 18:35 +0200
http://bitbucket.org/pypy/pypy/changeset/e36a83af4ecb/

Log:	Add a way to know if the program can use weakrefs.

diff --git a/pypy/rlib/rweakref.py b/pypy/rlib/rweakref.py
--- a/pypy/rlib/rweakref.py
+++ b/pypy/rlib/rweakref.py
@@ -9,6 +9,9 @@
 
 ref = weakref.ref    # basic regular weakrefs are supported in RPython
 
+def has_weakref_support():
+    return True      # returns False if --no-translation-rweakref
+
 
 class RWeakValueDictionary(object):
     """A dictionary containing weak values."""
@@ -68,6 +71,20 @@
 from pypy.annotation.bookkeeper import getbookkeeper
 from pypy.tool.pairtype import pairtype
 
+class Entry(extregistry.ExtRegistryEntry):
+    _about_ = has_weakref_support
+
+    def compute_result_annotation(self):
+        translator = self.bookkeeper.annotator.translator
+        res = translator.config.translation.rweakref
+        return self.bookkeeper.immutablevalue(res)
+
+    def specialize_call(self, hop):
+        from pypy.rpython.lltypesystem import lltype
+        hop.exception_cannot_occur()
+        return hop.inputconst(lltype.Bool, hop.s_result.const)
+
+
 class SomeWeakValueDict(annmodel.SomeObject):
     knowntype = RWeakValueDictionary
 
diff --git a/pypy/rlib/test/test_rweakref.py b/pypy/rlib/test/test_rweakref.py
new file mode 100644
--- /dev/null
+++ b/pypy/rlib/test/test_rweakref.py
@@ -0,0 +1,14 @@
+from pypy.rlib.rweakref import has_weakref_support
+from pypy.rpython.test.test_llinterp import interpret
+
+
+def test_has_weakref_support():
+    assert has_weakref_support()
+
+    res = interpret(lambda: has_weakref_support(), [],
+                    **{'translation.rweakref': True})
+    assert res == True
+
+    res = interpret(lambda: has_weakref_support(), [],
+                    **{'translation.rweakref': False})
+    assert res == False


More information about the pypy-commit mailing list