Re: pythton 调用 so 共享库不能转换参数类型

windwiny windwiny.ubt at gmail.com
Tue Jun 3 14:07:47 CEST 2008


On Jun 3, 3:19 pm, "Mark Tolonen" <M8R-yft... at mailinator.com> wrote:
> "windwiny" <windwiny.... at gmail.com> wrote in message
>
> news:c2600a0a-6ff4-4e3d-bfa6-2edf7869c60d at a9g2000prl.googlegroups.com...
>
>
>
> > 我初学python 的 ctypes, 现在的环境是 ubuntu 8.04, Python 2.5.2
> > (r252:60911,
> > May  7 2008, 15:19:09) , gcc (GCC) 4.2.3 (Ubuntu 4.2.3-2ubuntu7)
> > 我创建了一个 so 共享库
> > ---------------------
> > #include  <math.h>
> > double myfd(double x)  { return x * 2;}
> > float myff(float x)  { return x * 3;}
> > int myfi(int x)  { return x * 4 ;}
> > ---------------------
> > 用 gcc -shared a.c  -o liba.so  来生成了一个 liba.so ,
> > 用其它 c 程序链接这个库,里面的函数是能正确运行的
> > 我写了一个python 程序
> > ---------------------
> > # coding:utf-8
> > import ctypes as dll
> > p = dll.cdll.LoadLibrary("./liba.so")
>
> p.myfd.argtypes = [dll.c_double]
> p.myfd.restype = dll.c_double
> p.myff.argtypes = [dll.c_float]
> p.myff.restype = dll.c_float
>
> > print p.myfi(8)   # 得32,正确
> > try:
> >   print p.myfd(0.3) # 出错
> > except:
> >   print "except myfd()"
> > try:
> >   print p.myff(0.4) # 出错
> > except:
> >   print "except myff()"
> > ---------------------
> > 出错提示为 ctypes.ArgumentError: argument 1: <type
> > 'exceptions.TypeError'>:
> > Don't know how to convert parameter 1
>
> > 这是什么原因?
>
> --Mark (马克)

噢,原来还要设置参数和返回值类型。 谢了



More information about the Python-list mailing list