[pypy-svn] r22778 - in pypy/dist/pypy: annotation rpython/rctypes rpython/rctypes/test

gromit at codespeak.net gromit at codespeak.net
Sat Jan 28 13:21:36 CET 2006


Author: gromit
Date: Sat Jan 28 13:21:32 2006
New Revision: 22778

Modified:
   pypy/dist/pypy/annotation/binaryop.py
   pypy/dist/pypy/annotation/model.py
   pypy/dist/pypy/annotation/unaryop.py
   pypy/dist/pypy/rpython/rctypes/implementation.py
   pypy/dist/pypy/rpython/rctypes/test/test_rctypes.py
Log:
ADD: Memory ownership tracking for all cytpes objects.

Modified: pypy/dist/pypy/annotation/binaryop.py
==============================================================================
--- pypy/dist/pypy/annotation/binaryop.py	(original)
+++ pypy/dist/pypy/annotation/binaryop.py	Sat Jan 28 13:21:32 2006
@@ -755,9 +755,13 @@
                 elif idx < 0:
                     raise IndexError( "invalid index" )
         try:
+            # This is the case for unboxed values, which
+            # are those having memorystate NOMEMORY
             return s_cto.knowntype._type_.annotator_type
         except AttributeError:
-            return SomeCTypesObject(s_cto.knowntype._type_)
+            return SomeCTypesObject(
+                    s_cto.knowntype._type_,
+                    memorystate=SomeCTypesObject.MEMORYALIAS)
 
 class __extend__(pairtype(SomeCTypesObject, SomeSlice)):
     def setitem((s_cto, s_slice), s_iterable):

Modified: pypy/dist/pypy/annotation/model.py
==============================================================================
--- pypy/dist/pypy/annotation/model.py	(original)
+++ pypy/dist/pypy/annotation/model.py	Sat Jan 28 13:21:32 2006
@@ -429,8 +429,16 @@
 class SomeCTypesObject(SomeObject):
     """Stands for an object of the ctypes module."""
 
-    def __init__(self, knowntype):
+    NOMEMORY = "NOMEMORY"
+    OWNSMEMORY = "OWNSMEMORY"
+    MEMORYALIAS = "MEMORYALIAS"
+    MIXEDMEMORYOWNERSHIP = "MIXEDMEMORYOWNERSHIP"
+    
+    def __init__(self, knowntype, memorystate=None):
+        if memorystate is None:
+            memorystate = knowntype.default_memorystate
         self.knowntype = knowntype
+        self.memorystate = memorystate 
 
     def can_be_none(self):
         return False

Modified: pypy/dist/pypy/annotation/unaryop.py
==============================================================================
--- pypy/dist/pypy/annotation/unaryop.py	(original)
+++ pypy/dist/pypy/annotation/unaryop.py	Sat Jan 28 13:21:32 2006
@@ -628,7 +628,17 @@
     def getattr(cto, s_attr):
         if s_attr.is_constant() and isinstance(s_attr.const, str):
             attr = s_attr.const
-            atype = cto.knowntype._fields_def_[attr]
+            try:
+                atype = cto.knowntype._fields_def_[attr]
+            except AttributeError:
+                # We are dereferencing a pointer by accessing its contents attribute
+                if s_attr.const == "contents":
+                    return SomeCTypesObject(
+                            cto.knowntype._type_, SomeCTypesObject.MEMORYALIAS)
+                else:
+                    raise AttributeError(
+                            "%r object has no attribute %r" % (
+                                cto.knowntype, s_attr.const))
             try:
                 return atype.annotator_type
             except AttributeError:

Modified: pypy/dist/pypy/rpython/rctypes/implementation.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/implementation.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/implementation.py	Sat Jan 28 13:21:32 2006
@@ -50,12 +50,14 @@
         the_type.ll_type = ll_type
         if wrap_arg is not None:
             the_type.wrap_arg = wrap_arg
+            the_type.default_memorystate = SomeCTypesObject.OWNSMEMORY
         else:
             # !!!! attention !!!!
             # the basic c_types need some annotation information
             # at the moment that are exactly the types that have
             # no 'wrap_arg'. This might change in the future
             the_type.compute_result_annotation = classmethod(lambda cls, s_arg:SomeCTypesObject(cls))
+            the_type.default_memorystate = SomeCTypesObject.NOMEMORY
 
 create_ctypes_annotations()
 
@@ -110,6 +112,8 @@
 
     __metaclass__ = RStructureMeta
 
+    default_memorystate = SomeCTypesObject.OWNSMEMORY
+
     def compute_annotation(cls):
         return SomeCTypesObject(cls)
     compute_annotation = classmethod(compute_annotation)
@@ -121,11 +125,13 @@
         """
         Answer the result annotation of calling 'cls'.
         """
-        return SomeCTypesObject(cls)
+        return SomeCTypesObject(cls,SomeCTypesObject.OWNSMEMORY)
     compute_result_annotation = classmethod(compute_result_annotation)
 
 class RByrefObj(object):
 
