[pypy-svn] r23925 - in pypy/dist/pypy: annotation rpython/ootypesystem rpython/test
nik at codespeak.net
nik at codespeak.net
Thu Mar 2 19:20:41 CET 2006
Author: nik
Date: Thu Mar 2 19:20:36 2006
New Revision: 23925
Modified:
pypy/dist/pypy/annotation/description.py
pypy/dist/pypy/rpython/ootypesystem/rclass.py
pypy/dist/pypy/rpython/ootypesystem/rpbc.py
pypy/dist/pypy/rpython/test/test_rclass.py
Log:
(pedronis, nik)
fixed __del__ support in ootypesystem. __del__ is simply rtyped with a flag
"finalizer", the backends are responsible for taking appropriate measures.
fixed a horribly subtle bug in annotation, where emulated calls to __del__
methods were not correctly recorded.
Modified: pypy/dist/pypy/annotation/description.py
==============================================================================
--- pypy/dist/pypy/annotation/description.py (original)
+++ pypy/dist/pypy/annotation/description.py Thu Mar 2 19:20:36 2006
@@ -398,7 +398,7 @@
from pypy.annotation.model import s_None, SomeInstance
s_func = self.s_read_attribute('__del__')
args_s = [SomeInstance(classdef)]
- s = self.bookkeeper.emulate_pbc_call(None, s_func, args_s)
+ s = self.bookkeeper.emulate_pbc_call(classdef, s_func, args_s)
assert s_None.contains(s)
return classdef
Modified: pypy/dist/pypy/rpython/ootypesystem/rclass.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/rclass.py (original)
+++ pypy/dist/pypy/rpython/ootypesystem/rclass.py Thu Mar 2 19:20:36 2006
@@ -214,14 +214,16 @@
raise TyperError("class attribute overrides method")
allclassattributes[mangled] = name, s_value
- if '__init__' not in selfattrs and \
- self.classdef.classdesc.find_source_for("__init__") is not None:
- s_init = self.classdef.classdesc.s_get_value(self.classdef,
- '__init__')
- if isinstance(s_init, annmodel.SomePBC):
- mangled = mangle("__init__")
- allmethods[mangled] = "__init__", s_init
- # else: it's the __init__ of a builtin exception
+ special_methods = ["__init__", "__del__"]
+ for meth_name in special_methods:
+ if meth_name not in selfattrs and \
+ self.classdef.classdesc.find_source_for(meth_name) is not None:
+ s_meth = self.classdef.classdesc.s_get_value(self.classdef,
+ meth_name)
+ if isinstance(s_meth, annmodel.SomePBC):
+ mangled = mangle(meth_name)
+ allmethods[mangled] = meth_name, s_meth
+ # else: it's the __init__ of a builtin exception
#
# hash() support
@@ -259,7 +261,8 @@
# get method implementation
from pypy.rpython.ootypesystem.rpbc import MethodImplementations
methimpls = MethodImplementations.get(self.rtyper, s_value)
- m_impls = methimpls.get_impl(mangled, methdesc)
+ m_impls = methimpls.get_impl(mangled, methdesc,
+ is_finalizer=name == "__del__")
methods.update(m_impls)
Modified: pypy/dist/pypy/rpython/ootypesystem/rpbc.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/rpbc.py (original)
+++ pypy/dist/pypy/rpython/ootypesystem/rpbc.py Thu Mar 2 19:20:36 2006
@@ -101,14 +101,17 @@
return methodsimpl
get = staticmethod(get)
- def get_impl(self, name, methdesc):
+ def get_impl(self, name, methdesc, is_finalizer=False):
impls = {}
+ flags = {}
+ if is_finalizer:
+ flags['finalizer'] = True
for rowname, (row, M) in self.row_mapping.iteritems():
if methdesc is None:
- m = ootype.meth(M, _name=name, abstract=True)
+ m = ootype.meth(M, _name=name, abstract=True, **flags)
else:
impl_graph = row[methdesc.funcdesc].graph
- m = ootype.meth(M, _name=name, graph=impl_graph)
+ m = ootype.meth(M, _name=name, graph=impl_graph, **flags)
derived_name = row_method_name(name, rowname)
impls[derived_name] = m
return impls
Modified: pypy/dist/pypy/rpython/test/test_rclass.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rclass.py (original)
+++ pypy/dist/pypy/rpython/test/test_rclass.py Thu Mar 2 19:20:36 2006
@@ -377,76 +377,132 @@
res = interpret(f, [], type_system=self.ts)
assert res == 1
-def test__del__():
- class A(object):
- def __init__(self):
- self.a = 2
- def __del__(self):
- self.a = 3
- def f():
- a = A()
- return a.a
- t = TranslationContext()
- t.buildannotator().build_types(f, [])
- t.buildrtyper().specialize()
- graph = graphof(t, f)
- TYPE = graph.startblock.operations[0].args[0].value
- RTTI = getRuntimeTypeInfo(TYPE)
- queryptr = RTTI._obj.query_funcptr # should not raise
- destrptr = RTTI._obj.destructor_funcptr
- assert destrptr is not None
-
-def test_del_inheritance():
- class State:
- pass
- s = State()
- s.a_dels = 0
- s.b_dels = 0
- class A(object):
- def __del__(self):
- s.a_dels += 1
- class B(A):
- def __del__(self):
- s.b_dels += 1
- class C(A):
- pass
- def f():
- A()
- B()
- C()
- A()
- B()
- C()
- return s.a_dels * 10 + s.b_dels
- res = f()
- assert res == 42
- t = TranslationContext()
- t.buildannotator().build_types(f, [])
- t.buildrtyper().specialize()
- graph = graphof(t, f)
- TYPEA = graph.startblock.operations[0].args[0].value
- RTTIA = getRuntimeTypeInfo(TYPEA)
- TYPEB = graph.startblock.operations[3].args[0].value
- RTTIB = getRuntimeTypeInfo(TYPEB)
- TYPEC = graph.startblock.operations[6].args[0].value
- RTTIC = getRuntimeTypeInfo(TYPEC)
- queryptra = RTTIA._obj.query_funcptr # should not raise
- queryptrb = RTTIB._obj.query_funcptr # should not raise
- queryptrc = RTTIC._obj.query_funcptr # should not raise
- destrptra = RTTIA._obj.destructor_funcptr
- destrptrb = RTTIB._obj.destructor_funcptr
- destrptrc = RTTIC._obj.destructor_funcptr
- assert destrptra == destrptrc
- assert typeOf(destrptra).TO.ARGS[0] != typeOf(destrptrb).TO.ARGS[0]
- assert destrptra is not None
- assert destrptrb is not None
-
class TestLltype(BaseTestRclass):
ts = "lltype"
+ def test__del__(self):
+ class A(object):
+ def __init__(self):
+ self.a = 2
+ def __del__(self):
+ self.a = 3
+ def f():
+ a = A()
+ return a.a
+ t = TranslationContext()
+ t.buildannotator().build_types(f, [])
+ t.buildrtyper().specialize()
+ graph = graphof(t, f)
+ TYPE = graph.startblock.operations[0].args[0].value
+ RTTI = getRuntimeTypeInfo(TYPE)
+ queryptr = RTTI._obj.query_funcptr # should not raise
+ destrptr = RTTI._obj.destructor_funcptr
+ assert destrptr is not None
+
+ def test_del_inheritance(self):
+ class State:
+ pass
+ s = State()
+ s.a_dels = 0
+ s.b_dels = 0
+ class A(object):
+ def __del__(self):
+ s.a_dels += 1
+ class B(A):
+ def __del__(self):
+ s.b_dels += 1
+ class C(A):
+ pass
+ def f():
+ A()
+ B()
+ C()
+ A()
+ B()
+ C()
+ return s.a_dels * 10 + s.b_dels
+ res = f()
+ assert res == 42
+ t = TranslationContext()
+ t.buildannotator().build_types(f, [])
+ t.buildrtyper().specialize()
+ graph = graphof(t, f)
+ TYPEA = graph.startblock.operations[0].args[0].value
+ RTTIA = getRuntimeTypeInfo(TYPEA)
+ TYPEB = graph.startblock.operations[3].args[0].value
+ RTTIB = getRuntimeTypeInfo(TYPEB)
+ TYPEC = graph.startblock.operations[6].args[0].value
+ RTTIC = getRuntimeTypeInfo(TYPEC)
+ queryptra = RTTIA._obj.query_funcptr # should not raise
+ queryptrb = RTTIB._obj.query_funcptr # should not raise
+ queryptrc = RTTIC._obj.query_funcptr # should not raise
+ destrptra = RTTIA._obj.destructor_funcptr
+ destrptrb = RTTIB._obj.destructor_funcptr
+ destrptrc = RTTIC._obj.destructor_funcptr
+ assert destrptra == destrptrc
+ assert typeOf(destrptra).TO.ARGS[0] != typeOf(destrptrb).TO.ARGS[0]
+ assert destrptra is not None
+ assert destrptrb is not None
+
class TestOotype(BaseTestRclass):
ts = "ootype"
+
+ def test__del__(self):
+ class A(object):
+ def __init__(self):
+ self.a = 2
+ def __del__(self):
+ self.a = 3
+ def f():
+ a = A()
+ return a.a
+ t = TranslationContext()
+ t.buildannotator().build_types(f, [])
+ t.buildrtyper(type_system=self.ts).specialize()
+ graph = graphof(t, f)
+ TYPE = graph.startblock.operations[0].args[0].value
+ meth = TYPE._lookup("o__del___variant0")
+ assert meth.finalizer
+
+ def test_del_inheritance(self):
+ class State:
+ pass
+ s = State()
+ s.a_dels = 0
+ s.b_dels = 0
+ class A(object):
+ def __del__(self):
+ s.a_dels += 1
+ class B(A):
+ def __del__(self):
+ s.b_dels += 1
+ class C(A):
+ pass
+ def f():
+ A()
+ B()
+ C()
+ A()
+ B()
+ C()
+ return s.a_dels * 10 + s.b_dels
+ res = f()
+ assert res == 42
+ t = TranslationContext()
+ t.buildannotator().build_types(f, [])
+ t.buildrtyper(type_system=self.ts).specialize()
+ graph = graphof(t, f)
+ TYPEA = graph.startblock.operations[0].args[0].value
+ TYPEB = graph.startblock.operations[2].args[0].value
+ TYPEC = graph.startblock.operations[4].args[0].value
+ destra = TYPEA._lookup("o__del___variant0")
+ destrb = TYPEB._lookup("o__del___variant0")
+ destrc = TYPEC._lookup("o__del___variant0")
+ assert destra == destrc
+ assert destrb is not None
+ assert destra is not None
+
More information about the Pypy-commit
mailing list