[Tutor] general question about python integration: repost code included
ET
ed_tsang@yahoo.com
Mon, 20 Sep 1999 09:24:38 -0700 (PDT)
Hello, I noticed that my code attached last time are
not displayed properly. So I included the code once
more. Attached below is the ORIGIAL PROBLEM, FIRST
ANSWER received and then the codes. Thanks
--------------------------------------------------
POSTED:
Hello Martin,Sorry I don't quite get what you say.
Let me put in some sample code here.I have three
files:
1) mymathtest.py as the python script
2) mymath_wrapper.c as the wrapper file to mymath.c3)
mymath.c is my App.
From what I get, you are saying once I have this, then
I can have 1) and 2) in the one process, and 3) as
aseperate process?
How? Can you show me a concrete example, may be by
modifying the code I have as in attachment?
ORIGINAL PROBLEM:
I want to test the App program's ability to do>
interprocess communication. The way to do this is to
have my C driver program in one process and My App
program in another process. But in using Python
extendsion to C. How can I have the driving
programs(python script, C wrapper program) run in one
process while my Apps program in another process? Or
is there something I can do? If you have run in a
similar situation, or you are using pyton to drive
testing your Apps. What do you suggest?
FIRST ANSWER:
You should create a Python module for your C driver
program. Please see the "extending and embedding"
Python manual on how to create a new Python module.
Then, the C driver code, and the Python script will
both run in the same process; communicating to Your
App via interprocess communication. Regards,Martin
________________________________________________
mymathtest.py
#!/tools/2.5.1/local/bin/pythonif __name__==
'__main__': import sys
import mymath_wrapper import struct
print 'calling mymath_wrapper.c\n' print
mymath_wrapper.mymath(1,2)
print '\n'
print 'without recomplilation, calling
mymath_wrapper.c again\n'
print mymath_wrapper.mymath(3,4)
print 'using stucture to packed the init value
before send'
fmt = "l20sf" a = 250 b = 'knight who say
ni' c = 3.14159011841
packed_data = struct.pack(fmt,a,b,c)
# print struct.unpack(fmt, packed_data)
print mymath_wrapper.mystruct(packed_data)
------------------------------------------------------
mymath.h:
typedef struct blockstruct
{
int a;
int b;
int c;
} blockstruct;
typedef struct diffstruct
{
int a;
char b[20];
float c;
} diffstruct;
blockstruct globalstruct;
diffstruct *globaldiffstruct;
int mymath(blockstruct* localstruct, int a, int b)
{
printf("mymath is called\n");
localstruct->a = a;
localstruct->b = b;
localstruct->c = a + b;
return (int)(localstruct->c);
}
int mymath2(int a, int b)
{
printf("mymath2 is called\n");
globalstruct.a = a;
globalstruct.b = b;
globalstruct.c = a + b;
return (int)(globalstruct.c);
}
int mystruct(diffstruct* localstruct)
{
printf("mystruct is called\n");
return 1;
}
------------------------------------------------------
mymath_wrapper.c:
#include <Python.h>
#include "mymath.h"
PyObject*
pyfunc_mymath(self, args)
PyObject *self, *args;
{
PyObject *result;
int res, a, b;
blockstruct localstruct;
if(!PyArg_ParseTuple(args,"ii:mymath",&a,&b))
return NULL;
/* initialise a local structure */
res = mymath(&localstruct,a,b);
printf("localstruct a:%d b:%d
c:%d\n",localstruct.a,localstruct.b,localstruct.c);
/* initialise a global structure, don't need to pass
in address of global struct */
res = mymath2((a+1),(b+1));
printf("globalstruct a:%d b:%d
c:%d\n",globalstruct.a,globalstruct.b,globalstruct.c);
result = Py_BuildValue("i",res);
/* python side will print the last res value*/
return result;
}
PyObject*
pyfunc_mystruct(self, args)
PyObject *self, *args;
{
PyObject* string_from_Python;
if(!PyArg_ParseTuple(args,"S:mymath",&string_from_Python)
)
return NULL;
if(PyString_Size(string_from_Python)!= sizeof(struct
diffstruct))
{
PyErr_SetString(PyExc_AssertionError, "Given strgin
not a good size");
return NULL;
}
globaldiffstruct = (diffstruct
*)PyString_AsString(string_from_Python);
printf("globaldiffstruct calling within C:%d %s
%f\n",globaldiffstruct->a,
globaldiffstruct->b,globaldiffstruct->c);
/* python side will print the last res value*/
return (PyObject *) globaldiffstruct; /* not correct,
deal with this later */
}
static PyMethodDef mymathMethods[] =
{
{"mymath", pyfunc_mymath, 1},
{"mymath2", pyfunc_mymath, 1},
{"mystruct", pyfunc_mystruct, 1},
{NULL,NULL}
};
void initmymath_wrapper()
{
(void) Py_InitModule("mymath_wrapper",
mymathMethods);
}
__________________________________________________
Do You Yahoo!?
Bid and sell for free at http://auctions.yahoo.com