[pypy-svn] r25887 - in pypy/dist/pypy/rpython/rctypes: . test
arigo at codespeak.net
arigo at codespeak.net
Mon Apr 17 14:15:50 CEST 2006
Author: arigo
Date: Mon Apr 17 14:15:49 2006
New Revision: 25887
Modified:
pypy/dist/pypy/rpython/rctypes/rarray.py
pypy/dist/pypy/rpython/rctypes/test/test_ctypes.py
pypy/dist/pypy/rpython/rctypes/test/test_rarray.py
Log:
Automatic cast from array to pointer.
Modified: pypy/dist/pypy/rpython/rctypes/rarray.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/rarray.py (original)
+++ pypy/dist/pypy/rpython/rctypes/rarray.py Mon Apr 17 14:15:49 2006
@@ -5,8 +5,9 @@
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.rmodel import genreccopy, reccopy, C_ZERO
from pypy.rpython.rctypes.rprimitive import PrimitiveRepr
+from pypy.rpython.rctypes.rpointer import PointerRepr
from pypy.annotation.model import SomeCTypesObject
ArrayType = type(ARRAY(c_int, 10))
@@ -100,6 +101,16 @@
r_array.set_item_value(hop.llops, v_array, v_index, v_newvalue)
+class __extend__(pairtype(ArrayRepr, PointerRepr)):
+ def convert_from_to((r_from, r_to), v, llops):
+ # XXX keepalives
+ r_temp = r_to.r_memoryowner
+ v_owned_box = r_temp.allocate_instance(llops)
+ v_c_array = r_from.get_c_data_of_item(llops, v, C_ZERO)
+ r_temp.setvalue(llops, v_owned_box, v_c_array)
+ return llops.convertvar(v_owned_box, r_temp, r_to)
+
+
def ll_chararrayvalue(box):
from pypy.rpython.rctypes import rchar_p
p = box.c_data
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 17 14:15:49 2006
@@ -121,3 +121,14 @@
assert b.value == '!'
assert len(create_string_buffer(0)) == 0
+
+def test_array():
+ a = (c_int * 10)()
+
+ class S(Structure):
+ _fields_ = [('p', POINTER(c_int))]
+
+ s = S()
+ s.p = a
+ s.p.contents.value = 42
+ assert a[0] == 42
Modified: pypy/dist/pypy/rpython/rctypes/test/test_rarray.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/test_rarray.py (original)
+++ pypy/dist/pypy/rpython/rctypes/test/test_rarray.py Mon Apr 17 14:15:49 2006
@@ -17,7 +17,8 @@
except ImportError:
py.test.skip("this test needs ctypes installed")
-from ctypes import c_int, c_short, ARRAY, POINTER, pointer, c_char_p, c_char
+from ctypes import c_int, c_short, c_char_p, c_char, pointer
+from ctypes import ARRAY, POINTER, Structure
c_int_10 = ARRAY(c_int,10)
@@ -219,6 +220,19 @@
res = interpret(func, [])
assert ''.join(res.chars) == "xy"
+ def test_automatic_cast_array_to_pointer(self):
+ A = c_int * 10
+ class S(Structure):
+ _fields_ = [('p', POINTER(c_int))]
+ def func():
+ a = A()
+ s = S()
+ s.p = a
+ s.p.contents.value = 42
+ return a[0]
+ res = interpret(func, [])
+ assert res == 42
+
class Test_compilation:
def test_compile_array_access(self):
def access_array():
More information about the Pypy-commit
mailing list