[pypy-svn] r25857 - in pypy/dist/pypy/rpython/rctypes: . test

arigo at codespeak.net arigo at codespeak.net
Sat Apr 15 14:44:48 CEST 2006


Author: arigo
Date: Sat Apr 15 14:44:44 2006
New Revision: 25857

Added:
   pypy/dist/pypy/rpython/rctypes/aarray.py
      - copied, changed from r25852, pypy/dist/pypy/rpython/rctypes/rarray.py
   pypy/dist/pypy/rpython/rctypes/achar_p.py
      - copied, changed from r25852, pypy/dist/pypy/rpython/rctypes/rchar_p.py
   pypy/dist/pypy/rpython/rctypes/afunc.py
      - copied, changed from r25852, pypy/dist/pypy/rpython/rctypes/rfunc.py
   pypy/dist/pypy/rpython/rctypes/apointer.py
      - copied, changed from r25852, pypy/dist/pypy/rpython/rctypes/rpointer.py
   pypy/dist/pypy/rpython/rctypes/aprimitive.py
      - copied, changed from r25852, pypy/dist/pypy/rpython/rctypes/rprimitive.py
   pypy/dist/pypy/rpython/rctypes/astruct.py
      - copied, changed from r25852, pypy/dist/pypy/rpython/rctypes/rstruct.py
   pypy/dist/pypy/rpython/rctypes/avoid_p.py
      - copied, changed from r25852, pypy/dist/pypy/rpython/rctypes/rvoid_p.py
   pypy/dist/pypy/rpython/rctypes/test/test_import.py   (contents, props changed)
Removed:
   pypy/dist/pypy/rpython/rctypes/rfunc.py
Modified:
   pypy/dist/pypy/rpython/rctypes/implementation.py
   pypy/dist/pypy/rpython/rctypes/rarray.py
   pypy/dist/pypy/rpython/rctypes/rchar_p.py
   pypy/dist/pypy/rpython/rctypes/rpointer.py
   pypy/dist/pypy/rpython/rctypes/rprimitive.py
   pypy/dist/pypy/rpython/rctypes/rstruct.py
   pypy/dist/pypy/rpython/rctypes/rvoid_p.py
Log:
Split all r* modules in two files, the a* part doing the extregistering
and annotating, and the r* part containing the Repr.  This allows us to
annotate rpython programs using rctypes without importing the complete
rtyper.


Modified: pypy/dist/pypy/rpython/rctypes/implementation.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/implementation.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/implementation.py	Sat Apr 15 14:44:44 2006
@@ -1,11 +1,11 @@
 # Importing for side effect of registering types with extregistry
-import pypy.rpython.rctypes.rprimitive
-import pypy.rpython.rctypes.rpointer
-import pypy.rpython.rctypes.rarray
-import pypy.rpython.rctypes.rfunc
-import pypy.rpython.rctypes.rchar_p
-import pypy.rpython.rctypes.rstruct
-import pypy.rpython.rctypes.rvoid_p
+import pypy.rpython.rctypes.aprimitive
+import pypy.rpython.rctypes.apointer
+import pypy.rpython.rctypes.aarray
+import pypy.rpython.rctypes.afunc
+import pypy.rpython.rctypes.achar_p
+import pypy.rpython.rctypes.astruct
+import pypy.rpython.rctypes.avoid_p
 
 
 # Register the correspondance between SomeCTypesObject and the get_repr()

Modified: pypy/dist/pypy/rpython/rctypes/rarray.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/rarray.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/rarray.py	Sat Apr 15 14:44:44 2006
@@ -1,16 +1,16 @@
 from ctypes import ARRAY, c_int
-from pypy.annotation.model import SomeCTypesObject, SomeBuiltin
-from pypy.rpython import extregistry
 from pypy.rpython.rbuiltin import gen_cast_subarray_pointer
-from pypy.rpython.rmodel import Repr, IntegerRepr, inputconst
+from pypy.rpython.rmodel import IntegerRepr, inputconst
 from pypy.rpython.lltypesystem import lltype
 from pypy.annotation.pairtype import pairtype
 from pypy.rpython.rctypes.rmodel import CTypesRefRepr, CTypesValueRepr
 from pypy.rpython.rctypes.rmodel import genreccopy, reccopy
 from pypy.rpython.rctypes.rprimitive import PrimitiveRepr
+from pypy.annotation.model import SomeCTypesObject
 
 ArrayType = type(ARRAY(c_int, 10))
 
+
 class ArrayRepr(CTypesRefRepr):
     def __init__(self, rtyper, s_array):
         array_ctype = s_array.knowntype
