[pypy-svn] r46462 - in pypy/dist/pypy/rpython: . test
arigo at codespeak.net
arigo at codespeak.net
Tue Sep 11 11:43:05 CEST 2007
Author: arigo
Date: Tue Sep 11 11:43:05 2007
New Revision: 46462
Modified:
pypy/dist/pypy/rpython/rbuiltin.py
pypy/dist/pypy/rpython/rweakref.py
pypy/dist/pypy/rpython/test/test_rweakref.py
Log:
RTyping of RPython-level weakrefs (lltypesystem only in this check-in).
Modified: pypy/dist/pypy/rpython/rbuiltin.py
==============================================================================
--- pypy/dist/pypy/rpython/rbuiltin.py (original)
+++ pypy/dist/pypy/rpython/rbuiltin.py Tue Sep 11 11:43:05 2007
@@ -596,7 +596,10 @@
# _________________________________________________________________
# weakrefs
+import weakref
+
def rtype_weakref_create(hop):
+ # Note: this code also works for the RPython-level calls 'weakref.ref(x)'.
vlist = hop.inputargs(hop.args_r[0])
hop.exception_cannot_occur()
return hop.genop('weakref_create', vlist, resulttype=llmemory.WeakRef)
@@ -606,6 +609,7 @@
hop.exception_cannot_occur()
return hop.genop('weakref_deref', [v_wref], resulttype=c_ptrtype.value)
+BUILTIN_TYPER[weakref.ref] = rtype_weakref_create
BUILTIN_TYPER[llmemory.weakref_create] = rtype_weakref_create
BUILTIN_TYPER[llmemory.weakref_deref ] = rtype_weakref_deref
Modified: pypy/dist/pypy/rpython/rweakref.py
==============================================================================
--- pypy/dist/pypy/rpython/rweakref.py (original)
+++ pypy/dist/pypy/rpython/rweakref.py Tue Sep 11 11:43:05 2007
@@ -9,6 +9,24 @@
def rtyper_makekey(self):
return self.__class__,
-
class LLWeakRefRepr(Repr):
lowleveltype = llmemory.WeakRef
+
+# ____________________________________________________________
+#
+# RPython-level weakrefs
+
+class __extend__(annmodel.SomeWeakRef):
+ def rtyper_makerepr(self, rtyper):
+ return WeakRefRepr()
+ def rtyper_makekey(self):
+ return self.__class__,
+
+
+class WeakRefRepr(Repr):
+ lowleveltype = llmemory.WeakRef
+
+ def rtype_simple_call(self, hop):
+ v_wref, = hop.inputargs(self)
+ hop.exception_cannot_occur()
+ return hop.genop('weakref_deref', [v_wref], resulttype=hop.r_result)
Modified: pypy/dist/pypy/rpython/test/test_rweakref.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rweakref.py (original)
+++ pypy/dist/pypy/rpython/test/test_rweakref.py Tue Sep 11 11:43:05 2007
@@ -1,3 +1,4 @@
+import weakref
from pypy.rlib import rgc
from pypy.rpython.lltypesystem import lltype, llmemory
from pypy.rpython.test.test_llinterp import interpret
@@ -18,3 +19,29 @@
res = interpret(f, [])
assert res == lltype.nullptr(S)
+
+
+def test_weakref_simple():
+ class A:
+ pass
+ class B(A):
+ pass
+ class C(A):
+ pass
+
+ def f(n):
+ if n:
+ x = B()
+ x.hello = 42
+ r = weakref.ref(x)
+ else:
+ x = C()
+ x.hello = 64
+ r = weakref.ref(x)
+ return r().hello, x # returns 'x' too, to keep it alive
+
+ res = interpret(f, [1])
+ assert res.item0 == 42
+
+ res = interpret(f, [0])
+ assert res.item0 == 64
More information about the Pypy-commit
mailing list