[pypy-svn] r26473 - in pypy/dist/pypy/rpython/ootypesystem: . test

nik at codespeak.net nik at codespeak.net
Fri Apr 28 03:46:28 CEST 2006


Author: nik
Date: Fri Apr 28 03:46:23 2006
New Revision: 26473

Modified:
   pypy/dist/pypy/rpython/ootypesystem/ootype.py
   pypy/dist/pypy/rpython/ootypesystem/test/test_oodict.py
Log:
make it possible to initialize the value type of a Dict lazily.


Modified: pypy/dist/pypy/rpython/ootypesystem/ootype.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/ootype.py	(original)
+++ pypy/dist/pypy/rpython/ootypesystem/ootype.py	Fri Apr 28 03:46:23 2006
@@ -352,15 +352,19 @@
     KEYTYPE_T = object()
     VALUETYPE_T = object()
 
-    def __init__(self, KEYTYPE, VALUETYPE):
+    def __init__(self, KEYTYPE, VALUETYPE=None):
         self._KEYTYPE = KEYTYPE
         self._VALUETYPE = VALUETYPE
         self._null = _null_dict(self)
 
+        if VALUETYPE is not None:
+            self._init_methods()
+
+    def _init_methods(self):
         generic_types = {
             self.SELFTYPE_T: self,
-            self.KEYTYPE_T: KEYTYPE,
-            self.VALUETYPE_T: VALUETYPE
+            self.KEYTYPE_T: self._KEYTYPE,
+            self.VALUETYPE_T: self._VALUETYPE
             }
 
         self._GENERIC_METHODS = frozendict({
@@ -386,6 +390,18 @@
         return '%s%s' % (self.__class__.__name__,
                 saferecursive(str, "(...)")((self._KEYTYPE, self._VALUETYPE)))
 
+    def __eq__(self, other):
+        if not isinstance(other, Dict):
+            return False
+        if self._VALUETYPE is None or other._VALUETYPE is None:
+            raise TypeError("Can't compare uninitialized Dict type.")
+        return BuiltinADTType.__eq__(self, other) 
+
+    def __hash__(self):
+        if self._VALUETYPE is None:
+            raise TypeError("Can't hash uninitialized Dict type.")
+        return BuiltinADTType.__hash__(self)
+
     def _get_interp_class(self):
         return _dict
 
@@ -394,6 +410,10 @@
         VALUETYPE = self._specialize_type(self._VALUETYPE, generic_types)
         return self.__class__(KEYTYPE, VALUETYPE)
 
+    def _set_valuetype(self, VALUETYPE):
+        self._VALUETYPE = VALUETYPE
+        self._init_methods()
+
 class DictItemsIterator(BuiltinADTType):
     SELFTYPE_T = object()
     KEYTYPE_T = object()
@@ -966,5 +986,11 @@
 def hasItemType(LIST):
     return LIST._ITEMTYPE is not None
 
+def setValueType(DICT, VALUETYPE):
+    return DICT._set_valuetype(VALUETYPE)
+
+def hasValueType(DICT):
+    return DICT._VALUETYPE is not None
+
 
 ROOT = Instance('Root', None, _is_root=True)

Modified: pypy/dist/pypy/rpython/ootypesystem/test/test_oodict.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/test/test_oodict.py	(original)
+++ pypy/dist/pypy/rpython/ootypesystem/test/test_oodict.py	Fri Apr 28 03:46:23 2006
@@ -1,6 +1,6 @@
 import py
 from pypy.rpython.test.test_llinterp import interpret 
-from pypy.rpython.ootypesystem.ootype import Signed, Float, Dict, new, typeOf
+from pypy.rpython.ootypesystem.ootype import Signed, Float, Dict, new, typeOf, setValueType
 
 def test_new():
     DT = Dict(Signed, Float)
@@ -30,3 +30,16 @@
         items.append((it.ll_current_key(), it.ll_current_value()))
     items.sort()
     assert items == [(42, 43.0), (52, 53.0)]
+
+def test_optional_valuetype():
+    DT = Dict(Signed)
+    DT2 = Dict(Signed, Float)
+    assert DT != Signed
+    py.test.raises(TypeError, "DT == DT2")
+    py.test.raises(TypeError, "DT2 == DT")
+    py.test.raises(TypeError, hash, DT)
+    setValueType(DT, Float)
+    assert DT == DT2
+    assert DT2 == DT
+    assert hash(DT) == hash(DT2)
+



More information about the Pypy-commit mailing list