Python ctypes:获取变量类型和值的问题

4 投票
2 回答
3204 浏览
提问于 2025-04-16 17:34

我想在一些Python代码中使用一个C语言的库,方法是用ctypes来调用它。我的问题是,我对C语言不太了解。我已经尝试了几天阅读相关代码和C语言的结构,但在一个问题上卡住了。同时,我对Python也比较陌生,因为我现在正在从Matlab转过来,所以我对Python的了解主要限于科学计算方面。这个问题非常具体,但我觉得解决方案对一般的C库和Python的连接都有帮助。

我正在使用一个叫做Iphreeqc的库,这是一个地球化学模型,来自`http://wwwbrr.cr.usgs.gov/projects/GWC_coupled/phreeqc/index.html´(iphreeqc-2.18.0-5314.tar.gz,已经在OS X 10.6上从源代码编译,链接由于SO的防垃圾邮件措施而无法使用)。我想从C库的评估中提取值到Python中。

我怀疑我在Python代码中的结构和联合部分出错了,但我似乎无法弄明白问题出在哪里。

这是我的Python代码:

import ctypes

iphreeqc = ctypes.CDLL("libiphreeqc.0.dylib", ctypes.RTLD_GLOBAL)

# C structures from var.h 
class VAR_TYPE(ctypes.Structure):
    _fields_ = [
        ("TT_EMPTY",ctypes.c_int),
        ("TT_ERROR",ctypes.c_int),
        ("TT_LONG",ctypes.c_int),
        ("TT_DOUBLE",ctypes.c_int),
        ("TT_STRING",ctypes.c_int)]


(TT_EMPTY,
TT_ERROR,
TT_LONG,
TT_DOUBLE,
TT_STRING)=map(ctypes.c_int, xrange(5))


class VRESULT(ctypes.Structure):
    _fields_ = [
        ("VR_OK",ctypes.c_int),
        ("VR_OUTOFMEMORY",ctypes.c_int),
        ("VR_BADVARTYPE",ctypes.c_int),
        ("VR_INVALIDARG",ctypes.c_int),
        ("VR_INVALIDROW",ctypes.c_int),
        ("VR_INVALIDCOL",ctypes.c_int)]

(VR_OK,
 VR_OUTOFMEMORY,
 VR_BADVARTYPE,
 VR_INVALIDARG,
 VR_INVALIDROW,
 VR_INVALIDCOL)=map(ctypes.c_int, xrange(0,-6,-1))

class _U(ctypes.Union):
    _fields_ = [("lVal", ctypes.c_long),
                ("dVal", ctypes.c_double),
                ("sVal", ctypes.c_char),
                ("vresult", VRESULT)]

class VAR(ctypes.Structure):
   _anonymous_ = ("pvar",)
   _fields_ = [
        ("pvar", _U),
        ("type", VAR_TYPE)]

# Run model        
Id=iphreeqc.CreateIPhreeqc()
dbloade = iphreeqc.LoadDatabase(Id,"phreeqc.dat")
estring=iphreeqc.OutputErrorString(Id)

# Model input
iphreeqc.AccumulateLine(Id,"TITLE Example 2.--Temperature dependence of solubility")
iphreeqc.AccumulateLine(Id,"                  of gypsum and anhydrite             ")
iphreeqc.AccumulateLine(Id,"SOLUTION 1 Pure water                                 ")
iphreeqc.AccumulateLine(Id,"        pH      7.0                                   ")
iphreeqc.AccumulateLine(Id,"        temp    25.0                                  ")
iphreeqc.AccumulateLine(Id,"EQUILIBRIUM_PHASES 1                                  ")
iphreeqc.AccumulateLine(Id,"        Gypsum          0.0     1.0                   ")
iphreeqc.AccumulateLine(Id,"        Anhydrite       0.0     1.0                   ")
iphreeqc.AccumulateLine(Id,"REACTION_TEMPERATURE 1                                ")
iphreeqc.AccumulateLine(Id,"        25.0 75.0 in 50 steps                         ")
iphreeqc.AccumulateLine(Id,"SELECTED_OUTPUT                                       ")
iphreeqc.AccumulateLine(Id,"        -file   ex2.sel                               ")
iphreeqc.AccumulateLine(Id,"        -user_punch true                                  ")
iphreeqc.AccumulateLine(Id,"        -reset false                                  ")
iphreeqc.AccumulateLine(Id,"        -simulation false                                  ")
iphreeqc.AccumulateLine(Id,"        -selected_out true                                  ")
iphreeqc.AccumulateLine(Id,"        USER_PUNCH                                      ")
iphreeqc.AccumulateLine(Id,"        -start                                           ")
iphreeqc.AccumulateLine(Id,"        10 punch - LA('H+')                                  ")
iphreeqc.AccumulateLine(Id,"        -end                                            ")
iphreeqc.AccumulateLine(Id,"END                                                       ")

# run model
runout=iphreeqc.RunAccumulated(Id)
estring2=iphreeqc.OutputErrorString(Id)

a=iphreeqc.GetSelectedOutputRowCount(Id)
b=iphreeqc.GetSelectedOutputColumnCount(Id)
print a
print b # this works, gives correct number of rows and columns  

vart=VAR() 
iphreeqc.VarInit(ctypes.byref(vart)) 

c=iphreeqc.GetSelectedOutputValue(Id, 43, 0, ctypes.byref(vart)) #tries to extract value from row 43, column 1

print c # c is here VRESULT. this works properly giving the right error number (0 to -6). Gives 0 in this case which is VR_OK

我选中的行和列中的值是一个双精度浮点数,但我也尝试过用字符串值和长整型(整数)值,结果都没有成功。我希望vart.dVal(或者在字符串或长整型情况下的.sVal或.lVal)能包含我想要的值,但它并没有。我还希望VAR_TYPE中的某个TT_字段能是1,但它们都是0。看起来.dVal、.sVal和.lVal实际上包含的是VAR_TYPE的编号(在0到4之间),并且正确地报告这个值(例如,双精度类型是3,长整型是2)。

我的问题是:我该如何修正代码,使得VAR_TYPE字段能反映变量类型,而现在返回在.lVal字段中。我该如何将我想提取的值放到正确的vart.xVal字段中?我在Python的结构/联合代码中是否缺少了一些指针?

以下是C源代码中结构和联合的部分(Var.h):

http://wwwbrr.cr.usgs.gov/projects/GWC_coupled/iphreeqc/Var_8h_source.html

这是我在Python中尝试重现的C语言示例(v是我Python代码中的变量'vart',我暂时忽略循环等其他部分):

VAR v;
VarInit(&v);

  printf("selected-output:\n");
  for (i = 0; i < GetSelectedOutputRowCount(id); ++i) {
    for (j = 0; j < GetSelectedOutputColumnCount(id); ++j) {
      if (GetSelectedOutputValue(id, i, j, &v) == VR_OK) {
        switch (v.type) {
        case TT_LONG:
          printf("%ld ", v.lVal);
          break;
        case TT_DOUBLE:
          printf("%g ", v.dVal);
          break;
        case TT_STRING:
          printf("%s ", v.sVal);
          break;
        }
      }
      VarClear(&v);
    }
    printf("\n");
  }

这个C语言示例来自于:(往下滚动一点)http://wwwbrr.cr.usgs.gov/projects/GWC_coupled/iphreeqc/IPhreeqc_8h.html#a9f0ffd11e25a7e8f05d800623b14acf5

我在OS X 10.6.6上使用ctypes 1.1.0和Python 2.6.6

抱歉问题有点长,希望一些聪明的人能帮我,或者指引我正确的方向。

非常感谢

2 个回答

1

有一个叫做PhreeqPy的工具(http://www.phreeqpy.com),应该可以解决你的问题。需要说明的是:我是这个工具的作者。

PhreeqPy是一个封装了IPhreeqc的COM/DLL/共享库的工具。你可以在Linux或Mac OS上通过共享库来使用COM功能。

下面是处理联合体和结构的一部分代码:

class VARUNION(ctypes.Union):
    # pylint: disable-msg=R0903
    # no methods
    """Union with types.

    See Var.h in PHREEQC source.
    """
    c_int = ctypes.c_int
    _fields_ = [('long_value', ctypes.c_long),
                ('double_value', ctypes.c_double),
                ('string_value', ctypes.c_char_p),
                ('error_code', c_int)]

class VAR(ctypes.Structure):
    # pylint: disable-msg=R0903
    # no methods
    """Struct with data type and data values.

    See Var.h in PHREEQC source.
    """
    c_int = ctypes.c_int
    _fields_ = [('type', c_int),
                ('value', VARUNION)] 
2

一个问题是,枚举(enums)并不是结构体(structures)。另外,要确保你结构体里的字段顺序和C语言的头文件里的一样('type'要放在最前面)。试试这个:

import ctypes

VAR_TYPE = ctypes.c_int
TT_EMPTY  = 0
TT_ERROR  = 1
TT_LONG   = 2
TT_DOUBLE = 3
TT_STRING = 4

VRESULT = ctypes.c_int
VR_OK          =  0
VR_OUTOFMEMORY = -1
VR_BADVARTYPE  = -2
VR_INVALIDARG  = -3
VR_INVALIDROW  = -4
VR_INVALIDCOL  = -5

class _U(ctypes.Union):
    _fields_ = [
        ('lVal',ctypes.c_long),
        ('dVal',ctypes.c_double),
        ('sVal',ctypes.c_char_p),
        ('vresult',VRESULT)]

class VAR(ctypes.Structure):
    _anonymous_ = ('u',)
    _fields_ = [
        ('type',VAR_TYPE),
        ('u',_U)]

撰写回答