469,578 Members | 1,869 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,578 developers. It's quick & easy.

RE: swig or ctypes , under the gun and need help

Diez wrote...
I don't know swig, but if all you have is a real C-API, try &
use ctypes.
It's much easier to create bindings for, keeps you fully in
the warm and
cozy womb of python programming and doesn't need no
compilation to create
the actual binding.
You're right the ctypes does seem more pythonesque; however I'm still stuck trying return all these parameters
that the c api uses. my ctypes code is below. It just quits running when I try to print
one of the args I did a pass byref on, no error out, nothing. admittedly I'm a newbie to ctypes and not much of a c programmer
but I could sure use some help. my ctypes test code follows...

from ctypes import *

'''
create shared object file like so.
gcc -shared -o rug520.so rug520.c

the c api I want to call is like this.
int RugCalc( char * sMdsRecord,
char * sRehabType,
char * sModel,
int iQuarterlyFlag,
double nCmiArray[],
char * sRugHier,
char * sRugMax,
int * iRugHier,
int * iRugMax,
double * nCmiValueHier,
double * nCmiValueMax,
int * iAdlSum,
int * iCpsCode,
char * sRugsVersion,
char * sDllVersion,
int * iError );
'''
libc = CDLL("rug520.so")

CmiArrayDef = c_double * 59

ZeroCmi = CmiArrayDef( ) #this is a table used internally, but 0.0 should work until I figure out the rest.

def getrug(mds):
#print mds
sMdsRecord = c_char_p()
sRehabType = c_char_p()
sModel = c_char_p()
iQuarterlyFlag = c_int()
sRugHier = c_char_p()
sRugMax = c_char_p()
iRugHier = c_int()
iRugMax = c_int()
nCmiValueHier = c_double()
nCmiValueMax = c_double()
iAdlSum = c_int()
iCpsCode = c_int()
sRugsVersion = c_char_p()
sDllVersion = c_char_p()
iError = c_int()
sMdsRecord.value = mds
sRehabType = 'mcare'
sModel = '34'

results = libc.RugCalc(sMdsRecord, sRehabType, sModel, iQuarterlyFlag,
ZeroCmi,
byref(sRugHier),
byref(sRugMax),
byref(iRugHier),
byref(iRugMax),
byref(nCmiValueHier),
byref(nCmiValueMax),
byref(iAdlSum),
byref(iCpsCode),
byref(sRugsVersion),
byref(sDllVersion),
byref(iError ))
print 'results', results
print iQuarterlyFlag.value
print 'sRugMax', sRugMax #this print causes an exit, tried .value with same results
print 'return' #I never see this print.

datafile = open('mdsdata.txt')
for d in datafile:
if d[0]=='B':
getrug(d)
break


Aug 25 '08 #1
2 1190
Sells, Fred schrieb:
Diez wrote...
>I don't know swig, but if all you have is a real C-API, try &
use ctypes.
It's much easier to create bindings for, keeps you fully in
the warm and
cozy womb of python programming and doesn't need no
compilation to create
the actual binding.
You're right the ctypes does seem more pythonesque; however I'm still stuck trying return all these parameters
that the c api uses. my ctypes code is below. It just quits running when I try to print
one of the args I did a pass byref on, no error out, nothing. admittedly I'm a newbie to ctypes and not much of a c programmer
but I could sure use some help. my ctypes test code follows...

from ctypes import *

'''
create shared object file like so.
gcc -shared -o rug520.so rug520.c
You need to create a proper library. The above doesn't do that - it
creates as simple object file.

I don't know how to really do that without using autoconf/automake.

Diez
Aug 25 '08 #2
Diez B. Roggisch wrote:
Sells, Fred schrieb:
>Diez wrote...
>>I don't know swig, but if all you have is a real C-API, try & use
ctypes.
It's much easier to create bindings for, keeps you fully in the warm and
cozy womb of python programming and doesn't need no compilation to
create
the actual binding.
You're right the ctypes does seem more pythonesque; however I'm still
stuck trying return all these parameters
that the c api uses. my ctypes code is below. It just quits running
when I try to print one of the args I did a pass byref on, no error
out, nothing. admittedly I'm a newbie to ctypes and not much of a c
programmer
but I could sure use some help. my ctypes test code follows...

from ctypes import *

'''
create shared object file like so.
gcc -shared -o rug520.so rug520.c

You need to create a proper library. The above doesn't do that - it
creates as simple object file.

I don't know how to really do that without using autoconf/automake.

Diez
--
http://mail.python.org/mailman/listinfo/python-list
==
see info gcc
search shared (3.13 Options for Linking)

mkso.scr:
#!/bin/bash
# create a lib.so from param 1 filename
#
#vi $1
f=`basename $1 .c`
gcc -c -fPIC $f
ld -shared -fPIC $f.o -o $f.so
# end of file
I suppose $1 could be `cat *.c`
Haven't tried that, but.... Who knows?
(I write my libs to be libs.)

Steve
no******@hughes.net
Aug 26 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

19 posts views Thread by Thomas Heller | last post: by
1 post views Thread by Frank | last post: by
9 posts views Thread by Daniel Watrous | last post: by
reply views Thread by gianluca | last post: by
2 posts views Thread by Jean-Paul Calderone | last post: by
3 posts views Thread by Anish Chapagain | last post: by
1 post views Thread by Sells, Fred | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.