[pypy-svn] r26296 - in pypy/dist/pypy/rpython/ootypesystem: . test
nik at codespeak.net
nik at codespeak.net
Tue Apr 25 08:42:46 CEST 2006
Author: nik
Date: Tue Apr 25 08:42:42 2006
New Revision: 26296
Modified:
pypy/dist/pypy/rpython/ootypesystem/ootype.py
pypy/dist/pypy/rpython/ootypesystem/test/test_oolist.py
Log:
(dialtone, nik)
make it possible to set the item type of a list lazily.
Modified: pypy/dist/pypy/rpython/ootypesystem/ootype.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/ootype.py (original)
+++ pypy/dist/pypy/rpython/ootypesystem/ootype.py Tue Apr 25 08:42:42 2006
@@ -216,7 +216,7 @@
def _field_type(self, name):
try:
- return self._items[name]
+ return self._fields[name][0]
except KeyError:
raise TypeError("No field names %r" % name)
@@ -262,10 +262,13 @@
SELFTYPE_T = object()
ITEMTYPE_T = object()
- def __init__(self, ITEMTYPE):
+ def __init__(self, ITEMTYPE=None):
self._ITEMTYPE = ITEMTYPE
self._null = _null_list(self)
+ if ITEMTYPE is not None:
+ self._init_methods()
+ def _init_methods(self):
# This defines the abstract list interface that backends will
# have to map to their native list implementations.
# 'ITEMTYPE_T' is used as a placeholder for indicating
@@ -273,7 +276,7 @@
generic_types = {
self.SELFTYPE_T: self,
- self.ITEMTYPE_T: ITEMTYPE,
+ self.ITEMTYPE_T: self._ITEMTYPE,
}
# the methods are named after the ADT methods of lltypesystem's lists
@@ -301,6 +304,18 @@
# data structures. But it is important to make sure that attributes
# of supposedly equal Lists compare/hash equal.
+ def __eq__(self, other):
+ if not isinstance(other, List):
+ return False
+ if self._ITEMTYPE is None or other._ITEMTYPE is None:
+ raise TypeError("Can't compare uninitialized List type.")
+ return BuiltinADTType.__eq__(self, other)
+
+ def __hash__(self):
+ if self._ITEMTYPE is None:
+ raise TypeError("Can't hash uninitialized List type.")
+ return BuiltinADTType.__hash__(self)
+
def __str__(self):
return '%s(%s)' % (self.__class__.__name__,
saferecursive(str, "...")(self._ITEMTYPE))
@@ -315,6 +330,10 @@
def _defl(self):
return self._null
+ def _set_itemtype(self, ITEMTYPE):
+ self._ITEMTYPE = ITEMTYPE
+ self._init_methods()
+
class Dict(BuiltinADTType):
# placeholders for types
@@ -847,5 +866,8 @@
assert isinstance(typeOf(inst), (Instance, Tuple))
return inst._identityhash()
+def setItemType(LIST, ITEMTYPE):
+ return LIST._set_itemtype(ITEMTYPE)
+
ROOT = Instance('Root', None, _is_root=True)
Modified: pypy/dist/pypy/rpython/ootypesystem/test/test_oolist.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/test/test_oolist.py (original)
+++ pypy/dist/pypy/rpython/ootypesystem/test/test_oolist.py Tue Apr 25 08:42:42 2006
@@ -60,6 +60,18 @@
assert LT1 != LT3
assert hash(LT1) == hash(LT2)
+def test_optional_itemtype():
+ LT = List()
+ LT2 = List(Signed)
+ assert LT != Signed
+ py.test.raises(TypeError, "LT == LT2")
+ py.test.raises(TypeError, "LT2 == LT")
+ py.test.raises(TypeError, hash, LT)
+ setItemType(LT, Signed)
+ assert LT == LT2
+ assert LT2 == LT
+ assert hash(LT) == hash(LT2)
+
def test_recursive():
FORWARD = ForwardReference()
LT = List(FORWARD)
More information about the Pypy-commit
mailing list