<html>
<font face="Courier New, Courier">Hi,<br><br>
I have a problem with pyrex array management. <br>
I define a type <br>
ctypedef char machaine[10]<br>
And I try to pass an array of this type to a C function with no
success.<br>
Below is my C code and pyrex code.<br>
Please help me or I'm gonna be crazy...<br><br>
Thanks<br><br>
Eric Colleu<br><br>
--- apienc.c --- Begin<br><br>
#include <stdio.h><br>
#include "apienc.h"<br><br>
void testDeTableauDeString(machaine *montableau, int nbelt)<br>
{<br>
<x-tab> </x-tab>int i,
j;<br>
<x-tab> </x-tab>printf("testDeTableauDeString
%d\n", nbelt);<br>
<x-tab> </x-tab>for(i=0;i<nbelt;i++)
{<br>
<x-tab> </x-tab><x-tab> </x-tab>printf("%d
", i);<br>
<x-tab> </x-tab><x-tab> </x-tab>for(j=0;j<10;j++)
{<br>
<x-tab> </x-tab><x-tab> </x-tab><x-tab> </x-tab>printf("%2.2x
", montableau[i][j]);<br>
<x-tab> </x-tab><x-tab> </x-tab>}<br>
<x-tab> </x-tab><x-tab> </x-tab>printf("\n--
%s --\n", montableau[i]);<br>
<x-tab> </x-tab>}<br>
}<br>
--- apienc.c --- End<br>
--- apienc.h --- Begin<br><br>
typedef char machaine[10];<br><br>
void testDeTableauDeString(machaine *montableau, int nbelt);<br><br>
--- apienc.h --- End<br>
--- pywrap.pyx --- Begin<br><br>
import array<br>
cdef extern from "stdlib.h":<br>
<x-tab> </x-tab>void*
malloc(size_t)<br>
<x-tab> </x-tab>void
free(void*)<br>
<x-tab> </x-tab><br>
cdef extern from "string.h":<br>
<x-tab> </x-tab>void*
memcpy(void*, void*, size_t)<br>
<x-tab> </x-tab>void*
memset (void*, int, size_t)<br>
<x-tab> </x-tab><br>
cdef extern from "Python.h":<br>
<x-tab> </x-tab>char
*PyString_AsString(object)<br>
<x-tab> </x-tab>int
PyObject_AsReadBuffer(object obj, void **buffer, int *buffer_len)<br>
<x-tab> </x-tab>object
PyString_FromStringAndSize(char *, int)<br><br>
cdef extern from "apienc.h":<br>
<x-tab> </x-tab>ctypedef
char machaine[10]<br>
<x-tab> </x-tab>void
testDeTableauDeString(machaine *montableau, int nbelt)<br><br>
def testapi(stringlist):<br>
<x-tab> </x-tab>cdef
machaine *myarray<br>
<x-tab> </x-tab>cdef
machaine *p[10]<br>
<x-tab> </x-tab>cdef int
buflen<br>
<x-tab> </x-tab>for i, s
in enumerate(stringlist):<br>
<x-tab> </x-tab><x-tab> </x-tab>print
"Value of s : " + s<br>
<x-tab> </x-tab><x-tab> </x-tab>PyObject_AsReadBuffer(s,
<void **>&myarray, &buflen)<br>
<x-tab> </x-tab><x-tab> </x-tab>p[i]
= myarray<br>
<x-tab> </x-tab><x-tab> </x-tab>print
"buflen =", buflen<br>
<x-tab> </x-tab><x-tab> </x-tab>print
"Value of myarray : %s" % PyString_FromStringAndSize(<char
*>myarray, buflen)<br>
<x-tab> </x-tab><x-tab> </x-tab>print
"Value of p : %s" % PyString_FromStringAndSize(<char
*>p[i], buflen)<br>
<x-tab> </x-tab>testDeTableauDeString(p[0],
len(stringlist))<br><br>
--- pywrap.pyx --- End<br>
--- test.py --- Begin<br>
import pywrap<br>
pywrap.testapi(["polop", "pilip"])<br><br>
--- test.py --- End<br>
--- RESULT ---<br>
Value of s : polop<br>
buflen = 5<br>
Value of myarray : polop<br>
Value of p : polop<br>
Value of s : pilip<br>
buflen = 5<br>
Value of myarray : pilip<br>
Value of p : pilip<br>
testDeTableauDeString 2<br>
0 70 6f 6c 6f 70 00 00 00 00 00<br>
-- polop --<br>
1 00 00 03 00 00 00 38 ffffff84 1a 1e<br>
-- --<br>
--------------<br>
</font></html>