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

stephan at codespeak.net stephan at codespeak.net
Wed Jan 25 14:11:07 CET 2006


Author: stephan
Date: Wed Jan 25 14:10:57 2006
New Revision: 22642

Modified:
   pypy/dist/pypy/annotation/bookkeeper.py
   pypy/dist/pypy/annotation/model.py
   pypy/dist/pypy/rpython/rctypes/implementation.py
   pypy/dist/pypy/rpython/rctypes/interface.py
   pypy/dist/pypy/rpython/rctypes/test/_rctypes_test.c
   pypy/dist/pypy/rpython/rctypes/test/test_rctypes.py
Log:
ctype Structures can be annotated

Modified: pypy/dist/pypy/annotation/bookkeeper.py
==============================================================================
--- pypy/dist/pypy/annotation/bookkeeper.py	(original)
+++ pypy/dist/pypy/annotation/bookkeeper.py	Wed Jan 25 14:10:57 2006
@@ -354,8 +354,10 @@
         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(x, "compute_result_annotation"):
+        elif hasattr(tp, "compute_result_annotation"):
             result = SomeBuiltin(x.compute_result_annotation, methodname=x.__name__)
+        elif hasattr(tp, "compute_annotation"):
+            result = tp.compute_annotation()
         elif tp in EXTERNAL_TYPE_ANALYZERS:
             result = SomeExternalObject(tp)
         elif isinstance(x, lltype._ptr):
@@ -501,6 +503,8 @@
             return s_None
         elif t in EXTERNAL_TYPE_ANALYZERS:
             return SomeExternalObject(t)
+        elif hasattr(t, "compute_annotation"):
+            return t.compute_annotation()
         elif t.__module__ != '__builtin__' and t not in self.pbctypes:
             classdef = self.getuniqueclassdef(t)
             return SomeInstance(classdef)

Modified: pypy/dist/pypy/annotation/model.py
==============================================================================
--- pypy/dist/pypy/annotation/model.py	(original)
+++ pypy/dist/pypy/annotation/model.py	Wed Jan 25 14:10:57 2006
@@ -426,6 +426,15 @@
     def can_be_none(self):
         return True
 
+class SomeCTypesObject(SomeObject):
+    """Stands for an object of the ctypes module."""
+
+    def __init__(self, knowntype):
+        self.knowntype = knowntype
+
+    def can_be_none(self):
+        return False
+
 
 class SomeImpossibleValue(SomeObject):
     """The empty set.  Instances are placeholders for objects that

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 14:10:57 2006
@@ -7,7 +7,7 @@
 from ctypes import _FUNCFLAG_CDECL
 if sys.platform == "win32":
     from ctypes import _FUNCFLAG_STDCALL
-from pypy.annotation.model import SomeInteger
+from pypy.annotation.model import SomeInteger, SomeCTypesObject
 from pypy.rpython.lltypesystem.lltype import Signed
 
 
@@ -22,13 +22,17 @@
 POINTER(c_char).wrap_arg = staticmethod(
         lambda ll_type, arg_name: "RPyString_AsString(%s)" % arg_name)
 
+
 class FunctionPointerTranslation(object):
 
         def compute_result_annotation(self, *args_s):
             """
             Answer the annotation of the external function's result
             """
-            return self.restype.annotator_type
+            try:
+                return self.restype.annotator_type
+            except AttributeError:
+                return SomeCTypesObject(self.restype)
 
         def __hash__(self):
             return id(self)
@@ -50,6 +54,13 @@
             return answer
 
 
+class RStructure(Structure):
+
+    def compute_annotation(cls):
+        return SomeCTypesObject(cls)
+    compute_annotation = classmethod(compute_annotation)
+
+
 class RCDLL(CDLL):
     """
     This is the restricted version of ctypes' CDLL class.

Modified: pypy/dist/pypy/rpython/rctypes/interface.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/interface.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/interface.py	Wed Jan 25 14:10:57 2006
@@ -1,5 +1,5 @@
 from ctypes import _DLLS
-from implementation import RCDLL as CDLL, c_int, c_char_p, c_char, POINTER, Structure, byref
+from implementation import RCDLL as CDLL, c_int, c_char_p, c_char, POINTER, RStructure as Structure, byref
 try:
     from implementation import RWinDLL as WinDLL
 except ImportError:

Modified: pypy/dist/pypy/rpython/rctypes/test/_rctypes_test.c
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/_rctypes_test.c	(original)
+++ pypy/dist/pypy/rpython/rctypes/test/_rctypes_test.c	Wed Jan 25 14:10:57 2006
@@ -28,6 +28,16 @@
 	return in.x + in.y;
 }
 
+EXPORT(int) _testfunc_struct(point in)
+{
+	return in.x + in.y;
+}
+
+EXPORT(point) _testfunc_struct_id(point in)
+{
+	return in;
+}
+
 DL_EXPORT(void)
 init_rctypes_test(void)
 {

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 14:10:57 2006
@@ -35,6 +35,34 @@
         mod.tagpoint = tagpoint
         mod.byref = byref
 
+        # compile and load our local test C file
+        compile_c_module([py.path.local("_rctypes_test.c")], "_rctypes_test")
+
+        if sys.platform == "win32":
+            _rctypes_test = cdll.LoadLibrary("_rctypes_test.pyd")
+        else:
+            _rctypes_test = cdll.LoadLibrary("_rctypes_test.so")
+
+        # _test_struct
+        testfunc_struct = _rctypes_test._testfunc_struct
+        testfunc_struct.restype = c_int
+        testfunc_struct.argtypes = [tagpoint]
+
+        def py_testfunc_struct(inpoint):
+            return testfunc_struct(inpoint)
+
+        mod.py_testfunc_struct = py_testfunc_struct
+
+        # _test_struct_id
+        testfunc_struct_id = _rctypes_test._testfunc_struct_id
+        testfunc_struct_id.restype = tagpoint
+        testfunc_struct_id.argtypes = [tagpoint]
+
+        def py_testfunc_struct_id(inpoint):
+            return testfunc_struct_id(inpoint)
+
+        mod.py_testfunc_struct_id = py_testfunc_struct_id
+
 
 class Test_rctypes:
 
@@ -65,9 +93,6 @@
 
 class Test_structure:
 
-    def setup_class(cls):
-        compile_c_module([py.path.local("_rctypes_test.c")], "_rctypes_test")
-
     def test_simple_as_extension_module(self):
         import _rctypes_test as t0
         import _rctypes_test as t1
@@ -87,4 +112,24 @@
         assert out_point.x == 42
         assert out_point.y == 17
 
+    def test_structure(self):
+        in_point = tagpoint()
+        in_point.x = 10
+        in_point.y = 20
+        res = py_testfunc_struct(in_point)
+        assert res == 30
+
+    def test_annotate_struct(self):
+        a = RPythonAnnotator()
+        s = a.build_types(py_testfunc_struct, [int])
+        assert s.knowntype == int
+
+
+    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
 



More information about the Pypy-commit mailing list