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

arigo at codespeak.net arigo at codespeak.net
Thu Apr 27 10:59:26 CEST 2006


Author: arigo
Date: Thu Apr 27 10:59:24 2006
New Revision: 26422

Modified:
   pypy/dist/pypy/rpython/rctypes/astringbuf.py
   pypy/dist/pypy/rpython/rctypes/test/test_ctypes.py
   pypy/dist/pypy/rpython/rctypes/test/test_rstringbuf.py
Log:
ctypes.sizeof() support in rctypes.


Modified: pypy/dist/pypy/rpython/rctypes/astringbuf.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/astringbuf.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/astringbuf.py	Thu Apr 27 10:59:24 2006
@@ -1,8 +1,8 @@
 from pypy.rpython.extregistry import ExtRegistryEntry
 from pypy.rpython.rctypes.implementation import CTypesObjEntry
-from pypy.annotation.model import SomeCTypesObject, SomeString
+from pypy.annotation.model import SomeCTypesObject, SomeString, SomeInteger
 
-from ctypes import create_string_buffer, c_char
+from ctypes import create_string_buffer, c_char, sizeof
 
 
 class StringBufferType(object):
@@ -46,3 +46,29 @@
         from pypy.rpython.rctypes import rstringbuf
         return rstringbuf.StringBufRepr(rtyper, s_stringbuf,
                                         rstringbuf.STRBUFTYPE)
+
+
+class SizeOfFnEntry(ExtRegistryEntry):
+    "Annotation and rtyping of calls to ctypes.sizeof()"
+    _about_ = sizeof
+
+    def compute_result_annotation(self, s_arg):
+        return SomeInteger(nonneg=True)
+
+    def specialize_call(self, hop):
+        from pypy.rpython.lltypesystem import lltype, llmemory
+        from pypy.rpython.error import TyperError
+        [s_arg] = hop.args_s
+        [r_arg] = hop.args_r
+        if isinstance(s_arg, SomeCTypesObject):
+            if s_arg.knowntype is StringBufferType:
+                # sizeof(string_buffer) == len(string_buffer)
+                return r_arg.rtype_len(hop)
+        else:
+            if not s_arg.is_constant():
+                raise TyperError("ctypes.sizeof(non_ctypes_object)")
+            # XXX check that s_arg.const is really a ctypes type
+            ctype = s_arg.const
+            s_arg = SomeCTypesObject(ctype, SomeCTypesObject.OWNSMEMORY)
+            r_arg = hop.rtyper.getrepr(s_arg)
+        return hop.inputconst(lltype.Signed, llmemory.sizeof(r_arg.ll_type))

Modified: pypy/dist/pypy/rpython/rctypes/test/test_ctypes.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/test_ctypes.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/test/test_ctypes.py	Thu Apr 27 10:59:24 2006
@@ -191,3 +191,9 @@
     pythonapi.PyInt_FromLong.argtypes = [c_long]
     pythonapi.PyInt_FromLong.restype = W_Object
     assert isinstance(pythonapi.PyInt_FromLong(17), W_Object)
+
+def test_sizeof():
+    x = create_string_buffer(117)
+    assert sizeof(x) == 117    # assumes that chars are one byte each
+    x = (c_int * 42)()
+    assert sizeof(x) == 42 * sizeof(c_int)

Modified: pypy/dist/pypy/rpython/rctypes/test/test_rstringbuf.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/test_rstringbuf.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/test/test_rstringbuf.py	Thu Apr 27 10:59:24 2006
@@ -12,7 +12,7 @@
 import sys
 from pypy.rpython.test.test_llinterp import interpret
 
-from ctypes import create_string_buffer
+from ctypes import create_string_buffer, sizeof, c_int
 from pypy.rpython.rctypes.astringbuf import StringBufferType
 
 
@@ -86,7 +86,7 @@
         res = interpret(func, [0])
         assert res == 0
 
-    def test_annotate_value(self):
+    def test_specialize_value(self):
         def func(n):
             buf = create_string_buffer(n)
             buf[0] = 'x'
@@ -95,3 +95,25 @@
 
         res = interpret(func, [12])
         assert ''.join(res.chars) == "xy"
+
+    def test_specialize_sizeof(self):
+        def func(n):
+            buf = create_string_buffer(n)
+            return sizeof(buf)
+        res = interpret(func, [117])
+        assert res == 117
+
+
+class Test_compilation:
+    def test_compile_const_sizeof(self):
+        A = c_int * 42
+        def func():
+            x = c_int()
+            a = A()
+            return sizeof(x), sizeof(a), sizeof(c_int), sizeof(A)
+        fn = compile(func, [])
+        res = fn()
+        assert res[0] == sizeof(c_int)
+        assert res[1] == sizeof(c_int) * 42
+        assert res[2] == sizeof(c_int)
+        assert res[3] == sizeof(c_int) * 42



More information about the Pypy-commit mailing list