@@ -89,31 +89,3 @@
             # ByValue case (optimization; the above also works in this case)
             v_newvalue = r_array.r_item.getvalue(hop.llops, v_item)
             r_array.set_item_value(hop.llops, v_array, v_index, v_newvalue)
-
-# ____________________________________________________________
-
-def arraytype_specialize_call(hop):
-    r_array = hop.r_result
-    return hop.genop("malloc", [
-        hop.inputconst(lltype.Void, r_array.lowleveltype.TO), 
-        ], resulttype=r_array.lowleveltype,
-    )
-
-def arraytype_compute_annotation(metatype, type):
-    def compute_result_annotation(*arg_s):
-        return SomeCTypesObject(type, SomeCTypesObject.OWNSMEMORY)
-    return SomeBuiltin(compute_result_annotation, methodname=type.__name__)
-
-extregistry.register_type(ArrayType, 
-    compute_annotation=arraytype_compute_annotation,
-    specialize_call=arraytype_specialize_call)
-
-def array_instance_compute_annotation(type, instance):
-    return SomeCTypesObject(type, SomeCTypesObject.OWNSMEMORY)
-
-def arraytype_get_repr(rtyper, s_array):
-    return ArrayRepr(rtyper, s_array)
-
-extregistry.register_metatype(ArrayType,
-    compute_annotation=array_instance_compute_annotation,
-    get_repr=arraytype_get_repr)

Modified: pypy/dist/pypy/rpython/rctypes/rchar_p.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/rchar_p.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/rchar_p.py	Sat Apr 15 14:44:44 2006
@@ -1,10 +1,8 @@
-from pypy.rpython import extregistry
 from pypy.rpython.rmodel import inputconst
 from pypy.rpython.lltypesystem import lltype
 from pypy.rpython.rstr import StringRepr, string_repr
 from pypy.rpython.rctypes.rmodel import CTypesValueRepr, C_ZERO
 from pypy.rpython.rctypes.rarray import ArrayRepr
-from pypy.annotation import model as annmodel
 from pypy.annotation.pairtype import pairtype
 
 from ctypes import c_char, c_char_p
@@ -126,39 +124,3 @@
     else:
         box.c_data[0] = lltype.nullptr(CCHARP.TO)
     box.keepalive_str = string
-
-
-def c_char_p_compute_result_annotation(s_arg=None):
-    return annmodel.SomeCTypesObject(c_char_p,
-            annmodel.SomeCTypesObject.OWNSMEMORY)
-
-def c_char_p_specialize_call(hop):
-    r_char_p = hop.r_result
-    v_result = r_char_p.allocate_instance(hop.llops)
-    if len(hop.args_s):
-        v_value, = hop.inputargs(string_repr)
-        r_char_p.setstring(hop.llops, v_result, v_value)
-    return v_result
-
-extregistry.register_value(c_char_p,
-    compute_result_annotation=c_char_p_compute_result_annotation,
-    specialize_call=c_char_p_specialize_call
-    )
-
-def c_char_compute_annotation(the_type, instance):
-    return annmodel.SomeCTypesObject(c_char_p,
-                                     annmodel.SomeCTypesObject.OWNSMEMORY)
-
-def c_char_p_get_repr(rtyper, s_char_p):
-    return CCharPRepr(rtyper, s_char_p, CCHARP)
-
-entry = extregistry.register_type(c_char_p,
-        compute_annotation = c_char_compute_annotation,
-        get_repr           = c_char_p_get_repr,
-        )
-s_value_annotation = annmodel.SomeString(can_be_None=True)
-def c_char_p_get_field_annotation(s_char_p, fieldname):
-    assert fieldname == 'value'
-    return s_value_annotation
-entry.get_field_annotation = c_char_p_get_field_annotation
-entry.s_return_trick = s_value_annotation

Modified: pypy/dist/pypy/rpython/rctypes/rpointer.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/rpointer.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/rpointer.py	Sat Apr 15 14:44:44 2006
@@ -1,12 +1,10 @@
-from pypy.rpython.rmodel import Repr, IntegerRepr, inputconst
+from pypy.rpython.rmodel import IntegerRepr, inputconst
 from pypy.rpython.error import TyperError
-from pypy.rpython import extregistry
 from pypy.rpython.lltypesystem import lltype
-from pypy.annotation import model as annmodel
 from pypy.annotation.pairtype import pairtype
 from pypy.rpython.rctypes.rmodel import CTypesValueRepr, genreccopy
+from pypy.annotation.model import SomeCTypesObject
 
