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

goden at codespeak.net goden at codespeak.net
Thu Mar 2 00:55:14 CET 2006


Author: goden
Date: Thu Mar  2 00:55:09 2006
New Revision: 23877

Added:
   pypy/dist/pypy/rpython/rctypes/rprimitive.py
   pypy/dist/pypy/rpython/rctypes/test/test_rprimitive.py
Modified:
   pypy/dist/pypy/annotation/unaryop.py
   pypy/dist/pypy/rpython/rctypes/implementation.py
   pypy/dist/pypy/rpython/rctypes/test/test_rarray.py
   pypy/dist/pypy/rpython/rctypes/test/test_rctypes.py
Log:
- (arigo, goden)
removed the redundant Test_rarray class from test_rctypes.
cleaned up the test_rarray module a bit.
new rprimitive module for annotating unmodified ctypes objects.
disabled the previous annotation of ctypes objects for primitive types.
added a lookup to the extregistry in the unaryop annotation module to find
the types of attributes of ctypes objects.
skipped existing rctypes tests while the ctypes primitive annotation is
reworked to use the rpython.extregistry module.
now importing the ctypes types instead of customized rctypes versions.



Modified: pypy/dist/pypy/annotation/unaryop.py
==============================================================================
--- pypy/dist/pypy/annotation/unaryop.py	(original)
+++ pypy/dist/pypy/annotation/unaryop.py	Thu Mar  2 00:55:09 2006
@@ -13,6 +13,7 @@
 from pypy.annotation import builtin
 
 from pypy.annotation.binaryop import _clone ## XXX where to put this?
+from pypy.rpython import extregistry
 
 # convenience only!
 def immutablevalue(x):
@@ -644,7 +645,12 @@
                                 "%r object has no attribute %r" % (
                                     cto.knowntype, s_attr.const))
             else:
-                atype = cto.knowntype._fields_def_[attr]
+                if extregistry.is_registered_type(cto.knowntype):
+                    entry = extregistry.lookup_type(cto.knowntype)
+                    s_value = entry.fields_s[attr]
+                    return s_value
+                else:
+                    atype = cto.knowntype._fields_def_[attr]
             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	Thu Mar  2 00:55:09 2006
@@ -22,24 +22,25 @@
 
 # Importing for side effect of registering types with extregistry
 import pypy.rpython.rctypes.rarray