+    default_memorystate = SomeCTypesObject.MEMORYALIAS
+
     def __init__(self):
         self.__name__ = 'RByrefObj'
 
@@ -152,6 +158,8 @@
         assert answer is cls
         return SomeCTypesObject(cls)
     answer.compute_result_annotation = classmethod(compute_result_annotation)
+    #o#answer._fields_def_ = {"contents": cls}
+    answer.default_memorystate = SomeCTypesObject.MEMORYALIAS
     return answer
 
 
@@ -189,7 +197,7 @@
         Answer the result annotation of calling 'cls'.
         """
         assert answer is cls
-        return SomeCTypesObject(cls)
+        return SomeCTypesObject(cls, SomeCTypesObject.OWNSMEMORY)
     answer.compute_result_annotation = classmethod(compute_result_annotation)
     return answer
 

Modified: pypy/dist/pypy/rpython/rctypes/test/test_rctypes.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/test_rctypes.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/test/test_rctypes.py	Sat Jan 28 13:21:32 2006
@@ -3,6 +3,7 @@
 from pypy.translator.translator import TranslationContext
 from pypy.translator.c.test.test_genc import compile
 from pypy.translator.tool.cbuild import compile_c_module
+from pypy.annotation.model import SomeCTypesObject, SomeObject
 import sys
     
 try:
@@ -83,6 +84,18 @@
     res  = testfunc_byval(inpoint,oppoint)
     return res, oppoint
 
+def py_testfunc_POINTER_dereference(inpoint):
+    point = tagpoint()
+    oppoint = oppoint_type(point)
+    res  = testfunc_byval(inpoint,oppoint)
+    return res, oppoint.contents, oppoint[0]
+
+def py_test_mixed_memory_state( randomboolean ):
+    if randomboolean:
+        return tagpoint()
+    else:
+        return oppoint_type(tagpoint()).contents
+
 def py_test_simple_cint():
     return c_int(10)
 
@@ -222,32 +235,62 @@
         s = a.build_types(py_testfunc_struct, [int])
         assert s.knowntype == int
 
-
     def test_annotate_struct(self):
-        a = RPythonAnnotator()
+        t = TranslationContext()
+        a = t.buildannotator()
         s = a.build_types(py_testfunc_struct_id, [tagpoint])
         assert s.knowntype == tagpoint
+        assert s.memorystate == SomeCTypesObject.OWNSMEMORY
 
     def test_create_point(self):
-        a = RPythonAnnotator()
+        t = TranslationContext()
+        a = t.buildannotator()
         s = a.build_types(py_create_point,[])
         assert s.knowntype == int
 
     def test_annotate_byval(self):
-        a = RPythonAnnotator()
+        t = TranslationContext()
+        a = t.buildannotator()
         s = a.build_types(py_testfunc_byval,[tagpoint])
         assert s.knowntype == tuple
         assert len(s.items) == 2
         assert s.items[0].knowntype == int
         assert s.items[1].knowntype == tagpoint
+        assert s.items[1].memorystate == SomeCTypesObject.OWNSMEMORY 
 
     def test_annotate_POINTER(self):
-        a = RPythonAnnotator()
+        t = TranslationContext()
+        a = t.buildannotator()
         s = a.build_types(py_testfunc_POINTER,[tagpoint])
         assert s.knowntype == tuple
         assert len(s.items) == 2
         assert s.items[0].knowntype == int
         assert s.items[1].knowntype == POINTER(tagpoint)
+        assert s.items[1].memorystate == SomeCTypesObject.MEMORYALIAS 
+        #d#t.view()
+
+    def test_annotate_POINTER_dereference(self):
+        t = TranslationContext()
+        a = t.buildannotator()
+        s = a.build_types(py_testfunc_POINTER_dereference, [tagpoint])
+        assert s.knowntype == tuple
+        assert len(s.items) == 3
+        assert s.items[0].knowntype == int
+        assert s.items[1].knowntype == tagpoint
+        assert s.items[1].memorystate == SomeCTypesObject.MEMORYALIAS 
+        assert s.items[2].knowntype == tagpoint
+        assert s.items[2].memorystate == SomeCTypesObject.MEMORYALIAS 
+        #d#t.view()
+    
+    def test_annotate_mixed_memorystate(self):
+        t = TranslationContext()
+        a = t.buildannotator()
+        s = a.build_types(py_test_mixed_memory_state, [int])
+        #d#t.view()
+        assert s.knowntype == tagpoint
+        # This memory state will be supported in the future (#f#)
+        #f#assert s.memorystate == SomeCTypesObject.MIXEDMEMORYOWNERSHIP
+        assert isinstance(s, SomeObject)
 
     def test_annotate_simple_cint(self):
         a = RPythonAnnotator()



More information about the Pypy-commit mailing list