-from ctypes import POINTER, pointer, byref, c_int
 
 class PointerRepr(CTypesValueRepr):
     def __init__(self, rtyper, s_pointer):
@@ -14,8 +12,8 @@
         ref_ctype = ptr_ctype._type_
 
         # Find the repr and low-level type of the contents from its ctype
-        self.r_contents = rtyper.getrepr(annmodel.SomeCTypesObject(ref_ctype,
-                                     annmodel.SomeCTypesObject.MEMORYALIAS))
+        self.r_contents = rtyper.getrepr(SomeCTypesObject(ref_ctype,
+                                               SomeCTypesObject.MEMORYALIAS))
 
         ll_contents = lltype.Ptr(self.r_contents.c_data_type)
 
@@ -87,78 +85,3 @@
             raise TyperError("assignment to pointer[x] with x != 0")
         # copy the whole structure's content over
         genreccopy(hop.llops, v_new_c_data, v_target)
-
-# ____________________________________________________________
-
-def pointertype_compute_annotation(metatype, type):
-    def compute_result_annotation(*arg_s):
-        return annmodel.SomeCTypesObject(type,
-                annmodel.SomeCTypesObject.OWNSMEMORY)
-    return annmodel.SomeBuiltin(compute_result_annotation, 
-                                methodname=type.__name__)
-
-def pointertype_specialize_call(hop):
-    r_ptr = hop.r_result
-    v_result = r_ptr.allocate_instance(hop.llops)
-    if len(hop.args_s):
-        v_contentsbox, = hop.inputargs(r_ptr.r_contents)
-        r_ptr.setcontents(hop.llops, v_result, v_contentsbox)
-    return v_result
-
-def pointerinstance_compute_annotation(type, instance):
-    return annmodel.SomeCTypesObject(type,
-            annmodel.SomeCTypesObject.OWNSMEMORY)
-
-def pointerinstance_field_annotation(s_pointer, fieldname):
-    assert fieldname == "contents"
-    ptrtype = s_pointer.knowntype
-    return annmodel.SomeCTypesObject(ptrtype._type_,
-                                     annmodel.SomeCTypesObject.MEMORYALIAS)
-
-def pointerinstance_get_repr(rtyper, s_pointer):
-    return PointerRepr(rtyper, s_pointer)
-
-PointerType = type(POINTER(c_int))
-extregistry.register_type(PointerType,
-        compute_annotation=pointertype_compute_annotation,
-        specialize_call=pointertype_specialize_call)
-
-entry = extregistry.register_metatype(PointerType,
-        compute_annotation=pointerinstance_compute_annotation,
-        get_repr=pointerinstance_get_repr)
-entry.get_field_annotation = pointerinstance_field_annotation
-
-def pointerfn_compute_annotation(s_arg):
-    assert isinstance(s_arg, annmodel.SomeCTypesObject)
-    ctype = s_arg.knowntype
-    result_ctype = POINTER(ctype)
-    return annmodel.SomeCTypesObject(result_ctype,
-                                     annmodel.SomeCTypesObject.OWNSMEMORY)
-
-extregistry.register_value(pointer,
-        compute_result_annotation=pointerfn_compute_annotation,
-        # same rtyping for calling pointer() or calling a specific instance
-        # of PointerType:
-        specialize_call=pointertype_specialize_call)
-
-# byref() is equivalent to pointer() -- the difference is only an
-# optimization that is useful in ctypes but not in rctypes.
-extregistry.register_value(byref,
-        compute_result_annotation=pointerfn_compute_annotation,
-        specialize_call=pointertype_specialize_call)
-
-# constant-fold POINTER(CONSTANT_CTYPE) calls
-def POINTER_compute_annotation(s_arg):
-    from pypy.annotation.bookkeeper import getbookkeeper
-    assert s_arg.is_constant(), "POINTER(%r): argument must be constant" % (
-        s_arg,)
-    RESTYPE = POINTER(s_arg.const)
-    return getbookkeeper().immutablevalue(RESTYPE)
-
-def POINTER_specialize_call(hop):
-    assert hop.s_result.is_constant()
-    return hop.inputconst(lltype.Void, hop.s_result.const)
-
-extregistry.register_value(POINTER,
-        compute_result_annotation=POINTER_compute_annotation,
-        specialize_call=POINTER_specialize_call)

Modified: pypy/dist/pypy/rpython/rctypes/rprimitive.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/rprimitive.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/rprimitive.py	Sat Apr 15 14:44:44 2006
@@ -1,30 +1,10 @@
-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, inputconst
+from pypy.rpython.rmodel import inputconst
 from pypy.rpython.lltypesystem import lltype
 from pypy.annotation.pairtype import pairtype
 from pypy.rpython.rmodel import IntegerRepr, FloatRepr, CharRepr
 from pypy.rpython.error import TyperError
 from pypy.rpython.rctypes.rmodel import CTypesValueRepr
 