+import pypy.rpython.rctypes.rprimitive
 
 # ctypes_annotation_list contains various attributes that
 # are used by the pypy annotation.
 
 ctypes_annotation_list = [
-    (c_char,          Char,             None),
-    (c_byte,          Signed,           None),
-    (c_ubyte,         Unsigned,         None),
-    (c_short,         Signed,           None),
-    (c_ushort,        Unsigned,         None),
-    (c_int,           Signed,           None),
-    (c_uint,          Unsigned,         None),
-    (c_long,          Signed,           None),
-    (c_ulong,         Unsigned,         None),
-    (c_longlong,      SignedLongLong,   None),
-    (c_ulonglong,     UnsignedLongLong, None),
-    (c_float,         Float,            None),
-    (c_double,        Float,            None),
+#    (c_char,          Char,             None),
+#    (c_byte,          Signed,           None),
+#    (c_ubyte,         Unsigned,         None),
+#    (c_short,         Signed,           None),
+#    (c_ushort,        Unsigned,         None),
+#    (c_int,           Signed,           None),
+#    (c_uint,          Unsigned,         None),
+#    (c_long,          Signed,           None),
+#    (c_ulong,         Unsigned,         None),
+#    (c_longlong,      SignedLongLong,   None),
+#    (c_ulonglong,     UnsignedLongLong, None),
+#    (c_float,         Float,            None),
+#    (c_double,        Float,            None),
     (c_char_p,        None, 
             staticmethod(lambda ll_type, arg_name:"RPyString_AsString(%s)" % arg_name)),
     (POINTER(c_char), None, 

Added: pypy/dist/pypy/rpython/rctypes/rprimitive.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/rpython/rctypes/rprimitive.py	Thu Mar  2 00:55:09 2006
@@ -0,0 +1,46 @@
+from ctypes import c_char, c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint
+from ctypes import c_long, c_ulong, c_longlong, c_ulonglong, c_float
+from ctypes import c_double, c_char_p
+from pypy.annotation import model as annmodel
+from pypy.rpython import extregistry
+from pypy.rpython.rmodel import Repr
+from pypy.rpython.lltypesystem import lltype
+from pypy.annotation.pairtype import pairtype
+from pypy.rpython.rmodel import IntegerRepr
+
+ctypes_annotation_list = [
+    (c_char,          lltype.Char),
+    (c_byte,          lltype.Signed),
+    (c_ubyte,         lltype.Unsigned),
+    (c_short,         lltype.Signed),
+    (c_ushort,        lltype.Unsigned),
+    (c_int,           lltype.Signed),
+    (c_uint,          lltype.Unsigned),
+    (c_long,          lltype.Signed),
+    (c_ulong,         lltype.Unsigned),
+    (c_longlong,      lltype.SignedLongLong),
+    (c_ulonglong,     lltype.UnsignedLongLong),
+    (c_float,         lltype.Float),
+    (c_double,        lltype.Float),
+]
+
+def do_register(the_type, ll_type):
+    def annotation_function(s_arg):
+        return annmodel.SomeCTypesObject(the_type,
+                annmodel.SomeCTypesObject.OWNSMEMORY)
+
+    extregistry.register_value(the_type,
+        compute_result_annotation=annotation_function)
+    entry = extregistry.register_type(the_type)
+    entry.fields_s = {'value': annmodel.lltype_to_annotation(ll_type)}
+
+for the_type, ll_type in ctypes_annotation_list:
+    do_register(the_type, ll_type)
+
+#extregistry.register_type(ArrayType, 
+#    compute_annotation=arraytype_compute_annotation,
+#    specialize_call=arraytype_specialize_call)
+
+#def arraytype_get_repr(rtyper, s_array):
+#    return ArrayRepr(rtyper, s_array.knowntype)
+#extregistry.register_metatype(ArrayType, get_repr=arraytype_get_repr)

Modified: pypy/dist/pypy/rpython/rctypes/test/test_rarray.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/test_rarray.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/test/test_rarray.py	Thu Mar  2 00:55:09 2006
@@ -15,10 +15,7 @@
 except ImportError:
     py.test.skip("this test needs ctypes installed")
 
-from pypy.rpython.rctypes import cdll, c_char_p, c_int, c_char, \
-        c_char, c_byte, c_ubyte, c_short, c_ushort, c_uint,\
-        c_long, c_ulong, c_longlong, c_ulonglong, c_float, c_double, \
-        POINTER, Structure, byref, ARRAY
+from ctypes import c_int, ARRAY, POINTER
 
 c_int_10 = ARRAY(c_int,10)
 c_int_p_test = POINTER(c_int)

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	Thu Mar  2 00:55:09 2006
@@ -43,6 +43,8 @@
     py.test.skip("this test needs ctypes installed")
 
 
+py.test.skip("these tests are broken while the ctypes primitive types are ported to use the extregistry")
+
 from pypy.rpython.rctypes import cdll, c_char_p, c_int, c_char, \
         c_char, c_byte, c_ubyte, c_short, c_ushort, c_uint,\
         c_long, c_ulong, c_longlong, c_ulonglong, c_float, c_double, \
@@ -243,7 +245,6 @@
     s.y = y
     return s
 
-
 class Test_rctypes:
 
     def test_simple(self):
@@ -469,84 +470,3 @@
         fn = compile( py_test_compile_pointer, [ int, int ] )
         res = fn( -42, 42 )
         assert res == -42
-
-
-class Test_array:
-
-    def test_annotate_array(self):
-        a = RPythonAnnotator()
-        s = a.build_types(py_test_annotate_array, [])
-        assert s.knowntype == c_int_10
-
-        if conftest.option.view:
-            a.translator.view()
-
-    def test_annotate_array_access(self):
-        t = TranslationContext()
-        a = t.buildannotator()
-        s = a.build_types(py_test_annotate_array_content, [])
-        assert s.knowntype == int
-
-        if conftest.option.view:
-            t.view()
-
-    def test_annotate_pointer_access_as_array(self):
-        """
-        Make sure that pointers work the same way as arrays, for 
-        ctypes compatibility.
-
-        :Note: This works because pointer and array classes both
-        have a _type_ attribute, that contains the type of the 
-        object pointed to or in the case of an array the element type. 
-        """
-        t = TranslationContext()
-        a = t.buildannotator()
-        s = a.build_types(py_test_annotate_pointer_content, [])
-        assert s.knowntype == int
-        #d#t.view()
-
-    def test_annotate_array_slice_access(self):
-        t = TranslationContext()
-        a = t.buildannotator()
-        s = a.build_types(py_test_annotate_array_slice_content, [])
-        #d#t.view()
-        #d#print "v90:", s, type(s)
-        assert s.knowntype == list
-        s.listdef.listitem.s_value.knowntype == int
-
-    def test_annotate_array_access_variable(self):
-        t = TranslationContext()
-        a = t.buildannotator()
-        s = a.build_types(py_test_annotate_array_content_variable_index, [])
-        assert s.knowntype == int
-        #t#t.view()
-
-    def test_annotate_array_access_index_error_on_positive_index(self):
-        t = TranslationContext()
-        a = t.buildannotator()
-        
-        py.test.raises(IndexError, "s = a.build_types(py_test_annotate_array_content_index_error_on_positive_index,[])")
-
-    def test_annotate_array_access_index_error_on_negative_index(self):
-        t = TranslationContext()
-        a = t.buildannotator()
-        
-        py.test.raises(IndexError, "s = a.build_types(py_test_annotate_array_content_index_error_on_negative_index,[])")
-
-    def test_specialize_array(self):
-        res = interpret(py_test_annotate_array, [])
-        c_data = res.c_data
-        assert c_data[0] == 0
-        assert c_data[9] == 0
-        py.test.raises(IndexError, "c_data[10]")
-        py.test.raises(TypeError, "len(c_data)")
-
-    def test_specialize_array_access(self):
-        def test_specialize_array_access():
-            my_array = c_int_10()
-            my_array[0] = 1
-
-            return my_array[0]
-
-        res = interpret(test_specialize_array_access, [])
-        assert res == 1

Added: pypy/dist/pypy/rpython/rctypes/test/test_rprimitive.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/rpython/rctypes/test/test_rprimitive.py	Thu Mar  2 00:55:09 2006
@@ -0,0 +1,36 @@
+"""
+Test the rctypes implementation.
+"""
+
+import py.test
+from pypy.annotation.annrpython import RPythonAnnotator
+from pypy.translator.translator import TranslationContext
+from pypy.translator.c.test.test_genc import compile
+from pypy.annotation.model import SomeCTypesObject, SomeObject
+from pypy import conftest
+import sys
+from pypy.rpython.test.test_llinterp import interpret
+
+from ctypes import c_char, c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint
+from ctypes import c_long, c_ulong, c_longlong, c_ulonglong, c_float
+from ctypes import c_double, c_char_p
+
+class Test_annotation:
+    def test_simple(self):
+        res = c_int(42)
+        assert res.value == 42 
+
+    def test_annotate_c_int(self):
+        def func():
+            res = c_int(42)
+
+            return res.value
+
+        t = TranslationContext()
+        a = t.buildannotator()
+        s = a.build_types(func, [])
+
+        assert s.knowntype == int
+
+        if conftest.option.view:
+            t.view()



More information about the Pypy-commit mailing list