Author: Armin Rigo <[email protected]>
Branch: 
Changeset: r622:959c9878a261
Date: 2012-07-10 15:18 +0200
http://bitbucket.org/cffi/cffi/changeset/959c9878a261/

Log:    Extension to the C language: cast to an array type (reason
        documented).

diff --git a/c/_cffi_backend.c b/c/_cffi_backend.c
--- a/c/_cffi_backend.c
+++ b/c/_cffi_backend.c
@@ -2144,8 +2144,12 @@
     if (!PyArg_ParseTuple(args, "O!O:cast", &CTypeDescr_Type, &ct, &ob))
         return NULL;
 
-    if (ct->ct_flags & (CT_POINTER|CT_FUNCTIONPTR)) {
-        /* cast to a pointer or to a funcptr */
+    if (ct->ct_flags & (CT_POINTER|CT_FUNCTIONPTR|CT_ARRAY) &&
+        ct->ct_size >= 0) {
+        /* cast to a pointer, to a funcptr, or to an array.
+           Note that casting to an array is an extension to the C language,
+           which seems to be necessary in order to sanely get a
+           <cdata 'int[3]'> at some address. */
         unsigned PY_LONG_LONG value;
 
         if (CData_Check(ob)) {
diff --git a/c/test_c.py b/c/test_c.py
--- a/c/test_c.py
+++ b/c/test_c.py
@@ -1548,3 +1548,11 @@
 
 def test_abi():
     assert isinstance(FFI_DEFAULT_ABI, int)
+
+def test_cast_to_array():
+    # not valid in C!  extension to get a non-owning <cdata 'int[3]'>
+    BInt = new_primitive_type("int")
+    BIntP = new_pointer_type(BInt)
+    BArray = new_array_type(BIntP, 3)
+    x = cast(BArray, 0)
+    assert repr(x) == "<cdata 'int[3]' NULL>"
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to