-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,
-}.items()   # nb. platform-dependent duplicate ctypes are removed
 
 class PrimitiveRepr(CTypesValueRepr):
 
@@ -69,43 +49,3 @@
         # return this box possibly converted to the expected output repr,
         # which might be a memory-aliasing box
         return llops.convertvar(v_owned_box, r_temp, r_to)
-
-
-def primitive_specialize_call(hop):
-    r_primitive = hop.r_result
-    v_result = r_primitive.allocate_instance(hop.llops)
-    if len(hop.args_s):
-        v_value, = hop.inputargs(r_primitive.ll_type)
-        r_primitive.setvalue(hop.llops, v_result, v_value)
-    return v_result
-
-def do_register(the_type, ll_type):
-    def compute_result_annotation_function(s_arg=None):
-        return annmodel.SomeCTypesObject(the_type,
-                annmodel.SomeCTypesObject.OWNSMEMORY)
-
-    extregistry.register_value(the_type,
-        compute_result_annotation=compute_result_annotation_function,
-        specialize_call=primitive_specialize_call
-        )
-
-    def compute_prebuilt_instance_annotation(the_type, instance):
-        return annmodel.SomeCTypesObject(the_type,
-                annmodel.SomeCTypesObject.OWNSMEMORY)
-
-    def primitive_get_repr(rtyper, s_primitive):
-        return PrimitiveRepr(rtyper, s_primitive, ll_type)
-
-    entry = extregistry.register_type(the_type,
-            compute_annotation=compute_prebuilt_instance_annotation,
-            get_repr=primitive_get_repr,
-            )
-    s_value_annotation = annmodel.lltype_to_annotation(ll_type)
-    def primitive_get_field_annotation(s_primitive, fieldname):
-        assert fieldname == 'value'
-        return s_value_annotation
-    entry.get_field_annotation = primitive_get_field_annotation
-    entry.s_return_trick = s_value_annotation
-
-for the_type, ll_type in ctypes_annotation_list:
-    do_register(the_type, ll_type)

Modified: pypy/dist/pypy/rpython/rctypes/rstruct.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/rstruct.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/rstruct.py	Sat Apr 15 14:44:44 2006
@@ -1,14 +1,11 @@
-from ctypes import Structure
-from pypy.annotation.model import SomeCTypesObject, SomeBuiltin
-from pypy.rpython import extregistry
 from pypy.rpython.rmodel import inputconst
 from pypy.rpython.rbuiltin import gen_cast_structfield_pointer
 from pypy.rpython.lltypesystem import lltype
 from pypy.rpython.rctypes.rmodel import CTypesRefRepr, CTypesValueRepr
 from pypy.rpython.rctypes.rmodel import genreccopy, reccopy
 from pypy.rpython.rctypes.rprimitive import PrimitiveRepr
+from pypy.annotation.model import SomeCTypesObject
 
-StructType = type(Structure)
 
 class StructRepr(CTypesRefRepr):
     def __init__(self, rtyper, s_struct):
@@ -103,40 +100,3 @@
             # ByValue case (optimization; the above also works in this case)
             v_newvalue = r_field.getvalue(hop.llops, v_item)
             self.set_field_value(hop.llops, v_struct, name, v_newvalue)
-
-# ____________________________________________________________
-
-def structtype_specialize_call(hop):
-    r_struct = hop.r_result
-    return hop.genop("malloc", [
-        hop.inputconst(lltype.Void, r_struct.lowleveltype.TO), 
-        ], resulttype=r_struct.lowleveltype,
-    )
-
-def structtype_compute_annotation(metatype, type):
-    def compute_result_annotation(*arg_s):
-        return SomeCTypesObject(type, SomeCTypesObject.OWNSMEMORY)
-    return SomeBuiltin(compute_result_annotation, methodname=type.__name__)
-
-extregistry.register_type(StructType, 
-    compute_annotation=structtype_compute_annotation,
-    specialize_call=structtype_specialize_call)
-
-def struct_instance_compute_annotation(type, instance):
-    return SomeCTypesObject(type, SomeCTypesObject.OWNSMEMORY)
-
-def struct_instance_field_annotation(s_struct, fieldname):
-    structtype = s_struct.knowntype
-    for name, ctype in structtype._fields_:
-        if name == fieldname:
-            s_result = SomeCTypesObject(ctype, SomeCTypesObject.MEMORYALIAS)
-            return s_result.return_annotation()
-    raise AttributeError('%r has no field %r' % (structtype, fieldname))
-
-def structtype_get_repr(rtyper, s_struct):
-    return StructRepr(rtyper, s_struct)
-
-entry = extregistry.register_metatype(StructType,
-    compute_annotation=struct_instance_compute_annotation,
-    get_repr=structtype_get_repr)
-entry.get_field_annotation = struct_instance_field_annotation

