[pypy-svn] r57699 - in pypy/branch/oo-jit/pypy/rpython/ootypesystem: . test
antocuni at codespeak.net
antocuni at codespeak.net
Sun Aug 31 13:16:53 CEST 2008
Author: antocuni
Date: Sun Aug 31 13:16:50 2008
New Revision: 57699
Modified:
pypy/branch/oo-jit/pypy/rpython/ootypesystem/ootype.py
pypy/branch/oo-jit/pypy/rpython/ootypesystem/test/test_ootype.py
Log:
associate a Class object to each Record, as we do for Instances
Modified: pypy/branch/oo-jit/pypy/rpython/ootypesystem/ootype.py
==============================================================================
--- pypy/branch/oo-jit/pypy/rpython/ootypesystem/ootype.py (original)
+++ pypy/branch/oo-jit/pypy/rpython/ootypesystem/ootype.py Sun Aug 31 13:16:50 2008
@@ -307,6 +307,8 @@
# We try to keep Record as similar to Instance as possible, so backends
# can treat them polymorphically, if they choose to do so.
+
+ _classes = {}
def __init__(self, fields, _hints={}):
self._fields = frozendict()
@@ -315,6 +317,15 @@
self._null = _null_record(self)
self._hints = frozendict(_hints)
+ @property
+ def _class(self):
+ try:
+ return self._classes[self]
+ except KeyError:
+ cls = _class(self)
+ self._classes[self] = cls
+ return cls
+
def _defl(self):
return self._null
@@ -1767,7 +1778,11 @@
def runtimenew(class_):
assert isinstance(class_, _class)
assert class_ is not nullruntimeclass
- return make_instance(class_._INSTANCE)
+ TYPE = class_._INSTANCE
+ if isinstance(TYPE, Record):
+ return _record(TYPE)
+ else:
+ return make_instance(TYPE)
def static_meth(FUNCTION, name, **attrs):
return _static_meth(FUNCTION, _name=name, **attrs)
@@ -1812,7 +1827,7 @@
INSTANCE._override_default_for_fields(fields)
def runtimeClass(INSTANCE):
- assert isinstance(INSTANCE, Instance)
+ assert isinstance(INSTANCE, (Instance, Record))
return INSTANCE._class
def isSubclass(C1, C2):
Modified: pypy/branch/oo-jit/pypy/rpython/ootypesystem/test/test_ootype.py
==============================================================================
--- pypy/branch/oo-jit/pypy/rpython/ootypesystem/test/test_ootype.py (original)
+++ pypy/branch/oo-jit/pypy/rpython/ootypesystem/test/test_ootype.py Sun Aug 31 13:16:50 2008
@@ -32,7 +32,7 @@
assert d.a == 1
-def test_runtime_instanciation():
+def test_runtime_instantiation():
I = Instance("test", ROOT, {"a": Signed})
c = runtimeClass(I)
i = runtimenew(c)
@@ -40,6 +40,27 @@
assert typeOf(i) == I
assert typeOf(c) == Class
+def test_record_equivalence():
+ R1 = Record({"a": Signed})
+ R2 = Record({"a": Signed})
+ assert R1 == R2
+ assert hash(R1) == hash(R2)
+ assert R1._class is R2._class
+
+def test_runtime_record_instantiation():
+ R = Record({"a": Signed})
+ c = runtimeClass(R)
+ r = runtimenew(c)
+
+ assert typeOf(r) == R
+ assert typeOf(c) == Class
+
+def test_class_records():
+ R1 = Record({"a": Signed})
+ R2 = Record({"a": Signed})
+ assert R1 == R2
+ assert runtimeClass(R1) is runtimeClass(R2)
+
def test_classof():
I = Instance("test", ROOT, {"a": Signed})
c = runtimeClass(I)
More information about the Pypy-commit
mailing list