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

arigo at codespeak.net arigo at codespeak.net
Mon Apr 10 18:21:42 CEST 2006


Author: arigo
Date: Mon Apr 10 18:21:36 2006
New Revision: 25670

Modified:
   pypy/dist/pypy/rpython/rctypes/rprimitive.py
   pypy/dist/pypy/rpython/rctypes/test/test_ctypes.py
   pypy/dist/pypy/rpython/rctypes/test/test_rprimitive.py
Log:
Some more conversions for ctypes primitives.
Some head-scratching about keepalives in ctypes (not even rctypes!).


Modified: pypy/dist/pypy/rpython/rctypes/rprimitive.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/rprimitive.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/rprimitive.py	Mon Apr 10 18:21:36 2006
@@ -50,11 +50,19 @@
         self.setvalue(hop.llops, v_primitive, v_value)
 
 
-class __extend__(pairtype(IntegerRepr, PrimitiveRepr)):
+class __extend__(pairtype(IntegerRepr, PrimitiveRepr),
+                 pairtype(FloatRepr, PrimitiveRepr),
+                 pairtype(CharRepr, PrimitiveRepr)):
     def convert_from_to((r_from, r_to), v, llops):
+        # first convert 'v' to the precise expected low-level type
+        r_input = r_to.rtyper.primitive_to_repr[r_to.ll_type]
+        v = llops.convertvar(v, r_from, r_input)
+        # allocate a memory-owning box to hold a copy of the ll value 'v'
         r_temp = r_to.r_memoryowner
         v_owned_box = r_temp.allocate_instance(llops)
         r_temp.setvalue(llops, v_owned_box, v)
+        # 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)
 
 

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	Mon Apr 10 18:21:36 2006
@@ -3,6 +3,7 @@
 down to what aliases what and what exact types operations return.
 """
 
+import py
 from ctypes import *
 
 def test_primitive_pointer():
@@ -11,39 +12,46 @@
     x.value = 6
     assert x.value == 6
 
-    p = pointer(x)
+    p = pointer(x)                           #  p ---> x = 6
     assert isinstance(p.contents, c_int)
     p.contents.value += 1
-    assert x.value == 7
+    assert x.value == 7                      #  p ---> x = 7
 
     y = c_int(12)
-    p.contents = y
-    p.contents.value += 2
+    p.contents = y                           #  p ---> y = 12
+    p.contents.value += 2                    #  p ---> y = 14
     assert y.value == 14
     assert x.value == 7
 
-    pp = pointer(p)
-    pp.contents.contents = x
-    p.contents.value += 2
+    pp = pointer(p)                          #  pp ---> p ---> y = 14
+    pp.contents.contents = x                 #  pp ---> p ---> x = 7
+    p.contents.value += 2                    #  pp ---> p ---> x = 9
     assert x.value == 9
 
     assert isinstance(p[0], int)
-    p[0] += 1
+    p[0] += 1                                #  pp ---> p ---> x = 10
     assert x.value == 10
     z = c_int(86)
-    p[0] = z
+    p[0] = z                                 #  pp ---> p ---> x = 86  (not z!)
     assert x.value == 86
     z.value = 84
     assert x.value == 86
 
     assert isinstance(pp[0], POINTER(c_int))
     assert pp[0].contents.value == x.value == 86
-    pp[0].contents = z
+    pp[0].contents = z                       #  pp ---> p ---> z = 84
     assert p.contents.value == z.value == 84
 
+    py.test.skip("the rest fails sometimes -- did I get my keepalive"
+                 "expectations wrong?")
+
     q = pointer(y)
-    pp[0] = q
-    assert pp.contents.contents.value == y.value == 14
+    pp[0] = q                                #  pp ---> p ---> y = 14
+    assert y.value == 14                     #        (^^^ not q! )
+    assert p.contents.value == 14
+    assert pp.contents.contents.value == 14
+    q.contents = x
+    assert pp.contents.contents.value == 14
 
 
 def test_char_p():

Modified: pypy/dist/pypy/rpython/rctypes/test/test_rprimitive.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/test_rprimitive.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/test/test_rprimitive.py	Mon Apr 10 18:21:36 2006
@@ -10,6 +10,7 @@
 from pypy import conftest
 import sys
 from pypy.rpython.test.test_llinterp import interpret
+from pypy.rpython.rarithmetic import r_longlong, r_ulonglong
 
 try:
     import ctypes
@@ -18,7 +19,7 @@
 
 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 ctypes import c_double, c_char_p, pointer
 
 class Test_annotation:
     def test_simple(self):
@@ -198,6 +199,58 @@
         #             the precision to compare.
         assert ("%.2f" % res) == ("%.2f" % 4.3)
 
+    def test_value_for_various_types(self):
+        def func():
+            x = c_ushort(5)
+            x.value += 1
+            assert x.value == 6
+            x = c_char('A')
+            x.value = chr(ord(x.value) + 1)
+            assert x.value == 'B'
+            x = c_longlong(5)
+            x.value += 1
+            assert x.value == r_longlong(6)
+            x = c_ulonglong(5)
+            x.value += 1
+            assert x.value == r_ulonglong(6)
+            x = c_float(2.5)
+            x.value += 0.25
+            assert x.value == 2.75
+            x.value -= 1
+            assert x.value == 1.75
+            x = c_double(2.5)
+            x.value += 0.25
+            assert x.value == 2.75
+            x.value -= 1
+            assert x.value == 1.75
+        interpret(func, [])
+
+    def test_convert_from_llvalue(self):
+        def func():
+            x = c_ushort(5)
+            pointer(x)[0] += 1
+            assert x.value == 6
+            x = c_char('A')
+            pointer(x)[0] = chr(ord(pointer(x)[0]) + 1)
+            assert x.value == 'B'
+            x = c_longlong(5)
+            pointer(x)[0] += 1
+            assert x.value == r_longlong(6)
+            x = c_ulonglong(5)
+            pointer(x)[0] += 1
+            assert x.value == r_ulonglong(6)
+            x = c_float(2.5)
+            pointer(x)[0] += 0.25
+            assert x.value == 2.75
+            pointer(x)[0] -= 1
+            assert x.value == 1.75
+            x = c_double(2.5)
+            pointer(x)[0] += 0.25
+            assert x.value == 2.75
+            pointer(x)[0] -= 1
+            assert x.value == 1.75
+        interpret(func, [])
+
 class Test_compilation:
     def test_compile_c_int(self):
         def create_c_int():



More information about the Pypy-commit mailing list