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

时间:2011-05-13 10:37:22

标签: python struct union ctypes

我想通过使用ctypes在一些Python代码中使用C库。 我的问题是我不知道C.我一直在尝试阅读代码和C结构几天,但我遇到了一个问题。我也是Python的新手,因为我目前正在从Matlab进行切换,因此我的Python仅限于科学的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

所选行,列中的值是double,但我也尝试使用字符串值和long(整数)值,但没有成功。 我希望vart.dVal(或字符串或长的情况下的.s​​Val或lVal)包含我想要的值,但事实并非如此。 我还希望VAR_TYPE中的一个TT_字段为1,但它们都是0。 似乎.dVal,.sVal和.lVal实际上包含VAR_TYPE编号(介于0和4之间),并正确报告(即3表示double类型,2表示long)。

我的问题是:如何修复代码以使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 个答案:

答案 0 :(得分:2)

一个问题是枚举不是结构。还要确保结构中的字段与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)]

答案 1 :(得分:1)

PhreeqPy(http://www.phreeqpy.com)应该可以解决您的问题。 免责声明:我是作者。

PhreeqPy是IPhreeqc COM / DLL /共享库的包装器。 您应该通过Linux或Linux上的共享库获取COM功能 Mac OS。

以下是处理union和结构的代码部分:

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)]