[pypy-commit] pypy default: Allow RWeakValueDictionaries to be mixed with None

arigo noreply at buildbot.pypy.org
Thu Oct 1 16:32:41 CEST 2015


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r79919:3a3f357b1c84
Date: 2015-10-01 10:27 +0200
http://bitbucket.org/pypy/pypy/changeset/3a3f357b1c84/

Log:	Allow RWeakValueDictionaries to be mixed with None

diff --git a/rpython/rlib/_rweakvaldict.py b/rpython/rlib/_rweakvaldict.py
--- a/rpython/rlib/_rweakvaldict.py
+++ b/rpython/rlib/_rweakvaldict.py
@@ -2,6 +2,7 @@
 from rpython.rtyper.lltypesystem import lltype, llmemory, rdict
 from rpython.rtyper.lltypesystem.llmemory import weakref_create, weakref_deref
 from rpython.rtyper import rclass
+from rpython.rtyper.error import TyperError
 from rpython.rtyper.rclass import getinstancerepr
 from rpython.rtyper.rmodel import Repr
 from rpython.rlib.rweakref import RWeakValueDictionary
@@ -60,6 +61,8 @@
         self.dict_cache = {}
 
     def convert_const(self, weakdict):
+        if weakdict is None:
+            return lltype.nullptr(self.WEAKDICT)
         if not isinstance(weakdict, RWeakValueDictionary):
             raise TyperError("expected an RWeakValueDictionary: %r" % (
                 weakdict,))
diff --git a/rpython/rlib/rweakref.py b/rpython/rlib/rweakref.py
--- a/rpython/rlib/rweakref.py
+++ b/rpython/rlib/rweakref.py
@@ -25,6 +25,9 @@
     """A dictionary containing weak values."""
 
     def __init__(self, keyclass, valueclass):
+        """'keyclass' can be an RPython class or a type like 'int' or 'str'.
+        On the other hand, 'valueclass' must be an RPython class.
+        """
         self._dict = weakref.WeakValueDictionary()
         self._keyclass = keyclass
         self._valueclass = valueclass
@@ -99,6 +102,12 @@
         self.s_key = s_key
         self.valueclassdef = valueclassdef
 
+    def can_be_none(self):
+        return True
+
+    def noneify(self):
+        return self
+
     def rtyper_makerepr(self, rtyper):
         from rpython.rlib import _rweakvaldict
         return _rweakvaldict.WeakValueDictRepr(rtyper,
diff --git a/rpython/rlib/test/test_rweakvaldict.py b/rpython/rlib/test/test_rweakvaldict.py
--- a/rpython/rlib/test/test_rweakvaldict.py
+++ b/rpython/rlib/test/test_rweakvaldict.py
@@ -146,6 +146,25 @@
     py.test.raises(Exception, interpret, g, [1])
 
 
+def test_rpython_RWeakValueDictionary_or_None():
+    def g(d, key):
+        if d is None:
+            return None
+        return d.get(key)
+    def f(n):
+        x = X()
+        if n:
+            d = None
+        else:
+            d = RWeakValueDictionary(str, X)
+            d.set("a", x)
+        return g(d, "a") is x
+    assert f(0)
+    assert interpret(f, [0])
+    assert not f(1)
+    assert not interpret(f, [1])
+
+
 def test_bogus_makekey():
     class X: pass
     class Y: pass


More information about the pypy-commit mailing list