[pypy-svn] r67640 - in pypy/trunk/pypy/rpython: . lltypesystem ootypesystem test
antocuni at codespeak.net
antocuni at codespeak.net
Thu Sep 10 21:46:46 CEST 2009
Author: antocuni
Date: Thu Sep 10 21:46:45 2009
New Revision: 67640
Modified:
pypy/trunk/pypy/rpython/lltypesystem/rclass.py
pypy/trunk/pypy/rpython/ootypesystem/rclass.py
pypy/trunk/pypy/rpython/rclass.py
pypy/trunk/pypy/rpython/test/test_rclass.py
Log:
xmove the code to handle _immutable_ and _immutable_fields into
AbstractInstanceRepr, so that it can be used by both lltype and ootype. Move
the tests in the base class too, although one is still failing for ootype.
Modified: pypy/trunk/pypy/rpython/lltypesystem/rclass.py
==============================================================================
--- pypy/trunk/pypy/rpython/lltypesystem/rclass.py (original)
+++ pypy/trunk/pypy/rpython/lltypesystem/rclass.py Thu Sep 10 21:46:45 2009
@@ -8,8 +8,7 @@
AbstractInstanceRepr,\
MissingRTypeAttribute,\
getclassrepr, getinstancerepr,\
- get_type_repr, rtype_new_instance, \
- FieldListAccessor
+ get_type_repr, rtype_new_instance
from pypy.rpython.lltypesystem.lltype import \
Ptr, Struct, GcStruct, malloc, \
cast_pointer, cast_ptr_to_int, castable, nullptr, \
@@ -348,14 +347,7 @@
adtmeths = {}
if hints is None:
hints = {}
- if '_immutable_' in self.classdef.classdesc.classdict:
- hints = hints.copy()
- hints['immutable'] = True
- if '_immutable_fields_' in self.classdef.classdesc.classdict:
- hints = hints.copy()
- self.immutable_field_list = self.classdef.classdesc.classdict['_immutable_fields_'].value
- accessor = FieldListAccessor()
- hints['immutable_fields'] = accessor
+ hints = self._check_for_immutable_hints(hints)
if ('_hash_cache_' in fields or
'_hash_cache_' in self.rbase.allinstancefields):
adtmeths = adtmeths.copy()
@@ -374,10 +366,7 @@
attachRuntimeTypeInfo(self.object_type)
def _setup_repr_final(self):
- hints = self.object_type._hints
- if "immutable_fields" in hints:
- accessor = hints["immutable_fields"]
- self._parse_field_list(self.immutable_field_list, accessor)
+ AbstractInstanceRepr._setup_repr_final(self)
if self.gcflavor == 'gc':
if (self.classdef is not None and
self.classdef.classdesc.lookup('__del__') is not None):
Modified: pypy/trunk/pypy/rpython/ootypesystem/rclass.py
==============================================================================
--- pypy/trunk/pypy/rpython/ootypesystem/rclass.py (original)
+++ pypy/trunk/pypy/rpython/ootypesystem/rclass.py Thu Sep 10 21:46:45 2009
@@ -182,9 +182,7 @@
hints = classdef.classdesc.pyobj._rpython_hints
else:
hints = {}
- if '_immutable_' in self.classdef.classdesc.classdict:
- hints = hints.copy()
- hints['immutable'] = True
+ hints = self._check_for_immutable_hints(hints)
self.lowleveltype = ootype.Instance(classdef.name, b, {}, {}, _hints = hints)
self.prebuiltinstances = {} # { id(x): (x, _ptr) }
self.object_type = self.lowleveltype
@@ -318,7 +316,8 @@
def _setup_repr_final(self):
if self.classdef is None:
return
-
+ AbstractInstanceRepr._setup_repr_final(self)
+
# we attach methods here and not in _setup(), because we want
# to be sure that all the reprs of the input arguments of all
# our methods have been computed at this point
Modified: pypy/trunk/pypy/rpython/rclass.py
==============================================================================
--- pypy/trunk/pypy/rpython/rclass.py (original)
+++ pypy/trunk/pypy/rpython/rclass.py Thu Sep 10 21:46:45 2009
@@ -149,6 +149,17 @@
def _setup_repr(self):
pass
+ def _check_for_immutable_hints(self, hints):
+ if '_immutable_' in self.classdef.classdesc.classdict:
+ hints = hints.copy()
+ hints['immutable'] = True
+ if '_immutable_fields_' in self.classdef.classdesc.classdict:
+ hints = hints.copy()
+ self.immutable_field_list = self.classdef.classdesc.classdict['_immutable_fields_'].value
+ accessor = FieldListAccessor()
+ hints['immutable_fields'] = accessor
+ return hints
+
def __repr__(self):
if self.classdef is None:
clsname = 'object'
@@ -164,7 +175,10 @@
return 'InstanceR %s' % (clsname,)
def _setup_repr_final(self):
- pass
+ hints = self.object_type._hints
+ if "immutable_fields" in hints:
+ accessor = hints["immutable_fields"]
+ self._parse_field_list(self.immutable_field_list, accessor)
def _parse_field_list(self, fields, accessor):
with_suffix = {}
Modified: pypy/trunk/pypy/rpython/test/test_rclass.py
==============================================================================
--- pypy/trunk/pypy/rpython/test/test_rclass.py (original)
+++ pypy/trunk/pypy/rpython/test/test_rclass.py Thu Sep 10 21:46:45 2009
@@ -700,6 +700,60 @@
assert res == 0
+ def test_immutable(self):
+ class I(object):
+ _immutable_ = True
+
+ def __init__(self, v):
+ self.v = v
+
+ i = I(3)
+ def f():
+ return i.v
+
+ t, typer, graph = self.gengraph(f, [], backendopt=True)
+ assert summary(graph) == {}
+
+ def test_immutable_fields(self):
+ from pypy.jit.metainterp.typesystem import deref
+ class A(object):
+ _immutable_fields_ = ["x", "y[*]"]
+
+ def __init__(self, x, y):
+ self.x = x
+ self.y = y
+
+ def f():
+ return A(3, [])
+ t, typer, graph = self.gengraph(f, [])
+ A_TYPE = deref(graph.getreturnvar().concretetype)
+ accessor = A_TYPE._hints["immutable_fields"]
+ assert accessor.fields == {"inst_x" : "", "inst_y" : "[*]"} or \
+ accessor.fields == {"ox" : "", "oy" : "[*]"} # for ootype
+
+ def test_immutable_inheritance(self):
+ if self.type_system == 'ootype':
+ py.test.skip('fixme!')
+ class I(object):
+ def __init__(self, v):
+ self.v = v
+
+ class J(I):
+ _immutable_ = True
+ def __init__(self, v, w):
+ self.w = w
+ I.__init__(self, v)
+
+ j = J(3, 4)
+ def f():
+ j.v = j.v * 1 # make the annotator think it is mutated
+ j.w = j.w * 1 # make the annotator think it is mutated
+ return j.v + j.w
+
+ t, typer, graph = self.gengraph(f, [], backendopt=True)
+ assert summary(graph) == {"setfield": 2}
+
+
class TestLltype(BaseTestRclass, LLRtypeMixin):
def test__del__(self):
@@ -765,55 +819,6 @@
assert typeOf(destrptra).TO.ARGS[0] != typeOf(destrptrb).TO.ARGS[0]
assert destrptra is not None
assert destrptrb is not None
-
- def test_immutable(self):
- class I(object):
- _immutable_ = True
-
- def __init__(self, v):
- self.v = v
-
- i = I(3)
- def f():
- return i.v
-
- t, typer, graph = self.gengraph(f, [], backendopt=True)
- assert summary(graph) == {}
-
- def test_immutable_fields(self):
- class A(object):
- _immutable_fields_ = ["x", "y[*]"]
-
- def __init__(self, x, y):
- self.x = x
- self.y = y
-
- def f():
- return A(3, [])
- t, typer, graph = self.gengraph(f, [])
- A_TYPE = graph.getreturnvar().concretetype
- accessor = A_TYPE.TO._hints["immutable_fields"]
- assert accessor.fields == {"inst_x" : "", "inst_y" : "[*]"}
-
- def test_immutable_inheritance(self):
- class I(object):
- def __init__(self, v):
- self.v = v
-
- class J(I):
- _immutable_ = True
- def __init__(self, v, w):
- self.w = w
- I.__init__(self, v)
-
- j = J(3, 4)
- def f():
- j.v = j.v * 1 # make the annotator think it is mutated
- j.w = j.w * 1 # make the annotator think it is mutated
- return j.v + j.w
-
- t, typer, graph = self.gengraph(f, [], backendopt=True)
- assert summary(graph) == {"setfield": 2}
def test_instance_repr(self):
from pypy.rlib.objectmodel import current_object_addr_as_int
More information about the Pypy-commit
mailing list