[pypy-commit] pypy stm: Add an official way to ask some Struct whether a

arigo noreply at buildbot.pypy.org
Tue Jan 24 16:49:04 CET 2012


Author: Armin Rigo <arigo at tunes.org>
Branch: stm
Changeset: r51709:1fd090dc1127
Date: 2012-01-23 18:14 +0100
http://bitbucket.org/pypy/pypy/changeset/1fd090dc1127/

Log:	Add an official way to ask some Struct whether a
	{get,set}interiorfield() targets an immutable field or not.

diff --git a/pypy/rpython/lltypesystem/lltype.py b/pypy/rpython/lltypesystem/lltype.py
--- a/pypy/rpython/lltypesystem/lltype.py
+++ b/pypy/rpython/lltypesystem/lltype.py
@@ -352,6 +352,20 @@
                 pass
         return False
 
+    def _immutable_interiorfield(self, fields_v):
+        T = self
+        for v_field in fields_v:
+            fieldname = getattr(v_field, 'value', None)
+            if T._immutable_field(fieldname):
+                return True
+            if isinstance(fieldname, str):
+                assert isinstance(T, Struct)
+                T = getattr(T, fieldname)
+            else:
+                assert isinstance(T, Array)
+                T = T.OF
+        return False
+
 class RttiStruct(Struct):
     _runtime_type_info = None
 
diff --git a/pypy/rpython/lltypesystem/test/test_lltype.py b/pypy/rpython/lltypesystem/test/test_lltype.py
--- a/pypy/rpython/lltypesystem/test/test_lltype.py
+++ b/pypy/rpython/lltypesystem/test/test_lltype.py
@@ -804,6 +804,44 @@
                                          hints={'immutable': True})))
     assert S.d._immutable_field() == True
 
+def test_immutable_interiorfield():
+    class Constant:
+        def __init__(self, value):
+            self.value = value
+    #
+    for hints in [{}, {'immutable': True}]:
+        expected = 'immutable' in hints
+        #
+        T = Struct('T', ('a', lltype.Signed), hints=hints)
+        S = GcStruct('S', ('t', T))
+        immut = S._immutable_interiorfield([Constant('t'), Constant('a')])
+        assert immut == expected
+        #
+        T = Struct('T', ('a', lltype.Signed))
+        S = GcStruct('S', ('t', T), hints=hints)
+        immut = S._immutable_interiorfield([Constant('t'), Constant('a')])
+        assert immut == expected
+        #
+        A = Array(lltype.Signed, hints=hints)
+        S = GcStruct('S', ('ar', A))
+        immut = S._immutable_interiorfield([Constant('ar'), Constant(4)])
+        assert immut == expected
+        #
+        T = Struct('T', ('a', lltype.Signed), hints=hints)
+        A = Array(T)
+        S = GcStruct('S', ('ar', A))
+        immut = S._immutable_interiorfield([Constant('ar'), Constant(4),
+                                            Constant('a')])
+        assert immut == expected
+        #
+        T = Struct('T', ('a', lltype.Signed))
+        A = Array(T, hints=hints)
+        S = GcStruct('S', ('ar', A))
+        immut = S._immutable_interiorfield([Constant('ar'), Constant(4),
+                                            Constant('a')])
+        assert immut == expected
+
+
 def test_typedef():
     T = Typedef(Signed, 'T')
     assert T == Signed


More information about the pypy-commit mailing list