[pypy-svn] r18758 - in pypy/dist/pypy/rpython: . ootypesystem ootypesystem/test
arigo at codespeak.net
arigo at codespeak.net
Wed Oct 19 17:26:05 CEST 2005
Author: arigo
Date: Wed Oct 19 17:26:04 2005
New Revision: 18758
Modified:
pypy/dist/pypy/rpython/llinterp.py
pypy/dist/pypy/rpython/ootypesystem/ootype.py
pypy/dist/pypy/rpython/ootypesystem/rclass.py
pypy/dist/pypy/rpython/ootypesystem/test/test_ooclean.py
pypy/dist/pypy/rpython/ootypesystem/test/test_ootype.py
Log:
ootype: type() and issubtype() operations.
Modified: pypy/dist/pypy/rpython/llinterp.py
==============================================================================
--- pypy/dist/pypy/rpython/llinterp.py (original)
+++ pypy/dist/pypy/rpython/llinterp.py Wed Oct 19 17:26:04 2005
@@ -664,6 +664,12 @@
def op_instanceof(self, inst, INST):
return ootype.instanceof(inst, INST)
+ def op_classof(self, inst):
+ return ootype.classof(inst)
+
+ def op_subclassof(self, class1, class2):
+ return ootype.subclassof(class1, class2)
+
# by default we route all logging messages to nothingness
# e.g. tests can then switch on logging to get more help
# for failing tests
Modified: pypy/dist/pypy/rpython/ootypesystem/ootype.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/ootype.py (original)
+++ pypy/dist/pypy/rpython/ootypesystem/ootype.py Wed Oct 19 17:26:04 2005
@@ -275,8 +275,14 @@
return bool(inst) and isSubclass(inst._TYPE, INSTANCE)
def classof(inst):
+ assert isinstance(inst, _instance)
return runtimeClass(inst._TYPE)
+def subclassof(class1, class2):
+ assert isinstance(class1, _class)
+ assert isinstance(class2, _class)
+ return isSubclass(class1._INSTANCE, class2._INSTANCE)
+
def addFields(INSTANCE, fields):
INSTANCE._add_fields(fields)
Modified: pypy/dist/pypy/rpython/ootypesystem/rclass.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/rclass.py (original)
+++ pypy/dist/pypy/rpython/ootypesystem/rclass.py Wed Oct 19 17:26:04 2005
@@ -32,6 +32,11 @@
#
return getinstancerepr(self.rtyper, subclassdef).lowleveltype._class
+ def rtype_issubtype(self, hop):
+ class_repr = get_type_repr(self.rtyper)
+ vlist = hop.inputargs(class_repr, class_repr)
+ return hop.genop('subclassof', vlist, resulttype=ootype.Bool)
+
def mangle(name):
# XXX temporary: for now it looks like a good idea to mangle names
@@ -201,6 +206,10 @@
vinst, = hop.inputargs(self)
return hop.genop('oononnull', [vinst], resulttype=ootype.Bool)
+ def rtype_type(self, hop):
+ vinst, = hop.inputargs(self)
+ return hop.genop('classof', [vinst], resulttype=ootype.Class)
+
def convert_const(self, value):
if value is None:
return ootype.null(self.lowleveltype)
Modified: pypy/dist/pypy/rpython/ootypesystem/test/test_ooclean.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/test/test_ooclean.py (original)
+++ pypy/dist/pypy/rpython/ootypesystem/test/test_ooclean.py Wed Oct 19 17:26:04 2005
@@ -284,6 +284,33 @@
res = interpret(f, [0], type_system='ootype')
assert res == 0
+def test_issubclass_type():
+ class A:
+ pass
+ class B(A):
+ pass
+ def f(i):
+ if i == 0:
+ c1 = A()
+ else:
+ c1 = B()
+ return issubclass(type(c1), B)
+ res = interpret(f, [0], type_system='ootype')
+ assert res is False
+ res = interpret(f, [1], type_system='ootype')
+ assert res is True
+
+ def g(i):
+ if i == 0:
+ c1 = A()
+ else:
+ c1 = B()
+ return issubclass(type(c1), A)
+ res = interpret(g, [0], type_system='ootype')
+ assert res is True
+ res = interpret(g, [1], type_system='ootype')
+ assert res is True
+
def test_instance_comparison():
def f(flag):
a = Subclass()
Modified: pypy/dist/pypy/rpython/ootypesystem/test/test_ootype.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/test/test_ootype.py (original)
+++ pypy/dist/pypy/rpython/ootypesystem/test/test_ootype.py Wed Oct 19 17:26:04 2005
@@ -273,3 +273,18 @@
0, 0, 0, 1, 1,
0, 0, 0, 1, 1,
]
+
+def test_subclassof():
+ A = Instance("A", None)
+ B = Instance("B", A)
+ C = Instance("C", B)
+ result = []
+ for first in [A, B, C]:
+ for second in [A, B, C]:
+ result.append(subclassof(runtimeClass(first),
+ runtimeClass(second)))
+ assert result == [
+ 1, 0, 0,
+ 1, 1, 0,
+ 1, 1, 1,
+ ]
More information about the Pypy-commit
mailing list