[pypy-svn] r25195 - in pypy/dist/pypy/rpython/rctypes: . test
goden at codespeak.net
goden at codespeak.net
Fri Mar 31 18:23:19 CEST 2006
Author: goden
Date: Fri Mar 31 18:23:15 2006
New Revision: 25195
Added:
pypy/dist/pypy/rpython/rctypes/rpointer.py (contents, props changed)
pypy/dist/pypy/rpython/rctypes/test/test_rpointer.py (contents, props changed)
Modified:
pypy/dist/pypy/rpython/rctypes/implementation.py
pypy/dist/pypy/rpython/rctypes/test/test_rctypes.py
Log:
initial annotation of rctypes pointers using the extregistry
Modified: pypy/dist/pypy/rpython/rctypes/implementation.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/implementation.py (original)
+++ pypy/dist/pypy/rpython/rctypes/implementation.py Fri Mar 31 18:23:15 2006
@@ -23,6 +23,7 @@
# Importing for side effect of registering types with extregistry
import pypy.rpython.rctypes.rarray
import pypy.rpython.rctypes.rprimitive
+import pypy.rpython.rctypes.rpointer
# ctypes_annotation_list contains various attributes that
# are used by the pypy annotation.
@@ -41,10 +42,10 @@
# (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,
- staticmethod(lambda ll_type, arg_name:"RPyString_AsString(%s)" % arg_name)),
+# (c_char_p, None,
+# staticmethod(lambda ll_type, arg_name:"RPyString_AsString(%s)" % arg_name)),
+# (POINTER(c_char), None,
+# staticmethod(lambda ll_type, arg_name:"RPyString_AsString(%s)" % arg_name)),
]
def create_ctypes_annotations():
Added: pypy/dist/pypy/rpython/rctypes/rpointer.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/rpython/rctypes/rpointer.py Fri Mar 31 18:23:15 2006
@@ -0,0 +1,87 @@
+from pypy.rpython.rmodel import Repr
+from pypy.rpython import extregistry
+from pypy.annotation import model as annmodel
+
+from ctypes import POINTER, c_int
+
+class PointerRepr(Repr):
+ """XXX: todo
+ """
+ def __init__(self, rtyper, ctype, ref_type):
+ pass
+
+def get_repr(rtyper, s_pointer):
+ """XXX: todo
+ """
+ raise RuntimeError("foo")
+
+def registerPointerType(ptrtype):
+ """Adds a new pointer type to the extregistry.
+
+ Since pointers can be created to primitive ctypes objects, arrays,
+ structs and structs are not predefined each new pointer type is
+ registered in the extregistry as it is identified.
+
+ The new pointers that are created have a "contents" attribute
+ which, when retrieved, in effect dereferences the pointer and
+ returns the referenced value.
+ """
+ def compute_result_annotation(s_arg):
+ return annmodel.SomeCTypesObject(ptrtype,
+ annmodel.SomeCTypesObject.OWNSMEMORY)
+
+ def specialize_call(hop):
+ raise RuntimeError('foo')
+
+ type_entry = extregistry.register_type(ptrtype,
+ specialize_call=specialize_call,
+ get_repr=get_repr)
+ contentsType = annmodel.SomeCTypesObject(ptrtype._type_,
+ annmodel.SomeCTypesObject.MEMORYALIAS)
+ type_entry.fields_s = {'contents': contentsType}
+
+ return extregistry.register_value(ptrtype,
+ compute_result_annotation=compute_result_annotation)
+
+def POINTER_compute_annotation(metatype, the_type):
+ """compute the annotation of POINTER() calls to create a ctypes
+ pointer for the given type
+ """
+
+ def POINTER_compute_result_annotation(s_arg):
+ """Called to compute the result annotation of
+ POINTER(<ctypes type>). This happens to return a new
+ class which itself is treated as SomeBuiltin because when
+ called it creates a new pointer.
+
+ NOTE: To handle a myriad of possible pointer types, each
+ ctypes type that is passed to POINTER() calls is itself
+ registered if it isn't already.
+ """
+ ptrtype = POINTER(s_arg.const)
+
+ if not extregistry.is_registered_type(ptrtype):
+ entry = registerPointerType(ptrtype)
+ else:
+ entry = extregistry.lookup(ptrtype)
+
+ return annmodel.SomeBuiltin(entry.compute_result_annotation,
+ methodname=ptrtype.__name__)
+
+ # annotation of POINTER (not the call) is SomeBuitin which provides
+ # a way of computing the result annotation of POINTER(<ctypes type>)
+ return annmodel.SomeBuiltin(POINTER_compute_result_annotation,
+ methodname=the_type.__name__)
+
+# handles POINTER() calls
+value_entry = extregistry.register_value(POINTER,
+ compute_annotation=POINTER_compute_annotation)
+
+def POINTER_specialize_call(hop):
+ raise RuntimeError("foo")
+
+extregistry.register_type(POINTER, specialize_call=POINTER_specialize_call)
+
+#type_entry = extregistry.register_type(ptrmeta,
+# compute_annotation=compute_annotation,
+# get_repr=get_repr)
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 Fri Mar 31 18:23:15 2006
@@ -266,7 +266,7 @@
t.buildrtyper().specialize()
#d#t.view()
- def test_compile_simple(self):
+ def x_test_compile_simple(self):
fn = compile(o_atoi, [str])
res = fn("42")
assert res == 42
Added: pypy/dist/pypy/rpython/rctypes/test/test_rpointer.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/rpython/rctypes/test/test_rpointer.py Fri Mar 31 18:23:15 2006
@@ -0,0 +1,63 @@
+"""
+Test the rctypes pointer implementation
+"""
+
+import py.test
+from pypy.translator.translator import TranslationContext
+from pypy import conftest
+from pypy.rpython.test.test_llinterp import interpret
+
+from ctypes import c_int, c_float, POINTER
+
+import pypy.rpython.rctypes.implementation
+
+class Test_annotation:
+ def test_simple(self):
+ res = c_int(42)
+ ptrres = POINTER(c_int)(res)
+ assert res.value == ptrres.contents.value
+
+ def test_annotate_c_int_ptr(self):
+ def func():
+ res = c_int(42)
+ ptrtype = POINTER(c_int)
+ ptrres = ptrtype(res)
+ return ptrres.contents.value
+
+ t = TranslationContext()
+ a = t.buildannotator()
+ s = a.build_types(func, [])
+
+ if conftest.option.view:
+ t.view()
+
+ assert s.knowntype == int
+
+ def test_annotate_c_float_ptr(self):
+ def func():
+ res = c_float(4.2)
+ ptrtype = POINTER(c_float)
+ ptrres = ptrtype(res)
+ return ptrres.contents.value
+
+ t = TranslationContext()
+ a = t.buildannotator()
+ s = a.build_types(func, [])
+
+ if conftest.option.view:
+ t.view()
+
+ assert s.knowntype == float
+
+class Test_specialization:
+ def x_test_specialize_c_int_ptr(self):
+ def func():
+ res = c_int(42)
+ ptrtype = POINTER(c_int)
+ ptrres = ptrtype(res)
+
+ return ptrres.contents.value
+
+ res = interpret(func, [])
+
+ assert res == 42
More information about the Pypy-commit
mailing list