来自ctypes的公共访问变量

2024-09-30 01:37:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从Python脚本访问fortran77公共块中存储的数据。问题是我不知道这些数据存储在哪里。 我正在开发的Python应用程序使用不同的库。这些库包含具有以下指令的函数:

#include <tcsisc_common.inc>

公共块包含:

^{pr2}$

在Python库上使用了iPython(ipythi)的示例:

In [1]: import ctypes
In [2]: _libtcsisc= /home/jfreixa/project/bin/libtcsisc.so
In [3]: _tcsisc   = ctypes.CDLL(_libtcsisc, ctypes.RTLD_GLOBAL)

问题是我不知道如何得到IDEBUG。我尝试了以下方法,但我只是将tcsd作为一个c_初始化为0。在

In [4]: tcsd = ctypes.c_int.in_dll(_tcsisc, "TCSD_")
In [5]: tcsd
Out[5]: c_long(0)
In [6]: idebug = ctypes.c_int.in_dll(_tcsisc, "IDEBUG_")
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-ee5018286275> in <module>()

----> 1 idebug = ctypes.c_int.in_dll(_tcsisc,'IDEBUG_')

ValueError: ld.so.1: python2.7: fatal: IDEBUG_: can't find symbol

有没有办法正确地得到变量?在


Tags: 数据in脚本应用程序ctypesintdllvalueerror
1条回答
网友
1楼 · 发布于 2024-09-30 01:37:45

根据this page(特别是如何从C访问Fortran公共块)和一些关于如何从Python访问C结构的Q/A page来看,我们似乎可以按如下方式访问公共块(尽管这可能不是很容易移植,请参见下文):

mylib.f90型

subroutine fortsub()
    implicit none
    integer n
    common /mycom/ n
    print *, "fortsub> current /mycom/ n = ", n
end

编译:

^{pr2}$

在测试.py在

from __future__ import print_function
import ctypes

class Mycom( ctypes.Structure ):
    _fields_ = [ ( "n", ctypes.c_int ) ]

mylib = ctypes.CDLL( "./mylib.so" )

mycom = Mycom.in_dll( mylib, "mycom_" )

print( " python> modifying /mycom/ n to 777" )

mycom.n = 777

fortsub = mylib.fortsub_
fortsub()

测试:

 $ python test.py 
 python> modifying /mycom/ n to 777
 fortsub> current /mycom/ n =          777

这里,请注意公共块的名称(这里,mycom)是小写的,并附加一个下划线(假设为gfortran)。因为这种约定依赖于编译器,所以编写新的Fortran例程来设置/获取公共块中的值(特别是借助iso_c_binding)并从Python调用这些例程(正如@innoSPG在第一条注释中所建议的那样)可能更健壮/更易于移植。在


另一个包含不同类型和数组的示例可能如下所示:

mylib.f90型

subroutine initcom()
    implicit none
    integer          n( 2 ), w  !! assumed to be compatible with c_int
    real             f( 2 )     !!                      ... with c_float
    double precision d( 2 )     !!                      ... with c_double
    common /mycom/ n, f, d, w

    print *, "(fort) initializing /mycom/"
    n(:) = [ 1, 2 ]
    f(:) = [ 3.0, 4.0 ]
    d(:) = [ 5.0d0, 6.0d0 ]
    w    = 7
    call printcom()
end

subroutine printcom()
    implicit none
    integer          n( 2 ), w
    real             f( 2 )
    double precision d( 2 )
    common /mycom/ n, f, d, w

    print *, "(fort) current /mycom/"
    print *, "       n = ", n
    print *, "       f = ", f
    print *, "       d = ", d
    print *, "       w = ", w
end

在测试.py在

from __future__ import print_function
import ctypes

N = 2

class Mycom( ctypes.Structure ):
    _fields_ = [ ( "x", ctypes.c_int    * N ),
                 ( "y", ctypes.c_float  * N ),
                 ( "z", ctypes.c_double * N ),
                 ( "w", ctypes.c_int        ) ]

mylib = ctypes.CDLL( "./mylib.so" )

mycom = Mycom.in_dll( mylib, "mycom_" )

initcom = mylib.initcom_
initcom()

print( " (python) current /mycom/" )
print( "          x = ", mycom.x[:] )
print( "          y = ", mycom.y[:] )
print( "          z = ", mycom.z[:] )
print( "          w = ", mycom.w    )

print( " (python) modifying /mycom/ ..." )
for i in range( N ):
    mycom.x[ i ] = (i + 1) * 10
    mycom.y[ i ] = (i + 1) * 100
    mycom.z[ i ] = (i + 1) * 0.1
mycom.w = 777

printcom = mylib.printcom_
printcom()

测试:

 $ python test.py

 (fort) initializing /mycom/
 (fort) current /mycom/
        n =            1           2
        f =    3.0000000       4.0000000    
        d =    5.0000000000000000        6.0000000000000000     
        w =            7
 (python) current /mycom/
          x =  [1, 2]
          y =  [3.0, 4.0]
          z =  [5.0, 6.0]
          w =  7
 (python) modifying /mycom/ ...
 (fort) current /mycom/
        n =           10          20
        f =    100.00000       200.00000    
        d =   0.10000000000000001       0.20000000000000001     
        w =          777

相关问题 更多 >

    热门问题