How to get the output struct parameter by extending C function?

J

jammy

Hello,

I an just starting to use the ctypes module.
Now I want to expentd C in python.
In the function of C, I want to get the output struct parameter which
is chaged in the function.
following is my code:

----------- 1.Header file of C$B!J(Binformation.h$B!K(B-----------
typedef struct {
char name[20];
int height;
int weight;
} Infor;
int getInfor( char* name, int height, int weight, Infor* infor);


----------- 2.Source file of C$B!J(Binformation.c$B!K(B-----------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "Extest.h"
int getInfor( char* name, int height, int weight, Infor* infor)
{
if(name == NULL || infor == NULL)
return (-1);

printf(" [%s] get params: name[%s] height[%d] weight[%d]\n",
__FUNCTION__, name, height, weight);
strcpy(infor->name, name);
infor->height = height;
infor->weight = weight;
return (1);
}


----------- 3.information.c's warp file for Python calling$B!J(Binformation_wrap.c$B!K(B-----------

#include "Python.h"
#include "ceval.h"
#include "Extest.h"

static PyObject * Extest_getInfor(PyObject *self, PyObject *args)
{
char* name;
int height;
int weight;
Infor* infor;
int res;

if(!PyArg_ParseTuple(args, "siiO:infor", &name, &height, &weight,
&infor)){
return NULL;
}

printf(" ***** Before Call: infor.name[%s] infor.height[%d]
infor.weight[%d] *****\n",infor->name, infor->height,
infor->weight);//[1]initial value
res = getInfor(name, height, weight, infor);
printf(" ***** After Call: infor.name[%s] infor.height[%d]
infor.weight[%d] *****\n", infor->name, infor->height,
infor->weight);//[2]changed value

return (PyObject*)Py_BuildValue("i", res);
}

static PyMethodDef ExtestMethods[] = {
{ "getInfor", Extest_getInfor, METH_VARARGS },
{ NULL, NULL },
};

void initExtest(void)
{
PyObject *m;
m = Py_InitModule("Extest", ExtestMethods);
}


----------- 4.Makefile-----------
# Makefile of Extend C for Python
OBJS = Extest.o ExtestWrappers.o
TARGET = Extest.so
INCLUDES = -I /usr/local/include/python2.6/
MAKEFILE = Makefile
CC = gcc
CFLAGS = -fPIC -Wall -Wstrict-prototypes -Winline \
${INCLUDES} -O2
all : ${TARGET}
${TARGET} : ${OBJS} ${LIBS}
${CC} -shared -o ${TARGET} ${OBJS}
rm -f ${OBJS}
clean :
rm -f *.o *.Z* *~ ${TARGET}


----------- 5.test file of Python$B!J(Btest.py$B!K(B-----------
#!/usr/bin/env python2.6
#coding=utf8

import Extest
from ctypes import *

class Infor(Structure):
_fields_= [('name', c_char*20),
('height', c_int),
('weight', c_int)]

def main():
infor = Infor()
infor.name = 'NoName'
infor.height = -1
infor.weight = -1

name = 'TestName'
height = 175
weight = 160

print 'Before call getInfor(), Infor.name=%s Infor.height=%d
Infor.weight=%d' % (infor.name, infor.height, infor.weight)
Extest.getInfor(name, height, weight, byref(infor))
print 'After call getInfor(), Infor.name=%s Infor.height=%d
Infor.weight=%d' % (infor.name, infor.height, infor.weight) [3]want to
get value of infor changed
print ' ----- all DONE -----'

if __name__ == '__main__':
main()

----------- 6.result of running test.py -----------
[root@localhost obj_struct_test]# ./test.py
Before call getInfor(), Infor.name=NoName Infor.height=-1
Infor.weight=-1
sizeof(*infor)=28, sizeof(Infor)=28
***** [Extest_getInfor] Before Call: infor.name[]
infor.height[0] infor.weight[0] *****
[getInfor] get params: name[TestName] height[175]
weight[160]
***** [Extest_getInfor] After Call: infor.name[TestName]
infor.height[175] infor.weight[160] *****
After call getInfor(), Infor.name=NoName Infor.height=-1
Infor.weight=-1
----- all DONE -----

Looking for the printed information, in the file of
information_wrap.c(setp 3), In the comment 's tag[2], printed value is
changed valuse.
Why is the printed Information initial value atfer calling getInfor()
in test.py?
Thanks for your help!


yours: jammy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top