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

stephan at codespeak.net stephan at codespeak.net
Wed Jan 25 15:02:01 CET 2006


Author: stephan
Date: Wed Jan 25 15:01:56 2006
New Revision: 22649

Modified:
   pypy/dist/pypy/annotation/bookkeeper.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:
Structure creation, setattr, getattr gets annotated

Modified: pypy/dist/pypy/annotation/bookkeeper.py
==============================================================================
--- pypy/dist/pypy/annotation/bookkeeper.py	(original)
+++ pypy/dist/pypy/annotation/bookkeeper.py	Wed Jan 25 15:01:56 2006
@@ -354,7 +354,7 @@
         elif ishashable(x) and x in BUILTIN_ANALYZERS:
 	    _module = getattr(x,"__module__","unknown")
             result = SomeBuiltin(BUILTIN_ANALYZERS[x], methodname="%s.%s" % (_module, x.__name__))
-        elif hasattr(tp, "compute_result_annotation"):
+        elif hasattr(x, "compute_result_annotation"):
             result = SomeBuiltin(x.compute_result_annotation, methodname=x.__name__)
         elif hasattr(tp, "compute_annotation"):
             result = tp.compute_annotation()

Modified: pypy/dist/pypy/annotation/model.py
==============================================================================
--- pypy/dist/pypy/annotation/model.py	(original)
+++ pypy/dist/pypy/annotation/model.py	Wed Jan 25 15:01:56 2006
@@ -435,7 +435,6 @@
     def can_be_none(self):
         return False
 
-
 class SomeImpossibleValue(SomeObject):
     """The empty set.  Instances are placeholders for objects that
     will never show up at run-time, e.g. elements of an empty list."""

Modified: pypy/dist/pypy/annotation/unaryop.py
==============================================================================
--- pypy/dist/pypy/annotation/unaryop.py	(original)
+++ pypy/dist/pypy/annotation/unaryop.py	Wed Jan 25 15:01:56 2006
@@ -7,6 +7,7 @@
      SomeDict, SomeUnicodeCodePoint, SomeTuple, SomeImpossibleValue, \
      SomeInstance, SomeBuiltin, SomeFloat, SomeIterator, SomePBC, \
      SomeExternalObject, SomeTypedAddressAccess, SomeAddress, \
+     SomeCTypesObject,\
      unionof, set, missing_operation, add_knowntypedata
 from pypy.annotation.bookkeeper import getbookkeeper
 from pypy.annotation import builtin
@@ -620,6 +621,21 @@
         v = smeth(*llargs)
         return ll_to_annotation(v)
 
+class __extend__(SomeCTypesObject):
+    def setattr(cto, s_attr, s_value):
+        pass
+
+    def getattr(cto, s_attr):
+        if s_attr.is_constant() and isinstance(s_attr.const, str):
+            attr = s_attr.const
+            atype = cto.knowntype._fielddef_[attr]
+            try:
+                return atype.annotator_type
+            except AttributeError:
+                return SomeCTypesObject(atype)
+        else:
+            return SomeObject()
+
 #_________________________________________
 # memory addresses
 

Modified: pypy/dist/pypy/rpython/rctypes/implementation.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/implementation.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/implementation.py	Wed Jan 25 15:01:56 2006
@@ -54,12 +54,32 @@
             return answer
 
 
+class RStructureMeta(type(Structure)):
+    def __new__(mta,name,bases,clsdict):
+        _fields = clsdict.get('_fields_',None)
+        _adict = {}
+        if _fields is not None:
+            for attr, atype in _fields:
+                _adict[attr] = atype
+        clsdict['_fielddef_'] = _adict
+
+        return super(RStructureMeta,mta).__new__(mta, name, bases, clsdict)
+
 class RStructure(Structure):
 
+    __metaclass__ = RStructureMeta
+
     def compute_annotation(cls):
         return SomeCTypesObject(cls)
     compute_annotation = classmethod(compute_annotation)
 
+    def compute_result_annotation(cls, *args_s):
+        """
+        Answer the result annotation of calling 'cls'.
+        """
+        return SomeCTypesObject(cls)
+    compute_result_annotation = classmethod(compute_result_annotation)
+
 
 class RCDLL(CDLL):
     """

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	Wed Jan 25 15:01:56 2006
@@ -63,6 +63,14 @@
 
         mod.py_testfunc_struct_id = py_testfunc_struct_id
 
+        def py_create_point():
+            p = tagpoint()
+            p.x = 10
+            p.y = 20
+            return p.x + p.y
+
+        mod.py_create_point = py_create_point
+
 
 class Test_rctypes:
 
@@ -127,9 +135,11 @@
 
     def test_annotate_struct(self):
         a = RPythonAnnotator()
-        #try:
         s = a.build_types(py_testfunc_struct_id, [tagpoint])
-        #finally:
-        #    a.translator.view()
         assert s.knowntype == tagpoint
 
+    def test_create_point(self):
+        a = RPythonAnnotator()
+        s = a.build_types(py_create_point,[])
+        assert s.knowntype == int
+



More information about the Pypy-commit mailing list