[Tutor] Re: [Python-Help] general question about python integration with C

ET ed_tsang@yahoo.com
Sun, 19 Sep 1999 18:36:58 -0700 (PDT)


--0-424238335-937791418=:11258
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

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.c
3) 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 a
seperate process?  
How? Can you show me a concrete example, may be by
modifying the code I have as in attachment?

--- Martin von Loewis <loewis@informatik.hu-berlin.de>
wrote:
> > 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
> 

__________________________________________________
Do You Yahoo!?
Bid and sell for free at http://auctions.yahoo.com
--0-424238335-937791418=:11258
Content-Type: text/plain; name="mymathtest.py"
Content-Description: mymathtest.py
Content-Disposition: inline; filename="mymathtest.py"

#!/tools/2.5.1/local/bin/python


if __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)
     
     
     

--0-424238335-937791418=:11258
Content-Type: application/x-unknown-content-type-h_auto_file; name="mymath.h"
Content-Transfer-Encoding: base64
Content-Description: mymath.h
Content-Disposition: attachment; filename="mymath.h"

dHlwZWRlZiBzdHJ1Y3QgYmxvY2tzdHJ1Y3QNCnsNCglpbnQgYTsgDQoJaW50
IGI7DQoJaW50IGM7DQp9IGJsb2Nrc3RydWN0Ow0KDQp0eXBlZGVmIHN0cnVj
dCBkaWZmc3RydWN0DQp7DQoJaW50IGE7DQoJY2hhciBiWzIwXTsNCglmbG9h
dCBjOw0KDQp9IGRpZmZzdHJ1Y3Q7DQoNCg0KYmxvY2tzdHJ1Y3QgZ2xvYmFs
c3RydWN0Ow0KDQpkaWZmc3RydWN0ICpnbG9iYWxkaWZmc3RydWN0Ow0KDQpp
bnQgbXltYXRoKGJsb2Nrc3RydWN0KiBsb2NhbHN0cnVjdCwgaW50IGEsIGlu
dCBiKQ0Kew0KCXByaW50ZigibXltYXRoIGlzIGNhbGxlZFxuIik7DQoJbG9j
YWxzdHJ1Y3QtPmEgPSBhOw0KCWxvY2Fsc3RydWN0LT5iID0gYjsNCglsb2Nh
bHN0cnVjdC0+YyA9IGEgKyBiOwkNCglyZXR1cm4gKGludCkobG9jYWxzdHJ1
Y3QtPmMpOw0KfQ0KDQppbnQgbXltYXRoMihpbnQgYSwgaW50IGIpDQp7DQoJ
cHJpbnRmKCJteW1hdGgyIGlzIGNhbGxlZFxuIik7DQoJZ2xvYmFsc3RydWN0
LmEgPSBhOw0KCWdsb2JhbHN0cnVjdC5iID0gYjsNCglnbG9iYWxzdHJ1Y3Qu
YyA9IGEgKyBiOwkNCglyZXR1cm4gKGludCkoZ2xvYmFsc3RydWN0LmMpOw0K
fQ0KDQppbnQgbXlzdHJ1Y3QoZGlmZnN0cnVjdCogbG9jYWxzdHJ1Y3QpDQp7
DQoJcHJpbnRmKCJteXN0cnVjdCBpcyBjYWxsZWRcbiIpOw0KCQkNCglyZXR1
cm4gMTsNCn0NCg==

--0-424238335-937791418=:11258
Content-Type: application/x-unknown-content-type-c_auto_file; name="mymath_wrapper.c"
Content-Transfer-Encoding: base64
Content-Description: mymath_wrapper.c
Content-Disposition: attachment; filename="mymath_wrapper.c"

I2luY2x1ZGUgPFB5dGhvbi5oPg0KI2luY2x1ZGUgIm15bWF0aC5oIg0KDQpQ
eU9iamVjdCoNCnB5ZnVuY19teW1hdGgoc2VsZiwgYXJncykNCiBQeU9iamVj
dCAqc2VsZiwgKmFyZ3M7DQogew0KIFB5T2JqZWN0ICpyZXN1bHQ7DQogaW50
IHJlcywgYSwgYjsNCiBibG9ja3N0cnVjdCBsb2NhbHN0cnVjdDsNCiANCiBp
ZighUHlBcmdfUGFyc2VUdXBsZShhcmdzLCJpaTpteW1hdGgiLCZhLCZiKSkN
CiAJcmV0dXJuIE5VTEw7DQogDQogLyogaW5pdGlhbGlzZSBhIGxvY2FsIHN0
cnVjdHVyZSAqLw0KIHJlcyA9IG15bWF0aCgmbG9jYWxzdHJ1Y3QsYSxiKTsN
CiBwcmludGYoImxvY2Fsc3RydWN0IGE6JWQgYjolZCBjOiVkXG4iLGxvY2Fs
c3RydWN0LmEsbG9jYWxzdHJ1Y3QuYixsb2NhbHN0cnVjdC5jKTsNCiAvKiBp
bml0aWFsaXNlIGEgZ2xvYmFsIHN0cnVjdHVyZSwgZG9uJ3QgbmVlZCB0byBw
YXNzIGluIGFkZHJlc3Mgb2YgZ2xvYmFsIHN0cnVjdCAqLw0KIHJlcyA9IG15
bWF0aDIoKGErMSksKGIrMSkpOw0KIHByaW50ZigiZ2xvYmFsc3RydWN0IGE6
JWQgYjolZCBjOiVkXG4iLGdsb2JhbHN0cnVjdC5hLGdsb2JhbHN0cnVjdC5i
LGdsb2JhbHN0cnVjdC5jKTsNCiByZXN1bHQgPSBQeV9CdWlsZFZhbHVlKCJp
IixyZXMpOw0KIA0KIC8qIHB5dGhvbiBzaWRlIHdpbGwgcHJpbnQgdGhlIGxh
c3QgcmVzIHZhbHVlKi8NCiByZXR1cm4gc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