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

arigo at codespeak.net arigo at codespeak.net
Mon Apr 17 12:59:48 CEST 2006


Author: arigo
Date: Mon Apr 17 12:59:47 2006
New Revision: 25879

Added:
   pypy/dist/pypy/rpython/rctypes/astringbuf.py   (contents, props changed)
   pypy/dist/pypy/rpython/rctypes/test/test_rstringbuf.py   (contents, props changed)
Modified:
   pypy/dist/pypy/rpython/rctypes/implementation.py
Log:
create_string_buffer() needs special support to be able to
return arrays of characters of variable size.  Basic
annotation done.


Added: pypy/dist/pypy/rpython/rctypes/astringbuf.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/rpython/rctypes/astringbuf.py	Mon Apr 17 12:59:47 2006
@@ -0,0 +1,24 @@
+from pypy.rpython import extregistry
+from pypy.annotation import model as annmodel
+
+from ctypes import create_string_buffer, c_char
+
+
+class StringBufferType(object):
+    """Placeholder for the result type of create_string_buffer(),
+    which cannot be represented as a regular ctypes type because
+    the length is not an annotation-time constant.
+    """
+    _type_ = c_char
+    #_length_ = unspecified
+
+
+def stringbuf_compute_result_annotation(s_length):
+    if s_length.knowntype != int:
+        raise Exception("rctypes only supports create_string_buffer(length)")
+    return annmodel.SomeCTypesObject(StringBufferType,
+            annmodel.SomeCTypesObject.OWNSMEMORY)
+
+extregistry.register_value(create_string_buffer,
+    compute_result_annotation=stringbuf_compute_result_annotation,
+    )

Modified: pypy/dist/pypy/rpython/rctypes/implementation.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/implementation.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/implementation.py	Mon Apr 17 12:59:47 2006
@@ -6,6 +6,7 @@
 import pypy.rpython.rctypes.achar_p
 import pypy.rpython.rctypes.astruct
 import pypy.rpython.rctypes.avoid_p
+import pypy.rpython.rctypes.astringbuf
 
 
 # Register the correspondance between SomeCTypesObject and the get_repr()

Added: pypy/dist/pypy/rpython/rctypes/test/test_rstringbuf.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/rpython/rctypes/test/test_rstringbuf.py	Mon Apr 17 12:59:47 2006
@@ -0,0 +1,48 @@
+"""
+Test the rctypes implementation.
+"""
+
+import py.test
+import pypy.rpython.rctypes.implementation
+from pypy.annotation import model as annmodel
+from pypy.annotation.annrpython import RPythonAnnotator
+from pypy.translator.translator import TranslationContext
+from pypy import conftest
+from pypy.translator.c.test.test_genc import compile
+import sys
+from pypy.rpython.test.test_llinterp import interpret
+
+try:
+    import ctypes
+except ImportError:
+    py.test.skip("this test needs ctypes installed")
+
+from ctypes import create_string_buffer
+from pypy.rpython.rctypes.astringbuf import StringBufferType
+
+
+class Test_annotation:
+    def test_annotate_create(self):
+        def func(n):
+            return create_string_buffer(n)
+
+        a = RPythonAnnotator()
+        s = a.build_types(func, [int])
+        assert s.knowntype == StringBufferType
+
+        if conftest.option.view:
+            a.translator.view()
+
+    def test_annotate_access(self):
+        def func(n):
+            buf = create_string_buffer(n)
+            buf[0] = 'x'
+            buf[1] = 'y'
+            return buf[0]
+
+        a = RPythonAnnotator()
+        s = a.build_types(func, [int])
+        assert s == annmodel.SomeChar()
+
+        if conftest.option.view:
+            a.translator.view()



More information about the Pypy-commit mailing list