[Pyrex] using C char array problem

Eric Colleu ecolleu at besancon.parkeon.com
Tue May 23 09:44:57 UTC 2006


Hi,

I have a problem with pyrex array management.
I define a type
ctypedef char machaine[10]
And I try to pass an array of this type to a C function with no success.
Below is my C code and pyrex code.
Please help me or I'm gonna be crazy...

Thanks

Eric Colleu

--- apienc.c --- Begin

#include <stdio.h>
#include "apienc.h"

void testDeTableauDeString(machaine *montableau, int nbelt)
{
         int i, j;
         printf("testDeTableauDeString %d\n", nbelt);
         for(i=0;i<nbelt;i++) {
                 printf("%d ", i);
                 for(j=0;j<10;j++) {
                         printf("%2.2x ", montableau[i][j]);
                 }
                 printf("\n-- %s --\n", montableau[i]);
         }
}
--- apienc.c --- End
--- apienc.h --- Begin

typedef char machaine[10];

void testDeTableauDeString(machaine *montableau, int nbelt);

--- apienc.h --- End
--- pywrap.pyx --- Begin

import array
cdef extern from "stdlib.h":
         void* malloc(size_t)
         void free(void*)

cdef extern from "string.h":
         void* memcpy(void*, void*, size_t)
         void* memset (void*, int, size_t)

cdef extern from "Python.h":
         char *PyString_AsString(object)
         int PyObject_AsReadBuffer(object obj, void **buffer, int *buffer_len)
         object PyString_FromStringAndSize(char *, int)

cdef extern from "apienc.h":
         ctypedef char machaine[10]
         void testDeTableauDeString(machaine *montableau, int nbelt)

def testapi(stringlist):
         cdef machaine *myarray
         cdef machaine *p[10]
         cdef int buflen
         for i, s in enumerate(stringlist):
                 print "Value of s : " +  s
                 PyObject_AsReadBuffer(s, <void **>&myarray, &buflen)
                 p[i] = myarray
                 print "buflen =", buflen
                 print "Value of myarray : %s" % 
PyString_FromStringAndSize(<char *>myarray, buflen)
                 print "Value of p : %s" % PyString_FromStringAndSize(<char 
*>p[i], buflen)
         testDeTableauDeString(p[0], len(stringlist))

--- pywrap.pyx --- End
--- test.py --- Begin
import pywrap
pywrap.testapi(["polop", "pilip"])

--- test.py --- End
--- RESULT ---
Value of s : polop
buflen = 5
Value of myarray : polop
Value of p : polop
Value of s : pilip
buflen = 5
Value of myarray : pilip
Value of p : pilip
testDeTableauDeString 2
0 70 6f 6c 6f 70 00 00 00 00 00
-- polop --
1 00 00 03 00 00 00 38 ffffff84 1a 1e
--  --
--------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.copyleft.no/pipermail/pyrex/attachments/20060523/3d8845d3/attachment-0001.html


More information about the Pyrex mailing list