Modified: pypy/dist/pypy/rpython/rctypes/rvoid_p.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/rvoid_p.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/rvoid_p.py	Sat Apr 15 14:44:44 2006
@@ -1,72 +1,5 @@
-from pypy.rpython import extregistry
-from pypy.rpython.lltypesystem import lltype, llmemory
-from pypy.annotation import model as annmodel
-from pypy.rpython.rctypes.rmodel import CTypesValueRepr, C_ZERO
-from pypy.rpython.rctypes.rpointer import PointerRepr
-
-from ctypes import c_void_p, c_int, POINTER, cast
-
-PointerType = type(POINTER(c_int))
+from pypy.rpython.rctypes.rmodel import CTypesValueRepr
 
 
 class CVoidPRepr(CTypesValueRepr):
     pass  # No operations supported on c_void_p instances so far
-
-
-# c_void_p() as a function
-def c_void_p_compute_result_annotation(s_arg=None):
-    raise NotImplementedError("XXX calling c_void_p()")
-
-extregistry.register_value(c_void_p,
-    compute_result_annotation=c_void_p_compute_result_annotation,
-    )
-
-# c_void_p instances
-def c_void_compute_annotation(the_type, instance):
-    return annmodel.SomeCTypesObject(c_void_p,
-                                     annmodel.SomeCTypesObject.OWNSMEMORY)
-
-def c_void_p_get_repr(rtyper, s_void_p):
-    return CVoidPRepr(rtyper, s_void_p, llmemory.Address)
-
-extregistry.register_type(c_void_p,
-    compute_annotation = c_void_compute_annotation,
-    get_repr           = c_void_p_get_repr,
-    )
-
-# cast() support
-def cast_compute_result_annotation(s_arg, s_type):
-    assert s_type.is_constant(), "cast(p, %r): argument 2 must be constant" % (
-        s_type,)
-    type = s_type.const
-
-    def checkptr(ctype):
-        assert isinstance(ctype, PointerType) or ctype == c_void_p, (
-            "cast(): can only cast between pointers so far, not %r" % (ctype,))
-    checkptr(s_arg.knowntype)
-    checkptr(type)
-    return annmodel.SomeCTypesObject(type,
-                                     annmodel.SomeCTypesObject.OWNSMEMORY)
-
-def cast_specialize_call(hop):
-    assert isinstance(hop.args_r[0], (PointerRepr, CVoidPRepr))
-    targetctype = hop.args_s[1].const
-    v_box, c_targetctype = hop.inputargs(hop.args_r[0], lltype.Void)
-    v_adr = hop.args_r[0].getvalue(hop.llops, v_box)
-    if v_adr.concretetype != llmemory.Address:
-        v_adr = hop.genop('cast_ptr_to_adr', [v_adr],
-                          resulttype = llmemory.Address)
-
-    if targetctype == c_void_p:
-        # cast to void
-        v_result = v_adr
-    else:
-        # cast to pointer
-        v_result = hop.genop('cast_adr_to_ptr', [v_adr],
-                             resulttype = hop.r_result.ll_type)
-    return hop.r_result.return_value(hop.llops, v_result)
-
-extregistry.register_value(cast,
-    compute_result_annotation=cast_compute_result_annotation,
-    specialize_call=cast_specialize_call,
-    )

Added: pypy/dist/pypy/rpython/rctypes/test/test_import.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/rpython/rctypes/test/test_import.py	Sat Apr 15 14:44:44 2006
@@ -0,0 +1,18 @@
+"""
+Checks that importing and registering the annotations for rctypes
+doesn't bring in the whole rtyper.
+"""
+
+import py, sys
+
+def test_import():
+    gw = py.execnet.PopenGateway()
+    channel = gw.remote_exec('''
+        import sys
+        sys.path = channel.receive()
+        import pypy.rpython.rctypes.implementation
+        channel.send(sys.modules.keys())
+    ''')
+    channel.send(sys.path)
+    modules = channel.receive()
+    assert 'pypy.rpython.rmodel' not in modules



More information about the Pypy-commit mailing list