[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

Hello Martin,Sorry I don't quite get what you say.
Let me put in some sample code here.I have three
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?

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? 

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 
#!/tools/2.5.1/local/bin/pythonif __name__==
'__main__':     import sys
     import mymath_wrapper     import struct    
     print 'calling mymath_wrapper.c\n'     print
     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)       

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;

#include <Python.h>
#include "mymath.h"

pyfunc_mymath(self, args)
 PyObject *self, *args;
 PyObject *result;
 int res, a, b;
 blockstruct localstruct;
 	return NULL;
 /* initialise a local structure */
 res = mymath(&localstruct,a,b);
 printf("localstruct a:%d b:%d
 /* 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
 result = Py_BuildValue("i",res);
 /* python side will print the last res value*/
 return result;

pyfunc_mystruct(self, args)
 PyObject *self, *args;
 PyObject* string_from_Python;

 	return NULL;
 if(PyString_Size(string_from_Python)!= sizeof(struct
 	PyErr_SetString(PyExc_AssertionError, "Given strgin
not a good size");
 	return NULL;
 globaldiffstruct = (diffstruct
 printf("globaldiffstruct calling within C:%d  %s
 /* 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},
 void initmymath_wrapper()
 	(void) Py_InitModule("mymath_wrapper",

Do You Yahoo!?
Bid and sell for free at http://auctions.yahoo.com